2015-10-07 14:22:47 +02:00
|
|
|
module Phisol
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-10-07 09:05:34 +02:00
|
|
|
|
2015-10-09 17:06:00 +02:00
|
|
|
def on_operator_value statement
|
2015-10-09 16:51:14 +02:00
|
|
|
puts "operator #{statement.inspect}"
|
|
|
|
operator , left_e , right_e = *statement
|
2015-10-15 08:32:47 +02:00
|
|
|
# left and right must be expressions. Expressions return a register when compiled
|
|
|
|
left_reg = process(left_e)
|
|
|
|
right_reg = process(right_e)
|
|
|
|
raise "Not register #{left_reg}" unless left_reg.is_a?(Register::RegisterValue)
|
|
|
|
raise "Not register #{right_reg}" unless right_reg.is_a?(Register::RegisterValue)
|
|
|
|
puts "left #{left_reg}"
|
|
|
|
puts "right #{right_reg}"
|
|
|
|
@method.source.add_code Register::OperatorInstruction.new(statement,operator,left_reg,right_reg)
|
|
|
|
return left_reg # though this has wrong value attached
|
2015-09-19 15:28:41 +02:00
|
|
|
end
|
|
|
|
|
2015-10-09 17:06:00 +02:00
|
|
|
def on_assignment statement
|
2015-10-09 16:51:14 +02:00
|
|
|
puts statement.inspect
|
|
|
|
name , value = *statement
|
2015-09-19 15:28:41 +02:00
|
|
|
name = name.to_a.first
|
2015-09-20 15:52:26 +02:00
|
|
|
v = process(value)
|
2015-10-05 23:27:13 +02:00
|
|
|
index = @method.has_local( name )
|
2015-09-27 15:06:48 +02:00
|
|
|
if(index)
|
2015-10-06 14:26:57 +02:00
|
|
|
@method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index, :int ) , v )
|
2015-09-27 15:06:48 +02:00
|
|
|
else
|
2015-10-05 23:27:13 +02:00
|
|
|
index = @method.has_arg( name )
|
2015-09-27 15:06:48 +02:00
|
|
|
if(index)
|
2015-10-06 14:26:57 +02:00
|
|
|
@method.source.add_code Virtual::Set.new(Virtual::ArgSlot.new(index , :int ) , v )
|
2015-09-27 15:06:48 +02:00
|
|
|
else
|
|
|
|
raise "must define variable #{name} before using it in #{@method.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2015-09-19 15:28:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|