class Keisan::AST::Indexing

Attributes

indexes[R]

Public Class Methods

new(child, indexes = []) click to toggle source
# File lib/keisan/ast/indexing.rb, line 6
def initialize(child, indexes = [])
  @children = [child]
  @indexes = indexes
end

Public Instance Methods

evaluate(context = nil) click to toggle source
# File lib/keisan/ast/indexing.rb, line 23
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) click to toggle source
# File lib/keisan/ast/indexing.rb, line 19
def evaluate_assignments(context = nil)
  self
end
replace(variable, replacement) click to toggle source
Calls superclass method
# File lib/keisan/ast/indexing.rb, line 35
def replace(variable, replacement)
  super
  @indexes = indexes.map {|index| index.replace(variable, replacement)}
end
simplify(context = nil) click to toggle source
# File lib/keisan/ast/indexing.rb, line 31
def simplify(context = nil)
  evaluate(context)
end
to_s() click to toggle source
# File lib/keisan/ast/indexing.rb, line 15
def to_s
  "(#{child.to_s})[#{indexes.map(&:to_s).join(',')}]"
end
value(context = nil) click to toggle source
# File lib/keisan/ast/indexing.rb, line 11
def value(context = nil)
  return child.value(context).send(:[], *indexes.map {|index| index.value(context)})
end

Private Instance Methods

evaluate_hash(context) click to toggle source
# File lib/keisan/ast/indexing.rb, line 49
def evaluate_hash(context)
  if hash = extract_hash
    element = hash[@indexes.first.value(context)]
    element.nil? ? AST::Null.new : element
  end
end
evaluate_list(context) click to toggle source
# File lib/keisan/ast/indexing.rb, line 42
def evaluate_list(context)
  if list = extract_list
    element = list.children[@indexes.first.value(context)]
    element.nil? ? AST::Null.new : element
  end
end
extract_hash() click to toggle source
# File lib/keisan/ast/indexing.rb, line 66
def extract_hash
  if child.is_a?(AST::Hash)
    child
  elsif child.is_a?(Cell) && child.node.is_a?(AST::Hash)
    child.node
  else
    nil
  end
end
extract_list() click to toggle source
# File lib/keisan/ast/indexing.rb, line 56
def extract_list
  if child.is_a?(List)
    child
  elsif child.is_a?(Cell) && child.node.is_a?(List)
    child.node
  else
    nil
  end
end