-
Notifications
You must be signed in to change notification settings - Fork 2
/
env.lua
344 lines (298 loc) · 6.16 KB
/
env.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
-- Lemma Environment
---
require 'class/Fexpr'
require 'class/Macro'
require 'class/List'
require 'class/Vector'
require 'class/HashMap'
require 'class/Symbol'
require 'class/Error'
require 'class/Iter'
require 'interface/Seq'
require 'symtab'
require 'type'
---
-- NOTE: `lemma` table is defined in `exec.lua`
---
---
-- Some Lua functions that could really use customizing.
---
towrite, print = (function()
return function(s)
if lemma.type(s) == 'string' then
return string.format('%q', s)
elseif s == false then
return "false"
elseif s == nil then
return "nil"
end
return tostring(s)
end,
function(...)
local n = select('#', ...)
local args = {...}
for i = 1, n do
args[i] = tostring(args[i])
end
io.write(table.concat(args, '\t'))
io.write'\n'
end
end)()
function lemma.rawstring(t)
local old = getmetatable(t)
setmetatable(t, nil)
local s = tostring(t)
setmetatable(t, old)
return s
end
---
-- Write an external representation of t to stdout
---
function write(...)
local n = select('#', ...)
local args = {...}
if n > 0 then
for i = 1, n do
io.write(towrite(args[i]))
io.write(' ')
end
else
io.write('nil')
end
io.write('\n')
end
lemma.splice = Seq.lib.unpack
lemma.unsplice = Seq.lib.pack
lemma.append = Seq.lib.append
lemma.flatten = Seq.lib.flatten
---
-- "utility functions"
---
;(function(t)
for k, v in pairs(t) do
lemma[k] = function(...)
local args = {...}
local diff = args[1] or 0
for i = 2, #args do
diff = v(diff, args[i])
end
return diff
end
end
end){
['+'] = function(a, b) return a + b end,
['-'] = function(a, b) return a - b end,
['*'] = function(a, b) return a * b end,
['/'] = function(a, b) return a / b end,
['mod'] = function(a, b) return a % b end
}
;(function(t)
for k, v in pairs(t) do
lemma[k] = function(a, b)
return v(a, b)
end
end
end){
['='] = function(a, b) return a == b end,
['not='] = function(a, b) return a ~= b end,
['>'] = function(a, b) return a > b end,
['<'] = function(a, b) return a < b end,
['>='] = function(a, b) return a >= b end,
['<='] = function(a, b) return a <= b end
}
lemma['id?'] = function(a, b)
return rawequal(a, b)
end
lemma['not'] = function(a)
return not a
end
function lemma.str(...)
local n = select('#', ...)
local t = {...}
for i = 1, n do
local v = t[i]
if lemma.type(v) ~= 'string' then
t[i] = tostring(v)
end
end
return table.concat(t)
end
lemma.vector = Vector
lemma['hash-map'] = HashMap
function lemma.vec(seq)
return Vector(Seq.lib.unpack(seq))
end
function lemma.keys(t)
local list = List()
for k in pairs(t) do
list = list:cons(k)
end
return list:reverse()
end
function lemma.values(t)
local list = List()
for _, v in pairs(t) do
list = list:cons(v)
end
return list:reverse()
end
function lemma.get(t, k)
if lemma.type(k) == 'Symbol' then
k = k:string() -- TODO: is the really a good idea?
end
if not k then
return error'get: attempt to index table with nil'
end
if not t then
return error('get: attempt to index nil ['..k..']')
end
return t[k]
end
lemma['table-set!'] = function(t, k, v)
if not k then
return error'table-set!: attempt to index table with nil'
end
if not t then
return error('table-set!: attempt to index nil ['..k..']')
end
t[k] = v
return v
end
---
-- Create a raw Lua table with array part a (optional) and hash part h.
-- If no arguments are specified, returns an empty table.
---
function lemma.table(a, h)
if not h then
h = a
a = nil
end
local t
if a then
t = {Seq.lib.unpack(a)}
else
t = {}
end
if h then
for k, v in h do
t[k] = v
end
end
return t
end
function lemma.method(k)
if lemma.type(k) ~= 'string' then
return error('method: expected string, got '..tostring(k))
end
--k = k:string()
return function(t, ...)
if t == nil then
return error('method: attempt to index nil ['..k..']')
end
if t[k] == nil then
return error('method: method is nil ['..k..']')
end
return t[k](t, ...)
end
end
---
-- Set up the namespaces. All of the usual global stuff in Lua is moved
-- into a lua namespace, and the lemma namespace becomes the default.
---
lemma.lua = _G -- give stock lua stuff its own namespace
---
-- Copy some stuff from lua
---
lemma.eval = eval
lemma.read = read
lemma.write = write
lemma.print = print
--lemma.type = type -- defined in `type.lua`
lemma.tostring = tostring
lemma.foldl = Seq.lib.foldl
lemma.foldr = Seq.lib.foldr
lemma.filter = Seq.lib.filter
lemma.map = Seq.lib.map
lemma['for-each'] = Seq.lib.foreach
lemma.iter = Iter
function lemma.undefined()
return nil
end
function lemma.take(n, seq)
if lemma.type(n) ~= 'number' then
return error('take: number expected, got ', tostring(n))
end
if not implements(seq, 'Seq') then
return error('take: seq expected, got ', tostring(seq))
end
lst = {}
for i = 1, n do
if seq['empty?'](seq) then
n = i - 1
break
end
lst[i] = seq:first()
seq = seq:rest()
end
return List(table.unpack(lst, 1, n))
end
function lemma.drop(n, seq)
if lemma.type(n) ~= 'number' then
return error('take: number expected, got ', tostring(n))
end
if not implements(seq, 'Seq') then
return error('take: seq expected, got ', tostring(seq))
end
for i = 1, n do
if seq['empty?'](seq) then
break
end
seq = seq:rest()
end
return seq
end
function string.split(s, p)
f, s, k = s:gmatch(p)
return f(s, k)
end
lemma['assoc-meta'] = function(t, k, v)
if not lemma['*metadata*'][t] then
lemma['*metadata*'][t] = { [k] = v }
else
lemma['*metadata*'][t][k] = v
end
end
lemma['get-meta'] = function(t, k)
if lemma['*metadata*'][t] then
return lemma['*metadata*'][t][k]
end
end
function lemma.length(t)
if implements(t, 'Seq') then
return t:length()
elseif lemma.type(t) == 'table' then
return #t
elseif lemma.type(t) == 'string' then
return string.len(t)
else
return error("Don't know how to get length of "..lemma.type(t))
end
end
function lemma.range(a, b, by)
-- by defaults to 1
by = by or 1
-- make a optional
if not b then
a, b = by, a
end
a = a - by
local function until_inc(s, v)
local n = v + by
if n <= s then
return n
end
return nil
end
return Iter(until_inc, b, a)
end