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, #contains_a?, #differentiate, #differentiated, #equal, #evaluate_assignments, #evaluated, #false?, #not_equal, #or, #replaced, #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(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



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

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



53
54
55
56
57
58
59
60
# File 'lib/keisan/ast/parent.rb', line 53

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

#evaluate(context = nil) ⇒ Object



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

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

#freezeObject



38
39
40
41
# File 'lib/keisan/ast/parent.rb', line 38

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

#is_constant?Boolean

Returns:



79
80
81
# File 'lib/keisan/ast/parent.rb', line 79

def is_constant?
  @children.all?(&:is_constant?)
end

#replace(variable, replacement) ⇒ Object



74
75
76
77
# File 'lib/keisan/ast/parent.rb', line 74

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

#simplify(context = nil) ⇒ Object



68
69
70
71
72
# File 'lib/keisan/ast/parent.rb', line 68

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

#traverse(&block) ⇒ Object



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

def traverse(&block)
  value = super(&block)
  return value if value
  children.each do |child|
    value = child.traverse(&block)
    return value if value
  end
  false
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