#
# A simple calculator, version 3.
#

class Calculator3
  prechigh
    nonassoc UMINUS
    left '*' '/'
    left '+' '-'
  preclow
  options no_result_var
rule
  target  : exp
          | /* none */ { 0 }

  exp     : exp '+' exp { val[0] + val[2] }
          | exp '-' exp { val[0] - val[2] }
          | exp '*' exp { val[0] * val[2] }
          | exp '/' exp { val[0] / val[2] }
          | '(' exp ')' { val[1] }
          | '-' NUMBER  =UMINUS { -(val[1]) }
          | NUMBER
end

---- header ----
#
# generated by racc
#
require 'calc3.rex'

---- inner ----

---- footer ----

puts 'sample calc'
puts '"q" to quit.'
calc = Calculator3.new
while true
  print '>>> '; $stdout.flush
  str = $stdin.gets.strip
  break if /q/i === str
  begin
    p calc.scan_str(str)
  rescue ParseError
    puts 'parse error'
  end
end
