2014-05-05 09:02:02 +02:00
|
|
|
module Ast
|
2014-05-13 15:24:19 +02:00
|
|
|
|
2014-05-10 20:41:46 +02:00
|
|
|
class OperatorExpression < Expression
|
|
|
|
attr_reader :operator, :left, :right
|
|
|
|
|
|
|
|
def initialize operator, left, right
|
|
|
|
@operator, @left, @right = operator, left, right
|
|
|
|
end
|
|
|
|
def attributes
|
|
|
|
[:operator, :left, :right]
|
|
|
|
end
|
|
|
|
def inspect
|
|
|
|
self.class.name + ".new(" + operator.inspect + ", " + left.inspect + "," + right.inspect + ")"
|
|
|
|
end
|
2014-05-13 09:49:26 +02:00
|
|
|
def to_s
|
|
|
|
"#{left} #{operator} #{right}"
|
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
def compile context , into
|
2014-05-14 10:33:23 +02:00
|
|
|
puts "compile operator #{to_s}"
|
2014-05-13 15:24:19 +02:00
|
|
|
r_val = right.compile(context , into)
|
2014-05-19 10:29:18 +02:00
|
|
|
puts "compiled right #{r_val.inspect}"
|
2014-05-14 10:33:23 +02:00
|
|
|
if operator == "=" # assignment, value based
|
2014-05-13 15:24:19 +02:00
|
|
|
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
|
2014-05-14 10:33:23 +02:00
|
|
|
l_val = context.locals[left.name]
|
|
|
|
if( l_val ) #variable existed, move data there
|
|
|
|
l_val = l_val.move( into , r_val)
|
|
|
|
else
|
2014-05-21 15:42:36 +02:00
|
|
|
l_val = context.function.new_local.load( into , r_val )
|
2014-05-13 17:21:24 +02:00
|
|
|
end
|
2014-05-14 10:33:23 +02:00
|
|
|
context.locals[left.name] = l_val
|
|
|
|
return l_val
|
2014-05-10 20:41:46 +02:00
|
|
|
end
|
2014-05-14 10:33:23 +02:00
|
|
|
|
2014-05-13 15:24:19 +02:00
|
|
|
l_val = left.compile(context , into)
|
|
|
|
|
|
|
|
case operator
|
|
|
|
when ">"
|
2014-05-13 17:21:24 +02:00
|
|
|
code = l_val.less_or_equal into , r_val
|
2014-05-13 15:24:19 +02:00
|
|
|
when "+"
|
2014-05-21 15:42:36 +02:00
|
|
|
res = context.function.new_local
|
2014-05-19 10:29:18 +02:00
|
|
|
into.add_code res.is l_val + r_val
|
|
|
|
# code = res.plus into , l_val , r_val
|
|
|
|
code = res
|
2014-05-14 10:33:23 +02:00
|
|
|
when "-"
|
2014-05-21 15:42:36 +02:00
|
|
|
res = context.function.new_local
|
2014-05-14 21:04:03 +02:00
|
|
|
code = res.minus into , l_val , r_val
|
2014-05-13 15:24:19 +02:00
|
|
|
else
|
|
|
|
raise "unimplemented operator #{operator} #{self}"
|
2014-05-10 20:41:46 +02:00
|
|
|
end
|
2014-05-14 21:04:03 +02:00
|
|
|
code
|
2014-05-10 20:41:46 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|