2014-05-03 14:13:44 +02:00
|
|
|
module Vm
|
|
|
|
|
|
|
|
# name and args , return
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
class FunctionCall < Block
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
def initialize(name , args)
|
2014-05-05 08:35:40 +02:00
|
|
|
super(name)
|
2014-05-03 14:13:44 +02:00
|
|
|
@name = name
|
2014-05-05 08:35:40 +02:00
|
|
|
@values = args
|
2014-05-03 14:13:44 +02:00
|
|
|
@function = nil
|
|
|
|
end
|
2014-05-05 08:35:40 +02:00
|
|
|
attr_reader :function
|
|
|
|
def args
|
|
|
|
values
|
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
def compile context
|
|
|
|
@function = context.program.get_function @name
|
|
|
|
if @function
|
2014-05-05 08:35:40 +02:00
|
|
|
raise "error #{self}" unless @function.arity != @values.length
|
2014-05-03 14:13:44 +02:00
|
|
|
else
|
|
|
|
@function = context.program.get_or_create_function @name
|
|
|
|
end
|
2014-05-05 08:35:40 +02:00
|
|
|
args.each_with_index do |arg |
|
2014-05-03 14:13:44 +02:00
|
|
|
arg.compile context
|
|
|
|
end
|
2014-05-05 08:35:40 +02:00
|
|
|
args.each_with_index do |arg , index|
|
|
|
|
arg.load index
|
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
#puts "funcall #{self.inspect}"
|
2014-05-05 08:35:40 +02:00
|
|
|
self.do_call
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def do_call
|
2014-05-03 14:13:44 +02:00
|
|
|
Machine.instance.function_call self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|