# # DO NOT MODIFY!!!! # This file is automatically generated by Racc 1.4.9 # from Racc grammer file "". # require 'racc/parser.rb' # Generated grammar file. module Maths class Calculator < Racc::Parser module_eval(<<'...end calc.y/module_eval...', 'calc.y', 41) def parse(str) @q = [] until str.empty? case str # ignore whitespace when /\A\s+/ # percentage handling when /\Aas a percentage of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas a % of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas % of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas % of/ @q.push [:PERCENTAGE_OF, $&] when /\Aas a % change from/ @q.push [:PERCENTAGE_TO, $&] # special terms when /\Api/ @q.push [:NUMBER, BigDecimal.new("3.14159265358979323846264338327")] # variables and functions when /\A[A-Za-z]+[0-9]*/ if Maths::Functions::FUNCTIONS.has_key?($&) @q.push [:FUNC, $&] else @q.push [:VARIABLE, $&] end when /\A\$/ @q.push [:VARIABLE, $&] # numbers when /\A\d+(\.\d+)?([Ee]\d+)?/ @q.push [:NUMBER, BigDecimal.new($&)] # when /\A\d+/ # @q.push [:NUMBER, BigDecimal.new($&)] 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 ...end calc.y/module_eval... ##### State transition tables begin ### racc_action_table = [ 9, 16, 13, 14, 15, 9, 6, 10, 22, 23, 4, 6, 5, 8, 7, 4, 9, 5, 8, 7, 25, 9, 6, nil, nil, nil, 4, 6, 5, 8, 20, 4, 9, 5, 8, 20, nil, 9, 6, nil, nil, nil, 4, 6, 5, 8, 20, 4, 9, 5, 8, 20, nil, 9, 6, nil, nil, nil, 4, 6, 5, 8, 20, 4, 9, 5, 8, 20, nil, 9, 6, nil, nil, nil, 4, 6, 5, 8, 20, 4, 9, 5, 8, 20, nil, 9, 6, nil, nil, nil, 4, 6, 5, 8, 20, 4, 9, 5, 8, 20, nil, nil, 6, nil, nil, nil, 4, nil, 5, 8, 20, 16, 13, 14, 15, 11, 12, 17, 18, nil, nil, nil, 35, 16, 13, 14, 15, 11, 12, 17, 18, nil, nil, 34, 16, 13, 14, 15, 11, 12, 17, 18, 16, 13, 14, 15, 11, 12, 17, 18, 16, 13, 14, 15, 11, 12, 17, 18, 16, 13, 14, 15, 11, 12, 17, 16, 13, 14, 15 ] racc_action_check = [ 0, 27, 27, 27, 27, 23, 0, 1, 6, 7, 0, 23, 0, 0, 0, 23, 18, 23, 23, 23, 10, 4, 18, nil, nil, nil, 18, 4, 18, 18, 18, 4, 5, 4, 4, 4, nil, 17, 5, nil, nil, nil, 5, 17, 5, 5, 5, 17, 16, 17, 17, 17, nil, 9, 16, nil, nil, nil, 16, 9, 16, 16, 16, 9, 15, 9, 9, 9, nil, 11, 15, nil, nil, nil, 15, 11, 15, 15, 15, 11, 12, 11, 11, 11, nil, 13, 12, nil, nil, nil, 12, 13, 12, 12, 12, 13, 14, 13, 13, 13, nil, nil, 14, nil, nil, nil, 14, nil, 14, 14, 14, 21, 21, 21, 21, 21, 21, 21, 21, nil, nil, nil, 21, 19, 19, 19, 19, 19, 19, 19, 19, nil, nil, 19, 36, 36, 36, 36, 36, 36, 36, 36, 2, 2, 2, 2, 2, 2, 2, 2, 33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 32, 32, 32, 32, 32, 26, 26, 26, 26 ] racc_action_pointer = [ -3, 7, 138, nil, 18, 29, -8, -3, nil, 50, 20, 66, 77, 82, 93, 61, 45, 34, 13, 119, nil, 107, nil, 2, nil, nil, 161, -3, nil, nil, nil, nil, 154, 146, nil, nil, 130, nil ] racc_action_default = [ -3, -20, -1, -2, -20, -20, -20, -13, -14, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -13, -20, -12, -20, -15, 38, -4, -5, -6, -7, -8, -9, -16, -17, -10, -11, -18, -19 ] racc_goto_table = [ 3, 2, 1, nil, nil, 19, 21, nil, nil, nil, 24, nil, 26, 27, 28, 29, 30, 31, 32, 33, nil, nil, nil, 37, 36 ] racc_goto_check = [ 3, 2, 1, nil, nil, 2, 2, nil, nil, nil, 2, nil, 2, 2, 2, 2, 2, 2, 2, 2, nil, nil, nil, 3, 2 ] racc_goto_pointer = [ nil, 2, 1, 0 ] racc_goto_default = [ nil, nil, nil, nil ] racc_reduce_table = [ 0, 0, :racc_error, 1, 19, :_reduce_none, 1, 19, :_reduce_none, 0, 19, :_reduce_3, 3, 20, :_reduce_4, 3, 20, :_reduce_5, 3, 20, :_reduce_6, 3, 20, :_reduce_7, 3, 20, :_reduce_8, 3, 20, :_reduce_9, 3, 20, :_reduce_10, 3, 20, :_reduce_11, 2, 20, :_reduce_12, 1, 20, :_reduce_13, 1, 20, :_reduce_none, 2, 20, :_reduce_15, 3, 20, :_reduce_16, 3, 20, :_reduce_17, 3, 21, :_reduce_18, 3, 21, :_reduce_19 ] racc_reduce_n = 20 racc_shift_n = 38 racc_token_table = { false => 0, :error => 1, :UMINUS => 2, :FUNC => 3, "^" => 4, "*" => 5, "/" => 6, "%" => 7, "+" => 8, "-" => 9, :PERCENTAGE_OF => 10, :PERCENTAGE_TO => 11, "=" => 12, "(" => 13, ")" => 14, "|" => 15, :NUMBER => 16, :VARIABLE => 17 } racc_nt_base = 18 racc_use_result_var = true Racc_arg = [ racc_action_table, racc_action_check, racc_action_default, racc_action_pointer, racc_goto_table, racc_goto_check, racc_goto_default, racc_goto_pointer, racc_nt_base, racc_reduce_table, racc_token_table, racc_shift_n, racc_reduce_n, racc_use_result_var ] Racc_token_to_s_table = [ "$end", "error", "UMINUS", "FUNC", "\"^\"", "\"*\"", "\"/\"", "\"%\"", "\"+\"", "\"-\"", "PERCENTAGE_OF", "PERCENTAGE_TO", "\"=\"", "\"(\"", "\")\"", "\"|\"", "NUMBER", "VARIABLE", "$start", "target", "exp", "assign" ] Racc_debug_parser = false ##### State transition tables end ##### # reduce 0 omitted # reduce 1 omitted # reduce 2 omitted module_eval(<<'.,.,', 'calc.y', 14) def _reduce_3(val, _values, result) result = 0 result end .,., module_eval(<<'.,.,', 'calc.y', 16) def _reduce_4(val, _values, result) result += val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 17) def _reduce_5(val, _values, result) result -= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 18) def _reduce_6(val, _values, result) result *= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 19) def _reduce_7(val, _values, result) result /= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 20) def _reduce_8(val, _values, result) result %= val[2] result end .,., module_eval(<<'.,.,', 'calc.y', 21) def _reduce_9(val, _values, result) result = result.power(val[2].to_r) result end .,., module_eval(<<'.,.,', 'calc.y', 22) def _reduce_10(val, _values, result) result = val[1] result end .,., module_eval(<<'.,.,', 'calc.y', 23) def _reduce_11(val, _values, result) result = val[1].abs result end .,., module_eval(<<'.,.,', 'calc.y', 24) def _reduce_12(val, _values, result) result = -val[1] result end .,., module_eval(<<'.,.,', 'calc.y', 25) def _reduce_13(val, _values, result) result = Brain.lookup(val[0]) result end .,., # reduce 14 omitted module_eval(<<'.,.,', 'calc.y', 27) def _reduce_15(val, _values, result) result = Maths::Functions.exec(val[0], val[1]) result end .,., module_eval(<<'.,.,', 'calc.y', 28) def _reduce_16(val, _values, result) result = Percentage.build(val[0], val[2]) result end .,., module_eval(<<'.,.,', 'calc.y', 29) def _reduce_17(val, _values, result) result = Percentage.change(val[2], val[0]) result end .,., module_eval(<<'.,.,', 'calc.y', 31) def _reduce_18(val, _values, result) result = Brain.assign(val[0], val[2]) result end .,., module_eval(<<'.,.,', 'calc.y', 32) def _reduce_19(val, _values, result) result = Brain.assign(val[0], val[2]) result end .,., def _reduce_none(val, _values, result) val[0] end end # class Calculator end