aboutsummaryrefslogtreecommitdiff
path: root/tests/metaprogramming.nom
blob: e94a78f7db2ad85d0414cf1412908ef57b50c882 (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
#..
    Tests for the stuff defined in lib/metaprogramming.nom

use "core"

immediately
    compile [five] to: Lua value "5"
assume ((five) = 5) or barf "Compile to expression failed."

immediately
    compile [loc x] to: Lua "local _x = 99;"
lua> "do"
loc x
assume (%x is 99) or barf "Compile to statements with locals failed."
lua> "end"
assume (%x is (nil)) or barf "Failed to properly localize a variable."

immediately
    compile [asdf] to
        %tmp <- ""
        return: Lua %tmp
asdf
assume (%tmp is (nil)) or barf "compile to is leaking variables"

action [foo %x]
    %y <- (%x + 1)
    return %y
assume ((foo 10) = 11) or barf "Action didn't work."
assume (%y is (nil)) or barf "Action leaked a local into globals."

immediately
    parse [baz %] as: foo %
assume ((baz 10) = 11) or barf "Parse as action failed."

immediately
    parse [V] as: five
assume ((V) = 5) or barf "Parse as compile action failed."

remove action "foo %"
try: foo 99
..and if it succeeds: barf "Failed to delete action"

assume ((\(5 + 5) as value) = 10) or barf "%tree as value failed."

assume (((\(foo %x)'s source code) as text) = "foo %x") or barf "source code failed."

assume ((repr [1,2]) = "{1, 2}") or barf "repr failed."

assume ((type of {}) = "table") or barf "type of failed."

assume ((nomsu) = (=lua "nomsu")) or barf "nomsu failed"

assume (((\%x as lua identifier) as text) = "_x") or barf "converting to identifier failed."

assume ((run "return 99") = 99) or barf "run % failed."

say "Metaprogramming test passed."

%code <-: Lua "global_x = true;"
lua> %code
assume (=lua "global_x") or barf "Running lua from a variable failed."
%code <-: Lua value "global_x"
assume (=lua %code) or barf "Running lua from a variable failed."