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

View File

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