#!/usr/bin/env ruby
+# TODO:
+#
+# * Add unary operators for integers
+# * Add type checking to operators on constants
+# * Add optimization of constant operators
+# * Add increment/decrement optimization
+# * Implement function calls
+# * Implement string constants
+# * Implement if statement
+# * Implement tail call optimization?
+
require 'stringio'
require 'strscan'
end
def simple_expr()
- term()
+ # Handle unary ops first
+ if matches_any(["+", "-"])
+ op = consume()
+ left = term()
+ left = IR::Op.new(op.location, nil, op.text, left, nil)
+ else
+ left = term()
+ end
+ # now look for binary ops
+ while matches_any(["+", "-", :or])
+ op = consume()
+ right = term();
+ left = IR::Op.new(location, nil, op.text, left, right)
+ end
+ left
# 269 /* first term and +/- unary ops */
# 270 if (matches_oneof(p, (int[]){'+', '-', 0}))
# 271 {
elsif v.is_a? IR::Const then
emit_const(v)
elsif v.is_a? IR::Op then
- emit_op(v)
+ emit_binop(v)
else
- raise "not implemented"
+ raise "code emitting of #{v.class} not implemented"
end
end
end
end
- def self.emit_op(v)
- emit(v.right) if v.right
+ def self.emit_binop(v)
emit(v.left)
+ emit(v.right)
case v.op
+ when "+"
+ puts " addi"
+ when "-"
+ puts " subi"
when "*"
puts " muli"
when "/"