Class: Rucas::Symbolic::BinaryOpExpr

Inherits:
Struct
  • Object
show all
Includes:
OpExpr
Defined in:
lib/rucas/symbolic.rb,
lib/rucas/rewrite.rb,
lib/rucas/symbolic.rb

Overview

Binary operations.

Direct Known Subclasses

ArithmeticOpExpr, CompareOpExpr

Constant Summary

Constants included from OpExpr

OpExpr::OP_PRECEDENCE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OpExpr

#constant?, #precedence, #rewrite, #with

Methods included from Expr

make, #precedence, #rewrite, #simplify

Instance Attribute Details

#lhsObject

Returns the value of attribute lhs

Returns:

  • (Object)

    the current value of lhs



148
149
150
# File 'lib/rucas/symbolic.rb', line 148

def lhs
  @lhs
end

#opObject

Returns the value of attribute op

Returns:

  • (Object)

    the current value of op



148
149
150
# File 'lib/rucas/symbolic.rb', line 148

def op
  @op
end

#rhsObject

Returns the value of attribute rhs

Returns:

  • (Object)

    the current value of rhs



148
149
150
# File 'lib/rucas/symbolic.rb', line 148

def rhs
  @rhs
end

Instance Method Details

#childrenObject



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

def children; [lhs, rhs] end

#match(expr, bindings = {}) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/rucas/rewrite.rb', line 127

def match expr, bindings = {}
  if expr.is_a?(BinaryOpExpr) && self.op == expr.op
    lb = self.lhs.match(expr.lhs, bindings)
    rb = self.rhs.match(expr.rhs, lb) if lb
    return rb if rb
  end
  nil
end

#to_sObject



159
160
161
162
163
# File 'lib/rucas/symbolic.rb', line 159

def to_s
  inner = self.children.map{|c|
    c.precedence < self.precedence ? "(#{c})" : c.to_s}
  "#{inner.join(self.op_string)}"
end

#to_s_parenObject



154
155
156
157
# File 'lib/rucas/symbolic.rb', line 154

def to_s_paren
  op_string = " #{self.op} "
  "(#{self.children.map{|c| c.to_s_paren}.join(op_string)})"
end

#valueObject



136
137
138
139
140
# File 'lib/rucas/rewrite.rb', line 136

def value
  lv = lhs.value
  rv = rhs.value
  eval "(#{lv})#{self.op}(#{rv})" if lv && rv
end