Class: Dhaka::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/evaluator/evaluator.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

Performs the pass-through calculations for nodes with only one child_node for which an evaluation rule is not explicitly defined. Will probably be deprecated in future versions.



76
77
78
# File 'lib/evaluator/evaluator.rb', line 76

def method_missing(method_name)
  evaluate(child_nodes[0])
end

Class Method Details

.define_evaluation_rulesObject

Evaluation rules are defined within a block passed to this method.



86
87
88
89
90
# File 'lib/evaluator/evaluator.rb', line 86

def self.define_evaluation_rules
  self.actions = []
  yield
  check_definitions
end

Instance Method Details

#child_nodesObject

Returns the array of child nodes of the node being currently evaluated.



81
82
83
# File 'lib/evaluator/evaluator.rb', line 81

def child_nodes
  @node_stack[-1]
end

#evaluate(node) ⇒ Object

Evaluate a syntax tree node.



66
67
68
69
70
71
72
# File 'lib/evaluator/evaluator.rb', line 66

def evaluate node
  @node_stack ||= []
  @node_stack << node.child_nodes
  result = self.send(node.production.name)
  @node_stack.pop
  result
end