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 ) def initialize( object_class )
super() super()
add_instance_variable :layout add_instance_variable :layout ,:Layout
self.object_class = object_class self.object_class = object_class
end end
@ -47,30 +47,40 @@ module Parfait
# #
# TODO , later we would need to COPY the layout to keep the old constant # 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 # 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(name)
self.push(type)
self.get_length self.get_length
end end
def instance_names def instance_names
names = List.new names = List.new
name = true
each do |item| each do |item|
names.push item names.push(item) if name
name = ! name
end end
names names
end end
def instance_length 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 end
# index of the variable when using internal_object_get # index of the variable when using internal_object_get
# (internal_object_get is 1 based and 1 is always the layout) # (internal_object_get is 1 based and 1 is always the layout)
def variable_index name def variable_index name
has = index_of(name) has = super_index(name)
return nil unless has return nil unless has
raise "internal error #{name}:#{has}" if has < 1 raise "internal error #{name}:#{has}" if has < 1
has 1 + has / 2
end end
def inspect def inspect

View File

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

View File

@ -10,7 +10,7 @@ module Soml
index = for_class.object_layout.variable_index(name) index = for_class.object_layout.variable_index(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
#puts "Define field #{name} on class #{for_class.name}" #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 # not sure how to run class code yet. later
raise "value #{value}" if value raise "value #{value}" if value

View File

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

View File

@ -17,7 +17,7 @@ HERE
end end
def test_field 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 @root = :field_access
@string_input = <<HERE @string_input = <<HERE
self.bro self.bro

View File

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

View File

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