Module: Rucas::Symbolic::OpExpr

Includes:
Expr
Included in:
BinaryOpExpr, UnaryOpExpr
Defined in:
lib/rucas/symbolic.rb,
lib/rucas/rewrite.rb

Overview

Expression with an operator and operand(s).

Constant Summary collapse

OP_PRECEDENCE =

Operator precedence; you shouldn’t rely on the numbers, but (barring changes to Ruby), the order shouldn’t change.

Instance Method Summary collapse

Methods included from Expr

#children, make, #match, #simplify, #to_s_paren, #value

Instance Method Details

#constant?Boolean

Returns:

  • (Boolean)


96
# File 'lib/rucas/symbolic.rb', line 96

def constant?; children.all {|c| c.constant?} end

#precedenceObject

Precedence (in Ruby) of this operator; this affects how expressions are parenthesized.



92
93
94
# File 'lib/rucas/symbolic.rb', line 92

def precedence
  OP_PRECEDENCE[self.op]
end

#rewrite(pattern, output) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rucas/rewrite.rb', line 68

def rewrite pattern, output
  # Rewrite children first. 
  new_children = self.children.map{|c| c.rewrite(pattern, output)}
  new_self = self.class.new(*new_children)
  # See if it's a constant.
  v = new_self.value 
  if v
    Symbolic::Expr.make(v)
  else
    # It's not a constant; try to rewrite using the rule.
    bindings = pattern.match(new_self)
    return output.with(bindings) if bindings
    new_self
  end
end

#with(bindings) ⇒ Object



84
85
86
# File 'lib/rucas/rewrite.rb', line 84

def with bindings
  self.class.new(*self.children.map{|c| c.with(bindings)})
end