remove FieldDef

and on_field_def , :field_def and fixed all related tests
local variables will have to be determined outside the typed layer
This commit is contained in:
Torsten Ruger 2016-12-17 13:12:49 +02:00
parent c12f11fba5
commit 8845b91529
9 changed files with 38 additions and 48 deletions

View File

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

View File

@ -6,9 +6,5 @@ module Typed
@name , @value = n , v
end
end
class FieldDef < Statement
attr_accessor :name , :type , :value
end
end
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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