<commit_before>
.HLL 'Forth', ''
.namespace

.include 'languages/forth/words.pir'

.sub ' init' :load
    # load the libraries we depend on
    load_bytecode 'languages/forth/tokenstream.pbc'
    load_bytecode 'languages/forth/variablestack.pbc'
    load_bytecode 'languages/forth/virtualstack.pbc'
    
    # initialize the rstack
    .local pmc stack
    stack = new 'ResizablePMCArray'
    set_hll_global ' stack', stack

    # word dictionary - used for compilation
    .local pmc dict
    dict = new 'Hash'
    set_hll_global ' dict', dict

    .local pmc vars, vstack
    vars   = new 'Hash'
    vstack = new 'VariableStack'
    set_hll_global ' variables', vars
    set_hll_global ' vstack', vstack

    # register the actual compiler
    .local pmc compiler
    compiler = get_hll_global ' compile'
    compreg 'forth', compiler
.end

.sub main :main :anon
    .param pmc args
    .local int argc
    argc = elements args

    ' init'()
    if argc == 0 goto prompt

prompt:
    ' prompt'()
    end
.end


.sub ' prompt'
    .local pmc stdin, stdout, forth
    stdin  = getstdin
    stdout = getstdout
    forth  = compreg 'forth'

    $S0 = pop stdout
    print "Parrot Forth\n"

loop:
    print "> "
    $S0 = readline stdin
    unless stdin goto end

#    push_eh exception
      $P0 = forth($S0)
      $P0()
#    pop_eh

    print " ok\n"
    goto loop
end:
    .return()

exception:
    get_results '0, 0', $P0, $S0
    $S0 = $P0[0]
    print $S0
    print "\n"
    goto loop
.end


.sub ' compile'
    .param string input

    .local pmc code, stream, stack
    code   = new 'CodeString'
    stream = new 'TokenStream', input
    stack  = new 'VirtualStack'

    code.emit(<<"END_PIR")
.sub code :anon
    .local pmc stack
    stack = get_hll_global " stack"
END_PIR

    .local pmc token
next_token:
    unless stream goto done
    token = shift stream

    ' dispatch'(code, stream, stack, token)

    goto next_token
    
done:
    $S0 = stack.consolidate_to_cstack()
    code .= $S0
    code.emit(<<"END_PIR")
    .return(stack)
.end
END_PIR<commit_msg>[forth] Avoid using an opcode that doesn't exist, allow forth to build.<commit_after>
.HLL 'Forth', ''
.namespace

.include 'languages/forth/words.pir'

.sub ' init' :load
    # load the libraries we depend on
    load_bytecode 'languages/forth/tokenstream.pbc'
    load_bytecode 'languages/forth/variablestack.pbc'
    load_bytecode 'languages/forth/virtualstack.pbc'
    
    # initialize the rstack
    .local pmc stack
    stack = new 'ResizablePMCArray'
    set_hll_global ' stack', stack

    # word dictionary - used for compilation
    .local pmc dict
    dict = new 'Hash'
    set_hll_global ' dict', dict

    .local pmc vars, vstack
    vars   = new 'Hash'
    vstack = new 'VariableStack'
    set_hll_global ' variables', vars
    set_hll_global ' vstack', vstack

    # register the actual compiler
    .local pmc compiler
    compiler = get_hll_global ' compile'
    compreg 'forth', compiler
.end

.sub main :main :anon
    .param pmc args
    .local int argc
    argc = elements args

    ' init'()
    if argc == 0 goto prompt

prompt:
    ' prompt'()
    end
.end


.sub ' prompt'
    .local pmc stdin, stdout, forth
    stdin  = getstdin
    stdout = getstdout
    forth  = compreg 'forth'

    $S0 = pop stdout
    print "Parrot Forth\n"

loop:
    print "> "
    $S0 = readline stdin
    unless stdin goto end

#    push_eh exception
      $P0 = forth($S0)
      $P0()
#    pop_eh

    print " ok\n"
    goto loop
end:
    .return()

exception:
    get_results '0, 0', $P0, $S0
    $S0 = $P0[0]
    print $S0
    print "\n"
    goto loop
.end


.sub ' compile'
    .param pmc input

    .local pmc code, stream, stack
    code   = new 'CodeString'
    stream = new 'TokenStream', input
    stack  = new 'VirtualStack'

    code.emit(<<"END_PIR")
.sub code :anon
    .local pmc stack
    stack = get_hll_global " stack"
END_PIR

    .local pmc token
next_token:
    unless stream goto done
    token = shift stream

    ' dispatch'(code, stream, stack, token)

    goto next_token
    
done:
    $S0 = stack.consolidate_to_cstack()
    code .= $S0
    code.emit(<<"END_PIR")
    .return(stack)
.end
END_PIR
{'fraction_non_alphanumeric': 0.10173933762211103, 'fraction_numerical': 0.007624493685966166, 'mean_word_length': 2.426938775510204, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6168558', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1472, 'n_words': 436}