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

View File

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

View File

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

View File

@ -3,12 +3,12 @@ module Typed
def on_ClassStatement statement 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 @type = Parfait::Space.object_space.get_class_by_name!(statement.name).instance_type
#puts "Compiling class #{@clazz.name.inspect}" #puts "Compiling class #{@type.inspect}"
statement_value = process(statement.statements).last statement_value = process(statement.statements).last
@clazz = nil @type = nil
return statement_value return statement_value
end end
end end

View File

@ -5,11 +5,15 @@ module Typed
# receiver_ast , field_ast = *statement # receiver_ast , field_ast = *statement
receiver = process(statement.receiver) 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 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 index = type.variable_index(field_name)
value = use_reg(clazz.instance_type.type_at(index)) 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) 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 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) 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
init_method init_method
else else
create_method_for(@clazz, statement.name , args ).init_method create_method_for(@type, statement.name , args ).init_method
end end
@method.source = statement @method.source = statement
process(statement.statements) process(statement.statements)
@clazz = class_method if class_method @type = 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
@ -32,8 +32,8 @@ module Typed
def handle_receiver( statement ) def handle_receiver( statement )
return nil unless statement.receiver return nil unless statement.receiver
raise "Not covered #{statement.receiver}" unless ( statement.receiver.first == :self) raise "Not covered #{statement.receiver}" unless ( statement.receiver.first == :self)
class_method = @clazz class_method = @type
@clazz = @clazz.meta @type = @type.object_class.meta.get_type
class_method class_method
end end
end end

View File

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