-
Notifications
You must be signed in to change notification settings - Fork 10
/
hash.rb
99 lines (82 loc) · 1.96 KB
/
hash.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Keisan
module AST
class Hash < Node
include Enumerable
def initialize(key_value_pairs)
@hash = ::Hash[key_value_pairs.map(&:to_a).map {|k,v| [k.value, v.to_node]}]
end
def freeze
values.each(&:freeze)
super
end
def [](key)
key = key.to_node
return nil unless key.is_a?(AST::ConstantLiteral)
@hash[key.value] || Cell.new(Null.new).tap do |cell|
@hash[key.value] = cell
end
end
def traverse(&block)
value = super(&block)
return value if value
@hash.each do |k, v|
value = k.to_node.traverse(&block)
return value if value
value = v.traverse(&block)
return value if value
end
false
end
def evaluate(context = nil)
return self if frozen?
context ||= Context.new
@hash = ::Hash[
@hash.map do |key, val|
if val.is_a?(Cell)
[key, val]
else
[key, val.evaluate(context)]
end
end
]
self
end
def simplify(context = nil)
evaluate(context)
end
def each(&block)
@hash.each(&block)
end
def keys
@hash.keys
end
def values
@hash.values
end
def value(context = nil)
context ||= Context.new
evaluate(context)
::Hash[
@hash.map {|key, val|
[key, val.value(context)]
}
]
end
def to_s
"{#{@hash.map {|k,v| "#{k.is_a?(::String) ? "'#{k}'" : k}: #{v}"}.join(', ')}}"
end
def to_cell
h = self.class.new([])
h.instance_variable_set(:@hash, ::Hash[
@hash.map do |key, value|
[key, value.to_cell]
end
])
AST::Cell.new(h)
end
def is_constant?
@hash.all? {|k,v| v.is_constant?}
end
end
end
end