rubyx/lib/vool/statements/method_statement.rb

59 lines
1.5 KiB
Ruby
Raw Normal View History

2017-04-01 15:27:32 +02:00
module Vool
2017-04-08 16:22:53 +02:00
class MethodStatement < Statement
attr_reader :name, :args , :body , :clazz
2017-04-01 15:27:32 +02:00
def initialize( name , args , body , clazz = nil)
@name , @args , @body = name , args , body
raise "no bod" unless @body
@clazz = clazz
end
# there is no mom equivalent for a method definition, only a vool/parfait one
# Only the source of gets momed, this should never be called
def to_mom( _ )
raise "should not be called (call create_objects)"
2017-04-01 15:27:32 +02:00
end
def each(&block)
block.call(self)
@body.each(&block)
2017-04-08 11:10:42 +02:00
end
def normalize
MethodStatement.new( @name , @args , @body.normalize)
2017-04-08 16:22:53 +02:00
end
def create_objects(clazz)
@clazz = clazz
raise "no class" unless clazz
2017-04-08 16:22:53 +02:00
args_type = make_type
2018-03-14 15:54:47 +01:00
frame_type = make_frame
method = Parfait::VoolMethod.new(name , args_type , frame_type , body )
2017-04-08 16:22:53 +02:00
@clazz.add_method( method )
typed_method = method.create_parfait_method(clazz.instance_type)
head = @body.to_mom( typed_method )
compiler = Risc::MethodCompiler.new( typed_method )
compiler.add_mom(head)
head # return for testing
2017-04-08 16:22:53 +02:00
end
private
def make_type( )
type_hash = {}
2017-04-09 09:14:28 +02:00
@args.each {|arg| type_hash[arg] = :Object }
2017-04-08 16:22:53 +02:00
Parfait::NamedList.type_for( type_hash )
end
2018-03-14 15:54:47 +01:00
def make_frame
2017-04-08 16:22:53 +02:00
type_hash = {}
@body.each do |node|
next unless node.is_a?(LocalVariable) or node.is_a?(LocalAssignment)
type_hash[node.name] = :Object
end
2017-04-08 16:22:53 +02:00
Parfait::NamedList.type_for( type_hash )
end
2017-04-01 15:27:32 +02:00
end
end