def swap((stack, dictionary)): ''' Reverse the order of the top two items on the stack. ''' TOS, second, stack = pop(stack, 2) stack = push(stack, TOS, second) return stack, dictionary
def tuck((stack, dictionary)): ''' Put a "duplicate" of the item on the top of the stack just under the second item on the stack. (I.e. top, second, top.) ''' TOS, second, stack = pop(stack, 2) stack = push(stack, TOS, second, TOS) return stack, dictionary
def NewBranchWord((stack, dictionary)): ''' Create a new Branch command word. A branch consumes the top item on the stack and does one of two things depending on its "truth" value. Unlike Loops and Sequences which use all the items on the stack, Branch commands only take the top two items. The item on the top of the stack should be a function to use in case of "true" and the second should be a function to use for "false". ''' true, false, stack = pop(stack, 2) branch = (handle_branch, true, false) stack = push(stack, branch) return stack, dictionary
''' while True: TOS, I = _pop_TOS(I) if not TOS: break I = handle_sequence(I, loop) return I # This primitive permits us to create a sort of "constant" in the dictionary. def enstacken((stack, dictionary), stack_us): ''' Push the items in the body onto the stack. ''' stack = push(stack, *stack_us[1:]) return stack, dictionary def apply_func(I, func): ''' Given an interpreter and a function or combo-word tuple, apply the function or combo to the interpreter and return the modified interpreter. ''' if isinstance(func, tuple): handler = func[0] I = handler(I, func) else: I = func(I) return I
while True: TOS, I = _pop_TOS(I) if not TOS: break I = handle_sequence(I, loop) return I # This primitive permits us to create a sort of "constant" in the dictionary. def enstacken((stack, dictionary), stack_us): ''' Push the items in the body onto the stack. ''' stack = push(stack, *stack_us[1:]) return stack, dictionary def apply_func(I, func): ''' Given an interpreter and a function or combo-word tuple, apply the function or combo to the interpreter and return the modified interpreter. ''' if isinstance(func, tuple): handler = func[0] I = handler(I, func) else: I = func(I) return I