Implement class instance variables
as they are just the type of the meta_class, that was relatively simple. I feel this is what oo is meant to be, instance variables and methods for the objects, and since classes are objects, for them too. Class variables seem like a design mistake, weird scoping rules and no data hiding (left as an exercise to the reader)
This commit is contained in:
parent
b0d1948800
commit
fd46826b9c
@ -30,7 +30,7 @@ module Parfait
|
|||||||
def self.object_space
|
def self.object_space
|
||||||
Object.object_space
|
Object.object_space
|
||||||
end
|
end
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
# redefine the runtime version
|
# redefine the runtime version
|
||||||
def self.new( *args )
|
def self.new( *args )
|
||||||
|
@ -33,7 +33,7 @@ module Parfait
|
|||||||
@name = name
|
@name = name
|
||||||
@super_class_name = superclass
|
@super_class_name = superclass
|
||||||
@instance_methods = List.new
|
@instance_methods = List.new
|
||||||
set_instance_type( instance_type )
|
@instance_type = instance_type
|
||||||
@meta_class = MetaClass.new( self )
|
@meta_class = MetaClass.new( self )
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -65,13 +65,6 @@ module Parfait
|
|||||||
@instance_type = @instance_type.add_instance_variable( name , type )
|
@instance_type = @instance_type.add_instance_variable( name , type )
|
||||||
end
|
end
|
||||||
|
|
||||||
# setting the type generates all methods for this type
|
|
||||||
# (or will do, once we store the methods code to do that)
|
|
||||||
def set_instance_type( type )
|
|
||||||
raise "type must be type #{type}" unless type.is_a?(Type)
|
|
||||||
@instance_type = type
|
|
||||||
end
|
|
||||||
|
|
||||||
# return the super class, but raise exception if either the super class name
|
# return the super class, but raise exception if either the super class name
|
||||||
# or the super classs is nil.
|
# or the super classs is nil.
|
||||||
# Use only for non Object base class
|
# Use only for non Object base class
|
||||||
|
@ -30,7 +30,7 @@ module Parfait
|
|||||||
super()
|
super()
|
||||||
@clazz = clazz
|
@clazz = clazz
|
||||||
@instance_methods = List.new
|
@instance_methods = List.new
|
||||||
set_instance_type( clazz.get_type() )
|
@instance_type = Object.object_space.get_type_by_class_name(:Object)
|
||||||
end
|
end
|
||||||
|
|
||||||
def rxf_reference_name
|
def rxf_reference_name
|
||||||
@ -61,13 +61,6 @@ module Parfait
|
|||||||
@instance_type = @instance_type.add_instance_variable( name , type )
|
@instance_type = @instance_type.add_instance_variable( name , type )
|
||||||
end
|
end
|
||||||
|
|
||||||
# setting the type generates all methods for this type
|
|
||||||
# (or will do, once we store the methods code to do that)
|
|
||||||
def set_instance_type( type )
|
|
||||||
raise "type must be type #{type.class}:#{type}" unless type.is_a?(Type)
|
|
||||||
@instance_type = type
|
|
||||||
end
|
|
||||||
|
|
||||||
# Nil name means no superclass, and so nil returned
|
# Nil name means no superclass, and so nil returned
|
||||||
def super_class
|
def super_class
|
||||||
return nil
|
return nil
|
||||||
|
@ -190,6 +190,7 @@ module Parfait
|
|||||||
def add_instance_variable( name , type )
|
def add_instance_variable( name , type )
|
||||||
raise "No nil name" unless name
|
raise "No nil name" unless name
|
||||||
raise "No nil type" unless type
|
raise "No nil type" unless type
|
||||||
|
return self if @names.index_of(name)
|
||||||
hash = to_hash
|
hash = to_hash
|
||||||
hash[name] = type
|
hash[name] = type
|
||||||
return Type.for_hash( hash , object_class)
|
return Type.for_hash( hash , object_class)
|
||||||
|
@ -3,8 +3,9 @@ module Boot
|
|||||||
|
|
||||||
# a ruby object as a placeholder for the parfait Space during boot
|
# a ruby object as a placeholder for the parfait Space during boot
|
||||||
class Space
|
class Space
|
||||||
attr_reader :classes
|
attr_reader :classes , :types
|
||||||
def initialize
|
def initialize
|
||||||
|
@types = {}
|
||||||
@classes = {}
|
@classes = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -13,6 +14,9 @@ module Boot
|
|||||||
raise "No class for #{name}" unless cl
|
raise "No class for #{name}" unless cl
|
||||||
cl
|
cl
|
||||||
end
|
end
|
||||||
|
def get_type_by_class_name(name)
|
||||||
|
@types[name]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# another ruby object to shadow the parfait, just during booting.
|
# another ruby object to shadow the parfait, just during booting.
|
||||||
@ -82,6 +86,7 @@ module Parfait
|
|||||||
types.each do |name , type|
|
types.each do |name , type|
|
||||||
clazz = Boot::Class.new(type)
|
clazz = Boot::Class.new(type)
|
||||||
boot_space.classes[name] = clazz
|
boot_space.classes[name] = clazz
|
||||||
|
boot_space.types[name] = type
|
||||||
end
|
end
|
||||||
Parfait::Object.set_object_space( boot_space )
|
Parfait::Object.set_object_space( boot_space )
|
||||||
end
|
end
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
class Integer < DataObject4
|
|
||||||
def +(other)
|
|
||||||
return X.int_operator( :+ )
|
|
||||||
end
|
|
||||||
end
|
|
@ -45,11 +45,6 @@ module Vool
|
|||||||
Mom::MomCollection.new(method_compilers)
|
Mom::MomCollection.new(method_compilers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&block)
|
|
||||||
block.call(self)
|
|
||||||
@body.each(&block) if @body
|
|
||||||
end
|
|
||||||
|
|
||||||
# This creates the Parfait class. But doesn not handle reopening yet, so only new classes
|
# This creates the Parfait class. But doesn not handle reopening yet, so only new classes
|
||||||
# Creating the class involves creating the instance_type (or an initial version)
|
# Creating the class involves creating the instance_type (or an initial version)
|
||||||
# which means knowing all used names. So we go through the code looking for
|
# which means knowing all used names. So we go through the code looking for
|
||||||
@ -60,24 +55,37 @@ module Vool
|
|||||||
#FIXME super class check with "sup"
|
#FIXME super class check with "sup"
|
||||||
#existing class, don't overwrite type (parfait only?)
|
#existing class, don't overwrite type (parfait only?)
|
||||||
else
|
else
|
||||||
create_new_class
|
@clazz = Parfait.object_space.create_class(@name , @super_class_name )
|
||||||
end
|
end
|
||||||
|
create_types
|
||||||
@clazz
|
@clazz
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_new_class
|
# goes through the code looking for instance variables (and their assignments)
|
||||||
@clazz = Parfait.object_space.create_class(@name , @super_class_name )
|
# adding each to the respective type, ie class or meta_class, depending
|
||||||
#TODO this should start from Object Type and add one name at a time.
|
# on if they are instance or class instance variables.
|
||||||
# So the "trail" of types leading to this one exists.
|
#
|
||||||
# Also the Class always has a valid type.
|
# Class variables are deemed a design mistake, ie not implemented (yet)
|
||||||
ivar_hash = {}
|
def create_types
|
||||||
self.each do |node|
|
self.body.statements.each do |node|
|
||||||
next unless node.is_a?(InstanceVariable) or node.is_a?(IvarAssignment)
|
case node
|
||||||
ivar_hash[node.name] = :Object
|
when MethodExpression
|
||||||
|
target = @clazz
|
||||||
|
when ClassMethodExpression
|
||||||
|
target = @clazz.meta_class
|
||||||
|
else
|
||||||
|
raise "Only methods for now #{node.class}:#{node}"
|
||||||
|
end
|
||||||
|
node.each do |exp|
|
||||||
|
case exp
|
||||||
|
when InstanceVariable, IvarAssignment
|
||||||
|
target.add_instance_variable( exp.name , :Object )
|
||||||
|
when ClassVariable #, ClassVarAssignment
|
||||||
|
raise "Class variables not implemented #{node.name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@clazz.set_instance_type( Parfait::Type.for_hash( ivar_hash , @clazz ) )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s(depth = 0)
|
def to_s(depth = 0)
|
||||||
at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end")
|
at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end")
|
||||||
end
|
end
|
||||||
|
@ -11,7 +11,6 @@ module Vool
|
|||||||
raise "not meta" unless clazz.class == Parfait::MetaClass
|
raise "not meta" unless clazz.class == Parfait::MetaClass
|
||||||
raise( "no class in #{self}") unless clazz
|
raise( "no class in #{self}") unless clazz
|
||||||
method = clazz.add_method_for(name , make_arg_type , make_frame , body )
|
method = clazz.add_method_for(name , make_arg_type , make_frame , body )
|
||||||
#VoolMethod
|
|
||||||
compiler = method.compiler_for(clazz.instance_type)
|
compiler = method.compiler_for(clazz.instance_type)
|
||||||
each {|node| raise "Blocks not implemented" if node.is_a?(LambdaExpression)}
|
each {|node| raise "Blocks not implemented" if node.is_a?(LambdaExpression)}
|
||||||
compiler
|
compiler
|
||||||
|
@ -58,6 +58,10 @@ module Parfait
|
|||||||
assert_equal :Type , @type.type_at(0)
|
assert_equal :Type , @type.type_at(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_add_again
|
||||||
|
assert_equal @type.to_hash , @type.add_instance_variable(:type , :Type).to_hash
|
||||||
|
end
|
||||||
|
|
||||||
def test_inspect_added
|
def test_inspect_added
|
||||||
type = test_add_name
|
type = test_add_name
|
||||||
assert type.inspect.include?("boo") , type.inspect
|
assert type.inspect.include?("boo") , type.inspect
|
||||||
|
@ -19,10 +19,15 @@ module Vool
|
|||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
source = "class Integer;def +(other);X.int_operator(:+);end;end;" + class_main
|
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(class_main)
|
||||||
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source)
|
|
||||||
@ins = ret.compilers.find{|c|c.callable.name==:some_inst}.mom_instructions.next
|
@ins = ret.compilers.find{|c|c.callable.name==:some_inst}.mom_instructions.next
|
||||||
end
|
end
|
||||||
|
def test_class_inst
|
||||||
|
space_class = Parfait.object_space.get_class
|
||||||
|
assert_equal :Space , space_class.name
|
||||||
|
names = space_class.meta_class.instance_type.names
|
||||||
|
assert names.index_of(:inst) , names
|
||||||
|
end
|
||||||
def test_array
|
def test_array
|
||||||
check_array [SlotLoad, ReturnJump, Label, ReturnSequence, Label] , @ins
|
check_array [SlotLoad, ReturnJump, Label, ReturnSequence, Label] , @ins
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user