2014-05-05 09:02:02 +02:00
|
|
|
module Ast
|
|
|
|
class FunctionExpression < Expression
|
|
|
|
attr_reader :name, :params, :block
|
|
|
|
def initialize name, params, block
|
2014-05-10 14:24:56 +02:00
|
|
|
@name, @params, @block = name.to_sym, params, block
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
2014-05-10 11:54:10 +02:00
|
|
|
def attributes
|
|
|
|
[:name, :params, :block]
|
|
|
|
end
|
|
|
|
def inspect
|
|
|
|
self.class.name + ".new(" + name.inspect + ", ["+
|
|
|
|
params.collect{|m| m.inspect }.join( ",") +"] , [" +
|
|
|
|
block.collect{|m| m.inspect }.join( ",") +"] )"
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
2014-05-10 09:58:25 +02:00
|
|
|
|
|
|
|
def compile context
|
|
|
|
parent_locals = context.locals
|
|
|
|
context.locals = {}
|
2014-05-10 16:55:02 +02:00
|
|
|
args = []
|
|
|
|
params.each do |param|
|
|
|
|
args << param.compile(context) # making the argument a local
|
|
|
|
end
|
|
|
|
# args = params.collect{|p| Vm::Value.type p.name }
|
|
|
|
function = Vm::Function.new(name ,args )
|
|
|
|
context.program.add_function function
|
2014-05-10 09:58:25 +02:00
|
|
|
block.each do |b|
|
|
|
|
compiled = b.compile context
|
|
|
|
if compiled.is_a? Vm::Block
|
|
|
|
he.breaks.loose
|
|
|
|
else
|
|
|
|
function.body.add_code compiled
|
|
|
|
end
|
|
|
|
puts compiled.inspect
|
|
|
|
end
|
|
|
|
context.locals = parent_locals if parent_locals
|
|
|
|
function
|
|
|
|
end
|
|
|
|
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
|
|
|
end
|