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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#!/usr/bin/env nomsu -V3.6.5.6
#
This file contains code that supports manipulating and using collections like lists
and dictionaries.
use "core/metaprogramming.nom"
use "core/control_flow.nom"
use "core/operators.nom"
# List/dict functions:
# Indexing
test:
assume ((2 nd to last in [1, 2, 3, 4, 5]) is 4)
compile [..]
%index st to last in %list, %index nd to last in %list
%index rd to last in %list, %index th to last in %list
..to (Lua value "utils.nth_to_last(\(%list as lua expr), \(%index as lua expr))")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parse [last in %list] as (1 st to last in %list)
parse [first in %list] as %list.1
# Membership testing
test:
assume (3 is in [1, 2, 3, 4, 5])
action [%item is in %list, %list contains %item, %list has %item]:
for %key = %value in %list:
if (%key is %item): return (yes)
return (no)
test:
assume (99 isn't in [1, 2, 3])
action [..]
%item isn't in %list, %item is not in %list, %list doesn't contain %item
%list does not contain %item, %list doesn't have %item, %list does not have %item
..:
for %key = %value in %list:
if (%key is %item): return (no)
return (yes)
test:
assume ({x:no} has key "x")
parse [%list has key %index, %list has index %index] as (%list.%index != (nil))
test:
assume ({x:no} doesn't have key "y")
assume (not ({x:no} doesn't have key "x"))
parse [..]
%list doesn't have key %index, %list does not have key %index
%list doesn't have index %index, %list does not have index %index
..as (%list.%index == (nil))
compile [number of keys in %list] to (..)
Lua value "utils.size(\(%list as lua expr))"
test:
%list = [1, 2, 3, 4, 5]
append 6 to %list
assume ((last in %list) is 6)
pop from %list
assume ((last in %list) is 5)
remove index 1 from %list
assume ((first in %list) is 2)
compile [..]
append %item to %list, add %item to %list, to %list add %item, to %list append %item
..to (Lua "table.insert(\(%list as lua expr), \(%item as lua expr))")
compile [add %item to %list at index %i] to (..)
Lua ".."
table.insert(\(%list as lua expr), \(%i as lua expr), \(%item as lua expr))
compile [pop from %list, remove last from %list] to (..)
Lua value "table.remove(\(%list as lua expr))"
compile [remove index %index from %list] to (..)
Lua value "table.remove(\(%list as lua expr), \(%index as lua expr))"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# List Comprehension
test:
assume (((% * %) for % in [1, 2, 3]) == [1, 4, 9])
parse [%expression for %item in %iterable] as (..)
result of:
%comprehension = []
for %item in %iterable:
add %expression to %comprehension
return %comprehension
parse [..]
%expression for %index in %start to %stop via %step
%expression for %index in %start to %stop by %step
..as (..)
result of:
%comprehension = []
for %index in %start to %stop via %step:
add %expression to %comprehension
return %comprehension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test:
assume (((% * %) for % in 1 to 3) == [1, 4, 9])
parse [%expression for %var in %start to %stop] as (..)
%expression for %var in %start to %stop via 1
test:
assume (("\%k,\%v" for %k = %v in {x:1}) == ["x,1"])
parse [..]
%expression for %key = %value in %iterable
%expression for %key %value in %iterable
..as (..)
result of:
%comprehension = []
for %key = %value in %iterable:
add %expression to %comprehension
return %comprehension
# Dict comprehensions
test:
assume (((% * %) = % for % in [1, 2, 3]) == {1:1, 4:2, 9:3})
parse [%key = %value for %item in %iterable, %key %value for %item in %iterable] as
..(..)
result of:
%comprehension = {}
for %item in %iterable:
%comprehension.%key = %value
return %comprehension
test:
assume ((%k = (%v * %v) for %k = %v in {x:1, y:2, z:3}) == {x:1, y:4, z:9})
parse [..]
%key = %value for %src_key = %src_value in %iterable
%key %value for %src_key %src_value in %iterable
..as (..)
result of:
%comprehension = {}
for %src_key = %src_value in %iterable:
%comprehension.%key = %value
return %comprehension
parse [..]
%key = %value for %item in %start to %stop via %step
%key %value for %item in %start to %stop via %step
..as (..)
result of:
%comprehension = {}
for %item in %start to %stop via %step:
%comprehension.%key = %value
return %comprehension
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test:
assume (((% * %) = % for % in 1 to 3) == {1:1, 4:2, 9:3})
parse [..]
%key = %value for %item in %start to %stop
%key %value for %item in %start to %stop
..as (%key = %value for %item in %start to %stop via 1)
test:
assume (([[1, 2], [3, 4]] flattened) == [1, 2, 3, 4])
action [%lists flattened]:
%flat = []
for %list in %lists:
for %item in %list: add %item to %flat
return %flat
test:
assume ((entries in {x:1}) == [{key:"x", value:1}])
parse [entries in %dict] as ({key:%k, value:%v} for %k = %v in %dict)
test:
assume ((keys in {x:1}) == ["x"])
parse [keys in %dict, keys of %dict] as (%k for %k = %v in %dict)
test:
assume ((values in {x:1}) == [1])
parse [values in %dict, values of %dict] as (%v for %k = %v in %dict)
# Metatable stuff
test:
%t = {}
set %t 's metatable to {__tostring:[%] -> "XXX"}
assume ("\%t" == "XXX")
compile [set %dict 's metatable to %metatable] to (..)
Lua "setmetatable(\(%dict as lua expr), \(%metatable as lua expr));"
test:
assume (({} with fallback % -> (% + 1)).10 == 11)
compile [%dict with fallback %key -> %value] to (..)
Lua value ".."
(function(d)
local mt = {}
for k,v in pairs(getmetatable(d) or {}) do mt[k] = v end
mt.__index = function(self, \(%key as lua expr))
local value = \(%value as lua expr)
self[\(%key as lua expr)] = value
return value
end
return setmetatable(d, mt)
end)(\(%dict as lua expr))
# Sorting
test:
%x = [3, 1, 2]
sort %x
assume (%x == [1, 2, 3])
sort %x by % = (- %)
assume (%x == [3, 2, 1])
%keys = {1:999, 2:0, 3:50}
sort %x by % = %keys.%
assume (%x == [2, 3, 1])
compile [sort %items] to (Lua "table.sort(\(%items as lua expr));")
parse [sort %items by %item = %key_expr, sort %items by %item -> %key_expr] as (..)
do:
%keys = ({} with fallback %item -> %key_expr)
lua> "table.sort(\%items, function(x,y) return \%keys[x] < \%keys[y] end)"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test:
assume ((sorted [3, 1, 2]) == [1, 2, 3])
action [%items sorted, sorted %items]:
%copy = (% for % in %items)
sort %copy
return %copy
parse [%items sorted by %item = %key, %items sorted by %item -> %key] as (..)
result of:
%copy = (% for % in %items)
sort %copy by %item = %key
return %copy
test:
assume ((unique [1, 2, 1, 3, 2, 3]) == [1, 2, 3])
action [unique %items]:
%unique = []
%seen = {}
for % in %items:
unless %seen.%:
add % to %unique
%seen.% = (yes)
return %unique
|