Class: Rjq::AST::UnaryOp

Inherits:
Node
  • Object
show all
Defined in:
lib/rjq/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_span

Instance Method Summary collapse

Methods inherited from Node

#assign_value, #invalid_path_message, #paths, #take, #with_source_span

Constructor Details

#initialize(op, expression) ⇒ UnaryOp

Returns a new instance of UnaryOp.



787
788
789
790
# File 'lib/rjq/ast.rb', line 787

def initialize(op, expression)
  @op = op
  @expression = expression
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



785
786
787
# File 'lib/rjq/ast.rb', line 785

def expression
  @expression
end

#opObject (readonly)

Returns the value of attribute op.



785
786
787
# File 'lib/rjq/ast.rb', line 785

def op
  @op
end

Instance Method Details

#eval(input, context) ⇒ Object



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/rjq/ast.rb', line 792

def eval(input, context)
  @expression.eval(input, context).map do |value|
    case @op
    when '-'
      unless value.is_a?(Numeric)
        raise TypeError,
              "#{Value.type_of(value)} (#{AST.short_dump(value)}) cannot be negated"
      end

      next -0.0 if value.zero?

      value * -1
    when 'not'
      !Value.truthy?(value)
    else
      raise "unknown unary operator #{@op}"
    end
  end
end