예제 #1
0
def test_interpret_number():
    term = SyntaxNumber(int32(100))
    frame_stack = init_test_frame_stack(init_term_block(term),
                                        MachineExpressionStack([]))
    interpret_term(frame_stack)
    assert frame_stack == init_test_frame_stack(
        init_term_block(term),
        MachineExpressionStack([MachineNumber(int32(100))]),
        term_index=1,
    )
예제 #2
0
def test_interpret_comment():
    term = SyntaxComment("something")
    expression_stack = MachineExpressionStack([MachineNumber(int32(10))])
    frame_stack = init_test_frame_stack(init_term_block(term),
                                        expression_stack)
    interpret_term(frame_stack)
    assert frame_stack == init_test_frame_stack(
        init_term_block(term),
        MachineExpressionStack([MachineNumber(int32(10))]),
        term_index=1,
    )
예제 #3
0
def test_interpret_identifier_call():
    term = SyntaxIdentifier(sqrt.name)
    expression_stack = MachineExpressionStack([MachineNumber(int32(16))])
    frame_stack = init_test_frame_stack(init_term_block(term),
                                        expression_stack)
    interpret_term(frame_stack)
    assert frame_stack == init_test_frame_stack(
        init_term_block(term),
        MachineExpressionStack([MachineNumber(int32(4))]),
        term_index=1,
    )
예제 #4
0
def test_interpret_identifier_value():
    term = SyntaxIdentifier(PI.name)
    expression_stack = MachineExpressionStack([])
    frame_stack = init_test_frame_stack(init_term_block(term),
                                        expression_stack)
    interpret_term(frame_stack)
    assert frame_stack == init_test_frame_stack(
        init_term_block(term),
        MachineExpressionStack([PI.value]),
        term_index=1,
    )
def test_interpret_expression_enter():
    statement = SyntaxExpression([SyntaxNumber(int32(100))])
    frame_stack = init_test_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([]),
    )
    interpret_statement(frame_stack)
    assert frame_stack == init_test_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([MachineNumber(int32(100))]),
        term_index=1,
    )
예제 #6
0
def test_interpret_block_enter():
    block = SyntaxBlock(
        [SyntaxExpression([SyntaxNumber(int32(4))])]
    )
    frame_stack = init_test_frame_stack(
        block, MachineExpressionStack([])
    )
    interpret_block(frame_stack)
    assert frame_stack == init_test_frame_stack(
        block,
        MachineExpressionStack([MachineNumber(int32(4))]),
        term_index=1,
    )
def test_interpret_assignment_2_exit():
    statement = SyntaxAssignment(
        terms=[
            SyntaxNumber(int32(300)),
            SyntaxNumber(int32(400)),
        ],
        bindings=[
            SyntaxIdentifier("abc"),
            SyntaxIdentifier("def"),
        ],
    )
    frame_stack = init_test_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([]),
    )
    interpret_statement(frame_stack)
    interpret_statement(frame_stack)
    interpret_statement(frame_stack)
    assert frame_stack == init_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([]),
        test_environment({
            "abc": MachineNumber(int32(300)),
            "def": MachineNumber(int32(400)),
        }),
        statement_index=1,
    )
예제 #8
0
def test_interpret_block_exit():
    block = SyntaxBlock(
        [SyntaxExpression([SyntaxNumber(int32(4))])]
    )
    frame_stack = init_test_frame_stack(
        block, MachineExpressionStack([]), statement_index=1
    )
    interpret_block(frame_stack)
    assert frame_stack == MachineFrameStack([])
예제 #9
0
def test_interpret_block():
    term = init_term_block(SyntaxNumber(int32(2)))
    expression_stack = MachineExpressionStack([MachineNumber(int32(10))])
    frame_stack = init_test_frame_stack(init_term_block(term),
                                        expression_stack)
    interpret_term(frame_stack)
    assert len(frame_stack) == 2
    assert (frame_stack.frames[0] == init_test_frame_stack(
        init_term_block(term),
        MachineExpressionStack([MachineNumber(int32(10))]),
        term_index=1,
    ).frames[0])
    assert frame_stack.frames[1] == MachineFrame(
        instruction_pointer=MachineInstructionPointer(block=term,
                                                      statement_index=0,
                                                      term_index=0),
        environment=test_environment().extend(),
        expression_stack=MachineExpressionStack([MachineNumber(int32(10))]),
    )
예제 #10
0
def test_call_push_frame():
    call = GuestCall(
        definition_environment=test_environment(),
        definition_block=SyntaxBlock(
            [SyntaxExpression([SyntaxIdentifier("g")])]
        ),
    )
    block = SyntaxBlock(
        [SyntaxExpression([SyntaxIdentifier("f")])]
    )
    frame_stack = init_test_frame_stack(
        block,
        MachineExpressionStack([MachineNumber(int32(9))]),
    )
    call(frame_stack)
    assert len(frame_stack) == 2
    assert (
        frame_stack.frames[0]
        == init_test_frame_stack(
            block,
            MachineExpressionStack(
                [MachineNumber(int32(9))]
            ),
            term_index=1,
        ).frames[0]
    )
    assert frame_stack.frames[1] == MachineFrame(
        instruction_pointer=MachineInstructionPointer(
            block=call.definition_block,
            statement_index=0,
            term_index=0,
        ),
        environment=test_environment().extend(),
        expression_stack=MachineExpressionStack(
            [MachineNumber(int32(9))]
        ),
    )
예제 #11
0
def test_interpret_assignment_1_exit():
    statement = SyntaxAssignment(
        terms=[SyntaxNumber(int32(3))],
        bindings=[SyntaxIdentifier("x")],
    )
    frame_stack = init_test_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([MachineNumber(int32(4))]),
    )
    interpret_statement(frame_stack)
    interpret_statement(frame_stack)
    assert frame_stack == init_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([MachineNumber(int32(4))]),
        test_environment({"x": MachineNumber(int32(3))}),
        statement_index=1,
    )
예제 #12
0
def test_interpret_assignment_1_enter():
    statement = SyntaxAssignment(
        terms=[SyntaxNumber(int32(3))],
        bindings=[SyntaxIdentifier("x")],
    )
    frame_stack = init_test_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([MachineNumber(int32(4))]),
    )
    interpret_statement(frame_stack)
    assert len(frame_stack) == 2
    assert (frame_stack.frames[1].instruction_pointer is
            frame_stack.frames[0].instruction_pointer)
    assert (frame_stack.frames[1].environment is
            frame_stack.frames[0].environment)
    assert (frame_stack.current.expression_stack == MachineExpressionStack(
        [MachineNumber(int32(3))]))
예제 #13
0
def test_interpret_method_definition_exit():
    statement = SyntaxMethodDefinition(
        binding=SyntaxIdentifier("f"),
        definition=SyntaxExpression([SyntaxIdentifier("sqrt")]),
    )
    frame_stack = init_test_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([]),
    )
    interpret_statement(frame_stack)
    environment = frame_stack.current.environment
    assert frame_stack == init_frame_stack(
        init_statement_block(statement),
        MachineExpressionStack([]),
        environment,
        statement_index=1,
    )
    assert environment.base == test_environment().base
    assert environment.bindings == {
        "f": GuestCall(environment, statement.definition_block)
    }