def test_register_core(self): lib = PushTypeLibrary() assert set(lib.keys()) == { "bool", "int", "float", "char", "str", "code", "exec" } assert lib["char"] == PushChar assert lib["int"] == PushInt
def test_create_and_register(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("seq", (list, tuple)) assert set(lib.keys()) == {"seq", "exec", "code"} new_type = lib["seq"] assert new_type.is_instance((1, 2, 3)) assert new_type.is_instance([1, 2, 3])
def test_create_and_register(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("seq", (list, tuple)) assert set(lib.keys()) == {"seq", "exec", "code"} new_type = lib["seq"] assert new_type.is_instance((1, 2, 3)) assert new_type.is_instance([1, 2, 3])
def instructions(type_library: PushTypeLibrary): """Return all core printing instructions.""" i = [] for push_type in type_library.keys(): i.append(SimpleInstruction( "print_{t}".format(t=push_type), lambda x: [str(x)], input_stacks=[push_type], output_stacks=["stdout"], code_blocks=0, docstring="Prints the top {t}.".format(t=push_type) )) return i
def instructions(type_library: PushTypeLibrary): """Return all core printing instructions.""" i = [] for push_type in type_library.keys(): i.append(SimpleInstruction( "print_{t}".format(t=push_type), _wrap, input_stacks=[push_type], output_stacks=["stdout"], code_blocks=0, docstring="Prints the top {t}.".format(t=push_type) )), i.append(SimpleInstruction( "println_{t}".format(t=push_type), _wrap_and_newline, input_stacks=[push_type], output_stacks=["stdout"], code_blocks=0, docstring="Prints the top {t}.".format(t=push_type) )) return i
def instructions(type_library: PushTypeLibrary): """Return all core numeric instructions.""" i = [] for push_type in type_library.keys(): i.append(SimpleInstruction( "{t}_pop".format(t=push_type), lambda x: [], input_stacks=[push_type], output_stacks=[], code_blocks=(1 if push_type == "exec" else 0), docstring="Pops the top {t}.".format(t=push_type) )) i.append(SimpleInstruction( "{t}_dup".format(t=push_type), lambda x: [x, x], input_stacks=[push_type], output_stacks=[push_type, push_type], code_blocks=(1 if push_type == "exec" else 0), docstring="Duplicates the top {t}.".format(t=push_type) )) i.append(ProducesManyOfTypeInstruction( "{t}_dup_times".format(t=push_type), _dup_times, input_stacks=["int", push_type], output_stack=push_type, code_blocks=(1 if push_type == "exec" else 0), docstring="Duplicates the top {t} `n` times where `n` is from the int stack.".format(t=push_type) )) # Disabled due to performance issues. # i.append(StateToStateInstruction( # "{t}_dup_top_n".format(t=push_type), # _dup_top_n_factory(push_type), # stacks_used=[push_type, "int"], # code_blocks=0, # docstring="Duplicates the top n items on the {t} stack.".format(t=push_type) # )) i.append(SimpleInstruction( "{t}_swap".format(t=push_type), lambda a, b: [a, b], input_stacks=[push_type, push_type], output_stacks=[push_type, push_type], code_blocks=(2 if push_type == "exec" else 0), docstring="Swaps the top two {t}s.".format(t=push_type) )) i.append(SimpleInstruction( "{t}_rot".format(t=push_type), lambda a, b, c: [b, a, c], input_stacks=[push_type] * 3, output_stacks=[push_type] * 3, code_blocks=(3 if push_type == "exec" else 0), docstring="Rotates the top three {t}s.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_flush".format(t=push_type), _flusher(push_type), stacks_used=[push_type], code_blocks=0, docstring="Empties the {t} stack.".format(t=push_type) )) i.append(SimpleInstruction( "{t}_eq".format(t=push_type), lambda a, b: [a == b], input_stacks=[push_type, push_type], output_stacks=["bool"], code_blocks=0, docstring="Pushes True if the top two {t} are equal. Otherwise pushes False.".format(t=push_type) )) i.append(TakesStateInstruction( "{t}_stack_depth".format(t=push_type), _stack_depther(push_type), output_stacks=["int"], other_stacks=[push_type], code_blocks=0, docstring="Pushes the size of the {t} stack to the int stack.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_yank".format(t=push_type), _yanker(push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring="Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_yank_dup".format(t=push_type), _yank_duper(push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring="Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_shove".format(t=push_type), _shover(push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring="Shoves the top {t} deep in the stack based on an index from the int stack.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_shove_dup".format(t=push_type), _shove_duper(push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring="Shoves a copy of the top {t} deep in the stack based on an index from the int stack.".format(t=push_type) )) i.append(TakesStateInstruction( "{t}_is_empty".format(t=push_type), _is_emptyer(push_type), output_stacks=["bool"], other_stacks=[push_type], code_blocks=0, docstring="Pushes True if the {t} stack is empty. Pushes False otherwise.".format(t=push_type) )) for push_type_name, push_type in type_library.items(): if push_type_name == "code": continue i.append(SimpleInstruction( "code_from_{t}".format(t=push_type_name), partial(_make_code, push_type=push_type), input_stacks=[push_type_name], output_stacks=["code"], code_blocks=(1 if push_type_name == "exec" else 0), docstring="Moves the top {t} to the code stack.".format(t=push_type_name) )) return i
def test_register(self): lib = PushTypeLibrary(register_core=False) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
def test_unregister(self): lib = PushTypeLibrary() lib.unregister("char") lib.unregister("float") assert set(lib.keys()) == {"int", "str", "bool", "exec", "code"}
def test_register_duplicates(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("char", (int, )) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
def test_register_core(self): lib = PushTypeLibrary() assert set(lib.keys()) == ALL_CORE_TYPE_NAMES assert lib["char"] == PushChar assert lib["int"] == PushInt
def test_unregister(self): lib = PushTypeLibrary() lib.unregister("char") lib.unregister("float") assert set(lib.keys()) == ALL_CORE_TYPE_NAMES - {"char", "float"}
def instructions(type_library: PushTypeLibrary): """Return all core numeric instructions.""" i = [] for push_type in type_library.keys(): i.append( SimpleInstruction( "{t}_pop".format(t=push_type), _noop, input_stacks=[push_type], output_stacks=[], code_blocks=(1 if push_type == "exec" else 0), docstring="Pops the top {t}.".format(t=push_type))) i.append( SimpleInstruction( "{t}_dup".format(t=push_type), _dup, input_stacks=[push_type], output_stacks=[push_type, push_type], code_blocks=(1 if push_type == "exec" else 0), docstring="Duplicates the top {t}.".format(t=push_type))) i.append( ProducesManyOfTypeInstruction( "{t}_dup_times".format(t=push_type), _dup_times, input_stacks=["int", push_type], output_stack=push_type, code_blocks=(1 if push_type == "exec" else 0), docstring= "Duplicates the top {t} `n` times where `n` is from the int stack." .format(t=push_type))) # Disabled due to performance issues. # i.append(StateToStateInstruction( # "{t}_dup_top_n".format(t=push_type), # _dup_top_n_factory(push_type), # stacks_used=[push_type, "int"], # code_blocks=0, # docstring="Duplicates the top n items on the {t} stack.".format(t=push_type) # )) i.append( SimpleInstruction( "{t}_swap".format(t=push_type), _swap, input_stacks=[push_type, push_type], output_stacks=[push_type, push_type], code_blocks=(2 if push_type == "exec" else 0), docstring="Swaps the top two {t}s.".format(t=push_type))) i.append( SimpleInstruction( "{t}_rot".format(t=push_type), _rot, input_stacks=[push_type] * 3, output_stacks=[push_type] * 3, code_blocks=(3 if push_type == "exec" else 0), docstring="Rotates the top three {t}s.".format(t=push_type))) i.append( StateToStateInstruction( "{t}_flush".format(t=push_type), partial(_flush, type_name=push_type), stacks_used=[push_type], code_blocks=0, docstring="Empties the {t} stack.".format(t=push_type))) i.append( SimpleInstruction( "{t}_eq".format(t=push_type), _eq, input_stacks=[push_type, push_type], output_stacks=["bool"], code_blocks=0, docstring= "Pushes True if the top two {t} are equal. Otherwise pushes False." .format(t=push_type))) i.append( TakesStateInstruction( "{t}_stack_depth".format(t=push_type), partial(_stack_depth, type_name=push_type), output_stacks=["int"], other_stacks=[push_type], code_blocks=0, docstring="Pushes the size of the {t} stack to the int stack.". format(t=push_type))) i.append( StateToStateInstruction( "{t}_yank".format(t=push_type), partial(_yank, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring= "Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top." .format(t=push_type))) i.append( StateToStateInstruction( "{t}_yank_dup".format(t=push_type), partial(_yank_dup, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring= "Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top." .format(t=push_type))) i.append( StateToStateInstruction( "{t}_shove".format(t=push_type), partial(_shove, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring= "Shoves the top {t} deep in the stack based on an index from the int stack." .format(t=push_type))) i.append( StateToStateInstruction( "{t}_shove_dup".format(t=push_type), partial(_shove_dup, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring= "Shoves a copy of the top {t} deep in the stack based on an index from the int stack." .format(t=push_type))) i.append( TakesStateInstruction( "{t}_is_empty".format(t=push_type), partial(_is_empty, type_name=push_type), output_stacks=["bool"], other_stacks=[push_type], code_blocks=0, docstring= "Pushes True if the {t} stack is empty. Pushes False otherwise." .format(t=push_type))) for push_type_name, push_type in type_library.items(): if push_type_name == "code": continue i.append( SimpleInstruction( "code_from_{t}".format(t=push_type_name), partial(_make_code, push_type=push_type), input_stacks=[push_type_name], output_stacks=["code"], code_blocks=(1 if push_type_name == "exec" else 0), docstring="Moves the top {t} to the code stack.".format( t=push_type_name))) return i
def test_register(self): lib = PushTypeLibrary(register_core=False) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
def test_register_core(self): lib = PushTypeLibrary() assert set(lib.keys()) == {"bool", "int", "float", "char", "str", "code", "exec"} assert lib["char"] == PushChar assert lib["int"] == PushInt
def test_unregister(self): lib = PushTypeLibrary() lib.unregister("char") lib.unregister("float") assert set(lib.keys()) == {"int", "str", "bool", "exec", "code"}
def test_register_duplicates(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("char", (int, )) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar