aboutsummaryrefslogtreecommitdiff
path: root/core.nom
blob: 0b0193b62fc1acca3fca81245bdfb17e7634009a (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# Rule for making rules
lua block ".."
    |compiler:defmacro("rule %spec = %body", function(compiler, vars, kind)
    |    if kind == "Expression" then
    |        compiler:error("Cannot use rule definitions as an expression.")
    |    end
    |    local spec = compiler:get_invocations_from_definition(vars.spec.value, vars)
    |    return ("compiler:def("..compiler.utils.repr(spec,true)..", "..compiler:tree_to_lua(vars.body, vars)..")"), true
    |end)

rule: help %invocation ..=:
    lua block ".."
        |local fn_info = compiler.defs[vars.invocation]
        |if not fn_info then
        |    compiler:writeln("Function not found: "..compiler.utils.repr(vars.invocation, true))
        |else
        |    compiler:writeln("rule "..compiler.utils.repr(fn_info.invocations).." ="..(fn_info.src or ":\\n    <unknown source code>"))
        |end

# Macros
lua block ".."
    |local add_macro = function(compiler, vars, kind)
    |    local spec = compiler:get_invocations_from_definition(vars.spec.value, vars)
    |    local fn = compiler:tree_to_value(vars.body, vars)
    |    compiler:defmacro(spec, fn, vars.body.src)
    |    return "", true
    |end
    |compiler:defmacro("macro %spec = %body", add_macro)
    |
    |local add_macro_block = function(compiler, vars, kind)
    |    local spec = compiler:get_invocations_from_definition(vars.spec.value, vars)
    |    local fn = compiler:tree_to_value(vars.body, vars)
    |    local wrapper = function(compiler, vars, kind)
    |        if kind == "Expression" then
    |            compiler:error("Macro: "..spec.." was defined to be a block, but is being used as an expression.")
    |        end
    |        return ("do\\n"..fn(compiler, vars, kind).."\\nend"), true
    |    end
    |    compiler:defmacro(spec, wrapper, vars.body.src)
    |    return "", true
    |end
    |compiler:defmacro("macro block %spec = %body", add_macro_block)

# Compiler tools
rule:
    eval %code
    run %code
..=:
    lua expr "compiler:run(vars.code)"

macro: source code %code ..=:
    lua block ".."
        |if vars.code.value.type ~= "Thunk" then
        |    compiler:error("'source code %' only takes code blocks, not "..vars.code.value.type)
        |end
    lua expr "compiler.utils.repr(vars.code.value.value.src, true)"

rule: run file %filename ..=:
    lua block ".."
        |local file = io.open(vars.filename)
        |return compiler:run(file:read("*a"))

# Error functions
rule: error! ..=:
    lua block ".."
        |table.remove(compiler.callstack)
        |compiler:error()

rule: error %msg ..=:
    lua block ".."
        |table.remove(compiler.callstack)
        |compiler:error(vars.msg)

macro: as lua %block ..=:
    lua expr "compiler.utils.repr(compiler:tree_to_lua(vars.block.value.value, vars), true)"

macro block: show generated lua %block ..=:
    ".."|compiler:writeln(\lua expr "compiler.utils.repr(compiler:tree_to_lua(vars.block.value.value, vars), true)"\)

# Macro helper functions
rule: %tree as value ..=:
    lua expr ".."
        |compiler:tree_to_value(vars.tree, vars)

rule: %tree as lua block ..=:
    lua block ".."
        |return compiler:tree_to_lua(vars.tree, 'Statement'), true

rule: %tree as lua expr ..=:
    lua expr ".."
        |compiler:tree_to_lua(vars.tree, 'Expression')

# Moonscript!
macro block: moonscript block %moonscript_code ..=:
    lua block ".."
        |local parse, compile = require('moonscript.parse'), require('moonscript.compile')
        |local moon_code = compiler:tree_to_value(vars.moonscript_code, vars)
        |local tree, err = parse.string(moon_code)
        |if not tree then
        |    compiler:error("Failed to parse moonscript: "..err)
        |end
        |local lua_code, err, pos = compile.tree(tree)
        |if not lua_code then
        |    compiler:error(compile.format_error(err, pos, moon_code))
        |end
        |return "do\\n"..lua_code.."\\nend"

macro: moonscript %moonscript_code ..=:
    lua block ".."
        |local parse, compile = require('moonscript.parse'), require('moonscript.compile')
        |local moon_code = compiler:tree_to_value(vars.moonscript_code, vars)
        |local tree, err = parse.string(moon_code)
        |if not tree then
        |    compiler:error("Failed to parse moonscript: "..err)
        |end
        |local lua_code, err, pos = compile.tree(tree)
        |if not lua_code then
        |    compiler:error(compile.format_error(err, pos, moon_code))
        |end
        |return "(function(compiler, vars)\\n"..lua_code.."\\nend)(compiler, vars)"

# String functions
rule: join %strs ..=:
    lua block ".."
        |local str_bits = {}
        |for i,bit in ipairs(vars.strs) do str_bits[i] = compiler.utils.repr(bit) end
        |return table.concat(str_bits)

rule: join %strs with glue %glue ..=:
    lua block ".."
        |local str_bits = {}
        |for i,bit in ipairs(vars.strs) do str_bits[i] = compiler.utils.repr(bit) end
        |return table.concat(str_bits, vars.glue)

rule:
    capitalize %str
    %str capitalized
..=:
    lua expr ".."|vars.str:gsub("%l", string.upper, 1)

# Variable assignment
#..
    macro block: %var = %value ..=:
        lua block ".."
            |if vars.var.value.type ~= "Var" then
            |    compiler:error("Assignment operation has the wrong type for the left hand side. "
            |        .."Expected Var, but got: "..vars.var.value.type)
            |end
        ".."|\%var as lua expr\ = \%value as lua expr\

lua block ".."
    |local function helper(callback)
    |    return function(compiler, vars, kind)
    |        if kind == "Expression" then
    |            compiler:error("Cannot use an assignment operation as an expression value.")
    |        end
    |        if vars.var.value.type ~= "Var" then
    |            compiler:error("Assignment operation has the wrong type for the left hand side. "
    |                .."Expected Var, but got: "..vars.var.value.type)
    |        end
    |        if vars.value.value.type ~= "Thunk" then
    |            compiler:error("Assignment operation has the wrong type for the right hand side. "
    |                .."Expected Thunk, but got: "..vars.value.value.type.."\\nMaybe you used '=' instead of '=:'?")
    |        end
    |        local ret = "do\\n    local ret"
    |        ret = ret .. "\\n    "..compiler:tree_to_lua(vars.value.value.value, "Statement")
    |        ret = ret .. "\\n    "..callback(compiler:tree_to_lua(vars.var, "Expression"))
    |        return (ret.."\\nend"), true
    |    end
    |end
    |compiler:defmacro("%var = %value", helper(function(var) return var.." = ret" end))
    |compiler:defmacro("%var += %value", helper(function(var) return var.." = "..var.." + ret" end))
    |compiler:defmacro("%var -= %value", helper(function(var) return var.." = "..var.." - ret" end))
    |compiler:defmacro("%var *= %value", helper(function(var) return var.." = "..var.." * ret" end))
    |compiler:defmacro("%var /= %value", helper(function(var) return var.." = "..var.." / ret" end))
    |compiler:defmacro("%var ^= %value", helper(function(var) return var.." = "..var.." ^ ret" end))
    |compiler:defmacro("%var and= %value", helper(function(var) return var.." = "..var.." and ret" end))
    |compiler:defmacro("%var or= %value", helper(function(var) return var.." = "..var.." or ret" end))
    |compiler:defmacro("%var concat= %value", helper(function(var) return var.." = "..var.." .. ret" end))
    |compiler:defmacro("%var mod= %value", helper(function(var) return var.." = "..var.." % ret" end))

# Operators
macro:
    true
    yes
..=: "true"
macro:
    false
    no
..=: "false"
macro:
    nil
    null
..=: "nil"
macro block:
    nop
    pass
..=: ""
macro: %a + %b ..=: ".."|(\%a as lua expr\ + \%b as lua expr\)
macro: %a + %b + %c ..=: ".."|(\%a as lua expr\ + \%b as lua expr\ + \%c as lua expr\)
macro: %a + %b + %c + %d ..=: ".."|(\%a as lua expr\ + \%b as lua expr\ + \%c as lua expr\ + \%d as lua expr\)
macro: %a - %b ..=: ".."|(\%a as lua expr\ - \%b as lua expr\)
macro: %a * %b ..=: ".."|(\%a as lua expr\ * \%b as lua expr\)
macro: %a * %b * %c ..=: ".."|(\%a as lua expr\ * \%b as lua expr\ * \%c as lua expr\)
macro: %a * %b * %c * %d ..=: ".."|(\%a as lua expr\ * \%b as lua expr\ * \%c as lua expr\ * \%d as lua expr\)
macro: %a / %b ..=: ".."|(\%a as lua expr\ / \%b as lua expr\)
macro: %a === %b ..=: ".."|(\%a as lua expr\ == \%b as lua expr\)
macro: %a !== %b ..=: ".."|(\%a as lua expr\ ~= \%b as lua expr\)
macro: %a < %b ..=: ".."|(\%a as lua expr\ < \%b as lua expr\)
macro: %a < %b < %c ..=: ".."|((\%a as lua expr\ < \%b as lua expr\) and (\%b as lua expr\ < \%c as lua expr\))
macro: %a <= %b < %c ..=: ".."|((\%a as lua expr\ <= \%b as lua expr\) and (\%b as lua expr\ < \%c as lua expr\))
macro: %a <= %b <= %c ..=: ".."|((\%a as lua expr\ <= \%b as lua expr\) and (\%b as lua expr\ <= \%c as lua expr\))
macro: %a < %b <= %c ..=: ".."|((\%a as lua expr\ < \%b as lua expr\) and (\%b as lua expr\ <= \%c as lua expr\))
macro: %a > %b > %c ..=: ".."|((\%a as lua expr\ > \%b as lua expr\) and (\%b as lua expr\ > \%c as lua expr\))
macro: %a >= %b > %c ..=: ".."|((\%a as lua expr\ >= \%b as lua expr\) and (\%b as lua expr\ > \%c as lua expr\))
macro: %a >= %b >= %c ..=: ".."|((\%a as lua expr\ >= \%b as lua expr\) and (\%b as lua expr\ >= \%c as lua expr\))
macro: %a > %b >= %c ..=: ".."|((\%a as lua expr\ > \%b as lua expr\) and (\%b as lua expr\ >= \%c as lua expr\))
macro: %a <= %b ..=: ".."|(\%a as lua expr\ <= \%b as lua expr\)
macro: %a > %b ..=: ".."|(\%a as lua expr\ > \%b as lua expr\)
macro: %a >= %b ..=: ".."|(\%a as lua expr\ >= \%b as lua expr\)
macro: %a ^ %b ..=: ".."|(\%a as lua expr\ ^ \%b as lua expr\)
macro: %a and %b ..=: ".."|(\%a as lua expr\ and \%b as lua expr\)
macro: %a and %b and %c ..=:
    ".."|(\%a as lua expr\ and \%b as lua expr\ and \%c as lua expr\)
macro: %a and %b and %c and %d ..=:
    ".."|(\%a as lua expr\ and \%b as lua expr\ and \%c as lua expr\ and \%d as lua expr\)
macro: %a or %b ..=: ".."|(\%a as lua expr\ or \%b as lua expr\)
macro: %a or %b or %c ..=:
    ".."|(\%a as lua expr\ or \%b as lua expr\ or \%c as lua expr\)
macro: %a or %b or %c or %d ..=:
    ".."|(\%a as lua expr\ or \%b as lua expr\ or \%c as lua expr\ or \%d as lua expr\)
macro: %a mod %b ..=: ".."|(\%a as lua expr\ mod \%b as lua expr\)
macro: - %a ..=: ".."|-(\%a as lua expr\)
macro: not %a ..=: ".."|not (\%a as lua expr\)

rule: %a == %b ..=:
    lua expr "((vars.a == vars.b) or compiler.utils.equivalent(vars.a, vars.b))"
rule: %a != %b ..=:
    lua expr "((vars.a ~= vars.b) or not compiler.utils.equivalent(vars.a, vars.b))"

macro: repr %obj ..=:
    ".."|compiler.utils.repr(\%obj as lua expr\, true)

macro: say %str ..=:
    ".."|compiler:writeln(compiler.utils.repr(\%str as lua expr\))

# Control flow
rule: do %action ..=:
    lua expr "vars.action(compiler, setmetatable({}, {__index=vars}))"

macro block: return %return_value ..=:
    lua block ".."
        |if vars.return_value.value.type ~= "Thunk" then
        |    compiler:error("Assignment operation has the wrong type for the right hand side. "
        |        .."Expected Thunk, but got: "..vars.return_value.value.type.."\\nMaybe you used '=' instead of '=:'?")
        |end
    ".."|do
        |    local ret
        |    \lua expr "compiler:tree_to_lua(vars.return_value.value.value, 'Statement')"\
        |    return ret
        |end

macro block: return ..=:
    "return nil"

# Conditionals
macro block: if %condition %if_body ..=:
    ".."|if \%condition as lua expr\ then
        |    \(lua expr "vars.if_body.value.value") as lua block\
        |end

macro block: if %condition %if_body else %else_body ..=:
    ".."|if \%condition as lua expr\ then
        |    \(lua expr "vars.if_body.value.value") as lua block\
        |else
        |    \(lua expr "vars.else_body.value.value") as lua block\
        |end

# Ternary operator
macro: %if_expr if %condition else %else_expr ..=:
    ".."|(function(compiler, vars)
        |    if \%condition as lua expr\ then
        |        return \%if_expr as lua expr\
        |    else
        |        return \%else_expr as lua expr\
        |    end
        |end)(compiler, vars)

# Loop control flow
macro block: break ..=: "break"
macro block: continue ..=: "continue"
# TODO: add labeled break/continue?

# GOTOs
lua block ".."
    |local function lua_label(label)
    |    if label.type ~= "Var" then
    |        compiler:error("Goto label has the wrong type for the label. Expected Var, but got: "..label.type)
    |    end
    |    local bits = "abcdefghijklmnop"
    |    local lua_identifier = "label_"
    |    for i=1,#label.value do
    |        local byte = string.byte(label.value, i)
    |        local low = byte % 16
    |        local high = (byte - low) / 16
    |        lua_identifier = lua_identifier .. bits:sub(low+1,low+1) .. bits:sub(high+1,high+1)
    |    end
    |    return lua_identifier
    |end
    |
    |compiler:defmacro("-> %label", function(compiler, vars, kind)
    |    return "::"..lua_label(vars.label.value).."::", true
    |end)
    |compiler:defmacro("go to %label", function(compiler, vars, kind)
    |    return "goto "..lua_label(vars.label.value), true
    |end)

# While loops
macro block: repeat %body ..=:
    ".."|while true do
        |    \(lua expr "vars.body.value.value") as lua block\
        |end
macro block: repeat while %condition %body ..=:
    ".."|while \%condition as lua expr\ do
        |    \(lua expr "vars.body.value.value") as lua block\
        |end
macro block: repeat until %condition %body ..=:
    ".."|while not (\%condition as lua expr\) do
        |    \(lua expr "vars.body.value.value") as lua block\
        |end

# For loops
macro block: for %var in %iterable %body ..=:
    %var-type =: lua expr "vars.var.value.type"
    if (%var-type != "Var"):
        error ".."
            |For loop has the wrong type for the loop variable. Expected Var, but got: \%var-type\
    %var-code =: %var as lua expr
    ".."|local old_loopval = \%var-code\
        |for i,value in ipairs(\%iterable as lua expr\) do
        |    \%var-code\ = value
        |    \(lua expr "vars.body.value.value") as lua block\
        |end
        |\%var-code\ = old_loopval

macro block: for all %iterable %body ..=:
    ".."|local old_loopval = vars.it
        |for i,value in ipairs(\%iterable as lua expr\) do
        |    vars.it = value
        |    \(lua expr "vars.body.value.value") as lua block\
        |end
        |vars.it = old_loopval

# List Comprehension
# TODO: maybe make this lazy, or a lazy version?
macro: %expression for %var in %iterable ..=:
    %var-type =: lua expr "vars.var.value.type"
    if (%var-type != "Var"):
        error ".."
            |List comprehension has the wrong type for the loop variable. Expected Var, but got: \%var-type\
    %var-code =: %var as lua expr
    ".."|(function(game, vars)
        |    local comprehension = {}
        |    for i,value in ipairs(\%iterable as lua expr\) do
        |        \%var-code\ = value
        |        comprehension[i] = \%expression as lua expr\
        |    end
        |    return comprehension
        |end)(game, setmetatable({}, {__index=vars}))

macro: %expression for all %iterable ..=:
    ".."|(function(game, vars)
        |    local comprehension = {}
        |    for i,value in ipairs(\%iterable as lua expr\) do
        |        vars.it = value
        |        comprehension[i] = \%expression as lua expr\
        |    end
        |    return comprehension
        |end)(game, setmetatable({}, {__index=vars}))

# Dict comprehension
macro: %key -> %value for %var in %iterable ..=:
    %var-type =: lua expr "vars.var.value.type"
    if (%var-type != "Var"):
        error ".."
            |Dict comprehension has the wrong type for the loop variable. Expected Var, but got: \%var-type\
    %var-code =: %var as lua expr
    ".."|(function(game, vars)
        |    local comprehension = {}
        |    for i,value in ipairs(\%iterable as lua expr\) do
        |        \%var-code\ = value
        |        comprehension[\%key as lua expr\] = \%value as lua expr\
        |    end
        |    return comprehension
        |end)(game, setmetatable({}, {__index=vars}))

macro: %key -> %value for all %iterable ..=:
    ".."|(function(game, vars)
        |    local comprehension = {}
        |    for i,value in ipairs(\%iterable as lua expr\) do
        |        vars.it = value
        |        comprehension[\%key as lua expr\] = \%value as lua expr\
        |    end
        |    return comprehension
        |end)(game, setmetatable({}, {__index=vars}))

# Number ranges
rule: %start up to %stop ..=:
    lua expr "compiler.utils.range(vars.start,vars.stop-1)"

rule:
    %start thru %stop
    %start through %stop
..=:
    lua expr "compiler.utils.range(vars.start,vars.stop)"

rule: %start down to %stop ..=:
    lua expr "compiler.utils.range(vars.start,vars.stop+1,-1)"

rule:
    %start down thru %stop
    %start down through %stop
..=:
    lua expr "compiler.utils.range(vars.start,vars.stop,-1)"

rule: %start up to %stop via %step ..=:
    lua expr "compiler.utils.range(vars.start,vars.stop-1,vars.step)"

rule:
    %start thru %stop via %step
    %start through %stop via %step
..=:
    lua expr "compiler.utils.range(vars.start,vars.stop,vars.step)"

rule: %start down to %stop via %step ..=:
    lua expr "compiler.utils.range(vars.start,vars.stop+1,-vars.step)"

rule:
    %start down thru %stop via %step
    %start down through %stop via %step
..=:
    lua expr "compiler.utils.range(vars.start,vars.stop,-vars.step)"

# Common utility functions
rule: random number ..=: lua expr "math.random()"
rule: sum of %items ..=: lua expr "compiler.utils.sum(vars.items)"
rule: product of %items ..=: lua expr "compiler.utils.product(vars.items)"
rule: all of %items ..=: lua expr "compiler.utils.all(vars.items)"
rule: any of %items ..=: lua expr "compiler.utils.any(vars.items)"
rule:
    avg of %items
    average of %items
..=: lua expr "(compiler.utils.sum(vars.items)/#vars.items)"
rule:
    min of %items
    smallest of %items
    lowest of %items
..=:
    lua expr "compiler.utils.min(vars.items)"
rule:
    max of %items
    biggest of %items
    largest of %items
    highest of %items
..=:
    lua expr "compiler.utils.min(vars.items)"

rule: min of %items with respect to %keys ..=:
    lua expr "compiler.utils.min(vars.items, vars.keys)"
rule: max of %items with respect to %keys ..=:
    lua expr "compiler.utils.max(vars.items, vars.keys)"

# List/dict functions
macro:
    %list's %index
    %index st in %list
    %index nd in %list
    %index rd in %list
    %index th in %list
    %index in %list
    %list -> %index
..=:
    ".."|\%list as lua expr\[\%index as lua expr\]

macro block:
    %list's %index = %value
    %index st in %list = %value
    %index nd in %list = %value
    %index rd in %list = %value
    %index th in %list = %value
    %index in %list = %value
    %list -> %index = %value
..=:
    lua block ".."
        |if vars.value.value.type ~= "Thunk" then
        |    compiler:error("Dict assignment operation has the wrong type for the right hand side. "
        |        .."Expected Thunk, but got: "..vars.value.value.type.."\\nMaybe you used '=' instead of '=:'?")
        |end
    ".."|do
        |    local ret
        |    \lua expr "compiler:tree_to_lua(vars.value.value.value, 'Statement')"\
        |    \%list as lua expr\[\%index as lua expr\] = ret
        |end

macro:
    %item is in %list
    %list contains %item
..=:
    ".."|(\%list as lua expr\[\%index as lua expr\] ~= nil)

macro:
    length of %list
    size of %list
    number of %list
..=:
    ".."|#(\%list as lua expr\)

rule: dict %items ..=:
    %dict =: []
    for %pair in %items:
        lua block "vars.dict[vars.pair[1]] = vars.pair[2]"
    return: %dict

# Permission functions
rule: restrict %fn to within %whitelist ..=:
    lua block ".."
        |local fns = compiler:get_invocations(vars.fn)
        |local whitelist = compiler:get_invocations(vars.whitelist)
        |local whiteset = {}
        |for _,w in ipairs(whitelist) do
        |    if not compiler.defs[w] then
        |        compiler:error("Undefined function: "..tostring(w))
        |    else whiteset[w] = true
        |    end
        |end
        |for _,fn in ipairs(fns) do
        |    local fn_info = compiler.defs[fn]
        |    if fn_info == nil then
        |        compiler:error("Undefined function: "..tostring(fn))
        |    elseif not compiler:check_permission(fn) then
        |        compiler:writeln("You do not have permission to restrict function: "..tostring(fn))
        |    else
        |        compiler.defs[fn].whiteset = whiteset
        |    end
        |end

rule: allow %whitelist to use %fn ..=:
    lua block ".."
        |local fns = compiler:get_invocations(vars.fn)
        |local whitelist = compiler:get_invocations(vars.whitelist)
        |for _,w in ipairs(whitelist) do
        |    if not compiler.defs[w] then
        |        compiler:error("Undefined function: "..tostring(w))
        |    end
        |end
        |for _,fn in ipairs(fns) do
        |    local fn_info = compiler.defs[fn]
        |    if fn_info == nil then
        |        compiler:error("Undefined function: "..tostring(fn))
        |    elseif fn_info.whiteset == nil then
        |        compiler:writeln("Function is already allowed by everyone: "..tostring(fn))
        |    elseif not compiler:check_permission(fn) then
        |        compiler:writeln("You do not have permission to grant permissions for function: "..tostring(fn))
        |    else
        |        for _,w in ipairs(whitelist) do
        |            fn_info.whiteset[w] = true
        |        end
        |    end
        |end

rule: forbid %blacklist to use %fn ..=:
    lua block ".."
        |local fns = compiler:get_invocations(vars.fn)
        |local blacklist = compiler:get_invocations(vars.blacklist)
        |for _,b in ipairs(blacklist) do
        |    if not compiler.defs[b] then
        |        compiler:error("Undefined function: "..tostring(b))
        |    end
        |end
        |for _,fn in ipairs(fns) do
        |    local fn_info = compiler.defs[fn]
        |    if fn_info == nil then
        |        compiler:error("Undefined function: "..tostring(fn))
        |    elseif fn_info.whiteset == nil then
        |        compiler:writeln("Cannot remove items from a whitelist when there is no whitelist on function: "..tostring(fn))
        |    elseif not compiler:check_permission(fn) then
        |        compiler:writeln("You do not have permission to restrict function: "..tostring(fn))
        |    else
        |        for _,b in ipairs(blacklist) do fn_info.whiteset[b] = nil end
        |    end
        |end

# For unit testing
macro block: test %code yields %expected ..=:
    %generated =: lua expr "compiler.utils.repr(compiler:stringify_tree(vars.code.value.value), true)"
    %expected =: %expected as lua expr
    if (%generated != %expected):
        say "Test failed!"
        say "Expected:"
        say %expected
        say "But got:"
        say %generated
        error!
    return: ""