Class: Eqn::Expression
- Inherits:
-
Treetop::Runtime::SyntaxNode
- Object
- Treetop::Runtime::SyntaxNode
- Eqn::Expression
- Defined in:
- lib/eqn/expression.rb
Defined Under Namespace
Classes: ExprGroup
Instance Method Summary collapse
Instance Method Details
#left_associative? ⇒ Boolean
3 4 5 |
# File 'lib/eqn/expression.rb', line 3 def left_associative? elements.any? && elements.last.left_associative? end |
#term? ⇒ Boolean
7 8 9 |
# File 'lib/eqn/expression.rb', line 7 def term? elements.empty? end |
#value ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/eqn/expression.rb', line 11 def value if elements.count == 1 elements.shift.value else base = elements.shift.value # Aggressively consume left associative operators to maintain associativity. while left_associative? op, num_expr = elements.shift.value num_expr_operand = num_expr.elements.shift base = base.send(op, num_expr_operand.value) elements.push num_expr.elements.shift unless num_expr.term? end # Apply next right-associative operator (if any) or return. if term? base else op, num_expr = elements.shift.value base.send(op, num_expr.value) end end end |