mostly moving to ruby instance variables

This commit is contained in:
Torsten Ruger 2016-12-29 18:51:24 +02:00
parent e3de4efe02
commit e69d9a492e
3 changed files with 55 additions and 34 deletions

View File

@ -21,26 +21,27 @@ module Parfait
class Space < Object class Space < Object
def initialize def initialize(classes )
raise "Space can not be instantiated by new, you'd need a space to do so. Chicken and egg" @classes = classes
end @types = Dictionary.new
attributes [:classes , :types, :first_message]
# need a two phase init for the object space (and generally parfait) because the space
# is an interconnected graph, so not everthing is ready
def late_init
message = Message.new(nil) message = Message.new(nil)
50.times do 50.times do
self.first_message = Message.new message @first_message = Message.new message
#puts "INIT caller #{message.object_id} to #{self.first_message.object_id}" #puts "INIT caller #{message.object_id} to #{@first_message.object_id}"
message.set_caller self.first_message message.set_caller @first_message
message = self.first_message message = @first_message
end end
classes.each do |name , cl| @classes.each do |name , cl|
types[cl.instance_type.hash] = cl.instance_type @types[cl.instance_type.hash] = cl.instance_type
end end
end end
def self.attributes
[:classes , :types, :first_message]
end
attr_reader :types , :classes , :first_message
# Make the object space globally available # Make the object space globally available
def self.object_space def self.object_space
@@object_space @@object_space
@ -65,8 +66,8 @@ module Parfait
# return nili if no such class. Use bang version if create should be implicit # return nili if no such class. Use bang version if create should be implicit
def get_class_by_name( name ) def get_class_by_name( name )
raise "get_class_by_name #{name}.#{name.class}" unless name.is_a?(Symbol) raise "get_class_by_name #{name}.#{name.class}" unless name.is_a?(Symbol)
c = self.classes[name] c = @classes[name]
#puts "MISS, no class #{name} #{name.class}" unless c # " #{self.classes}" #puts "MISS, no class #{name} #{name.class}" unless c # " #{@classes}"
#puts "CLAZZ, #{name} #{c.get_type.get_length}" if c #puts "CLAZZ, #{name} #{c.get_type.get_length}" if c
c c
end end
@ -76,16 +77,18 @@ module Parfait
def get_class_by_name! name def get_class_by_name! name
c = get_class_by_name(name) c = get_class_by_name(name)
return c if c return c if c
create_class name , get_class_by_name(:Object) create_class name
end end
# this is the way to instantiate classes (not Parfait::Class.new) # this is the way to instantiate classes (not Parfait::Class.new)
# so we get and keep exactly one per name # so we get and keep exactly one per name
def create_class name , superclass def create_class( name , superclass = nil )
raise "create_class #{name.class}" unless name.is_a? Symbol raise "create_class #{name.class}" unless name.is_a? Symbol
superclass = :Object unless superclass superclass = :Object unless superclass
c = Class.new(name , superclass ) raise "create_class #{superclass.class}" unless superclass.is_a? Symbol
self.classes[name] = c type = get_class_by_name(superclass).instance_type
c = Class.new(name , superclass , type )
@classes[name] = c
end end
def sof_reference_name def sof_reference_name

View File

@ -22,7 +22,12 @@ module Parfait
class TypedMethod < Object class TypedMethod < Object
attributes [:name , :source , :instructions , :binary ,:arguments , :for_type, :locals ] def self.attributes
[:name , :source , :instructions , :binary ,:arguments , :for_type, :locals ]
end
attr_reader :name , :instructions , :for_type ,:arguments , :locals , :binary
# not part of the parfait model, hence ruby accessor # not part of the parfait model, hence ruby accessor
attr_accessor :source attr_accessor :source
@ -31,12 +36,15 @@ module Parfait
raise "No class #{name}" unless type raise "No class #{name}" unless type
raise "For type, not class #{type}" unless type.is_a?(Type) raise "For type, not class #{type}" unless type.is_a?(Type)
raise "Wrong argument type, expect Type not #{arguments.class}" unless arguments.is_a? Type raise "Wrong argument type, expect Type not #{arguments.class}" unless arguments.is_a? Type
self.for_type = type @for_type = type
self.name = name @name = name
self.binary = BinaryCode.new 0 @binary = BinaryCode.new 0
self.arguments = arguments @arguments = arguments
self.locals = Type.new Space.object_space.get_class_by_name( :Object ) @locals = Type.new Space.object_space.get_class_by_name( :Object )
end
def set_instructions(inst)
@instructions = inst
end end
# determine whether this method has an argument by the name # determine whether this method has an argument by the name
@ -47,7 +55,7 @@ module Parfait
end end
def add_argument(name , type) def add_argument(name , type)
self.arguments = self.arguments.add_instance_variable(name,type) @arguments = @arguments.add_instance_variable(name,type)
end end
def arguments_length def arguments_length
@ -55,10 +63,10 @@ module Parfait
end end
def argument_name( index ) def argument_name( index )
arguments.get(index * 2 + 1) arguments.names.get(index + 1)
end end
def argument_type( index ) def argument_type( index )
arguments.get(index * 2 + 2) arguments.types.get(index + 1)
end end
# determine if method has a local variable or tmp (anonymous local) by given name # determine if method has a local variable or tmp (anonymous local) by given name
@ -71,7 +79,7 @@ module Parfait
def add_local( name , type ) def add_local( name , type )
index = has_local name index = has_local name
return index if index return index if index
self.locals = self.locals.add_instance_variable(name,type) @locals = @locals.add_instance_variable(name,type)
end end
def locals_length def locals_length
@ -79,18 +87,19 @@ module Parfait
end end
def locals_name( index ) def locals_name( index )
locals.get(index * 2 + 1) locals.names.get(index + 1)
end end
def locals_type( index ) def locals_type( index )
locals.get(index * 2 + 2) locals.types.get(index + 1)
end end
def sof_reference_name def sof_reference_name
"Method: " + self.name.to_s "Method: " + @name.to_s
end end
def inspect def inspect
"#{self.for_type.object_class.name}:#{name}(#{arguments.inspect})" "#{@for_type.object_class.name}:#{name}(#{arguments.inspect})"
end end
end end

View File

@ -74,4 +74,13 @@ class TestSpace < MiniTest::Test
assert all.include?(:next_message) assert all.include?(:next_message)
end end
def test_create_class
assert @machine.space.create_class( :NewClass )
end
def test_created_class_is_stored
@machine.space.create_class( :NewerClass )
assert @machine.space.get_class_by_name(:NewerClass)
end
end end