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
|
|
|
|
2018-03-15 08:16:56 +01:00
|
|
|
def initialize( name , args , body , clazz = nil)
|
2017-04-12 10:52:23 +02:00
|
|
|
@name , @args , @body = name , args , body
|
2018-03-16 08:03:11 +01:00
|
|
|
raise "no bod" unless @body
|
2018-03-15 08:16:56 +01:00
|
|
|
@clazz = clazz
|
2017-04-12 10:52:23 +02:00
|
|
|
end
|
|
|
|
|
2018-06-26 19:46:58 +02:00
|
|
|
def to_mom(clazz)
|
2018-06-27 16:09:50 +02:00
|
|
|
@clazz = clazz || raise( "no class in #{self}")
|
|
|
|
method = @clazz.add_method_for(name , make_arg_type , make_frame , body )
|
|
|
|
method.compile_to_risc(clazz.instance_type)
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:52:56 +01:00
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
|
|
|
@body.each(&block)
|
2017-04-08 11:10:42 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 08:16:56 +01:00
|
|
|
def normalize
|
|
|
|
MethodStatement.new( @name , @args , @body.normalize)
|
2017-04-08 16:22:53 +02:00
|
|
|
end
|
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
def has_yield?
|
|
|
|
each{|statement| return true if statement.is_a?(YieldStatement)}
|
|
|
|
return false
|
|
|
|
end
|
2017-04-08 16:22:53 +02:00
|
|
|
|
2018-06-27 16:09:50 +02:00
|
|
|
def make_arg_type( )
|
2017-04-08 16:22:53 +02:00
|
|
|
type_hash = {}
|
2017-04-09 09:14:28 +02:00
|
|
|
@args.each {|arg| type_hash[arg] = :Object }
|
2018-06-28 19:15:24 +02:00
|
|
|
type_hash[:implicit_block] = :Object if has_yield?
|
2017-04-08 16:22:53 +02:00
|
|
|
Parfait::NamedList.type_for( type_hash )
|
|
|
|
end
|
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
private
|
|
|
|
|
2018-03-14 15:54:47 +01:00
|
|
|
def make_frame
|
2017-04-08 16:22:53 +02:00
|
|
|
type_hash = {}
|
2018-03-15 12:52:56 +01:00
|
|
|
@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
|