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_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 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_interpret_expression_enter() -> None: 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, )
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_identifier_value() -> None: 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( [cast(MachineValue, PI.value_or_call)] ), term_index=1, )
def test_interpret_block_exit() -> None: 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([])
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_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_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_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) }