typed methods now for type only

This commit is contained in:
Torsten Ruger 2016-12-14 13:24:42 +02:00
parent b3a9d8b1bc
commit 9a0e2bcb11
7 changed files with 57 additions and 44 deletions

View File

@ -58,10 +58,10 @@ module Typed
@regs = []
return unless method
@method = method
@clazz = method.for_class
@type = method.for_type
@current = method.instructions
end
attr_reader :clazz , :method
attr_reader :type , :method
# Dispatches `code` according to it's class name, for class NameExpression
@ -94,20 +94,21 @@ module Typed
raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
clazz = Register.machine.space.get_class_by_name class_name
raise "No such class #{class_name}" unless clazz
create_method_for( clazz , method_name , args)
create_method_for( clazz.instance_type , method_name , args)
end
# create a method for the given class ( Parfait class object)
# create a method for the given type ( Parfait type object)
# method_name is a Symbol
# args a ruby array
# the created method is set as the current and the given class too
# args a hash that will be converted to a type
# the created method is set as the current and the given type too
# return the compiler (for chaining)
def create_method_for clazz , method_name , args
@clazz = clazz
def create_method_for( type , method_name , args )
@type = type
raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type
raise "Args must be Hash #{args}" unless args.is_a?(Hash)
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
arguments = Parfait::Type.new_for_hash( clazz , args )
@method = clazz.create_instance_method( method_name , arguments)
arguments = Parfait::Type.new_for_hash( type.object_class , args )
@method = type.create_instance_method( method_name , arguments)
self
end
@ -116,7 +117,7 @@ module Typed
# return self for chaining
def init_method
source = "_init_method"
name = "#{method.for_class.name}.#{method.name}"
name = "#{method.for_type.name}.#{method.name}"
@method.instructions = Register::Label.new(source, name)
@current = enter = method.instructions
add_code Register::Label.new( source, "return #{name}")
@ -144,6 +145,7 @@ module Typed
# require a (temporary) register. code must give this back with release_reg
def use_reg type , value = nil
raise "Not type #{type.inspect}" unless type.is_a?(Symbol) or type.is_a?(Parfait::Type)
if @regs.empty?
reg = Register.tmp_reg(type , value)
else

View File

@ -9,14 +9,14 @@ module Typed
new_message = Register.resolve_to_register(:new_message)
add_code Register.get_slot(statement, :message , :next_message , new_message )
me = get_me( statement )
clazz = get_clazz(me)
type = get_my_type(me)
# move our receiver there
add_code Register.set_slot( statement , me , :new_message , :receiver)
set_message_details(statement , statement.arguments)
set_arguments(statement.arguments)
ret = use_reg( :Integer ) #TODO real return type
do_call(clazz , statement)
do_call(type , statement)
# the effect of the method is that the NewMessage Return slot will be filled, return it
# but move it into a register too
add_code Register.get_slot(statement, :new_message , :return_value , ret )
@ -29,27 +29,34 @@ module Typed
if statement.receiver
me = process( statement.receiver )
else
me = use_reg @method.for_class.name
me = use_reg @method.for_type
add_code Register.get_slot(statement, :message , :receiver , me )
end
me
end
def get_clazz( me )
def get_my_type( me )
if(me.type == :MetaClass)
clazz = me.value.meta
else
# now we have to resolve the method name (+ receiver) into a callable method
clazz = Register.machine.space.get_class_by_name(me.type)
raise "Remove this test #{me.class}"
end
clazz
# now we have to resolve the method name (+ receiver) into a callable method
case me.type
when Parfait::Type
type = me.type
when Symbol
type = Register.machine.space.get_class_by_name(me.type).instance_type
else
raise me.inspect
end
raise "Not type #{type}" unless type.is_a? Parfait::Type
type
end
def do_call clazz , statement
def do_call( type , statement )
name = statement.name
#puts "clazz #{clazz.name}"
raise "No such class" unless clazz
method = clazz.resolve_method(name)
#puts "type #{type.inpect}"
raise "No such class" unless type
method = type.resolve_method(name)
#puts Register.machine.space.get_class_by_name(:Integer).method_names.to_a
raise "Method not implemented #{clazz.name}.#{name}" unless method
raise "Method not implemented #{type.inspect}.#{name}" unless method
Register.issue_call( self , method )
end
def set_message_details name_s , arguments

View File

@ -4,13 +4,13 @@ module Typed
def on_ClassField statement
#type , name , value = *statement
for_class = @clazz
raise "no class" unless for_class
index = for_class.instance_type.variable_index(statement.name)
raise "class field already defined:#{name} for class #{for_class.name}" if index
for_type = @type
raise "no type" unless for_type
index = for_type.variable_index(statement.name)
raise "class field already defined:#{name} for class #{for_type}" if index
#FIXME should not hack into current type, but create a new
for_class.instance_type.send(:private_add_instance_variable, statement.name , statement.type)
for_type.send(:private_add_instance_variable, statement.name , statement.type)
return nil # statements don't reurn values, only expressions
end

View File

@ -3,12 +3,12 @@ module Typed
def on_ClassStatement statement
raise "classes dont yet play babushka, get coding #{statement.name}" if @clazz
raise "classes dont yet play babushka, get coding #{statement.name}" if @type
@clazz = Parfait::Space.object_space.get_class_by_name! statement.name
#puts "Compiling class #{@clazz.name.inspect}"
@type = Parfait::Space.object_space.get_class_by_name!(statement.name).instance_type
#puts "Compiling class #{@type.inspect}"
statement_value = process(statement.statements).last
@clazz = nil
@type = nil
return statement_value
end
end

View File

@ -5,11 +5,15 @@ module Typed
# receiver_ast , field_ast = *statement
receiver = process(statement.receiver)
clazz = Register.machine.space.get_class_by_name receiver.type
type = receiver.type
if(type.is_a?(Symbol))
type = Parfait::Space.object_space.get_class_by_name(type).instance_type
end
field_name = statement.field.name
index = clazz.instance_type.variable_index(field_name)
raise "no such field:#{field_name} for class #{clazz.name}:#{clazz.instance_type.inspect}" unless index
value = use_reg(clazz.instance_type.type_at(index))
index = type.variable_index(field_name)
raise "no such field:#{field_name} for class #{type.inspect}" unless index
value = use_reg(type.type_at(index))
add_code Register.get_slot(statement , receiver , index, value)

View File

@ -9,19 +9,19 @@ module Typed
class_method = handle_receiver( statement ) #nil for instance method
@method = @clazz.get_instance_method( statement.name )
@method = @type.get_instance_method( statement.name )
if(@method)
#puts "Warning, redefining method #{name}" unless name == :main
#TODO check args / type compatibility
init_method
else
create_method_for(@clazz, statement.name , args ).init_method
create_method_for(@type, statement.name , args ).init_method
end
@method.source = statement
process(statement.statements)
@clazz = class_method if class_method
@type = class_method if class_method
@method = nil
# function definition is a statement, does not return any value
return nil
@ -32,8 +32,8 @@ module Typed
def handle_receiver( statement )
return nil unless statement.receiver
raise "Not covered #{statement.receiver}" unless ( statement.receiver.first == :self)
class_method = @clazz
@clazz = @clazz.meta
class_method = @type
@type = @type.object_class.meta.get_type
class_method
end
end

View File

@ -34,7 +34,7 @@ module Typed
end
def handle_special_self(statement)
ret = use_reg @clazz.name
ret = use_reg @type
add_code Register.get_slot(statement , :message , :receiver , ret )
return ret
end