Class: EPNode
- Inherits:
-
Array
- Object
- Array
- EPNode
- Defined in:
- lib/expressionparser.rb
Overview
SimpleExpression library Copyright © 2007 Noah Gibbs
This library is in the public domain, and may be redistributed in any way.
Instance Method Summary collapse
- #eval(vars = {}) ⇒ Object
-
#optimize(vars = {}) ⇒ Object
For now, just do constant folding.
Instance Method Details
#eval(vars = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/expressionparser.rb', line 27 def eval(vars = {}) args = self[1..-1].collect { |arg| EPNode.prveval(arg, vars) } op = self[0] op = "**" if op == "^" # exponentiation uses different op in ruby if SimpleExpression.operator?(op) if self.length == 2 return (op == "-" ? -args[0] : args[0]) elsif self.length == 3 return (args[0]).send(op, args[1]) else raise "Unexpected number of arguments to #{op}!" end end if SimpleExpression.function?(op) return Math.send(op, args[0]) end return vars[op] if vars[op] and self.length == 1 raise "Unknown expression in EPNode##eval!" end |
#optimize(vars = {}) ⇒ Object
For now, just do constant folding
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/expressionparser.rb', line 53 def optimize(vars = {}) args = self[1..-1].collect { |arg| EPNode.prveval(arg, vars, false) } if args.any? { |arg| arg == :NoInfo } opt = [ nil ] * args.length args.each_with_index do |arg, index| opt[index] = args[index] opt[index] = self[index + 1] if args[index] == :NoInfo end return EPNode.new(opt) end self.eval(vars) end |