aboutsummaryrefslogtreecommitdiff
path: root/core/metaprogramming.nom
blob: a446f42779c960f66c1ac3eeb0dceaf48961dc66 (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
#..
    This File contains actions for making actions and compile-time actions and some helper
    functions to make that easier.

# Compile-time action to make compile-time actions:
immediately:
    lua> ".."
        nomsu:define_compile_action("compile %actions to %lua", \(!! code location !!), function(tree, \%actions, \%lua)
            local lua = Lua(tree.source, "nomsu:define_compile_action(");
            local stubs = {};
            for i, action in ipairs(\%actions.value) do
                stubs[i] = action:get_stub(true);
            end
            lua:append(repr(stubs), ", ", repr(tree.source:get_line()), ", function(tree");
            local args = {};
            for i,tok in ipairs(\%actions.value[1].value) do
                if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
            end
            if \%lua.type == "Text" then
                error("Invalid type for 'compile % to %', expected a dict with expr/statements, but got text.", 0);
            end
            for i, arg in ipairs(args) do
                lua:append(", ");
                lua:append(arg);
            end
            local body_lua = \%lua:as_lua(nomsu);
            body_lua:convert_to_statements("return ");
            body_lua:remove_free_vars(args);
            body_lua:declare_locals();
            lua:append(")\\n    ", body_lua, "\\nend);");
            return lua;
        end);

# Compile-time action to make actions
immediately:
    compile [action %actions %body] to:
        lua> ".."
            local lua = Lua(tree.source, "nomsu:define_action(");
            local stubs = {};
            for i, action in ipairs(\%actions.value) do
                stubs[i] = action:get_stub(true);
            end
            lua:append(repr(stubs), ", ", repr(tree.source:get_line()), ", function(");
            local args = {};
            for i,tok in ipairs(\%actions.value[1].value) do
                if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
            end
            for i, arg in ipairs(args) do
                lua:append(arg);
                if i < #args then lua:append(", ") end
            end
            local body_lua = \%body:as_lua(nomsu);
            body_lua:convert_to_statements("return ");
            body_lua:remove_free_vars(args);
            body_lua:declare_locals();
            lua:append(")\\n    ", body_lua, "\\nend);")
            return lua;

# Macro to make nomsu macros:
immediately:
    compile [parse %shorthand as %longhand] to:
        lua> ".."
            local lua = Lua(tree.source, "nomsu:define_compile_action(");
            local stubs = {};
            for i, action in ipairs(\%shorthand.value) do
                stubs[i] = action:get_stub(true);
            end
            lua:append(repr(stubs), ", ", repr(tree.source:get_line()), ", function(tree");
            local args = {};
            for i,tok in ipairs(\%shorthand.value[1].value) do
                if tok.type == "Var" then args[#args+1] = nomsu:var_to_lua_identifier(tok.value); end
            end
            for i, arg in ipairs(args) do
                lua:append(", ");
                lua:append(arg);
            end
            local template = repr(tostring(\%longhand:as_nomsu()));
            local replacements = {};
            for i, a in ipairs(args) do replacements[i] = a.."="..a; end
            replacements = "{"..table.concat(replacements, ", ").."}";
            lua:append([[)
                local template = nomsu:parse(Nomsu(]]..repr(tree.source)..[[, ]]..template..[[));
                local replacement = nomsu:tree_with_replaced_vars(template, ]]..replacements..[[);
                return replacement:as_lua(nomsu);
            end);
            ]]);
            return lua;

action [remove action %stub]:
    lua> ".."
        local fn = ACTIONS[\%stub];
        local metadata = nomsu.action_metadata[fn];
        for i=#metadata.aliases,1,-1 do
            metadata.arg_orders[metadata.aliases[i]] = nil;
            table.remove(metadata.aliases, i);
        end
        ACTIONS[\%stub] = nil;

