diff --git a/lib/typed/compiler.rb b/lib/typed/compiler.rb index 5e3d681f..1bdff9d4 100644 --- a/lib/typed/compiler.rb +++ b/lib/typed/compiler.rb @@ -2,7 +2,7 @@ require_relative "tree" module Typed - CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" , + CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_statement" , "collections" , "field_def" , "field_access", "function_statement" , "if_statement" , "name_expression" , "operator_expression" , "return_statement", "statement_list", @@ -92,7 +92,7 @@ module Typed # class_name and method_name are pretty clear, args are given as a ruby array 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 + 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/compiler/class_field.rb b/lib/typed/compiler/class_field.rb deleted file mode 100644 index 19c8e138..00000000 --- a/lib/typed/compiler/class_field.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Typed - module ClassField - - def on_ClassField statement - #type , name , value = *statement - - for_type = @type - raise "no type" unless for_type - index = for_type.variable_index(statement.name) - raise "class field already defined:#{name} for class #{for_type}" if index - - #FIXME should not hack into current type, but create a new - for_type.send(:private_add_instance_variable, statement.name , statement.type) - - return nil # statements don't reurn values, only expressions - end - end -end diff --git a/lib/typed/tree/to_code.rb b/lib/typed/tree/to_code.rb index 26c07e96..ecac55f4 100644 --- a/lib/typed/tree/to_code.rb +++ b/lib/typed/tree/to_code.rb @@ -49,14 +49,6 @@ module Typed w end - def on_class_field statement - type , name = *statement - w = Tree::ClassField.new() - w.type = type - w.name = name - w - end - def on_while_statement statement branch_type , condition , statements = *statement w = Tree::WhileStatement.new() diff --git a/test/elf/test_hello.rb b/test/elf/test_hello.rb index 997d62f8..b2e63ee5 100644 --- a/test/elf/test_hello.rb +++ b/test/elf/test_hello.rb @@ -13,7 +13,7 @@ class HelloTest < MiniTest::Test end def test_string_put - @input = s(:statements, s(:class, :Object, s(:derives, nil), + @input = s(:statements, s(:class, :Space, s(:derives, nil), s(:statements, s(:function, :Integer, s(:name, :main), s(:parameters), s(:statements, s(:return, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "Hello again\\n"))))))))) diff --git a/test/typed/statements/helper.rb b/test/typed/statements/helper.rb index 03bc7ab7..fa3afc7b 100644 --- a/test/typed/statements/helper.rb +++ b/test/typed/statements/helper.rb @@ -8,11 +8,11 @@ module Statements Register.machine.boot # force boot to reset main end - def clean_compile(statements) - compiler = Typed::Compiler.new + def clean_compile(clazz_name , method_name , args , statements) + compiler = Typed::Compiler.new.create_method(clazz_name,method_name,args ).init_method compiler.process( Typed.ast_to_code( statements ) ) end - + def check assert @expect , "No output given" compiler = Typed::Compiler.new Register.machine.space.get_main diff --git a/test/typed/statements/test_call.rb b/test/typed/statements/test_call.rb index 46455374..eafd3c5e 100644 --- a/test/typed/statements/test_call.rb +++ b/test/typed/statements/test_call.rb @@ -5,7 +5,7 @@ class TestCallStatement < MiniTest::Test include Statements def test_call_constant_int - clean_compile s(:statements, s(:class, :Integer, s(:derives, nil), s(:statements, s(:function, :Integer, s(:name, :putint), s(:parameters), s(:statements, s(:return, s(:int, 1))))))) + clean_compile :Integer, :putint, {}, s(:statements, s(:return, s(:int, 1))) @input = s(:call, s(:name, :putint), s(:arguments), s(:receiver, s(:int, 42))) @expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant , SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer , @@ -15,7 +15,7 @@ class TestCallStatement < MiniTest::Test def test_call_constant_string - clean_compile s(:statements, s(:class, :Word, s(:derives, nil), s(:statements, s(:function, :Integer, s(:name, :putstring), s(:parameters), s(:statements, s(:return, s(:int, 1))))))) + clean_compile :Word, :putstring,{}, s(:statements, s(:return, s(:int, 1))) @input =s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "Hello"))) @expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant , @@ -25,7 +25,7 @@ class TestCallStatement < MiniTest::Test end def _test_call_local_int - clean_compile s(:statements, s(:class, :Integer, s(:derives, nil), s(:statements, s(:function, :Integer, s(:name, :putint), s(:parameters), s(:statements, s(:return, s(:int, 1))))))) + 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)))) @expect = [Label, LoadConstant, GetSlot, SetSlot, GetSlot, GetSlot, GetSlot , @@ -36,7 +36,7 @@ class TestCallStatement < MiniTest::Test end def test_call_local_class - clean_compile s(:statements, s(:class, :List, s(:derives, :Object), s(:statements, s(:function, :Integer, s(:name, :add), s(:parameters), s(:statements, s(:return, s(:int, 1))))))) + 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)))) @expect = [Label, GetSlot, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot , diff --git a/test/typed/statements/test_class.rb b/test/typed/statements/test_class.rb index 387b9de9..c93225ea 100644 --- a/test/typed/statements/test_class.rb +++ b/test/typed/statements/test_class.rb @@ -5,7 +5,7 @@ class TestClassStatements < MiniTest::Test include Statements def class_def - clean_compile s(:statements, s(:class, :Bar, s(:derives, nil), s(:statements, s(:function, :Integer, s(:name, :buh), s(:parameters), s(:statements, s(:return, s(:int, 1))), s(:receiver, :self))))) + clean_compile :Bar, :buh, {}, s(:statements, s(:return, s(:int, 1))) end def test_class_defs @@ -27,12 +27,12 @@ class TestClassStatements < MiniTest::Test end def test_class_field - clean_compile s(:statements, s(:class, :Space, s(:derives, nil), s(:statements, s(:class_field, :Integer, :boo2)))) - - @input =s(:statements, s(:return, s(:field_access, s(:receiver, s(:name, :self)), - s(:field,s(:name, :boo2))))) - @expect = [Label, GetSlot,GetSlot,SetSlot,Label,FunctionReturn] - check + # clean_compile :Space, s(:class_field, :Integer, :boo2) + #FIXME class_field handling unclear at the moment + # @input =s(:statements, s(:return, s(:field_access, s(:receiver, s(:name, :self)), + # s(:field,s(:name, :boo2))))) + # @expect = [Label, GetSlot,GetSlot,SetSlot,Label,FunctionReturn] + # check end end end diff --git a/test/typed/statements/test_fields.rb b/test/typed/statements/test_fields.rb index c605cbe8..dcf59d95 100644 --- a/test/typed/statements/test_fields.rb +++ b/test/typed/statements/test_fields.rb @@ -13,10 +13,9 @@ module Register end def test_field_arg - clean_compile s(:statements, s(:class, :Space, s(:derives, nil), s(:statements, - s(:function, :Integer, s(:name, :get_name), s(:parameters, s(:parameter, :Message, :main)), + clean_compile :Space, :get_name, { :main => :Message}, s(:statements, s(:return, s(:field_access, - s(:receiver, s(:name, :main)), s(:field, s(:name, :name))))))))) + 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))))) @expect = [Label, GetSlot, GetSlot, SetSlot, LoadConstant, SetSlot, LoadConstant , diff --git a/test/typed/test_to_code.rb b/test/typed/test_to_code.rb index 12dc42a3..59fc9a0d 100644 --- a/test/typed/test_to_code.rb +++ b/test/typed/test_to_code.rb @@ -16,10 +16,6 @@ class ToCodeTest < MiniTest::Test @statement = s(:field_def, :Integer, s(:name, :abba), s(:int, 5)) check "FieldDef" end - def test_class_field - @statement = s(:class_field, :Integer, :fff, s(:int, 3)) - check "ClassField" - end def test_simple_while @statement = s(:while_statement, :false, s(:conditional,s(:int, 1)), s(:statements)) check "WhileStatement"