def test_call_generate_values(): 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 call_test(binding, args, results): 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)
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, )
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, )
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_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, )
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, )
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, )
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(): 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_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, )
def interpret_assignment_enter(frame_stack: MachineFrameStack, ) -> None: old_frame = frame_stack.current new_frame = MachineFrame( old_frame.instruction_pointer, MachineExpressionStack([]), old_frame.environment, ) frame_stack.push(new_frame)
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))]))
def test_call_delegate_to_host_call(): 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_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([])
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))]), )
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) }
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)
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))] ), )
def statement_loop(statement_text): block = parse(statement_text) expression_stack = MachineExpressionStack([]) interpret(block, expression_stack, environment) return expression_stack.values