Class: Keisan::AST::UnaryLogicalNot

Inherits:
UnaryOperator show all
Defined in:
lib/keisan/ast/unary_logical_not.rb

Constant Summary

Constants inherited from Operator

Operator::ARITIES, Operator::ARITY_PRIORITY_ASSOCIATIVITY, Operator::ASSOCIATIVITIES, Operator::ASSOCIATIVITY_OF_PRIORITY, Operator::PRIORITIES

Instance Attribute Summary

Attributes inherited from Parent

#children

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UnaryOperator

arity, associativity, #child, #initialize, priority, #to_s

Methods inherited from Operator

#arity, arity, #associativity, associativity, associativity_of_priority, #blank_value, #evaluate_assignments, #initialize, priority, #priority, #symbol, #to_s

Methods inherited from Parent

#==, #deep_dup, #freeze, #initialize, #is_constant?, #replace, #traverse, #unbound_functions, #unbound_variables

Methods inherited from Node

#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiate, #differentiated, #equal, #evaluate_assignments, #evaluated, #false?, #is_constant?, #not_equal, #or, #replace, #replaced, #simplified, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #unbound_variables, #well_defined?, #|, #~

Constructor Details

This class inherits a constructor from Keisan::AST::UnaryOperator

Class Method Details

.symbolObject



8
9
10
# File 'lib/keisan/ast/unary_logical_not.rb', line 8

def self.symbol
  :"!"
end

Instance Method Details

#evaluate(context = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/keisan/ast/unary_logical_not.rb', line 12

def evaluate(context = nil)
  context ||= Context.new
  node = child.evaluate(context).to_node
  case node
  when AST::Boolean
    AST::Boolean.new(!node.value)
  else
    if node.is_constant?
      raise Keisan::Exceptions::InvalidFunctionError.new("Cannot take unary logical not of non-boolean constant")
    else
      super
    end
  end
end

#simplify(context = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/keisan/ast/unary_logical_not.rb', line 27

def simplify(context = nil)
  context ||= Context.new
  node = child.simplify(context).to_node
  case node
  when AST::Boolean
    AST::Boolean.new(!node.value)
  else
    super
  end
end

#value(context = nil) ⇒ Object



4
5
6
# File 'lib/keisan/ast/unary_logical_not.rb', line 4

def value(context = nil)
  return !child.value(context)
end