adding types to layout

so we can test assignment
This commit is contained in:
Torsten Ruger 2015-10-29 12:45:29 +02:00
parent e4201143b3
commit 7d7b7ca995
7 changed files with 51 additions and 38 deletions

View File

@ -33,7 +33,7 @@ module Parfait
def initialize( object_class )
super()
add_instance_variable :layout
add_instance_variable :layout ,:Layout
self.object_class = object_class
end
@ -47,30 +47,40 @@ module Parfait
#
# TODO , later we would need to COPY the layout to keep the old constant
# but now we are concerned with booting, ie getting a working structure
def add_instance_variable name
def add_instance_variable name , type
raise "Name shouldn't be nil" unless name
raise "Type shouldn't be nil" unless type
self.push(name)
self.push(type)
self.get_length
end
def instance_names
names = List.new
name = true
each do |item|
names.push item
names.push(item) if name
name = ! name
end
names
end
def instance_length
self.get_length
self.get_length / 2
end
alias :super_index :index_of
def index_of(name)
raise "Use variable_index instead"
end
# index of the variable when using internal_object_get
# (internal_object_get is 1 based and 1 is always the layout)
def variable_index name
has = index_of(name)
has = super_index(name)
return nil unless has
raise "internal error #{name}:#{has}" if has < 1
has
1 + has / 2
end
def inspect

View File

@ -93,8 +93,8 @@ module Register
# helper to create a Layout, name is the parfait name, ie :Layout
def layout_for( name , ivars )
l = Parfait::Layout.allocate.fake_init
l.add_instance_variable :layout
ivars.each {|n| l.add_instance_variable n }
l.add_instance_variable :layout , :Layout
ivars.each {|n,t| l.add_instance_variable( n , t) }
l
end
@ -112,23 +112,26 @@ module Register
# unfortuantely that constant condenses every detail about the system, class names
# and all instance variable names. Really have to find a better way
def layout_names
{ :Word => [:char_length] ,
:List => [:indexed_length] ,
{ :Word => {:char_length => :Integer} ,
:List => {:indexed_length => :Integer} ,
# Assumtion is that name is the last of message
:Message => [ :next_message , :receiver , :frame , :return_address ,
:return_value, :caller , :name , :indexed_length ],
:MetaClass => [:me],
:Integer => [],
:Object => [],
:Kernel => [], #fix, kernel is a class, but should be a module
:BinaryCode => [],
:Space => [:classes , :first_message ],
:Frame => [:next_frame , :indexed_length],
:Layout => [:object_class,:instance_methods,:indexed_length] ,
:Class => [:object_layout , :name , :instance_methods , :super_class_name ],
:Dictionary => [:keys , :values ] ,
:Method => [:name , :source , :instructions , :binary ,:arguments , :for_class, :locals ] ,
:Variable => [:type , :name , :value ]
:Message => { :next_message => :Message, :receiver => :Object, :frame => :Frame ,
:return_address => :Integer, :return_value => :Integer,
:caller => :Message , :name => :Word , :indexed_length => :Integer },
:MetaClass => {:me => :Class},
:Integer => {},
:Object => {},
:Kernel => {}, #fix, kernel is a class, but should be a module
:BinaryCode => {},
:Space => {:classes => :Dictionary , :first_message => :Message},
:Frame => {:next_frame => :Frame, :indexed_length => :Integer},
:Layout => {:object_class => :Class, :instance_methods => :List , :indexed_length => :Integer} ,
:Class => {:object_layout => :Layout, :name => :Word, :instance_methods => :List,
:super_class_name => :Word},
:Dictionary => {:keys => :List , :values => :List } ,
:Method => {:name => :Word, :source => :Object, :instructions => :Object, :binary => :Object,
:arguments => :List , :for_class => :Class, :locals => :List } ,
:Variable => {:type => :Class, :name => :Word , :value => :Object}
}
end

View File

@ -10,7 +10,7 @@ module Soml
index = for_class.object_layout.variable_index(name)
#raise "class field already defined:#{name} for class #{for_class.name}" if index
#puts "Define field #{name} on class #{for_class.name}"
index = for_class.object_layout.add_instance_variable( name ) #TODO need typing
index = for_class.object_layout.add_instance_variable( name , type )
# not sure how to run class code yet. later
raise "value #{value}" if value

View File

@ -12,14 +12,14 @@ module Soml
process(condition)
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
true_block = Register::Label.new(statement, "if_true")
true_block = Register::Label.new(if_true, "if_true")
add_code branch_class.new( condition , true_block )
# compile the false block
reset_regs
process_all(if_false) if if_false
merge = Register::Label.new(statement , "if_merge")
add_code Register::Branch.new(statement, merge )
add_code Register::Branch.new(if_false, merge )
# compile the true block
add_code true_block

View File

@ -17,7 +17,7 @@ HERE
end
def test_field
Register.machine.space.get_class_by_name(:Object).object_layout.add_instance_variable(:bro)
Register.machine.space.get_class_by_name(:Object).object_layout.add_instance_variable(:bro,:Object)
@root = :field_access
@string_input = <<HERE
self.bro

View File

@ -37,16 +37,16 @@ class TestLayout < MiniTest::Test
end
def test_layout_length
assert_equal 9 , @mess.get_layout.indexed_length , @mess.get_layout.inspect
assert_equal 9 , @mess.get_layout.internal_object_get(4)
assert_equal 9 , @mess.get_layout.instance_length , @mess.get_layout.inspect
assert_equal 18 , @mess.get_layout.internal_object_get(4)
end
def test_layout_length_index
assert_equal 4 , @mess.get_layout.get_layout.variable_index(:indexed_length)
assert_equal 4 , @mess.get_layout.get_layout.get_offset
assert_equal 4 , @mess.get_layout.get_offset
assert_equal 4 , @mess.get_layout.get_layout.indexed_length
assert_equal 4 , @mess.get_layout.get_layout.internal_object_get(4)
assert_equal 8 , @mess.get_layout.get_layout.indexed_length
assert_equal 8 , @mess.get_layout.get_layout.internal_object_get(4)
end
def test_layout_methods
@ -78,11 +78,11 @@ class TestLayout < MiniTest::Test
def test_add_name
layout = Parfait::Layout.new Register.machine.space.get_class_by_name(:Layout)
layout.add_instance_variable :boo
layout.add_instance_variable :boo , :Object
assert_equal 2 , layout.variable_index(:boo)
assert_equal 2 , layout.get_length
assert_equal 4 , layout.get_length
assert_equal :layout , layout.get(1)
assert_equal :boo , layout.get(2)
assert_equal :boo , layout.get(3)
layout
end
@ -93,8 +93,8 @@ class TestLayout < MiniTest::Test
def test_each
layout = test_add_name
assert_equal 2 , layout.get_length
counter = [:boo , :layout]
assert_equal 4 , layout.get_length
counter = [:boo , :Object, :layout , :Layout]
layout.each do |item|
assert_equal item , counter.delete(item)
end

View File

@ -13,7 +13,7 @@ class TestPositioning < MiniTest::Test
list = Register.new_list([1,2,3,4,5])
list.set_layout( Parfait::Layout.new Object)
# TODO check why this is 64 and not 32
assert_equal 32 , list.word_length
assert_equal 64 , list.word_length
end
def test_layout
layout = Parfait::Layout.new Object