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))] ), )
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))]), )
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, )
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))])
def test_interpret_comment() -> None: 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, )
def test_interpret_identifier_call() -> None: 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, )
def test_interpret_expression_exit() -> None: statement = SyntaxExpression([SyntaxNumber(int32(100))]) frame_stack = init_test_frame_stack( init_statement_block(statement), MachineExpressionStack([MachineNumber(int32(100))]), term_index=1, ) interpret_statement(frame_stack) assert frame_stack == init_test_frame_stack( init_statement_block(statement), MachineExpressionStack([MachineNumber(int32(100))]), statement_index=1, term_index=0, )
def interpret_number_term( frame_stack: MachineFrameStack, number: SyntaxNumber[NBitBase], ) -> None: expression_stack = frame_stack.current.expression_stack expression_stack.push(MachineNumber(number.value)) next_term(frame_stack)
def test_add_blob(number: integer[NBitBase], expected: bytes) -> None: call_test( add, [MachineBlob(bytearray()), MachineNumber(number)], [MachineBlob(bytearray(expected))], )
def test_interpret_assignment_1_enter() -> 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) 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))]))
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, )
def test_interpret_block_enter() -> None: 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 from_python(value: Any) -> MachineValue: if isinstance(value, IOBase): return MachineStream(value) elif isinstance(value, MachineValue): return value elif isinstance(value, bytearray): return MachineBlob(value) elif isinstance(value, integer): return MachineNumber(value) else: raise AssertionError(value)
def test_interpret_number() -> None: 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, )
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)), ])
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)), ] )
def test_sqrt() -> None: call_test( sqrt, [MachineNumber(int32(4))], [MachineNumber(int32(2))], )