-
Notifications
You must be signed in to change notification settings - Fork 8
/
match.lua
executable file
·191 lines (172 loc) · 3.96 KB
/
match.lua
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env lua5.1
-- IsArray tries to guess if its input is an array-like table, which
-- is a table with hopefully consecutive integer keys starting with 1.
-- Not sure what to do about a zero-length table. I guess that's just
-- a table.
function isArray(m)
return type(m) == "table" and nil ~= m[1]
end
-- Render generates something like a JSON representation of its input.
function render(x)
if x == nil then
return "nil"
elseif type(x) == "table" then
if isArray(x) then
local base = "["
for i, v in pairs(x) do
if 1 < i then
base = base .. ","
end
base = base .. render(v)
end
base = base .. "]"
return base
end
local base = "{"
local first = true
for p, v in pairs(x) do
if not first then
base = base .. ","
end
first = false
base = base .. "\"" .. p .. "\":" .. render(v)
end
base = base .. "}"
return base
else
return "\"" .. x .. "\""
end
end
match = (function()
local isVar = function(s)
if type(s) == "string" then
return string.sub(s, 1, 1) == "?"
end
return false
end
local copyTable = function(m)
local acc = {}
for b, v in pairs(m) do
acc[b] = v
end
return acc
end
local extend = function(bs,b,v)
local acc = copyTable(bs)
acc[b] = v
return acc
end
local slice = function(xs, start)
local acc = {}
for i, x in pairs(xs) do
if start <= i then
table.insert(acc, x)
end
end
return acc
end
local match
local arraycatMatch = function(ctx,bss,p,m)
print("trace", "arraycatMatch", render(bss), render(p), render(m))
if len(p) == 0 then
return bss
end
local px = p[1]
local acc = {}
for i, bs in pairs(bss) do
for j, mx in pairs(m) do
local bss_ = match(ctx,px,mx,bs)
if 0 < len(bss_) then -- no 'continue'
local m_ = copyTable(m)
table.remove(m_, j)
bss_ = arraycatMatch(ctx, bss_, slice(m_, 2), m_)
for k, my in pairs(bss_) do
table.insert(acc, my)
end
end
end
end
return acc
end
local matchWithBindings = function(ctx,bss,v,mv)
print("trace", "matchWithBindings", render(bss), render(v),render(mv))
local acc = {}
for i, bs in pairs(bss) do
local bss_ = match(ctx,v,mv,bs)
if bss_ ~= nil then
for j, bs_ in pairs(bss_) do
table.insert(acc, bs_)
end
end
end
return acc
end
local len = function(m) -- What?
if m == nil then
return 0
end
local acc = 0
for p, v in pairs(m) do
acc = acc + 1
end
return acc
end
local mapcatMatch = function(ctx,bss,p,m)
print("trace", "mapcatMatch", render(bss), render(p), render(m))
for k, v in pairs(p) do
local mv = m[k]
if mv == nil then
return {}
end
local acc = matchWithBindings(ctx,bss,v,mv)
if len(acc) == 0 then
return {}
end
bss = acc
end
return bss
end
match = function(ctx,p,m,bs)
print("trace", "match", render(p), render(m), render(bs))
if bs == nil then
bs = {}
end
if isVar(p) then
local binding = bs[p]
if binding ~= nil then
return match(ctx, binding, m, bs)
else
return {extend(bs,p,m)}
end
else
if type(p) == "table" then
if isArray(p) then
if isArray(m) then
return arraycatMatch(ctx, {bs}, p, m)
end
return {}
end
if type(m) == "table" then
if p == nil then
return {}
end
if len(p) == 0 then
return {bs}
end
return mapcatMatch(ctx, {bs}, p, m)
else
return {}
end
else
if p == m then
return {bs}
end
return {}
end
end
end
return match
end)()
pattern = {["a"] = {["b"] = "?b", ["d"] = "?d"}}
message = {["a"] = {["b"] = 1, ["c"] = 2, ["d"] = 3}}
print("matched", render(match(nil, pattern, message, nil)))