2017-04-01 15:27:32 +02:00
|
|
|
module Vool
|
2019-08-19 10:33:12 +02:00
|
|
|
class MethodExpression < Expression
|
2018-07-30 09:26:11 +02:00
|
|
|
attr_reader :name, :args , :body
|
2017-04-01 15:27:32 +02:00
|
|
|
|
2018-07-30 09:26:11 +02:00
|
|
|
def initialize( name , args , body )
|
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
|
2019-08-19 09:40:22 +02:00
|
|
|
raise "Not Vool #{@body}" unless @body.is_a?(Statement)
|
2017-04-12 10:52:23 +02:00
|
|
|
end
|
|
|
|
|
2018-06-26 19:46:58 +02:00
|
|
|
def to_mom(clazz)
|
2018-07-30 09:26:11 +02:00
|
|
|
raise( "no class in #{self}") unless clazz
|
2019-08-08 11:19:27 +02:00
|
|
|
method = make_method(clazz)
|
2018-07-09 15:48:23 +02:00
|
|
|
compiler = method.compiler_for(clazz.instance_type)
|
|
|
|
compiler
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
|
2019-08-08 11:19:27 +02:00
|
|
|
# Class to be passed in is a Parfait class
|
|
|
|
# return VoolMethod
|
|
|
|
#
|
|
|
|
# extracted call to create the VoolMethod as this is the place
|
|
|
|
# where we have all the info. Used in testing.
|
|
|
|
def make_method(clazz)
|
2019-09-21 17:50:33 +02:00
|
|
|
clazz.add_instance_method_for(name , make_arg_type , make_frame , body )
|
2019-08-08 11:19:27 +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-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-07-24 10:35:49 +02:00
|
|
|
type_hash[:implicit_block] = :Block if has_yield?
|
2019-09-18 21:07:05 +02:00
|
|
|
Parfait::Type.for_hash( type_hash )
|
2017-04-08 16:22:53 +02:00
|
|
|
end
|
|
|
|
|
2018-07-03 21:18:19 +02:00
|
|
|
def to_s(depth = 0)
|
|
|
|
arg_str = @args.collect{|a| a.to_s}.join(', ')
|
2019-09-19 19:48:21 +02:00
|
|
|
at_depth(depth , "def #{name}(#{arg_str})\n#{@body.to_s(depth + 1)}\nend")
|
2018-07-03 21:18:19 +02:00
|
|
|
end
|
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
private
|
|
|
|
|
2018-03-14 15:54:47 +01:00
|
|
|
def make_frame
|
2018-07-09 16:55:45 +02:00
|
|
|
nodes = []
|
|
|
|
@body.each { |node| nodes << node }
|
|
|
|
nodes.dup.each do |node|
|
2019-08-19 13:33:02 +02:00
|
|
|
next unless node.is_a?(LambdaExpression)
|
2018-07-09 16:55:45 +02:00
|
|
|
node.each {|block_scope| nodes.delete(block_scope)}
|
|
|
|
end
|
2017-04-08 16:22:53 +02:00
|
|
|
type_hash = {}
|
2018-07-09 16:55:45 +02:00
|
|
|
nodes.each do |node|
|
2018-03-15 12:52:56 +01:00
|
|
|
next unless node.is_a?(LocalVariable) or node.is_a?(LocalAssignment)
|
|
|
|
type_hash[node.name] = :Object
|
|
|
|
end
|
2019-09-18 21:07:05 +02:00
|
|
|
Parfait::Type.for_hash( type_hash )
|
2017-04-08 16:22:53 +02:00
|
|
|
end
|
|
|
|
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
end
|