2014-06-25 01:47:59 +02:00
|
|
|
module Ast
|
|
|
|
class FunctionExpression < Expression
|
|
|
|
# attr_reader :name, :params, :body , :receiver
|
2014-07-14 10:29:38 +02:00
|
|
|
def compile frame , method
|
2014-07-16 12:20:47 +02:00
|
|
|
args = params.collect do |p|
|
|
|
|
raise "error, arguemnt must be a identifier, not #{p}" unless p.is_a? NameExpression
|
|
|
|
Virtual::Argument.new( p.name , Virtual::Mystery.new )
|
|
|
|
end
|
2014-07-14 13:06:09 +02:00
|
|
|
r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new
|
2014-07-16 12:20:47 +02:00
|
|
|
method = Virtual::Method.new(name , args , r )
|
2014-07-14 13:29:33 +02:00
|
|
|
frame = frame.new_frame
|
|
|
|
return_type = nil
|
|
|
|
body.each do |ex|
|
2014-07-14 23:00:00 +02:00
|
|
|
return_type = ex.compile(frame , method )
|
|
|
|
raise return_type.inspect if return_type.is_a? Virtual::Instruction
|
2014-07-14 13:29:33 +02:00
|
|
|
end
|
|
|
|
method.return_type = return_type
|
|
|
|
method
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
def scratch
|
2014-06-25 01:47:59 +02:00
|
|
|
args = []
|
|
|
|
locals = {}
|
|
|
|
params.each_with_index do |param , index|
|
|
|
|
arg = param.name
|
2014-06-26 16:52:15 +02:00
|
|
|
register = Virtual::RegisterReference.new(Virtual::RegisterMachine.instance.receiver_register).next_reg_use(index + 1)
|
|
|
|
arg_value = Virtual::Integer.new(register)
|
2014-06-25 01:47:59 +02:00
|
|
|
locals[arg] = arg_value
|
|
|
|
args << arg_value
|
|
|
|
end
|
|
|
|
# class depends on receiver
|
2014-06-26 16:52:15 +02:00
|
|
|
me = Virtual::Integer.new( Virtual::RegisterMachine.instance.receiver_register )
|
2014-06-25 01:47:59 +02:00
|
|
|
if receiver.nil?
|
|
|
|
clazz = context.current_class
|
|
|
|
else
|
|
|
|
c = context.object_space.get_or_create_class receiver.name.to_sym
|
|
|
|
clazz = c.meta_class
|
|
|
|
end
|
|
|
|
|
2014-06-26 16:52:15 +02:00
|
|
|
function = Virtual::Function.new(name , me , args )
|
2014-06-25 01:47:59 +02:00
|
|
|
clazz.add_function function
|
|
|
|
|
|
|
|
parent_locals = context.locals
|
|
|
|
parent_function = context.function
|
|
|
|
context.locals = locals
|
|
|
|
context.function = function
|
|
|
|
|
|
|
|
last_compiled = nil
|
|
|
|
body.each do |b|
|
|
|
|
puts "compiling in function #{b}"
|
|
|
|
last_compiled = b.compile(context)
|
2014-06-26 16:52:15 +02:00
|
|
|
raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Virtual::Word
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
|
2014-06-26 16:52:15 +02:00
|
|
|
return_reg = Virtual::Integer.new(Virtual::RegisterMachine.instance.return_register)
|
|
|
|
if last_compiled.is_a?(Virtual::IntegerConstant) or last_compiled.is_a?(Virtual::ObjectConstant)
|
2014-06-25 01:47:59 +02:00
|
|
|
return_reg.load function , last_compiled if last_compiled.register_symbol != return_reg.register_symbol
|
|
|
|
else
|
|
|
|
return_reg.move( function, last_compiled ) if last_compiled.register_symbol != return_reg.register_symbol
|
|
|
|
end
|
|
|
|
function.set_return return_reg
|
|
|
|
|
|
|
|
context.locals = parent_locals
|
|
|
|
context.function = parent_function
|
|
|
|
function
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|