lib/maths/calc.rb in maths-0.0.10 vs lib/maths/calc.rb in maths-0.0.11

- old
+ new

@@ -8,11 +8,11 @@ # Generated grammar file. module Maths class Calculator < Racc::Parser -module_eval(<<'...end calc.y/module_eval...', 'calc.y', 39) +module_eval(<<'...end calc.y/module_eval...', 'calc.y', 41) def parse(str) @q = [] until str.empty? case str @@ -28,10 +28,13 @@ 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 @@ -65,95 +68,100 @@ ...end calc.y/module_eval... ##### State transition tables begin ### racc_action_table = [ - 9, 16, 13, 14, 15, 21, 6, 10, 9, 4, - 22, 5, 8, 7, 6, 24, 9, 4, nil, 5, - 8, 7, 6, nil, 9, 4, nil, 5, 8, 19, - 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, - 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, - nil, 5, 8, 19, 6, nil, 9, 4, nil, 5, - 8, 19, 6, nil, 9, 4, nil, 5, 8, 19, - 6, nil, 9, 4, nil, 5, 8, 19, 6, nil, - 9, 4, nil, 5, 8, 19, 6, nil, 9, 4, - nil, 5, 8, 19, 6, nil, nil, 4, nil, 5, - 8, 19, 16, 13, 14, 15, 11, 12, 17, nil, - nil, nil, 33, 16, 13, 14, 15, 11, 12, 17, - nil, nil, 32, 16, 13, 14, 15, 11, 12, 17, - 16, 13, 14, 15, 11, 12, 17, 16, 13, 14, - 15, 11, 12, 17, 16, 13, 14, 15 ] + 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, 26, 26, 26, 26, 6, 0, 1, 22, 0, - 7, 0, 0, 0, 22, 10, 17, 22, nil, 22, - 22, 22, 17, nil, 4, 17, nil, 17, 17, 17, - 4, nil, 5, 4, nil, 4, 4, 4, 5, nil, - 16, 5, nil, 5, 5, 5, 16, nil, 15, 16, - nil, 16, 16, 16, 15, nil, 9, 15, nil, 15, - 15, 15, 9, nil, 14, 9, nil, 9, 9, 9, - 14, nil, 11, 14, nil, 14, 14, 14, 11, nil, - 12, 11, nil, 11, 11, 11, 12, nil, 13, 12, - nil, 12, 12, 12, 13, nil, nil, 13, nil, 13, - 13, 13, 20, 20, 20, 20, 20, 20, 20, nil, - nil, nil, 20, 18, 18, 18, 18, 18, 18, 18, - nil, nil, 18, 31, 31, 31, 31, 31, 31, 31, - 2, 2, 2, 2, 2, 2, 2, 34, 34, 34, - 34, 34, 34, 34, 25, 25, 25, 25 ] + 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, 126, nil, 21, 29, -10, -1, nil, 53, - 15, 69, 77, 85, 61, 45, 37, 13, 109, nil, - 98, nil, 5, nil, nil, 140, -3, nil, nil, nil, - nil, 119, nil, nil, 133, nil ] + -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, -19, -1, -2, -19, -19, -19, -13, -14, -19, - -19, -19, -19, -19, -19, -19, -19, -19, -19, -13, - -19, -12, -19, -15, 36, -4, -5, -6, -7, -8, - -9, -16, -10, -11, -17, -18 ] + -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, 18, 20, nil, nil, nil, - 23, nil, 25, 26, 27, 28, 29, 30, 31, nil, - nil, nil, 35, 34 ] + 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, nil, - nil, nil, 3, 2 ] + 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, 18, :_reduce_none, - 1, 18, :_reduce_none, - 0, 18, :_reduce_3, - 3, 19, :_reduce_4, - 3, 19, :_reduce_5, - 3, 19, :_reduce_6, - 3, 19, :_reduce_7, - 3, 19, :_reduce_8, - 3, 19, :_reduce_9, - 3, 19, :_reduce_10, - 3, 19, :_reduce_11, - 2, 19, :_reduce_12, - 1, 19, :_reduce_13, 1, 19, :_reduce_none, - 2, 19, :_reduce_15, - 3, 19, :_reduce_16, + 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, 20, :_reduce_18 ] + 3, 21, :_reduce_18, + 3, 21, :_reduce_19 ] -racc_reduce_n = 19 +racc_reduce_n = 20 -racc_shift_n = 36 +racc_shift_n = 38 racc_token_table = { false => 0, :error => 1, :UMINUS => 2, @@ -163,18 +171,19 @@ "/" => 6, "%" => 7, "+" => 8, "-" => 9, :PERCENTAGE_OF => 10, - "=" => 11, - "(" => 12, - ")" => 13, - "|" => 14, - :NUMBER => 15, - :VARIABLE => 16 } + :PERCENTAGE_TO => 11, + "=" => 12, + "(" => 13, + ")" => 14, + "|" => 15, + :NUMBER => 16, + :VARIABLE => 17 } -racc_nt_base = 17 +racc_nt_base = 18 racc_use_result_var = true Racc_arg = [ racc_action_table, @@ -202,10 +211,11 @@ "\"/\"", "\"%\"", "\"+\"", "\"-\"", "PERCENTAGE_OF", + "PERCENTAGE_TO", "\"=\"", "\"(\"", "\")\"", "\"|\"", "NUMBER", @@ -223,111 +233,118 @@ # reduce 1 omitted # reduce 2 omitted -module_eval(<<'.,.,', 'calc.y', 13) +module_eval(<<'.,.,', 'calc.y', 14) def _reduce_3(val, _values, result) result = 0 result end .,., -module_eval(<<'.,.,', 'calc.y', 15) +module_eval(<<'.,.,', 'calc.y', 16) def _reduce_4(val, _values, result) result += val[2] result end .,., -module_eval(<<'.,.,', 'calc.y', 16) +module_eval(<<'.,.,', 'calc.y', 17) def _reduce_5(val, _values, result) result -= val[2] result end .,., -module_eval(<<'.,.,', 'calc.y', 17) +module_eval(<<'.,.,', 'calc.y', 18) def _reduce_6(val, _values, result) result *= val[2] result end .,., -module_eval(<<'.,.,', 'calc.y', 18) +module_eval(<<'.,.,', 'calc.y', 19) def _reduce_7(val, _values, result) result /= val[2] result end .,., -module_eval(<<'.,.,', 'calc.y', 19) +module_eval(<<'.,.,', 'calc.y', 20) def _reduce_8(val, _values, result) result %= val[2] result end .,., -module_eval(<<'.,.,', 'calc.y', 20) +module_eval(<<'.,.,', 'calc.y', 21) def _reduce_9(val, _values, result) result = result.power(val[2]) result end .,., -module_eval(<<'.,.,', 'calc.y', 21) +module_eval(<<'.,.,', 'calc.y', 22) def _reduce_10(val, _values, result) result = val[1] result end .,., -module_eval(<<'.,.,', 'calc.y', 22) +module_eval(<<'.,.,', 'calc.y', 23) def _reduce_11(val, _values, result) result = val[1].abs result end .,., -module_eval(<<'.,.,', 'calc.y', 23) +module_eval(<<'.,.,', 'calc.y', 24) def _reduce_12(val, _values, result) result = -val[1] result end .,., -module_eval(<<'.,.,', 'calc.y', 24) +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', 26) +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', 27) +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 = Brain.assign(val[0], val[2]) + result = Percentage.change(val[2], val[0]) result end .,., -module_eval(<<'.,.,', 'calc.y', 30) +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 .,.,