class Calcula::Exprs::BinopExpr

Expression for binary operators. An expression 10(2) is also a binary operator. Its operator will be `:PAREN_O`

@author Paul T.

Public Class Methods

new(op, left, right) click to toggle source

@param op [Calcula::Token] Should have type prefixed with `OP_` @param left [Calcula::Expr] @param right [Calcula::Expr]

# File lib/Exprs/BinopExpr.rb, line 12
def initialize(op, left, right)
  @op = op
  @left = left
  @right = right
end

Public Instance Methods

children() click to toggle source

@see Calcula::Expr#children @param (see Calcula::Expr#children) @return (see Calcula::Expr#children)

# File lib/Exprs/BinopExpr.rb, line 50
def children
  [@left, @right]
end
to_s(form: :src) click to toggle source

@see Calcula::Expr#to_s @param (see Calcula::Expr#to_s) @return (see Calcula::Expr#to_s)

# File lib/Exprs/BinopExpr.rb, line 21
def to_s(form: :src)
  leftTxt = @left.to_s(form: form)
  rightTxt = @right.to_s(form: form)
  case form
  when :src, :ruby then
    case @op.id
    when :PAREN_O then
      "#{leftTxt}#{"." if form == :ruby}(#{rightTxt})"
    when :OP_EQ then
      "#{leftTxt}#{form == :ruby ? "==" : "="}#{rightTxt}"
    when :AND then
      "#{leftTxt}#{form == :ruby ? "&&" : " and "}#{rightTxt}"
    when :OR then
      "#{leftTxt}#{form == :ruby ? "||" : " or "}#{rightTxt}"
    when :COMPOSE then
      "->(*x){#{leftTxt}.(#{rightTxt}.(*x))}"
    else
      "#{leftTxt}#{@op.text}#{rightTxt}"
    end
  when :tree then
    "(#{@op.text} #{leftTxt} #{rightTxt})"
  else
    nil
  end
end