rubyx/lib/ast/operator_expressions.rb

52 lines
1.5 KiB
Ruby

module Ast
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
def to_s
"#{left} #{operator} #{right}"
end
def compile context , into
puts "compile operator #{to_s}"
r_val = right.compile(context , into)
if operator == "=" # assignment, value based
raise "Can only assign variables, not #{left}" unless left.is_a?(NameExpression)
l_val = context.locals[left.name]
if( l_val ) #variable existed, move data there
l_val = l_val.move( into , r_val)
else
if r_val.is_a? Vm::IntegerConstant
next_register = context.function.next_register
l_val = Vm::Integer.new(next_register).load( into , r_val )
else
l_val = r_val
end
end
context.locals[left.name] = l_val
return l_val
end
l_val = left.compile(context , into)
case operator
when ">"
code = l_val.less_or_equal into , r_val
when "+"
code = l_val.plus into , r_val
when "-"
code = l_val.minus into , r_val
else
raise "unimplemented operator #{operator} #{self}"
end
end
end
end