class ReDuxml::Evaluator
Constants
- LOGIC_FILE
Attributes
param_hash[R]
parser[R]
Public Class Methods
new(logic=nil)
click to toggle source
# File lib/re_duxml/evaluate.rb, line 17 def initialize(logic=nil) @parser = Parser.new(logic || LOGIC_FILE) @param_hash = {} end
Public Instance Methods
evaluate(_expr, _param_hash={})
click to toggle source
# File lib/re_duxml/evaluate.rb, line 26 def evaluate(_expr, _param_hash={}) _param_hash.each do |key, val| @param_hash[key.to_s] = _param_hash[key] end expr = resolve_params _expr return expr if expr.parameterized? return expr if Regexp.identifier.match(expr).to_s == expr return expr.to_i if expr.to_i.to_s == expr result = reduce parser.parse expr case when result.respond_to?(:imaginary), result.class == TrueClass, result.class == FalseClass then result when result.respond_to?(:name) then result.name when result.respond_to?(:to_sexp) then result.print(parser.logic) else result.to_s end end
to_s()
click to toggle source
# File lib/re_duxml/evaluate.rb, line 22 def to_s "#<Evaluator: param_hash: '#{param_hash.to_s}' parser_logic: '#{parser}'>" end
Private Instance Methods
reduce(_ast)
click to toggle source
# File lib/re_duxml/evaluate.rb, line 58 def reduce(_ast) return _ast if _ast.children.empty? ast = _ast.type.respond_to?(:symbol) ? _ast : new_ast(parser.logic[_ast.type.to_s], _ast.children.dup) if ast.children.any? operator = ast.type args = ast.children.collect do |c| new_child = c.children.any? ? reduce(c) : c.type if new_child.is_a?(Node) && new_child.type.is_a?(Symbol) new_ast(parser.logic[new_child.type.to_s], *new_child.children.dup) else new_child end end.flatten begin result = case operator.position when :prefix method(operator.ruby).call(*args) else args.first.send(operator.ruby, *args[1..-1]) end result.nil? ? ast : result rescue ast end else ast end end
resolve_params(_expr)
click to toggle source
# File lib/re_duxml/evaluate.rb, line 46 def resolve_params(_expr) expr = _expr.dup _expr.scan(Regexp.identifier) do |match| param_name = match[0] param_value = param_hash[param_name] || param_name expr.gsub!(/\b#{Regexp.quote(param_name)}\b/, param_value.to_s) end expr end