wit metaclass and class functions
needs to go back to arrays first
This commit is contained in:
parent
0d001ec0fc
commit
b7d0ee8f99
@ -1,7 +1,5 @@
|
||||
module Parfait
|
||||
|
||||
# TODO : rethink - possibly needs to be a module to be mixed into Object
|
||||
#
|
||||
# class that acts like a class, but is really the object
|
||||
|
||||
# described in the ruby language book as the eigenclass, what you get with
|
||||
@ -9,19 +7,23 @@ module Parfait
|
||||
# class << self <--- this is called the eigenclass, or metaclass, and really is just
|
||||
# .... the class object but gives us the ability to use the
|
||||
# syntax as if it were a class
|
||||
# PS: can't say i fancy the << self syntax and am considerernig adding a
|
||||
# keyword for it, like meta
|
||||
# In effect it is a very similar construct to def self.function(...)
|
||||
# So one could write def meta.function(...) and thus define on the meta-class
|
||||
#
|
||||
|
||||
class MetaClass < Object
|
||||
# no name, nor nothing. as this is just the object really
|
||||
attribute :me
|
||||
|
||||
# def initialize(object)
|
||||
# super()
|
||||
# self.functions = []
|
||||
# self.me_self = object
|
||||
# end
|
||||
def initialize(object)
|
||||
super()
|
||||
self.me = object
|
||||
end
|
||||
|
||||
def super_class
|
||||
Space.object_space.get_class_by_name(self.me.super_class_name).meta
|
||||
end
|
||||
|
||||
def name
|
||||
"Meta#{me.name}".to_sym
|
||||
end
|
||||
# in a non-booting version this should map to _add_singleton_method
|
||||
# def add_function function
|
||||
# raise "not a function #{function}" unless function.is_a? Register::Function
|
||||
|
@ -5,44 +5,41 @@ module Soml
|
||||
#puts statement.inspect
|
||||
return_type , name , parameters, kids , receiver = *statement
|
||||
name = name.to_a.first
|
||||
raise "Already in method #{@method}" if @method
|
||||
|
||||
args = parameters.to_a.collect do |p|
|
||||
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
||||
Parfait::Variable.new( *p)
|
||||
end
|
||||
|
||||
if receiver
|
||||
# compiler will always return slot. with known value or not
|
||||
r = receiver.first
|
||||
if( r.is_a? Parfait::Class )
|
||||
class_name = r.value.name
|
||||
class_method = nil
|
||||
if(receiver )
|
||||
if( receiver.first == :self) #class method
|
||||
class_method = @clazz
|
||||
@clazz = @clazz.meta_class
|
||||
else
|
||||
if( r != :self)
|
||||
raise "unimplemented case in function #{r}"
|
||||
else
|
||||
r = Register::Self.new()
|
||||
class_name = method.for_class.name
|
||||
end
|
||||
raise "Not covered #{receiver}"
|
||||
end
|
||||
else
|
||||
r = @clazz
|
||||
class_name = @clazz.name
|
||||
end
|
||||
raise "Already in method #{@method}" if @method
|
||||
r = @clazz
|
||||
class_name = @clazz.name
|
||||
|
||||
@method = @clazz.get_instance_method( name )
|
||||
if(@method)
|
||||
#puts "Warning, redefining method #{name}" unless name == :main
|
||||
#TODO check args / type compatibility
|
||||
@method.source.init @method
|
||||
else
|
||||
@method = Register::MethodSource.create_method(class_name, return_type, name , args )
|
||||
@method.for_class.add_instance_method @method
|
||||
@method = Register::MethodSource.create_method_for(@clazz, return_type, name , args )
|
||||
@clazz.add_instance_method @method
|
||||
end
|
||||
@method.source.receiver = r
|
||||
#puts "compile method #{@method.name}"
|
||||
|
||||
kids.to_a.each do |ex|
|
||||
ret = process(ex)
|
||||
process(ex)
|
||||
end
|
||||
@clazz = class_method if class_method
|
||||
@method = nil
|
||||
# function definition is a statement, does not return any value
|
||||
return nil
|
||||
|
@ -7,7 +7,7 @@ class TestBasicClass < MiniTest::Test
|
||||
def test_class_def
|
||||
@string_input = <<HERE
|
||||
class Bar
|
||||
int buh()
|
||||
int self.buh()
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
@ -6,4 +6,5 @@ require_relative "test_word"
|
||||
require_relative "test_dictionary"
|
||||
require_relative "test_method"
|
||||
require_relative "test_class"
|
||||
require_relative "test_meta"
|
||||
require_relative "test_attributes"
|
||||
|
66
test/parfait/test_meta.rb
Normal file
66
test/parfait/test_meta.rb
Normal file
@ -0,0 +1,66 @@
|
||||
require_relative "../helper"
|
||||
|
||||
class TestMeta < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@space = Register.machine.boot.space
|
||||
@try = @space.create_class(:Try , :Object).meta
|
||||
end
|
||||
|
||||
def foo_method for_class = :Try
|
||||
args = Register.new_list [ Parfait::Variable.new(:Integer , :bar )]
|
||||
::Parfait::Method.new @space.get_class_by_name(for_class) , :foo , args
|
||||
end
|
||||
|
||||
def test_meta
|
||||
assert @try
|
||||
end
|
||||
def test_meta_object
|
||||
assert @space.get_class_by_name(:Object).meta
|
||||
end
|
||||
|
||||
def test_new_superclass
|
||||
assert_equal Parfait::MetaClass , @try.super_class.class
|
||||
assert_equal :MetaObject , @try.super_class.name
|
||||
end
|
||||
def pest_new_methods
|
||||
assert_equal @try.method_names.class, @try.instance_methods.class
|
||||
assert_equal @try.method_names.get_length , @try.instance_methods.get_length
|
||||
end
|
||||
def pest_add_method
|
||||
foo = foo_method
|
||||
assert_equal foo , @try.add_instance_method(foo)
|
||||
assert_equal 1 , @try.instance_methods.get_length
|
||||
assert_equal ":foo" , @try.method_names.inspect
|
||||
end
|
||||
def pest_remove_method
|
||||
pest_add_method
|
||||
assert_equal true , @try.remove_instance_method(:foo)
|
||||
end
|
||||
def pest_remove_nothere
|
||||
assert_raises RuntimeError do
|
||||
@try.remove_instance_method(:foo)
|
||||
end
|
||||
end
|
||||
def pest_create_method
|
||||
@try.create_instance_method :bar, Register.new_list( [ Parfait::Variable.new(:Integer , :bar )])
|
||||
assert_equal ":bar" , @try.method_names.inspect
|
||||
end
|
||||
def pest_method_get
|
||||
pest_add_method
|
||||
assert_equal Parfait::Method , @try.get_instance_method(:foo).class
|
||||
end
|
||||
def pest_method_get_nothere
|
||||
assert_equal nil , @try.get_instance_method(:foo)
|
||||
pest_remove_method
|
||||
assert_equal nil , @try.get_instance_method(:foo)
|
||||
end
|
||||
def pest_resolve
|
||||
foo = foo_method :Object
|
||||
@space.get_class_by_name(:Object).add_instance_method(foo)
|
||||
assert_equal :foo , @try.resolve_method(:foo).name
|
||||
end
|
||||
def pest_meta
|
||||
assert @try.meta_class
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user