Class: Keisan::AST::Indexing

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

Constant Summary

Constants inherited from Operator

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

Instance Attribute Summary collapse

Attributes inherited from Parent

#children

Instance Method Summary collapse

Methods inherited from UnaryOperator

arity, associativity, #child, priority

Methods inherited from Operator

arity, #arity, associativity, #associativity, associativity_of_priority, #blank_value, #priority, priority, #symbol, symbol

Methods inherited from Parent

#==, #is_constant?, #traverse, #unbound_functions, #unbound_variables

Methods inherited from Node

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

Constructor Details

#initialize(child, indexes = []) ⇒ Indexing

Returns a new instance of Indexing.



6
7
8
9
# File 'lib/keisan/ast/indexing.rb', line 6

def initialize(child, indexes = [])
  @children = [child]
  @indexes = indexes
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



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

def indexes
  @indexes
end

Instance Method Details

#deep_dupObject



11
12
13
14
15
16
17
# File 'lib/keisan/ast/indexing.rb', line 11

def deep_dup
  dupped = super
  dupped.instance_variable_set(
    :@indexes, indexes.map(&:deep_dup)
  )
  dupped
end

#evaluate(context = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/keisan/ast/indexing.rb', line 36

def evaluate(context = nil)
  context ||= Context.new
  @children = children.map {|child| child.evaluate(context)}
  @indexes = indexes.map {|index| index.evaluate(context)}

  evaluate_list(context) || evaluate_hash(context) || self
end

#evaluate_assignments(context = nil) ⇒ Object



32
33
34
# File 'lib/keisan/ast/indexing.rb', line 32

def evaluate_assignments(context = nil)
  self
end

#freezeObject



19
20
21
22
# File 'lib/keisan/ast/indexing.rb', line 19

def freeze
  indexes.each(&:freeze)
  super
end

#replace(variable, replacement) ⇒ Object



48
49
50
51
# File 'lib/keisan/ast/indexing.rb', line 48

def replace(variable, replacement)
  super
  @indexes = indexes.map {|index| index.replace(variable, replacement)}
end

#simplify(context = nil) ⇒ Object



44
45
46
# File 'lib/keisan/ast/indexing.rb', line 44

def simplify(context = nil)
  evaluate(context)
end

#to_sObject



28
29
30
# File 'lib/keisan/ast/indexing.rb', line 28

def to_s
  "(#{child.to_s})[#{indexes.map(&:to_s).join(',')}]"
end

#value(context = nil) ⇒ Object



24
25
26
# File 'lib/keisan/ast/indexing.rb', line 24

def value(context = nil)
  return child.value(context).send(:[], *indexes.map {|index| index.value(context)})
end