2015-05-08 14:10:30 +02:00
|
|
|
module Virtual
|
|
|
|
module Compiler
|
2015-05-04 13:22:22 +02:00
|
|
|
# function attr_reader :name, :params, :body , :receiver
|
2015-05-06 14:14:47 +02:00
|
|
|
def self.compile_function expression, method
|
2015-05-05 14:11:09 +02:00
|
|
|
args = expression.params.collect do |p|
|
|
|
|
raise "error, argument must be a identifier, not #{p}" unless p.is_a? Ast::NameExpression
|
2014-08-20 16:14:52 +02:00
|
|
|
p.name
|
2014-07-16 12:20:47 +02:00
|
|
|
end
|
2015-05-20 16:11:13 +02:00
|
|
|
if expression.receiver
|
|
|
|
#Do something clever instead of
|
|
|
|
r = Compiler.compile(expression.receiver, method )
|
|
|
|
if( r.is_a? Parfait::Class )
|
|
|
|
class_name = r.name
|
|
|
|
else
|
|
|
|
raise "unimplemented #{r}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
r = Self.new()
|
|
|
|
class_name = method.for_class.name
|
|
|
|
end
|
2015-07-03 19:13:03 +02:00
|
|
|
new_method = MethodSource.create_method(class_name, expression.name , args )
|
|
|
|
new_method.source.receiver = r
|
2015-05-24 12:53:49 +02:00
|
|
|
new_method.for_class.add_instance_method new_method
|
2015-06-01 16:31:35 +02:00
|
|
|
|
2014-07-24 20:56:31 +02:00
|
|
|
#frame = frame.new_frame
|
2014-07-14 13:29:33 +02:00
|
|
|
return_type = nil
|
2015-05-05 14:11:09 +02:00
|
|
|
expression.body.each do |ex|
|
2015-05-06 14:14:47 +02:00
|
|
|
return_type = Compiler.compile(ex,new_method )
|
2015-05-08 14:19:30 +02:00
|
|
|
raise return_type.inspect if return_type.is_a? Instruction
|
2014-07-14 13:29:33 +02:00
|
|
|
end
|
2015-07-03 19:13:03 +02:00
|
|
|
new_method.source.return_type = return_type
|
2015-07-19 11:34:23 +02:00
|
|
|
Return.new(return_type)
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
def scratch
|
2014-06-25 01:47:59 +02:00
|
|
|
args = []
|
|
|
|
locals = {}
|
2015-05-05 14:11:09 +02:00
|
|
|
expression.params.each_with_index do |param , index|
|
2014-06-25 01:47:59 +02:00
|
|
|
arg = param.name
|
2015-06-27 20:16:46 +02:00
|
|
|
register = Register::RegisterReference.new(RegisterMachine.instance.receiver_register).next_reg_use(index + 1)
|
2015-05-08 14:19:30 +02:00
|
|
|
arg_value = Integer.new(register)
|
2014-06-25 01:47:59 +02:00
|
|
|
locals[arg] = arg_value
|
|
|
|
args << arg_value
|
|
|
|
end
|
|
|
|
# class depends on receiver
|
2015-05-08 14:19:30 +02:00
|
|
|
me = Integer.new( RegisterMachine.instance.receiver_register )
|
2015-05-05 14:11:09 +02:00
|
|
|
if expression.receiver.nil?
|
2014-06-25 01:47:59 +02:00
|
|
|
clazz = context.current_class
|
|
|
|
else
|
2015-05-16 11:54:11 +02:00
|
|
|
c = context.object_space.get_class_by_name expression.receiver.name.to_sym
|
2014-06-25 01:47:59 +02:00
|
|
|
clazz = c.meta_class
|
|
|
|
end
|
|
|
|
|
2015-05-08 14:19:30 +02:00
|
|
|
function = Function.new(name , me , args )
|
2015-05-04 13:22:22 +02:00
|
|
|
clazz.add_code_function function
|
2014-06-25 01:47:59 +02:00
|
|
|
|
|
|
|
parent_locals = context.locals
|
|
|
|
parent_function = context.function
|
|
|
|
context.locals = locals
|
|
|
|
context.function = function
|
|
|
|
|
|
|
|
last_compiled = nil
|
2015-05-05 14:11:09 +02:00
|
|
|
expression.body.each do |b|
|
2014-06-25 01:47:59 +02:00
|
|
|
puts "compiling in function #{b}"
|
|
|
|
last_compiled = b.compile(context)
|
2015-05-08 14:19:30 +02:00
|
|
|
raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Word
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-05-08 14:19:30 +02:00
|
|
|
return_reg = Integer.new(RegisterMachine.instance.return_register)
|
|
|
|
if last_compiled.is_a?(IntegerConstant) or last_compiled.is_a?(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
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2014-06-25 01:47:59 +02:00
|
|
|
context.locals = parent_locals
|
|
|
|
context.function = parent_function
|
|
|
|
function
|
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|