diff --git a/lib/typed/compiler.rb b/lib/typed/compiler.rb index fd6f7fde..ccb13535 100644 --- a/lib/typed/compiler.rb +++ b/lib/typed/compiler.rb @@ -3,7 +3,7 @@ require_relative "tree" module Typed CompilerModules = [ "assignment" , "basic_values" , "call_site", - "class_statement" , "collections" , "field_def" , "field_access", + "class_statement" , "collections" , "field_access", "function_statement" , "if_statement" , "name_expression" , "operator_expression" , "return_statement", "statement_list", "while_statement"] @@ -98,7 +98,6 @@ module Typed def create_method( class_name , method_name , args = {}) raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol clazz = Register.machine.space.get_class_by_name! class_name - raise "No such class #{class_name}" unless clazz create_method_for( clazz.instance_type , method_name , args) end diff --git a/lib/typed/tree/assignment.rb b/lib/typed/tree/assignment.rb index 499d6358..d8d12e26 100644 --- a/lib/typed/tree/assignment.rb +++ b/lib/typed/tree/assignment.rb @@ -6,9 +6,5 @@ module Typed @name , @value = n , v end end - - class FieldDef < Statement - attr_accessor :name , :type , :value - end end end diff --git a/lib/typed/tree/to_code.rb b/lib/typed/tree/to_code.rb index ecac55f4..b94cae2a 100644 --- a/lib/typed/tree/to_code.rb +++ b/lib/typed/tree/to_code.rb @@ -40,15 +40,6 @@ module Typed params end - def on_field_def statement - type , name , value = *statement - w = Tree::FieldDef.new() - w.type = type - w.name = process(name) - w.value = process(value) if value - w - end - def on_while_statement statement branch_type , condition , statements = *statement w = Tree::WhileStatement.new() diff --git a/test/typed/statements/test_assign.rb b/test/typed/statements/test_assign.rb index f9824a98..02235c29 100644 --- a/test/typed/statements/test_assign.rb +++ b/test/typed/statements/test_assign.rb @@ -4,12 +4,10 @@ module Register class TestAssignStatement < MiniTest::Test include Statements - def setup - Register.machine.boot - end - def test_assign_op - @input = s(:statements, s(:field_def, :Integer, s(:name, :n), s(:operator_value, :+, s(:int, 10), s(:int, 1)))) + Register.machine.space.get_main.add_local(:r , :Integer) + + @input = s(:statements, s(:assignment, s(:name, :r), s(:operator_value, :+, s(:int, 10), s(:int, 1)))) @expect = [Label, LoadConstant, LoadConstant, OperatorInstruction, GetSlot, SetSlot, Label , FunctionReturn] @@ -17,21 +15,25 @@ class TestAssignStatement < MiniTest::Test end def test_assign_local - @input =s(:statements, s(:field_def, :Integer, s(:name, :runner)), s(:assignment, s(:name, :runner), s(:int, 5))) + Register.machine.space.get_main.add_local(:r , :Integer) + @input =s(:statements, s(:assignment, s(:name, :r), s(:int, 5))) @expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn] check end def test_assign_local_assign - @input = s(:statements, s(:field_def, :Integer, s(:name, :runner), s(:int, 5))) + Register.machine.space.get_main.add_local(:r , :Integer) + + @input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5))) @expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn] check end def test_assign_call - @input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:call, s(:name, :main), s(:arguments)))) + Register.machine.space.get_main.add_local(:r , :Integer) + @input = s(:statements, s(:assignment, s(:name, :r), s(:call, s(:name, :main), s(:arguments)))) @expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant , SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer , GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn] @@ -39,7 +41,8 @@ class TestAssignStatement < MiniTest::Test end def test_frame_get - @input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:int, 5)), s(:return, s(:name, :r))) + Register.machine.space.get_main.add_local(:r , :Integer) + @input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)), s(:return, s(:name, :r))) @expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, SetSlot , Label, FunctionReturn] was = check @@ -49,7 +52,8 @@ class TestAssignStatement < MiniTest::Test end def test_assign_int - @input = s(:statements, s(:field_def, :Integer, s(:name, :r), s(:int, 5)) ) + Register.machine.space.get_main.add_local(:r , :Integer) + @input = s(:statements, s(:assignment, s(:name, :r), s(:int, 5)) ) @expect = [Label, LoadConstant, GetSlot, SetSlot, Label, FunctionReturn] was = check set = was.next(3) diff --git a/test/typed/statements/test_call.rb b/test/typed/statements/test_call.rb index 8c3624cb..6724046f 100644 --- a/test/typed/statements/test_call.rb +++ b/test/typed/statements/test_call.rb @@ -25,8 +25,9 @@ class TestCallStatement < MiniTest::Test end def _test_call_local_int + Register.machine.space.get_main.add_local(:testi , :Integer) clean_compile :Integer, :putint, {}, s(:statements, s(:return, s(:int, 1))) - @input = s(:statements, s(:field_def, :Integer, s(:name, :testi), s(:int, 20)), s(:call, s(:name, :putint), s(:arguments), s(:receiver, s(:name, :testi)))) + @input = s(:statements, s(:assignment, s(:name, :testi), s(:int, 20)), s(:call, s(:name, :putint), s(:arguments), s(:receiver, s(:name, :testi)))) @expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, GetSlot , SetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot , @@ -36,9 +37,10 @@ class TestCallStatement < MiniTest::Test end def test_call_local_class + Register.machine.space.get_main.add_local(:test_l , :List) clean_compile :List, :add, {}, s(:statements, s(:return, s(:int, 1))) - @input =s(:statements, s(:field_def, :List, s(:name, :test_l)), s(:call, s(:name, :add), s(:arguments), s(:receiver, s(:name, :test_l)))) + @input =s(:statements, s(:call, s(:name, :add), s(:arguments), s(:receiver, s(:name, :test_l)))) @expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot , LoadConstant, SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label , RegisterTransfer, GetSlot, GetSlot, Label, FunctionReturn] diff --git a/test/typed/statements/test_fields.rb b/test/typed/statements/test_fields.rb index dcf59d95..ed32840e 100644 --- a/test/typed/statements/test_fields.rb +++ b/test/typed/statements/test_fields.rb @@ -6,17 +6,19 @@ module Register include Statements def test_field_frame - @input = s(:statements, s(:field_def, :Message, s(:name, :m)), - s(:return, s(:field_access, s(:receiver, s(:name, :m)), s(:field, s(:name, :name))))) + Register.machine.space.get_main.add_local( :m , :Message) + @input = s(:statements, s(:return, s(:field_access, + s(:receiver, s(:name, :m)), s(:field, s(:name, :name))))) @expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, Label, FunctionReturn] check end def test_field_arg + Register.machine.space.get_main.add_local( :m , :Message) clean_compile :Space, :get_name, { :main => :Message}, s(:statements, s(:return, s(:field_access, s(:receiver, s(:name, :main)), s(:field, s(:name, :name))))) - @input =s(:statements, s(:field_def, :Message, s(:name, :m)), s(:return, s(:call, s(:name, :get_name), s(:arguments, s(:name, :m))))) + @input =s(:statements, s(:return, s(:call, s(:name, :get_name), s(:arguments, s(:name, :m))))) @expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant , SetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, RegisterTransfer , @@ -25,16 +27,9 @@ module Register check end - def test_self_field - @input = s(:statements, s(:field_def, :Type, s(:name, :l), s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :type)))), s(:return, s(:int, 1))) - - @expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot , - Label, FunctionReturn] - check - end - def test_message_field - @input = s(:statements, s(:field_def, :Word, s(:name, :name), s(:field_access, s(:receiver, s(:name, :message)), s(:field, s(:name, :name)))), s(:return, s(:name, :name))) + Register.machine.space.get_main.add_local(:name , :Word) + @input = s(:statements, s(:assignment, s(:name, :name), s(:field_access, s(:receiver, s(:name, :message)), s(:field, s(:name, :name)))), s(:return, s(:name, :name))) @expect = [Label, RegisterTransfer, GetSlot, GetSlot, SetSlot, GetSlot, GetSlot , SetSlot, Label, FunctionReturn] diff --git a/test/typed/statements/test_return.rb b/test/typed/statements/test_return.rb index f1233008..b3180fb8 100644 --- a/test/typed/statements/test_return.rb +++ b/test/typed/statements/test_return.rb @@ -11,13 +11,15 @@ class TestReturnStatement < MiniTest::Test end def test_return_local - @input = s(:statements,s(:field_def, :Integer, s(:name, :runner)), s(:return, s(:name, :runner))) + Register.machine.space.get_main.add_local(:runner , :Integer) + @input = s(:statements, s(:return, s(:name, :runner))) @expect = [Label, GetSlot,GetSlot ,SetSlot,Label,FunctionReturn] check end def test_return_local_assign - @input = s(:statements, s(:field_def, :Integer, s(:name, :runner), s(:int, 5)), s(:return, s(:name, :runner))) + Register.machine.space.get_main.add_local(:runner , :Integer) + @input = s(:statements, s(:assignment, s(:name, :runner), s(:int, 5)), s(:return, s(:name, :runner))) @expect = [Label, LoadConstant,GetSlot,SetSlot,GetSlot,GetSlot ,SetSlot, Label,FunctionReturn] check @@ -32,7 +34,8 @@ class TestReturnStatement < MiniTest::Test end def pest_return_space_length # need to add runtime first - @input = s(:statements, s(:field_def, :Type, s(:name, :l), s(:call, s(:name, :get_type), s(:arguments), s(:receiver, s(:name, :space)))), s(:return, s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :runner))))) + Register.machine.space.get_main.add_local(:l , :Type) + @input = s(:statements, s(:assignment, s(:name, :l), s(:call, s(:name, :get_type), s(:arguments), s(:receiver, s(:name, :space)))), s(:return, s(:field_access, s(:receiver, s(:name, :self)), s(:field, s(:name, :runner))))) @expect = [Label, GetSlot,GetSlot ,SetSlot,Label,FunctionReturn] check end diff --git a/test/typed/statements/test_while.rb b/test/typed/statements/test_while.rb index 146a7618..1d6dd0b6 100644 --- a/test/typed/statements/test_while.rb +++ b/test/typed/statements/test_while.rb @@ -14,7 +14,9 @@ module Register end def test_while_assign - @input = s(:statements, s(:field_def, :Integer, s(:name, :n), s(:int, 5)), s(:while_statement, :plus, s(:conditional, s(:name, :n)), s(:statements, s(:assignment, s(:name, :n), s(:operator_value, :-, s(:name, :n), s(:int, 1))))), s(:return, s(:name, :n))) + Register.machine.space.get_main.add_local(:n , :Integer) + + @input = s(:statements, s(:assignment, s(:name, :n), s(:int, 5)), s(:while_statement, :plus, s(:conditional, s(:name, :n)), s(:statements, s(:assignment, s(:name, :n), s(:operator_value, :-, s(:name, :n), s(:int, 1))))), s(:return, s(:name, :n))) @expect = [Label, LoadConstant, GetSlot, SetSlot, Branch, Label, GetSlot , GetSlot, LoadConstant, OperatorInstruction, GetSlot, SetSlot, Label, GetSlot , @@ -24,7 +26,9 @@ module Register def test_while_return - @input = s(:statements, s(:field_def, :Integer, s(:name, :n), s(:int, 10)), s(:while_statement, :plus, s(:conditional, s(:operator_value, :-, s(:name, :n), s(:int, 5))), s(:statements, s(:assignment, s(:name, :n), s(:operator_value, :+, s(:name, :n), s(:int, 1))), s(:return, s(:name, :n))))) + Register.machine.space.get_main.add_local(:n , :Integer) + + @input = s(:statements, s(:assignment, s(:name, :n), s(:int, 10)), s(:while_statement, :plus, s(:conditional, s(:operator_value, :-, s(:name, :n), s(:int, 5))), s(:statements, s(:assignment, s(:name, :n), s(:operator_value, :+, s(:name, :n), s(:int, 1))), s(:return, s(:name, :n))))) @expect = [Label, LoadConstant, GetSlot, SetSlot, Branch, Label, GetSlot , GetSlot, LoadConstant, OperatorInstruction, GetSlot, SetSlot, GetSlot, GetSlot , diff --git a/test/typed/test_to_code.rb b/test/typed/test_to_code.rb index 59fc9a0d..f4abc2d1 100644 --- a/test/typed/test_to_code.rb +++ b/test/typed/test_to_code.rb @@ -12,10 +12,6 @@ class ToCodeTest < MiniTest::Test @statement = s(:field_access, s(:receiver, s(:name, :m)), s(:field, s(:name, :index))) check "FieldAccess" end - def test_field_def_value - @statement = s(:field_def, :Integer, s(:name, :abba), s(:int, 5)) - check "FieldDef" - end def test_simple_while @statement = s(:while_statement, :false, s(:conditional,s(:int, 1)), s(:statements)) check "WhileStatement"