Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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

        
Пример #4
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)
        # 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)])
Пример #5
0
def op_restore(c: AF_Continuation) -> None:
    assert checkpoints.depth(), "No checkpoints saved."
    checkpoint = checkpoints.pop()
    Type.types = checkpoint[0]
    Type.ctors = checkpoint[1]

    ## TODO : This doesn't seem to be resetting our stacks.
    s = Stack()
    r = Stack()
    c = Continuation(stack=s, rstack=r)

    if checkpoints.depth() == 0:
        op_checkpoint(c)
Пример #6
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)

        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)
Пример #7
0
    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.
Пример #8
0
def see_handler(cont: AF_Continuation) -> None:
    print("Calling see_handler")
    symbol: Optional[Symbol] = cont.symbol
    symbol_id: str = ""
    if symbol:
        symbol_id = symbol.s_id
    t = Type.types.get(symbol_id)
    if t:
        so = StackObject(value="Type", stype=Type(symbol_id))
        cont.stack.tos().value.push(so)
        return
    s: Stack = cont.stack.tos().value
    r: Stack = Stack()
    fcont = Continuation(s, r)
    cont.op, found = Type.op(symbol_id, fcont)
    if found:
        for i in cont.op.words:
            print(i.name, i.sig)
    else:
        print("See: Failed to find word {}".format(symbol_id))

    cont.stack.pop()
Пример #9
0
 def setUp(self) -> None:
     self.s = Stack()
     self.r = Stack()
     self.c = Continuation(self.s,
                           self.r,
                           symbol=Symbol("Unknown", Location()))
Пример #10
0
    """
    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)

    print("ActorForth interpreter. ^C to exit.")
    print_words()
Пример #11
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)
Пример #12
0
class TestExecution(unittest.TestCase):

    def setUp(self) -> None:
        self.stack = Stack()
        self.cont = Continuation(self.stack)
        self.save_types = deepcopy(Type.types)
        self.save_ctors = deepcopy(Type.ctors)

    def tearDown(self) -> None:
        Type.types = deepcopy(self.save_types)
        Type.ctors = deepcopy(self.save_ctors)

    def execute(self, code) -> Any:
        self.cont.execute(interpret(self.cont, io.StringIO(code)))
        return self.cont.stack.tos().value

    def test_compile_double(self) -> None:
        code =  """
                debug on
                double : Int -> Int;
                    dup + .

                2 int double
                """
        assert self.execute(code) == 4

    def test_compile_double_literal(self) -> None:
        code =  """
                double : Int -> Int;
                    2 int * .

                2 int double
                """
        assert self.execute(code) == 4

    def test_compile_combo(self) -> None:
        code =  """
                is_equal : Any Any -> Bool;
                    == .

                double : Int -> Int;
                    dup +.

                combo : Int Int -> Bool;
                    double
                    swap
                    double
                    is_equal.

                8 int 8 int combo
                4 int 2 int combo
                """

        assert self.execute(code) == False
        assert self.cont.stack.depth() == 2
        self.cont.stack.pop()
        assert self.cont.stack.depth() == 1
        assert self.cont.stack.tos().value == True

    def test_compile_double_drop(self) -> None:
        code =  """
                double : Int -> Int ;
                    2 int *.
                5 int double
                """
        assert self.execute(code) == 10

    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