Example #1
0
def test_call_push_frame() -> None:
    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))]),
    )
Example #2
0
def test_call_delegate_to_host_call() -> None:
    call = GuestCall(
        definition_environment=test_environment(),
        definition_block=SyntaxBlock(
            [SyntaxExpression([SyntaxIdentifier("sqrt")])]),
    )
    block = SyntaxBlock([SyntaxExpression([SyntaxIdentifier("f")])])
    expression_stack = MachineExpressionStack([MachineNumber(int32(16))])
    environment = test_environment({"f": call})
    interpret(block, expression_stack, environment)
    assert expression_stack == MachineExpressionStack(
        [MachineNumber(int32(4))])
Example #3
0
def test_interpret_block() -> None:
    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))]
        ),
    )
Example #4
0
def test_interpret_assignment_2_exit() -> None:
    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,
    )
Example #5
0
def call_test(
    binding: MachineBinding,
    args: list[MachineValue],
    results: list[MachineValue],
) -> None:
    term = SyntaxIdentifier(binding.name)
    block = SyntaxBlock([SyntaxExpression([term])])
    expression_stack = MachineExpressionStack(args)
    environment = test_environment()
    interpret(block, expression_stack, environment)
    assert expression_stack == MachineExpressionStack(results)
Example #6
0
def test_call_generate_values() -> None:
    call = GuestCall(
        definition_environment=test_environment(),
        definition_block=SyntaxBlock([
            SyntaxExpression([
                SyntaxNumber(int32(2)),
                SyntaxNumber(int32(3)),
                SyntaxNumber(int32(4)),
            ])
        ]),
    )
    block = SyntaxBlock([SyntaxExpression([SyntaxIdentifier("seq")])])
    expression_stack = MachineExpressionStack([MachineNumber(int32(1))])
    environment = test_environment({"seq": call})
    interpret(block, expression_stack, environment)
    assert expression_stack == MachineExpressionStack([
        MachineNumber(int32(1)),
        MachineNumber(int32(2)),
        MachineNumber(int32(3)),
        MachineNumber(int32(4)),
    ])
Example #7
0
def test_interpret_assignment_1_exit() -> None:
    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,
    )
Example #8
0
def test_interpret_method_definition_exit() -> None:
    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)
    }
Example #9
0
def test_interpret() -> None:
    block = SyntaxBlock(
        [
            SyntaxExpression(
                [
                    SyntaxNumber(int32(1)),
                    SyntaxNumber(int32(2)),
                    SyntaxNumber(int32(3)),
                ]
            )
        ]
    )
    expression_stack = MachineExpressionStack([])
    environment = test_environment()
    interpret(block, expression_stack, environment)
    assert expression_stack == MachineExpressionStack(
        [
            MachineNumber(int32(1)),
            MachineNumber(int32(2)),
            MachineNumber(int32(3)),
        ]
    )