Sha256: ab7f5471a03ee3da7aad1ab0bcdca0bccf14d6cd91a10f051f326b8fc5711512
Contents?: true
Size: 1017 Bytes
Versions: 18
Compression:
Stored size: 1017 Bytes
Contents
# $Id$ # # Very simple calculater. class Calcp prechigh nonassoc UMINUS left '*' '/' left '+' '-' preclow rule target: exp | /* none */ { result = 0 } exp: exp '+' exp { result += val[2] } | exp '-' exp { result -= val[2] } | exp '*' exp { result *= val[2] } | exp '/' exp { result /= val[2] } | '(' exp ')' { result = val[1] } | '-' NUMBER =UMINUS { result = -val[1] } | NUMBER end ---- header # $Id$ ---- inner def parse(str) @q = [] until str.empty? case str when /\A\s+/ when /\A\d+/ @q.push [:NUMBER, $&.to_i] when /\A.|\n/o s = $& @q.push [s, s] end str = $' end @q.push [false, '$end'] do_parse end def next_token @q.shift end ---- footer parser = Calcp.new puts puts 'type "Q" to quit.' puts while true puts print '? ' str = gets.chop! break if /q/i =~ str begin puts "= #{parser.parse(str)}" rescue ParseError puts $! end end
Version data entries
18 entries across 18 versions & 2 rubygems