rubyx/lib/old_ast/operator_expressions.rb

56 lines
1.7 KiB
Ruby
Raw Normal View History

2014-05-05 10:02:02 +03:00
module Ast
class OperatorExpression < Expression
# attr_reader :operator, :left, :right
def compile context
into = context.function
puts "compiling operator #{to_s}"
r_val = right.compile(context)
#puts "compiled right #{r_val.inspect}"
if operator == "=" # assignment, value based
2014-06-24 12:20:59 +03:00
if(left.is_a? VariableExpression)
left.make_setter
l_val = left.compile(context)
elsif left.is_a?(NameExpression)
puts context.inspect unless context.locals
l_val = context.locals[left.name]
if( l_val ) #variable existed, move data there
l_val = l_val.move( into , r_val)
else
l_val = context.function.new_local.move( into , r_val )
end
context.locals[left.name] = l_val
else
2014-06-24 12:20:59 +03:00
raise "Can only assign variables, not #{left}"
end
return l_val
end
2014-06-24 12:20:59 +03:00
l_val = left.compile(context)
case operator
when ">"
2014-05-22 21:38:57 +03:00
code = l_val.greater_than into , r_val
when "<"
code = l_val.less_than into , r_val
2014-05-28 20:10:16 +03:00
when ">="
code = l_val.greater_or_equal into , r_val
when "<="
code = l_val.less_or_equal into , r_val
when "=="
code = l_val.equals into , r_val
when "+"
res = context.function.new_local
into.add res , l_val , r_val
code = res
when "-"
res = context.function.new_local
2014-05-14 22:04:03 +03:00
code = res.minus into , l_val , r_val
when "<<"
res = context.function.new_local
code = res.left_shift into , l_val , r_val
else
raise "unimplemented operator #{operator} #{self}"
end
2014-05-14 22:04:03 +03:00
code
end
end
2014-05-05 10:02:02 +03:00
end