Derive Class and MetaClass from Behaviour
Finally! If i remember the module was from before i had any non Object superclasses unified code, unified bugs :-) just makes sense
This commit is contained in:
parent
3b7248df4e
commit
a496ea7e4b
@ -3,11 +3,14 @@
|
|||||||
# instance_methods is the attribute in the including class that has the methods
|
# instance_methods is the attribute in the including class that has the methods
|
||||||
|
|
||||||
module Parfait
|
module Parfait
|
||||||
module Behaviour
|
class Behaviour < Object
|
||||||
|
|
||||||
def initialize
|
attr_reader :instance_type , :instance_methods
|
||||||
|
|
||||||
|
def initialize(type)
|
||||||
super()
|
super()
|
||||||
@instance_methods = List.new
|
@instance_methods = List.new
|
||||||
|
@instance_type = type
|
||||||
end
|
end
|
||||||
|
|
||||||
def methods
|
def methods
|
||||||
@ -24,9 +27,14 @@ module Parfait
|
|||||||
names
|
names
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_instance_method_for(name , type , frame , body )
|
||||||
|
method = Parfait::VoolMethod.new(name , type , frame , body )
|
||||||
|
add_instance_method( method )
|
||||||
|
end
|
||||||
|
|
||||||
def add_instance_method( method )
|
def add_instance_method( method )
|
||||||
raise "not implemented #{method.class} #{method.inspect}" unless method.is_a? VoolMethod
|
raise "not implemented #{method.class} #{method.inspect}" unless method.is_a? VoolMethod
|
||||||
raise "HMM"
|
@instance_methods.push(method)
|
||||||
method
|
method
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -52,5 +60,10 @@ module Parfait
|
|||||||
method
|
method
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# adding an instance changes the instance_type to include that variable
|
||||||
|
def add_instance_variable( name , type)
|
||||||
|
@instance_type = @instance_type.add_instance_variable( name , type )
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -15,11 +15,9 @@
|
|||||||
# Each type in turn has a list of CallableMethods that hold binary code
|
# Each type in turn has a list of CallableMethods that hold binary code
|
||||||
|
|
||||||
module Parfait
|
module Parfait
|
||||||
class Class < Object
|
class Class < Behaviour
|
||||||
include Behaviour
|
|
||||||
|
|
||||||
attr_reader :instance_type , :name , :instance_methods
|
attr_reader :name , :super_class_name , :meta_class
|
||||||
attr_reader :super_class_name , :meta_class
|
|
||||||
|
|
||||||
def self.type_length
|
def self.type_length
|
||||||
6
|
6
|
||||||
@ -29,11 +27,9 @@ module Parfait
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize( name , superclass , instance_type)
|
def initialize( name , superclass , instance_type)
|
||||||
super()
|
super(instance_type)
|
||||||
@name = name
|
@name = name
|
||||||
@super_class_name = superclass
|
@super_class_name = superclass
|
||||||
@instance_methods = List.new
|
|
||||||
@instance_type = instance_type
|
|
||||||
@meta_class = MetaClass.new( self )
|
@meta_class = MetaClass.new( self )
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -48,26 +44,6 @@ module Parfait
|
|||||||
inspect
|
inspect
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_method_for(name , type , frame , body )
|
|
||||||
method = Parfait::VoolMethod.new(name , type , frame , body )
|
|
||||||
add_method( method )
|
|
||||||
method
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_method(method)
|
|
||||||
raise "Must be untyped method #{method}" unless method.is_a? Parfait::VoolMethod
|
|
||||||
@instance_methods.push(method)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_method(name)
|
|
||||||
@instance_methods.find{|m| m.name == name }
|
|
||||||
end
|
|
||||||
|
|
||||||
# adding an instance changes the instance_type to include that variable
|
|
||||||
def add_instance_variable( name , type)
|
|
||||||
@instance_type = @instance_type.add_instance_variable( name , 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
|
||||||
|
@ -14,10 +14,9 @@
|
|||||||
# There is a one to one relationship between a class instance and it's meta_class instance.
|
# There is a one to one relationship between a class instance and it's meta_class instance.
|
||||||
|
|
||||||
module Parfait
|
module Parfait
|
||||||
class MetaClass < Object
|
class MetaClass < Behaviour
|
||||||
include Behaviour
|
|
||||||
|
|
||||||
attr_reader :instance_type , :instance_methods , :clazz
|
attr_reader :clazz
|
||||||
|
|
||||||
def self.type_length
|
def self.type_length
|
||||||
4
|
4
|
||||||
@ -27,9 +26,10 @@ module Parfait
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize( clazz )
|
def initialize( clazz )
|
||||||
super()
|
type = Object.object_space.get_type_by_class_name(:Object)
|
||||||
|
raise "No type for #{clazz.name}" unless type
|
||||||
|
super( type )
|
||||||
@clazz = clazz
|
@clazz = clazz
|
||||||
@instance_type = Object.object_space.get_type_by_class_name(:Object)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def rxf_reference_name
|
def rxf_reference_name
|
||||||
@ -43,25 +43,6 @@ module Parfait
|
|||||||
def to_s
|
def to_s
|
||||||
inspect
|
inspect
|
||||||
end
|
end
|
||||||
def add_method_for(name , type , frame , body )
|
|
||||||
method = Parfait::VoolMethod.new(name , type , frame , body )
|
|
||||||
add_method( method )
|
|
||||||
method
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_method(method)
|
|
||||||
raise "Must be untyped method #{method}" unless method.is_a? Parfait::VoolMethod
|
|
||||||
@instance_methods.push(method)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_method(name)
|
|
||||||
@instance_methods.find{|m| m.name == name }
|
|
||||||
end
|
|
||||||
|
|
||||||
# adding an instance changes the instance_type to include that variable
|
|
||||||
def add_instance_variable( name , type)
|
|
||||||
@instance_type = @instance_type.add_instance_variable( name , 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
|
||||||
|
@ -129,6 +129,8 @@ module Parfait
|
|||||||
List: :Data16 ,
|
List: :Data16 ,
|
||||||
CallableMethod: :Callable,
|
CallableMethod: :Callable,
|
||||||
Block: :Callable,
|
Block: :Callable,
|
||||||
|
Class: :Behaviour,
|
||||||
|
MetaClass: :Behaviour ,
|
||||||
ReturnAddress: :Integer}
|
ReturnAddress: :Integer}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -136,11 +138,11 @@ module Parfait
|
|||||||
# 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 self.type_names
|
def self.type_names
|
||||||
{BinaryCode: {next_code: :BinaryCode} ,
|
{Behaviour: {instance_type: :Type , instance_methods: :List } ,
|
||||||
|
BinaryCode: {next_code: :BinaryCode} ,
|
||||||
Block: {binary: :BinaryCode, next_callable: :Block,
|
Block: {binary: :BinaryCode, next_callable: :Block,
|
||||||
arguments_type: :Type , self_type: :Type, frame_type: :Type,
|
arguments_type: :Type , self_type: :Type, frame_type: :Type,
|
||||||
name: :Word , blocks: :Block } ,
|
name: :Word , blocks: :Block } ,
|
||||||
|
|
||||||
CacheEntry: {cached_type: :Type , cached_method: :CallableMethod } ,
|
CacheEntry: {cached_type: :Type , cached_method: :CallableMethod } ,
|
||||||
Callable: {binary: :BinaryCode,next_callable: :Callable ,
|
Callable: {binary: :BinaryCode,next_callable: :Callable ,
|
||||||
arguments_type: :Type , self_type: :Type, frame_type: :Type,
|
arguments_type: :Type , self_type: :Type, frame_type: :Type,
|
||||||
|
@ -10,7 +10,7 @@ module Vool
|
|||||||
def to_mom(clazz)
|
def to_mom(clazz)
|
||||||
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_instance_method_for(name , make_arg_type , make_frame , body )
|
||||||
#puts "CLass method Class:#{clazz}:#{name}"
|
#puts "CLass method Class:#{clazz}:#{name}"
|
||||||
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)}
|
||||||
|
@ -21,7 +21,7 @@ module Vool
|
|||||||
# extracted call to create the VoolMethod as this is the place
|
# extracted call to create the VoolMethod as this is the place
|
||||||
# where we have all the info. Used in testing.
|
# where we have all the info. Used in testing.
|
||||||
def make_method(clazz)
|
def make_method(clazz)
|
||||||
clazz.add_method_for(name , make_arg_type , make_frame , body )
|
clazz.add_instance_method_for(name , make_arg_type , make_frame , body )
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&block)
|
def each(&block)
|
||||||
|
@ -14,7 +14,7 @@ module Mom
|
|||||||
|
|
||||||
def test_method
|
def test_method
|
||||||
in_test_vool()
|
in_test_vool()
|
||||||
method = Parfait.object_space.get_class_by_name(:Test).get_method(:meth)
|
method = Parfait.object_space.get_class_by_name(:Test).get_instance_method(:meth)
|
||||||
assert_equal Parfait::VoolMethod , method.class
|
assert_equal Parfait::VoolMethod , method.class
|
||||||
end
|
end
|
||||||
def test_method_mom_col
|
def test_method_mom_col
|
||||||
|
@ -4,7 +4,7 @@ module Parfait
|
|||||||
class TestSpace < ParfaitTest
|
class TestSpace < ParfaitTest
|
||||||
|
|
||||||
def classes
|
def classes
|
||||||
[:BinaryCode,:Block,:CacheEntry,:Callable,:CallableMethod,:Class,
|
[:Behaviour ,:BinaryCode,:Block,:CacheEntry,:Callable,:CallableMethod,:Class,
|
||||||
:DataObject,:Data4,:Data8,:Data16,:Data32,:Dictionary,:Factory, :Integer,:FalseClass,
|
:DataObject,:Data4,:Data8,:Data16,:Data32,:Dictionary,:Factory, :Integer,:FalseClass,
|
||||||
:List,:Message, :MetaClass,:NilClass,:Object,:ReturnAddress,
|
:List,:Message, :MetaClass,:NilClass,:Object,:ReturnAddress,
|
||||||
:Space,:TrueClass,:Type,:VoolMethod,:Word]
|
:Space,:TrueClass,:Type,:VoolMethod,:Word]
|
||||||
|
@ -22,13 +22,13 @@ module Parfait
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_name
|
def test_name
|
||||||
assert_equal "BinaryCode_Type" , @types.values.first.name
|
assert_equal "Behaviour_Type" , @types.values.first.name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_hash
|
def test_to_hash
|
||||||
hash = @first.to_hash
|
hash = @first.to_hash
|
||||||
assert_equal hash[:type] , :Type
|
assert_equal hash[:type] , :Type
|
||||||
assert_equal 2 , hash.length
|
assert_equal 3 , hash.length
|
||||||
end
|
end
|
||||||
def test_add_is_different
|
def test_add_is_different
|
||||||
type = @first.add_instance_variable :random , :Integer
|
type = @first.add_instance_variable :random , :Integer
|
||||||
|
@ -38,7 +38,7 @@ module Risc
|
|||||||
ret = main_ticks(49)
|
ret = main_ticks(49)
|
||||||
assert_equal FunctionReturn , ret.class
|
assert_equal FunctionReturn , ret.class
|
||||||
assert_equal :r3 , ret.register.symbol
|
assert_equal :r3 , ret.register.symbol
|
||||||
assert_equal 37852 , @interpreter.get_register(ret.register)
|
assert_equal 38236 , @interpreter.get_register(ret.register)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -38,7 +38,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def len
|
def len
|
||||||
1471
|
1479
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_collect_all_types
|
def test_collect_all_types
|
||||||
@ -70,7 +70,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def len
|
def len
|
||||||
2951
|
2959
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -54,7 +54,7 @@ module Risc
|
|||||||
end
|
end
|
||||||
def test_pc
|
def test_pc
|
||||||
@interpreter.tick
|
@interpreter.tick
|
||||||
assert_equal t = 37416 , @interpreter.pc
|
assert_equal t = 37800 , @interpreter.pc
|
||||||
@interpreter.tick
|
@interpreter.tick
|
||||||
assert_equal t + 4 , @interpreter.pc
|
assert_equal t + 4 , @interpreter.pc
|
||||||
end
|
end
|
||||||
|
@ -24,7 +24,7 @@ module Risc
|
|||||||
assert_equal 0 , Position.get(@linker.cpu_init).at
|
assert_equal 0 , Position.get(@linker.cpu_init).at
|
||||||
end
|
end
|
||||||
def test_cpu_at
|
def test_cpu_at
|
||||||
assert_equal "0x931c" , Position.get(@linker.cpu_init.first).to_s
|
assert_equal "0x941c" , Position.get(@linker.cpu_init.first).to_s
|
||||||
end
|
end
|
||||||
def test_cpu_label
|
def test_cpu_label
|
||||||
assert_equal Position , Position.get(@linker.cpu_init.first).class
|
assert_equal Position , Position.get(@linker.cpu_init.first).class
|
||||||
|
@ -15,7 +15,7 @@ module Risc
|
|||||||
def create_method(body = "@ivar = 5;return")
|
def create_method(body = "@ivar = 5;return")
|
||||||
in_test_vool("def meth; #{body};end")
|
in_test_vool("def meth; #{body};end")
|
||||||
test = Parfait.object_space.get_class_by_name(:Test)
|
test = Parfait.object_space.get_class_by_name(:Test)
|
||||||
test.get_method(:meth)
|
test.get_instance_method(:meth)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_method_has_source
|
def test_method_has_source
|
||||||
@ -55,7 +55,7 @@ module Risc
|
|||||||
def test_callable_method_has_one_local
|
def test_callable_method_has_one_local
|
||||||
in_test_vool("def meth; local = 5 ; a = 6;return;end")
|
in_test_vool("def meth; local = 5 ; a = 6;return;end")
|
||||||
test = Parfait.object_space.get_class_by_name(:Test)
|
test = Parfait.object_space.get_class_by_name(:Test)
|
||||||
method = test.get_method(:meth)
|
method = test.get_instance_method(:meth)
|
||||||
assert_equal 3 , method.frame_type.instance_length
|
assert_equal 3 , method.frame_type.instance_length
|
||||||
assert_equal 1 , method.frame_type.variable_index(:local)
|
assert_equal 1 , method.frame_type.variable_index(:local)
|
||||||
assert_equal 2 , method.frame_type.variable_index(:a)
|
assert_equal 2 , method.frame_type.variable_index(:a)
|
||||||
|
@ -38,7 +38,7 @@ module RubyX
|
|||||||
|
|
||||||
def test_load
|
def test_load
|
||||||
object = Parfait.object_space.get_class_by_name(:Object)
|
object = Parfait.object_space.get_class_by_name(:Object)
|
||||||
assert_equal Parfait::VoolMethod , object.get_method(:set_type).class
|
assert_equal Parfait::VoolMethod , object.get_instance_method(:set_type).class
|
||||||
assert_equal Parfait::CallableMethod , object.instance_type.get_method(:set_type).class
|
assert_equal Parfait::CallableMethod , object.instance_type.get_method(:set_type).class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user