aboutsummaryrefslogtreecommitdiff
path: root/lib/scopes.nom
blob: 3f6eff1e37da879b6600845a15c6508a3bcfdd9b (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
require "lib/metaprogramming.nom"
require "lib/operators.nom"
require "lib/control_flow.nom"
require "lib/collections.nom"

compile [<%var> = %value] to code: ".."
    nomsu.defs['#vars'][\(repr (%var's "value"))] = \(%value as lua);

compile [<%var>] to: "nomsu.defs['#vars'][\(repr (%var's "value"))]"

compile [str %] to: "tostring(\(% as lua))"

compile [scope] to: "nomsu.defs"
compile [parent scope] to: "getmetatable(nomsu.defs).__index"

# TODO: fix this file
return
compile [using %scoped do %actions] to code: ".."
    do
        local old_scope, old_vars = nomsu.defs, vars;
        local use_vars = setmetatable({}, {__index=old_scope['#vars']});
        local scope = setmetatable({['#vars']=use_vars}, {__index=old_scope});
        nomsu.defs = scope;
        local ok, ret = pcall(function(nomsu, vars)
            local ret;
            do
                \(%scoped as lua statements)
            end
            getmetatable(scope).__newindex = old_scope;
            getmetatable(use_vars).__newindex = old_vars;
            do
                \(%actions as lua statements)
            end
            return ret;
        end, nomsu, use_vars);
        nomsu.defs = old_scope;
        if not ok then nomsu:error(ret); end
    end

parse [scoped %actions] as: using %actions do (:pass)

parse [wrap %signature with %body] as:
    using:
        run ((nomsu)->*["defs",nomsu "get_stub" [\%signature->*["value",1]],"src"])
    ..do:
        rule %signature = %body