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
|
2014-05-25 10:35:45 +02:00
|
|
|
l_val = l_val.move( into , r_val)
|
2014-05-14 10:33:23 +02:00
|
|
|
else
|
2014-05-22 12:59:47 +02:00
|
|
|
l_val = context.function.new_local.move( 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)
|
2014-05-21 18:33:58 +02:00
|
|
|
into = into.scope binding
|
|
|
|
|
2014-05-13 15:24:19 +02:00
|
|
|
case operator
|
|
|
|
when ">"
|
2014-05-22 20:38:57 +02:00
|
|
|
code = l_val.greater_than into , r_val
|
2014-05-24 09:41:57 +02:00
|
|
|
when "<"
|
|
|
|
code = l_val.less_than into , r_val
|
2014-05-25 07:43:07 +02:00
|
|
|
when "=="
|
|
|
|
code = l_val.equals 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-21 18:33:58 +02:00
|
|
|
into.res = l_val + r_val
|
2014-05-19 10:29:18 +02:00
|
|
|
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
|