2014-05-05 09:02:02 +02:00
|
|
|
module Ast
|
2014-05-10 20:41:46 +02:00
|
|
|
class OperatorExpression < Expression
|
2014-06-04 21:03:45 +02:00
|
|
|
# attr_reader :operator, :left, :right
|
2014-05-13 15:24:19 +02:00
|
|
|
def compile context , into
|
2014-06-02 14:11:48 +02:00
|
|
|
puts "compiling operator #{to_s}"
|
2014-05-13 15:24:19 +02:00
|
|
|
r_val = right.compile(context , into)
|
2014-06-02 14:11:48 +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-06-07 16:59:44 +02:00
|
|
|
|
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-28 19:10:16 +02:00
|
|
|
when ">="
|
|
|
|
code = l_val.greater_or_equal into , r_val
|
|
|
|
when "<="
|
|
|
|
code = l_val.less_or_equal 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-06-07 16:59:44 +02:00
|
|
|
into.add 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-06-02 14:11:48 +02:00
|
|
|
when "<<"
|
|
|
|
res = context.function.new_local
|
|
|
|
code = res.left_shift 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
|