retiring ClassField and class_field from typed layer

Creating classes is the responsibility of melon
This commit is contained in:
Torsten Ruger 2016-12-16 19:05:49 +02:00
parent dd6dede6ef
commit f8cefcde3c
9 changed files with 19 additions and 50 deletions

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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")))))))))

View File

@ -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

View File

@ -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 ,

View File

@ -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

View File

@ -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 ,

View File

@ -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"