<commit_before># level1 definitions (control, iteration)

from cat.namespace import *
import re
ns = NameSpace()

@define(ns, 'if')
def _if( cat ) :
    '''
    if : (bool:condition function:true_func function:false_func -> any:ans)
    
    desc:
        Executes one predicate or another whether the condition is true
        condition: the logical condition that 'if' will test
        true_func: the function to be executed if the condition is True
        false_func: the function to be executed if the condition is False
        ans: the result of the functions (may be no value pushed onto the stack)
        
        Example: 3 eqz ['ERROR 'red writeln] ['OK 'green writeln] if
    tags:
        control,function,condition,if
    '''
    ffalse, ftrue, cond = cat.stack.pop_n(3)
    
    if cond:
        cat.stack.push( ftrue )
    
    else:
        cat.stack.push( ffalse )
    
    cat.eval( 'apply' )

@define(ns, 'while')
def _while( cat ) :
    '''
    while : (function:exec_func function:test_cond -> any:ans)
    
    desc:
        Executes a block of code (function) repeatedly until the condition returns false
        exec_func: the function to execute repeatedly
        test_cond: the test condition
        ans: the result of executing the exec_func (may result in nothing pushed onto the stack)
        
        Example:  0 4 [[inc] dip] [dec dup gez] while pop -> 4
    tags:
        control,iteration,while
    '''
    b, f = cat.stack.pop_2()
    
    while True :
        cat.eval( b )
        
        if not cat.stack.pop() :
            break
        
        else :
            cat.eval( f )

@define(ns, 'repeat')
def repeat( cat ) :
    '''
    repeat : (function:exec_func int:n -> any:ans)

    desc:
        Executes a loop a fixed number of times
        exec_func: the function to be repeated
        n: number of times to repeat the function
        ans: the result of executing exec_func (may have no staak effect)
    test:
        in:
            3 [inc] 3 repeat
        out:
            6
    test:
        in:
            3 [2 mul] 2 repeat
        out:
            12
    test:
        in:
            3 [inc] 0 repeat
        out:
            3
    tags:
        control,iteration,repeat
    '''
    n, f = cat.stack.pop_2()
    n    = abs( int(n) )
    
    while n > 0 :
        cat.eval( f )
        n -= 1

@define(ns, 'foreach,for_each,for')
def foreach( cat ) :
    '''
    foreach  : (list:args function:exec_func -> any:ans)
    for_each : (list:args function:exec_func -> any:ans)
    for      : (list:args function:exec_func -> any:ans)

    desc:
        Executes a function with each item in the list, and consumes the list.
        args: a list of arguments
        exec_func: the function to be applied to each element of the argument list
        ans: result of the exec_func (may be no stack effect)
    test:
        in:
            0 [1 2 3 4] list [add] foreach
        out:
            10
    tags:
        control,iteration,for
    '''
    f, a = cat.stack.pop_2()
    
    for x in a :
        cat.stack.push( x )
        cat.eval( f )

@define(ns, 'pass')
def _pass( cat ) :
    '''
    pass : (-- -> --)
    
    desc:
        Does nothing (no-op)
    
        Example: pass
    tags:
        control
    '''
    return

@define(ns, 'halt')
def halt( cat ) :
    '''
    "halt : (A int:error_code -> A )
    
    desc:
        Halts the program with an error code by raising an exception
        error_code: an arbitrary integer error code
        
        Example: 3 halt
    tags:
        control,exception,halt
    '''
    n = int( cat.stack.pop() )
    raise Exception, "halt: Program halted with error code %d" % n

