2017-04-01 16:27:32 +03:00
|
|
|
module Vool
|
2017-04-08 17:22:53 +03:00
|
|
|
class MethodStatement < Statement
|
|
|
|
attr_reader :name, :args , :body , :clazz
|
2017-04-01 16:27:32 +03:00
|
|
|
|
2018-03-15 12:46:56 +05:30
|
|
|
def initialize( name , args , body , clazz = nil)
|
2017-04-12 11:52:23 +03:00
|
|
|
@name , @args , @body = name , args , body
|
2018-03-16 12:33:11 +05:30
|
|
|
raise "no bod" unless @body
|
2018-03-15 12:46:56 +05:30
|
|
|
@clazz = clazz
|
2017-04-12 11:52:23 +03:00
|
|
|
end
|
|
|
|
|
2018-03-16 11:03:29 +05:30
|
|
|
# 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
|
2017-04-12 11:52:23 +03:00
|
|
|
def to_mom( _ )
|
2018-03-16 11:03:29 +05:30
|
|
|
raise "should not be called (call create_objects)"
|
2017-04-01 16:27:32 +03:00
|
|
|
end
|
|
|
|
|
2018-03-15 17:22:56 +05:30
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
|
|
|
@body.each(&block)
|
2017-04-08 12:10:42 +03:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:46:56 +05:30
|
|
|
def normalize
|
|
|
|
MethodStatement.new( @name , @args , @body.normalize)
|
2017-04-08 17:22:53 +03:00
|
|
|
end
|
|
|
|
|
2018-03-15 17:22:56 +05:30
|
|
|
def create_objects(clazz)
|
|
|
|
@clazz = clazz
|
|
|
|
raise "no class" unless clazz
|
2017-04-08 17:22:53 +03:00
|
|
|
args_type = make_type
|
2018-03-14 20:24:47 +05:30
|
|
|
frame_type = make_frame
|
|
|
|
method = Parfait::VoolMethod.new(name , args_type , frame_type , body )
|
2017-04-08 17:22:53 +03:00
|
|
|
@clazz.add_method( method )
|
2018-03-13 16:16:06 +05:30
|
|
|
typed_method = method.create_parfait_method(clazz.instance_type)
|
2018-03-16 11:03:29 +05:30
|
|
|
head = @body.to_mom( typed_method )
|
2018-03-25 19:37:51 +03:00
|
|
|
compiler = Risc::MethodCompiler.new( typed_method )
|
2018-03-14 17:41:09 +05:30
|
|
|
compiler.add_mom(head)
|
2018-03-16 11:03:29 +05:30
|
|
|
head # return for testing
|
2017-04-08 17:22:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def make_type( )
|
|
|
|
type_hash = {}
|
2017-04-09 10:14:28 +03:00
|
|
|
@args.each {|arg| type_hash[arg] = :Object }
|
2017-04-08 17:22:53 +03:00
|
|
|
Parfait::NamedList.type_for( type_hash )
|
|
|
|
end
|
|
|
|
|
2018-03-14 20:24:47 +05:30
|
|
|
def make_frame
|
2017-04-08 17:22:53 +03:00
|
|
|
type_hash = {}
|
2018-03-15 17:22:56 +05:30
|
|
|
@body.each do |node|
|
|
|
|
next unless node.is_a?(LocalVariable) or node.is_a?(LocalAssignment)
|
|
|
|
type_hash[node.name] = :Object
|
|
|
|
end
|
2017-04-08 17:22:53 +03:00
|
|
|
Parfait::NamedList.type_for( type_hash )
|
|
|
|
end
|
|
|
|
|
2017-04-01 16:27:32 +03:00
|
|
|
end
|
|
|
|
end
|