fix class def tests and code

This commit is contained in:
Torsten Ruger 2016-12-10 22:41:49 +02:00
parent aa5641a29b
commit e82c86f6c1
2 changed files with 38 additions and 34 deletions

View File

@ -9,7 +9,8 @@ module Typed
index = for_class.instance_type.variable_index(statement.name) index = for_class.instance_type.variable_index(statement.name)
raise "class field already defined:#{name} for class #{for_class.name}" if index raise "class field already defined:#{name} for class #{for_class.name}" if index
for_class.instance_type.add_instance_variable( statement.name , statement.type ) #FIXME should not hack into current type, but create a new
for_class.instance_type.send(:private_add_instance_variable, statement.name , statement.type)
return nil # statements don't reurn values, only expressions return nil # statements don't reurn values, only expressions
end end

View File

@ -1,39 +1,40 @@
require_relative 'helper' require_relative 'helper'
module Register module Register
class TestClassStatements #< MiniTest::Test class TestClassStatements < MiniTest::Test
include Statements 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)))))
end
def test_class_defs def test_class_defs
@input = <<HERE class_def
class Bar @input = s(:statements, s(:return, s(:int, 1)))
int self.buh()
return 1
end
end
class Space
int main()
return 1
end
end
HERE
@expect = [Label, LoadConstant,SetSlot,Label,FunctionReturn] @expect = [Label, LoadConstant,SetSlot,Label,FunctionReturn]
check check
end end
def test_class_call def test_class_call
@input = <<HERE class_def
class Bar @input = s(:statements,
int self.buh() s(:return,
return 1 s(:call,
end s(:name, :buh),
end s(:arguments),
class Space s(:receiver,
int main() s(:class_name, :Bar)))))
return Bar.buh()
end
end
HERE
@expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant , @expect = [Label, GetSlot, LoadConstant, SetSlot, LoadConstant, SetSlot, LoadConstant ,
SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer , SetSlot, LoadConstant, SetSlot, RegisterTransfer, FunctionCall, Label, RegisterTransfer ,
GetSlot, GetSlot, SetSlot, Label, FunctionReturn] GetSlot, GetSlot, SetSlot, Label, FunctionReturn]
@ -41,14 +42,16 @@ HERE
end end
def test_class_field def test_class_field
@input = <<HERE clean_compile s(:statements,
class Space s(:class, :Space,
field int boo2 s(:derives, nil),
int main() s(:statements,
return self.boo2 s(:class_field, :Integer, :boo2))))
end
end @input = s(:statements,
HERE s(:return,
s(:field_access, s(:receiver, s(:name, :self)),
s(:field, s(:name, :boo2)))))
@expect = [Label, GetSlot,GetSlot,SetSlot,Label,FunctionReturn] @expect = [Label, GetSlot,GetSlot,SetSlot,Label,FunctionReturn]
check check
end end