Class: Keisan::AST::Parent

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

Direct Known Subclasses

Function, List, MultiLine, Operator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<=, #>, #>=, #^, #and, #coerce, #differentiate, #equal, #evaluate_assignments, #evaluated, #false?, #not_equal, #or, #simplified, #to_cell, #to_node, #true?, #value, #well_defined?, #|, #~

Constructor Details

#initialize(children = []) ⇒ Parent

Returns a new instance of Parent.



6
7
8
9
10
11
12
# File 'lib/keisan/ast/parent.rb', line 6

def initialize(children = [])
  children = Array.wrap(children).map do |child|
    child.is_a?(Cell) ? child : child.to_node
  end
  raise Exceptions::InternalError.new unless children.is_a?(Array)
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/keisan/ast/parent.rb', line 33

def ==(other)
  return false unless self.class == other.class

  children.size == other.children.size && children.map.with_index {|_,i|
    children[i] == other.children[i]
  }.all? {|bool|
    bool == true
  }
end

#deep_dupObject



43
44
45
46
47
48
49
50
# File 'lib/keisan/ast/parent.rb', line 43

def deep_dup
  dupped = dup
  dupped.instance_variable_set(
    :@children,
    dupped.children.map(&:deep_dup)
  )
  dupped
end

#evaluate(context = nil) ⇒ Object



52
53
54
55
56
# File 'lib/keisan/ast/parent.rb', line 52

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

#freezeObject



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

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

#replace(variable, replacement) ⇒ Object



64
65
66
67
# File 'lib/keisan/ast/parent.rb', line 64

def replace(variable, replacement)
  @children = children.map {|child| child.replace(variable, replacement)}
  self
end

#simplify(context = nil) ⇒ Object



58
59
60
61
62
# File 'lib/keisan/ast/parent.rb', line 58

def simplify(context = nil)
  context ||= Context.new
  @children = @children.map {|child| child.simplify(context)}
  self
end

#unbound_functions(context = nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/keisan/ast/parent.rb', line 21

def unbound_functions(context = nil)
  context ||= Context.new
  children.inject(Set.new) do |fns, child|
    fns | child.unbound_functions(context)
  end
end

#unbound_variables(context = nil) ⇒ Object



14
15
16
17
18
19
# File 'lib/keisan/ast/parent.rb', line 14

def unbound_variables(context = nil)
  context ||= Context.new
  children.inject(Set.new) do |vars, child|
    vars | child.unbound_variables(context)
  end
end