immediately:
    action [%tree as lua]:
        =lua "\%tree:as_lua(nomsu)"

    action [%tree as lua expr]:
        lua> ".."
            local lua = \%tree:as_lua(nomsu);
            if not lua.is_value then
                error("Invalid thing to convert to lua expr: "..\%tree.source:get_text());
            end
            return lua;

    action [%tree as lua statements]:
        lua> ".."
            local lua = \%tree:as_lua(nomsu);
            lua:convert_to_statements();
            return lua;
    
    compile [declare locals in %tree] to
        Lua "\(%tree as lua expr):declare_locals();"
    
    compile [declare locals %locals in %tree] to
        Lua "\(%tree as lua expr):declare_locals(\(%locals as lua expr));"
    
    compile [remove free vars %vars from %tree] to:
        Lua "\(%tree as lua expr):remove_free_vars(unpack(\(%vars as lua expr)));"

    action [%tree as value]:
        =lua "nomsu:tree_to_value(\%tree)"

    
    action [%tree's stub]:
        =lua "nomsu:tree_to_stub(\%tree)"
    
immediately:
    parse [%var <-write %code] as: lua> "\%var:append(\%code);"
    parse [to %var write %code] as: lua> "\%var:append(\%code);"

immediately:
    action [%tree's source code, %tree' source code]:
        =lua "\%tree.source:get_text()"

    compile [repr %obj] to: Lua value "repr(\(%obj as lua expr))"
    compile [%obj as text] to: Lua value "tostring(\(%obj as lua expr))"
    compile [type of %obj] to: Lua value "type(\(%obj as lua expr))"

immediately:
    compile [nomsu] to: Lua value "nomsu"
    compile [%var as lua identifier] to: Lua value "nomsu:var_to_lua_identifier(\(%var as lua expr))"

action [action %names metadata]:
    =lua "nomsu.action_metadata[ACTIONS[\%names]]"

# Get the source code for a function
action [help %action]:
    lua> ".."
        local metadata = \(action %action metadata);
        if not metadata then
            print("Action not found: "..repr(\%action));
        else
            print(metadata.src or "<unknown source code>");
        end

# Compiler tools
immediately:
    compile [run %code] to:
        Lua value "nomsu:run(\(%code as lua expr), \(=lua "tree.source"))"
    parse [enable debugging] as: lua> "nomsu.debug = true;"
    parse [disable debugging] as: lua> "nomsu.debug = false;"

immediately:
    compile [show lua %block] to:
        lua> ".."
            local \%lua = \%block:as_lua(nomsu);
            return Lua(\%block.source, "print(", repr(tostring(\%lua)), ");");

immediately:
    compile [say %message] to:
        lua> ".."
            if \%message.type == "Text" then
                return Lua(tree.source, "print(", \(%message as lua expr), ");");
            else
                return Lua(tree.source, "print(stringify(", \(%message as lua expr), "));");
            end

immediately:
    compile [source] to: Lua value (=lua "tree.source") "tree.source"

immediately:
    action [Lua %]: Lua (=lua "tree.source") %
    action [Lua value %]: Lua value (=lua "tree.source") %

# Return
immediately:
    #.. Return statement is wrapped in a do..end block because Lua is unhappy if you
        put code after a return statement, unless you wrap it in a block.
    compile [return] to: Lua "do return; end"
    compile [return %return_value] to: Lua "do return \(%return_value as lua expr); end"

# Error functions
immediately:
    compile [barf] to: Lua "error(nil, 0);"
    compile [barf %msg] to: Lua "error(\(%msg as lua expr), 0);"
    compile [assume %condition] to:
        lua> "local \%assumption = 'Assumption failed: '..tostring(\%condition.source:get_text());"
        return:
            Lua ".."
                if not \(%condition as lua expr) then
                    error(\(repr %assumption), 0);
                end
    
    # TODO: factor this out and replace with "unless %condition: barf %message"
    compile [assume %condition or barf %message] to:
        Lua ".."
            if not \(%condition as lua expr) then
                error(\(%message as lua expr), 0);
            end

# Literals
immediately:
    compile [yes] to: Lua value "true"
    compile [no] to: Lua value "false"
    compile [nothing, nil, null] to: Lua value "nil"