<commit_before>###
# [if]

.HLL 'Tcl', 'tcl_group'
.namespace

.sub '&if'
    .param pmc argv :slurpy

    .local int argc
    argc = elements argv

    if argc == 0 goto no_args

    .local pmc __expr, __script, __boolean
    __expr    = get_root_global ['_tcl'], '__expr'
    __script  = get_root_global ['_tcl'], '__script'
    __boolean = get_root_global ['_tcl'], '__boolean'

    .local string cond, code
    cond = argv[0]
    $I0  = 1
    if argc == 1 goto no_script
    code = argv[1]

    unless code == 'then' goto loop
    $I0  = 2
    if argc == 2 goto no_script
    code = argv[2]

loop:
    $P0 = __expr(cond)
    $P1 = $P0()
    $I1 = __boolean($P1)
    unless $I1 goto next
    $P0 = __script(code)
    .return $P0()

next:
    inc $I0
    if $I0 == argc goto nothing

    $S0 = argv[$I0]
    if $S0 == 'elseif' goto elseif
    if $S0 == 'else'   goto else

    # 'else' is optional
    dec $I0
    goto else

elseif:
    inc $I0
    if $I0 == argc goto no_expression

    cond = argv[$I0]
    inc $I0
    if $I0 == argc goto no_script

    code = argv[$I0]
    unless code == 'then' goto loop

    inc $I0
    if $I0 == argc goto no_script

    code = argv[$I0]
    goto loop

else:
    inc $I0
    if $I0 == argc goto no_script
    code = argv[$I0]

    inc $I0
    if $I0 != argc goto extra_words_after_else

    $P0 = __script(code)
    .return $P0()

extra_words_after_else:
    .throw('wrong # args: extra words after "else" clause in "if" command')

nothing:
    .return('')

no_args:
    .throw('wrong # args: no expression after "if" argument')

no_script:
    dec $I0
    $S0 = argv[$I0]
    $S0 = 'wrong # args: no script following "' . $S0
    $S0 = $S0 . '" argument'
    .throw($S0)

no_expression:
    dec $I0
    $S0 = argv[$I0]
    $S0 = 'wrong # args: no expression after "' . $S0
    $S0 = $S0 . '" argument'
    .throw($S0)
.end
<commit_msg>[tcl]: Fix the errors for [if] (all tests pass)<commit_after>###
# [if]

.HLL 'Tcl', 'tcl_group'
.namespace

.sub '&if'
    .param pmc argv :slurpy

    .local int argc
    argc = elements argv

    .local pmc __expr
    __expr = get_root_global ['_tcl'], '__expr'

    if argc == 0 goto no_args

    # we have to do arg checking first, to make sure we got the proper type of
    # exception. but [expr] checking has to happen before that. so replace each
    # string expression with a Sub that represents it. and while we're at it,
    # strip out the "then"s.

    $S0 = argv[0]
    $P0 = __expr($S0)
    argv[0] = $P0

    $I0 = 1
    if $I0 == argc goto no_script
    $S0 = argv[$I0]
    unless $S0 == 'then' goto arg_next

    delete argv[$I0]
    dec argc
    if $I0 == argc goto no_script
arg_next:
    inc $I0
    if $I0 == argc goto arg_end

    $S0 = argv[$I0]
    if $S0 == 'elseif' goto arg_elseif
    if $S0 == 'else'   goto arg_else

    # 'else' is optional
    dec $I0
    goto arg_else

arg_elseif:
    inc $I0
    if $I0 == argc goto no_expression
    $S0 = argv[$I0]
    $P0 = __expr($S0)
    argv[$I0] = $P0

    inc $I0
    if $I0 == argc goto no_script
    $S0 = argv[$I0]
    unless $S0 == 'then' goto arg_next
    
    delete argv[$I0]
    dec argc
    if $I0 == argc goto no_script
    goto arg_next

arg_else:
    inc $I0
    if $I0 == argc goto no_script

    inc $I0
    if $I0 != argc goto extra_words_after_else
arg_end:

    # now we can do the actual evaluation
    .local pmc __script, __boolean
    __script  = get_root_global ['_tcl'], '__script'
    __boolean = get_root_global ['_tcl'], '__boolean'

    .local pmc    cond
    .local string code
    cond = argv[0]
    code = argv[1]
    $I0  = 1

loop:
    $P1 = cond()
    $I1 = __boolean($P1)
    unless $I1 goto next
    $P0 = __script(code)
    .return $P0()

next:
    inc $I0
    if $I0 == argc goto nothing

    $S0 = argv[$I0]
    if $S0 == 'elseif' goto elseif
    if $S0 == 'else'   goto else

    # 'else' is optional
    dec $I0
    goto else

elseif:
    inc $I0
    cond = argv[$I0]
    inc $I0
    code = argv[$I0]
    goto loop

else:
    inc $I0
    code = argv[$I0]
    $P0  = __script(code)
    .return $P0()

extra_words_after_else:
    .throw('wrong # args: extra words after "else" clause in "if" command')

nothing:
    .return('')

no_args:
    .throw('wrong # args: no expression after "if" argument')

no_script:
    dec $I0
    $S0 = argv[$I0]
    $S0 = 'wrong # args: no script following "' . $S0
    $S0 = $S0 . '" argument'
    .throw($S0)

no_expression:
    dec $I0
    $S0 = argv[$I0]
    $S0 = 'wrong # args: no expression after "' . $S0
    $S0 = $S0 . '" argument'
    .throw($S0)
.end

{'fraction_non_alphanumeric': 0.13118326432774025, 'fraction_numerical': 0.030289823490956637, 'mean_word_length': 2.271560940841055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1670726', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1783, 'n_words': 560}