def test_op_with_wrong_type_signature(self) -> None: stack = Stack() stack.push(StackObject(stype=TTest, value="tparm")) make_word_context("test", op_print, [TParm1]) with self.assertRaises(Exception): Type.op("test", stack)
def test_op_with_type_multi_input_multi_output_signature(self) -> None: stack = Stack() cont = Continuation(stack) cont.stack.push(StackObject(stype=TTest, value="ttest")) cont.stack.push(StackObject(stype=TParm1, value="tparm")) cont.stack.push(StackObject(stype=TParm1, value="tparm")) make_word_context("test", lambda cont: 42, [TTest, TParm1, TParm1], [TTest, TParm1]) print_words() op, found = Type.op("test", cont) #, "Test") assert found assert op.sig == TypeSignature([ StackObject(stype=TTest), StackObject(stype=TParm1), StackObject(stype=TParm1) ], [StackObject(stype=TTest), StackObject(stype=TParm1)]) op( cont ) # Do this so the lambda doesn't get counted as a missed branch in our code coverage. op, found = Type.op("not found", cont) assert not found
def setUp(self) -> None: self.stack = Stack() self.cont = Continuation(self.stack) self.save_types = deepcopy(Type.types) self.save_ctors = deepcopy(Type.ctors) # Clear up all the types. the_types = ["Any", "CodeCompile", "Parm1", "Test"] for t in the_types: x = Type(t) Type.register_ctor("Test", Operation('nop', TOp), [StackObject(stype=TParm1)])
def setUp(self) -> None: self.stack = Stack() self.cont = Continuation(self.stack) self.save_types = deepcopy(Type.types) self.save_ctors = deepcopy(Type.ctors) self.zero_pattern = StackObject(stype=TInt, value=0) self.one_pattern = StackObject(stype=TInt, value=1) self.any_pattern = StackObject(stype=TAny) self.true_pattern = StackObject(stype=TBool, value=True) self.false_pattern = StackObject(stype=TBool, value=False) self.int_pattern = StackObject(stype=TInt) op_debug(self.cont) op_on(self.cont)
def test_op_with_no_type_signature(self) -> None: def stack_fun(s: Stack) -> None: s.push(42) s = Stack() c = Continuation(s) make_word_context("test", stack_fun) op, found = Type.op("test", c) assert found assert op.sig == TypeSignature([], []) op( s ) # Do this so the lambda doesn't get counted as a missed branch in our code coverage.
def test_compile_no_input_no_output(self) -> None: code = """ noinputnooutput : -> ; stack . """ stack = Stack() cont = Continuation(stack) cont = cont.execute(interpret(cont, io.StringIO(code))) op, found = Type.op("noinputnooutput", cont ) #, "Test") assert found assert op.sig == TypeSignature([],[]) op, found = Type.op("not found", cont) assert not found
def test_compile_multi_input_multi_output(self) -> None: code = """ multiinput : Bool Int Int Int -> Int ; + == == . False bool 1 int 1 int 1 int """ stack = Stack() cont = Continuation(stack) cont = cont.execute(interpret(cont, io.StringIO(code))) op, found = Type.op("multiinput", cont ) #, "Test") assert found assert op.sig == TypeSignature([StackObject(stype=TBool),StackObject(stype=TInt),StackObject(stype=TInt),StackObject(stype=TInt)], [StackObject(stype=TInt)]) op, found = Type.op("not found", cont) assert not found
def test_match_in(self) -> None: empty_sig = TypeSignature([], []) s = Stack() assert empty_sig.match_in(s) s.push(StackObject(stype=TParm1)) sig = TypeSignature([StackObject(stype=TParm1)], []) # We can do this twice because it doesn't consume the stack. assert sig.match_in(s) assert sig.match_in(s) s.push(StackObject(stype=TTest)) assert sig.match_in(s) == False
def setUp(self) -> None: self.s = Stack() self.r = Stack() self.c = Continuation(self.s, self.r, symbol=Symbol("Unknown", Location()))
class TestGenericTypeStuff(unittest.TestCase): def setUp(self) -> None: self.s = Stack() self.r = Stack() self.c = Continuation(self.s, self.r, symbol=Symbol("Unknown", Location())) def test_make_atom(self) -> None: self.c.symbol.s_id = "test" #op_name = "test" make_atom(self.c) item = self.c.stack.pop() assert item.value == "test" assert item.stype == TAtom def test_op_print(self) -> None: self.c.stack.push(StackObject(stype=TAtom, value="test")) op_print(self.c) assert self.c.stack.depth() == 0 def test_op_dup(self) -> None: @dataclass class DupTest: val: str self.c.stack.push(StackObject(value=DupTest("Something"), stype=TAtom)) op_dup(self.c) op_dup(self.c) item1 = self.c.stack.pop() self.c.stack.tos().value.val = "SomethingElse" assert item1.value.val == "Something" item2 = self.c.stack.pop() assert item2.value.val == "SomethingElse" assert item1 != item2 item3 = self.c.stack.pop() assert item3.value.val == "Something" assert item2 != item3 assert item1 == item3 def test_op_swap(self) -> None: self.c.symbol.s_id = "first" make_atom(self.c) self.c.symbol.s_id = "second" make_atom(self.c) op_swap(self.c) item1 = self.c.stack.pop() item2 = self.c.stack.pop() assert item1.value == "first" assert item2.value == "second" def test_op_drop(self) -> None: self.s.push(StackObject(stype=TAtom, value="test")) op_drop(self.c) assert self.c.stack.depth() == 0 def test_op_2dup(self) -> None: self.c.symbol.s_id = "first" make_atom(self.c) self.c.symbol.s_id = "second" make_atom(self.c) op_2dup(self.c) assert self.c.stack.depth() == 4 item1 = self.c.stack.pop() item2 = self.c.stack.pop() item3 = self.c.stack.pop() item4 = self.c.stack.pop() assert item1.value == item3.value assert item2.value == item4.value assert item1.value != item2.value assert self.c.stack.depth() == 0
def stack_fun(s: Stack) -> None: s.push(42)
def afc(code: str) -> TextIO: """ Given a string of ActorForth code, returns a file stream that can be executed by the interpreter. cont = interpret(cont, afc("1 int 2 int +")) """ return StringIO(code) """ INTRO 1.3 : Establish our stack and build our stateful Continutaion from it. """ stack = Stack() rstack = Stack() cont = Continuation(stack, rstack) def do_repl(filename: str, handle: TextIO): global cont # Set Debug on or off initially. op_debug(cont) op_off(cont) #op_on(cont) # Checkpoint our initial setup. op_checkpoint(cont)
class TestGenericReturnStackOps(unittest.TestCase): def setUp(self) -> None: self.s = Stack() self.r = Stack() self.c = Continuation(self.s, symbol=Symbol("Unknown", Location())) self.c.rstack = self.r def test_make_ratom(self) -> None: self.c.symbol.s_id = "test" make_ratom(self.c) item = self.c.rstack.pop() assert item.value == "test" assert item.stype == TAtom def test_op_rdup(self) -> None: self.test_make_ratom() op_rdup(self.c) item1 = self.c.rstack.pop() item2 = self.c.rstack.pop() assert item1 == item2 def test_op_rswap(self) -> None: self.c.symbol.s_id = "first" make_ratom(self.c) self.c.symbol.s_id = "second" make_ratom(self.c) op_rswap(self.c) item1 = self.c.rstack.pop() item2 = self.c.rstack.pop() assert item1.value == "first" assert item2.value == "second" def test_op_rdrop(self) -> None: self.r.push(StackObject(stype=TAtom, value="test")) op_rdrop(self.c) assert self.c.rstack.depth() == 0 def test_op_2rdup(self) -> None: self.c.symbol.s_id = "first" make_ratom(self.c) self.c.symbol.s_id = "second" make_ratom(self.c) op_2rdup(self.c) assert self.c.rstack.depth() == 4 item1 = self.c.rstack.pop() item2 = self.c.rstack.pop() item3 = self.c.rstack.pop() item4 = self.c.rstack.pop() assert item1.value == item3.value assert item2.value == item4.value assert item1.value != item2.value assert self.c.rstack.depth() == 0 def test_rstack(self) -> None: op_rstack(self.c) make_ratom(self.c) op_rstack(self.c) self.c.prompt = None op_rstack(self.c) def test_move_between_stacks(self) -> None: self.c.symbol.s_id = "test" make_ratom(self.c) assert self.c.rstack.tos().value == "test" assert self.c.stack.depth() == 0 op_mov_to_dstack(self.c) assert self.c.stack.tos().value == "test" assert self.c.rstack.depth() == 0 op_mov_to_rstack(self.c) assert self.c.rstack.tos().value == "test" assert self.c.stack.depth() == 0
def setUp(self) -> None: self.stack = Stack() self.cont = Continuation(self.stack) self.save_types = deepcopy(Type.types) self.save_ctors = deepcopy(Type.ctors)