@define(ns, 'dispatch1')
def dispatch1( cat ) :
    '''
    dispatch1 : (list:functions any:arg -> any:ans)
    
    desc:
        Dynamically dispatches a function depending on the object on top of the stack
        functions: the list of "indexed" functions to be executed
        arg: the selector "index"
        ans: the result of executing the function associated with the selector arg
        
        Example: 3 [[dup] 1 [drop] 2 [swap] 3] list 1 dispatch1 => 3 3
    tags:
        control,functions,dispatch<commit_msg>Added new word 'dispatch'<commit_after># level1 definitions (control, iteration)

from cat.namespace import *
import re
ns = NameSpace()

@define(ns, 'if')
def _if( cat ) :
    '''
    if : (bool:condition function:true_func function:false_func -> any:ans)
    
    desc:
        Executes one predicate or another whether the condition is true
        condition: the logical condition that 'if' will test
        true_func: the function to be executed if the condition is True
        false_func: the function to be executed if the condition is False
        ans: the result of the functions (may be no value pushed onto the stack)
        
        Example: 3 eqz ['ERROR 'red writeln] ['OK 'green writeln] if
    tags:
        control,function,condition,if
    '''
    ffalse, ftrue, cond = cat.stack.pop_n(3)
    
    if cond:
        cat.stack.push( ftrue )
    
    else:
        cat.stack.push( ffalse )
    
    cat.eval( 'apply' )

@define(ns, 'while')
def _while( cat ) :
    '''
    while : (function:exec_func function:test_cond -> any:ans)
    
    desc:
        Executes a block of code (function) repeatedly until the condition returns false
        exec_func: the function to execute repeatedly
        test_cond: the test condition
        ans: the result of executing the exec_func (may result in nothing pushed onto the stack)
        
        Example:  0 4 [[inc] dip] [dec dup gez] while pop -> 4
    tags:
        control,iteration,while
    '''
    b, f = cat.stack.pop_2()
    
    while True :
        cat.eval( b )
        
        if not cat.stack.pop() :
            break
        
        else :
            cat.eval( f )

@define(ns, 'repeat')
def repeat( cat ) :
    '''
    repeat : (function:exec_func int:n -> any:ans)

    desc:
        Executes a loop a fixed number of times
        exec_func: the function to be repeated
        n: number of times to repeat the function
        ans: the result of executing exec_func (may have no staak effect)
    test:
        in:
            3 [inc] 3 repeat
        out:
            6
    test:
        in:
            3 [2 mul] 2 repeat
        out:
            12
    test:
        in:
            3 [inc] 0 repeat
        out:
            3
    tags:
        control,iteration,repeat
    '''
    n, f = cat.stack.pop_2()
    n    = abs( int(n) )
    
    while n > 0 :
        cat.eval( f )
        n -= 1

@define(ns, 'foreach,for_each,for')
def foreach( cat ) :
    '''
    foreach  : (list:args function:exec_func -> any:ans)
    for_each : (list:args function:exec_func -> any:ans)
    for      : (list:args function:exec_func -> any:ans)

    desc:
        Executes a function with each item in the list, and consumes the list.
        args: a list of arguments
        exec_func: the function to be applied to each element of the argument list
        ans: result of the exec_func (may be no stack effect)
    test:
        in:
            0 [1 2 3 4] list [add] foreach
        out:
            10
    tags:
        control,iteration,for
    '''
    f, a = cat.stack.pop_2()
    
    for x in a :
        cat.stack.push( x )
        cat.eval( f )

@define(ns, 'pass')
def _pass( cat ) :
    '''
    pass : (-- -> --)
    
    desc:
        Does nothing (no-op)
    
        Example: pass
    tags:
        control
    '''
    return

@define(ns, 'halt')
def halt( cat ) :
    '''
    "halt : (A int:error_code -> A )
    
    desc:
        Halts the program with an error code by raising an exception
        error_code: an arbitrary integer error code
        
        Example: 3 halt
    tags:
        control,exception,halt
    '''
    n = int( cat.stack.pop() )
    raise Exception, "halt: Program halted with error code %d" % n

@define(ns, 'dispatch')
def dispatch( cat ) :
    '''
    dispatch1 : (any:arg list:functions -> any:ans)
    
    desc:
        Dynamically dispatches a function depending on the index object
        functions: the list of "indexed" functions to be executed
        arg: the selector "index". Note: there is a special value that always
             matches whatever the index: 'default
        ans: the result of executing the function associated with the selector arg
        
        Example: 3 1 [1 [dup] 2 [drop] 3 [swap]] list dispatch => 3 3
    tags:
        control,functions,dispatch
    '''
    lst, obj = cat.stack.pop_2()
    
    for i in range(len(lst) / 2) :
        t = lst[2 * i]
        f = lst[2 * i + 1]
        
        if t == obj or t == 'default' :
            cat.eval( f )
            return
    
    raise Exception, "dispatch: Could not dispatch function"

@define(ns, 'dispatch1')
def dispatch1( cat ) :
    '''
    dispatch1 : (list:functions any:arg -> any:ans)
    
    desc:
        Dynamically dispatches a function depending on the object on top of the stack
        functions: the list of "indexed" functions to be executed
        arg: the selector "index"
        ans: the result of executing the function associated with the selector arg
        
        Example: 3 [[dup] 1 [drop] 2 [swap] 3] list 1 dispatch1 => 3 3
    tags:
        control,functions,dispatch
{'fraction_non_alphanumeric': 0.09334189681010491, 'fraction_numerical': 0.010169128666238493, 'mean_word_length': 1.8668303160478674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '3994102', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2771, 'n_words': 1037}