Sha256: f9e43874220bdb9d983761075479ae283d529b08cf5218202cf81a35ea746d4b
Contents?: true
Size: 1.17 KB
Versions: 18
Compression:
Stored size: 1.17 KB
Contents
# $Id$ # # A simple calculator, version 2. # This file contains Japanese characters (encoding=EUC-JP). class Calculator2 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 # $Id$ ---- inner def evaluate(str) @tokens = [] until str.empty? case str when /\A\s+/ ; when /\A\d+/ @tokens.push [:NUMBER, $&.to_i] when /\A.|\n/ s = $& @tokens.push [s, s] end str = $' end @tokens.push [false, '$'] do_parse end def next_token @tokens.shift end ---- footer puts '超豪華電卓 2 号機' puts 'Q で終了します' calc = Calculator2.new while true print '>>> '; $stdout.flush str = $stdin.gets.strip break if /q/i =~ str begin p calc.evaluate(str) rescue ParseError puts 'parse error' end end
Version data entries
18 entries across 18 versions & 2 rubygems