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
|
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
|
# class that acts like a class, but is really the object
|
||||||
|
|
||||||
# described in the ruby language book as the eigenclass, what you get with
|
# 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
|
# class << self <--- this is called the eigenclass, or metaclass, and really is just
|
||||||
# .... the class object but gives us the ability to use the
|
# .... the class object but gives us the ability to use the
|
||||||
# syntax as if it were a class
|
# 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
|
class MetaClass < Object
|
||||||
# no name, nor nothing. as this is just the object really
|
attribute :me
|
||||||
|
|
||||||
# def initialize(object)
|
def initialize(object)
|
||||||
# super()
|
super()
|
||||||
# self.functions = []
|
self.me = object
|
||||||
# self.me_self = object
|
end
|
||||||
# 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
|
# in a non-booting version this should map to _add_singleton_method
|
||||||
# def add_function function
|
# def add_function function
|
||||||
# raise "not a function #{function}" unless function.is_a? Register::Function
|
# raise "not a function #{function}" unless function.is_a? Register::Function
|
||||||
|
@ -5,44 +5,41 @@ module Soml
|
|||||||
#puts statement.inspect
|
#puts statement.inspect
|
||||||
return_type , name , parameters, kids , receiver = *statement
|
return_type , name , parameters, kids , receiver = *statement
|
||||||
name = name.to_a.first
|
name = name.to_a.first
|
||||||
|
raise "Already in method #{@method}" if @method
|
||||||
|
|
||||||
args = parameters.to_a.collect do |p|
|
args = parameters.to_a.collect do |p|
|
||||||
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
||||||
Parfait::Variable.new( *p)
|
Parfait::Variable.new( *p)
|
||||||
end
|
end
|
||||||
|
|
||||||
if receiver
|
class_method = nil
|
||||||
# compiler will always return slot. with known value or not
|
if(receiver )
|
||||||
r = receiver.first
|
if( receiver.first == :self) #class method
|
||||||
if( r.is_a? Parfait::Class )
|
class_method = @clazz
|
||||||
class_name = r.value.name
|
@clazz = @clazz.meta_class
|
||||||
else
|
else
|
||||||
if( r != :self)
|
raise "Not covered #{receiver}"
|
||||||
raise "unimplemented case in function #{r}"
|
|
||||||
else
|
|
||||||
r = Register::Self.new()
|
|
||||||
class_name = method.for_class.name
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
|
||||||
r = @clazz
|
r = @clazz
|
||||||
class_name = @clazz.name
|
class_name = @clazz.name
|
||||||
end
|
|
||||||
raise "Already in method #{@method}" if @method
|
|
||||||
@method = @clazz.get_instance_method( name )
|
@method = @clazz.get_instance_method( name )
|
||||||
if(@method)
|
if(@method)
|
||||||
#puts "Warning, redefining method #{name}" unless name == :main
|
#puts "Warning, redefining method #{name}" unless name == :main
|
||||||
#TODO check args / type compatibility
|
#TODO check args / type compatibility
|
||||||
@method.source.init @method
|
@method.source.init @method
|
||||||
else
|
else
|
||||||
@method = Register::MethodSource.create_method(class_name, return_type, name , args )
|
@method = Register::MethodSource.create_method_for(@clazz, return_type, name , args )
|
||||||
@method.for_class.add_instance_method @method
|
@clazz.add_instance_method @method
|
||||||
end
|
end
|
||||||
@method.source.receiver = r
|
@method.source.receiver = r
|
||||||
#puts "compile method #{@method.name}"
|
#puts "compile method #{@method.name}"
|
||||||
|
|
||||||
kids.to_a.each do |ex|
|
kids.to_a.each do |ex|
|
||||||
ret = process(ex)
|
process(ex)
|
||||||
end
|
end
|
||||||
|
@clazz = class_method if class_method
|
||||||
@method = nil
|
@method = nil
|
||||||
# function definition is a statement, does not return any value
|
# function definition is a statement, does not return any value
|
||||||
return nil
|
return nil
|
||||||
|
@ -7,7 +7,7 @@ class TestBasicClass < MiniTest::Test
|
|||||||
def test_class_def
|
def test_class_def
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
class Bar
|
class Bar
|
||||||
int buh()
|
int self.buh()
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,4 +6,5 @@ require_relative "test_word"
|
|||||||
require_relative "test_dictionary"
|
require_relative "test_dictionary"
|
||||||
require_relative "test_method"
|
require_relative "test_method"
|
||||||
require_relative "test_class"
|
require_relative "test_class"
|
||||||
|
require_relative "test_meta"
|
||||||
require_relative "test_attributes"
|
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…
x
Reference in New Issue
Block a user