class Calcula::Exprs::UnaryExpr

Expression for unary operators

@author Paul T.

Public Class Methods

mkPostfix(op, base) click to toggle source

Creates a postfix unary expression. Shorthand for `UnaryExpr.new(op, base, prefixed: false)`.

@see Calcula::Exprs::UnaryExpr#initialize @see Calcula::Exprs::UnaryExpr#mkPrefix @param op [Calcula::Token] Should have type prefixed with `OP_` @param base [Calcula::Expr] @return [Calcula::Exprs::UnaryExpr] The newly created postfix unary expression

# File lib/Exprs/UnaryExpr.rb, line 81
def self.mkPostfix(op, base)
  new(op, base, prefixed: false)
end
mkPrefix(op, base) click to toggle source

Creates a prefixed unary expression. Shorthand for `UnaryExpr.new(op, base, prefixed: true)`.

@see Calcula::Exprs::UnaryExpr#initialize @see Calcula::Exprs::UnaryExpr#mkPostfix @param op [Calcula::Token] Should have type prefixed with `OP_` @param base [Calcula::Expr] @return [Calcula::Exprs::UnaryExpr] The newly created prefix unary expression

# File lib/Exprs/UnaryExpr.rb, line 68
def self.mkPrefix(op, base)
  new(op, base, prefixed: true)
end
new(op, base, prefixed:) click to toggle source

@param op [Calcula::Token] Should have type prefixed with `OP_` @param base [Calcula::Expr] @param prefixed [true, false]

# File lib/Exprs/UnaryExpr.rb, line 11
def initialize(op, base, prefixed:)
  @op = op
  @base = base
  @prefixed = prefixed
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/UnaryExpr.rb, line 56
def children
  [@base]
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/UnaryExpr.rb, line 20
def to_s(form: :src)
  baseTxt = @base.to_s(form: form)
  case form
  when :src then
    @prefixed ? "#{@op.text}#{baseTxt}" : "#{baseTxt}#{@op.text}"
  when :tree then
    "(#{@op.text} #{baseTxt})"
  when :ruby then
    if @prefixed then
      case @op.id
      when :OP_ADD then
        "(#{baseTxt}).abs"
      when :NOT then
        "!(#{baseTxt})"
      else
        "#{@op.text}#{baseTxt}"
      end
    else
      case @op.id
      when :ROUND_DOLLAR then
        "(#{baseTxt}).to_f.round(2)"
      when :DISP then
        "puts _ = #{baseTxt}\n_"
      when :ASSERT then
        "_ = #{baseTxt}\nif _ == false then\n  raise 'Equation #{@base.to_s} failed'\nend"
      else "#{baseTxt}#{@op.text}"
      end
    end
  else
    nil
  end
end