-
Notifications
You must be signed in to change notification settings - Fork 10
/
parent.rb
84 lines (72 loc) · 1.99 KB
/
parent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Keisan
module AST
class Parent < Node
attr_reader :children
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
def unbound_variables(context = nil)
context ||= Context.new
children.inject(Set.new) do |vars, child|
vars | child.unbound_variables(context)
end
end
def unbound_functions(context = nil)
context ||= Context.new
children.inject(Set.new) do |fns, child|
fns | child.unbound_functions(context)
end
end
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
def freeze
children.each(&:freeze)
super
end
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
def deep_dup
dupped = dup
dupped.instance_variable_set(
:@children,
dupped.children.map(&:deep_dup)
)
dupped
end
def evaluate(context = nil)
context ||= Context.new
@children = children.map {|child| child.evaluate(context)}
self
end
def simplify(context = nil)
context ||= Context.new
@children = @children.map {|child| child.simplify(context)}
self
end
def replace(variable, replacement)
@children = children.map {|child| child.replace(variable, replacement)}
self
end
def is_constant?
@children.all?(&:is_constant?)
end
end
end
end