aboutsummaryrefslogtreecommitdiff
path: root/core/scopes.nom
blob: 72631c00933b4d5ad2d7a54d1d8a9e8236b331f3 (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
use "core/metaprogramming.nom"
use "core/text.nom"
use "core/operators.nom"
use "core/collections.nom"
use "core/control_flow.nom"

compile [using %definitions %body, using %definitions do %body] to
    %setup_lua <-
        Lua ".."
            local fell_through = false
            local ok, ret = pcall(function()
                \(%definitions as lua statements)
                fell_through = true
            end)
    %body_lua <-
        Lua ".."
            local fell_through = false
            local ok, ret = pcall(function()
                \(%body as lua statements)
                fell_through = true
            end)
    remove free vars (declare locals in %setup_lua) from %body_lua
    %lua <-
        Lua ".."
            do
                local old_actions, old_compile_actions, old_arg_orders = ACTIONS, COMPILE_ACTIONS, ARG_ORDERS
                ACTIONS = setmetatable({}, {__index=old_actions})
                COMPILE_ACTIONS = setmetatable({}, {__index=old_compile_actions})
                ARG_ORDERS = setmetatable({}, {__index=old_arg_orders})
                \%setup_lua
                if not ok then
                    ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders
                    error(ret)
                end
                if not fell_through then
                    ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders
                    return ret
                end
                getmetatable(ACTIONS).__newindex = old_actions
                getmetatable(COMPILE_ACTIONS).__newindex = old_compile_actions
                getmetatable(ARG_ORDERS).__newindex = old_arg_orders
                \%body_lua
                ACTIONS, COMPILE_ACTIONS, ARG_ORDERS = old_actions, old_compile_actions, old_arg_orders
                if not ok then
                    error(ret)
                end
                if not fell_through then
                    return ret
                end
            end
    declare locals in %lua
    return %lua

parse [using %] as: using % (do nothing)