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:
Torsten Rüger 2019-09-19 15:48:27 +03:00
parent b0d1948800
commit fd46826b9c
10 changed files with 46 additions and 43 deletions

View File

@ -30,7 +30,7 @@ module Parfait
def self.object_space
Object.object_space
end
class Object
# redefine the runtime version
def self.new( *args )

View File

@ -33,7 +33,7 @@ module Parfait
@name = name
@super_class_name = superclass
@instance_methods = List.new
set_instance_type( instance_type )
@instance_type = instance_type
@meta_class = MetaClass.new( self )
end
@ -65,13 +65,6 @@ module Parfait
@instance_type = @instance_type.add_instance_variable( name , type )
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
# or the super classs is nil.
# Use only for non Object base class

View File

@ -30,7 +30,7 @@ module Parfait
super()
@clazz = clazz
@instance_methods = List.new
set_instance_type( clazz.get_type() )
@instance_type = Object.object_space.get_type_by_class_name(:Object)
end
def rxf_reference_name
@ -61,13 +61,6 @@ module Parfait
@instance_type = @instance_type.add_instance_variable( name , type )
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
def super_class
return nil

View File

@ -190,6 +190,7 @@ module Parfait
def add_instance_variable( name , type )
raise "No nil name" unless name
raise "No nil type" unless type
return self if @names.index_of(name)
hash = to_hash
hash[name] = type
return Type.for_hash( hash , object_class)

View File

@ -3,8 +3,9 @@ module Boot
# a ruby object as a placeholder for the parfait Space during boot
class Space
attr_reader :classes
attr_reader :classes , :types
def initialize
@types = {}
@classes = {}
end
@ -13,6 +14,9 @@ module Boot
raise "No class for #{name}" unless cl
cl
end
def get_type_by_class_name(name)
@types[name]
end
end
# another ruby object to shadow the parfait, just during booting.
@ -82,6 +86,7 @@ module Parfait
types.each do |name , type|
clazz = Boot::Class.new(type)
boot_space.classes[name] = clazz
boot_space.types[name] = type
end
Parfait::Object.set_object_space( boot_space )
end

View File

@ -1,5 +0,0 @@
class Integer < DataObject4
def +(other)
return X.int_operator( :+ )
end
end

View File

@ -45,11 +45,6 @@ module Vool
Mom::MomCollection.new(method_compilers)
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
# 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
@ -60,24 +55,37 @@ module Vool
#FIXME super class check with "sup"
#existing class, don't overwrite type (parfait only?)
else
create_new_class
@clazz = Parfait.object_space.create_class(@name , @super_class_name )
end
create_types
@clazz
end
def create_new_class
@clazz = Parfait.object_space.create_class(@name , @super_class_name )
#TODO this should start from Object Type and add one name at a time.
# So the "trail" of types leading to this one exists.
# Also the Class always has a valid type.
ivar_hash = {}
self.each do |node|
next unless node.is_a?(InstanceVariable) or node.is_a?(IvarAssignment)
ivar_hash[node.name] = :Object
# goes through the code looking for instance variables (and their assignments)
# adding each to the respective type, ie class or meta_class, depending
# on if they are instance or class instance variables.
#
# Class variables are deemed a design mistake, ie not implemented (yet)
def create_types
self.body.statements.each do |node|
case node
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
@clazz.set_instance_type( Parfait::Type.for_hash( ivar_hash , @clazz ) )
end
def to_s(depth = 0)
at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end")
end

View File

@ -11,7 +11,6 @@ module Vool
raise "not meta" unless clazz.class == Parfait::MetaClass
raise( "no class in #{self}") unless clazz
method = clazz.add_method_for(name , make_arg_type , make_frame , body )
#VoolMethod
compiler = method.compiler_for(clazz.instance_type)
each {|node| raise "Blocks not implemented" if node.is_a?(LambdaExpression)}
compiler

View File

@ -58,6 +58,10 @@ module Parfait
assert_equal :Type , @type.type_at(0)
end
def test_add_again
assert_equal @type.to_hash , @type.add_instance_variable(:type , :Type).to_hash
end
def test_inspect_added
type = test_add_name
assert type.inspect.include?("boo") , type.inspect

View File

@ -19,10 +19,15 @@ module Vool
end
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(source)
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(class_main)
@ins = ret.compilers.find{|c|c.callable.name==:some_inst}.mom_instructions.next
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
check_array [SlotLoad, ReturnJump, Label, ReturnSequence, Label] , @ins
end