예제 #1
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)
예제 #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))])
예제 #3
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)),
    ])
예제 #4
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)),
        ]
    )
예제 #5
0
def main_run(
    source_filename: str,
    environment: Optional[MachineEnvironment] = None,
) -> None:
    with open(source_filename, "r") as source_file:
        source_text = source_file.read()
    root_block = parse(source_text)
    root_expression_stack = MachineExpressionStack([])
    if not environment:
        environment = base_environment()
    interpret(root_block, root_expression_stack, environment)
    assert not root_expression_stack, root_expression_stack
    assert "main" in environment, environment.bindings
    main_call = environment["main"]
    assert isinstance(main_call, GuestCall), main_call
    main_expression_stack = MachineExpressionStack([])
    interpret(
        main_call.definition_block,
        main_expression_stack,
        main_call.definition_environment,
    )
    if main_expression_stack.values:
        print(main_expression_stack.values)
예제 #6
0
 def statement_loop(statement_text: str, ) -> list[MachineValue]:
     block = parse(statement_text)
     expression_stack = MachineExpressionStack([])
     interpret(block, expression_stack, environment)
     return expression_stack.values