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

lua> "\
    ..NOMSU_CORE_VERSION = 10
    NOMSU_LIB_VERSION = 7"
lua> "\
    ..do
        local mangle_index = 0
        function mangler()
            local my_mangle_index = mangle_index
            mangle_index = mangle_index + 1
            return function(varname)
                return (varname..(("\\3%X"):format(my_mangle_index))):as_lua_id()
            end
        end
    end
    compile.action["define mangler"] = function(compile)
        return LuaCode("local mangle = mangler()")
    end"

lua> "\
    ..compile.action["1 ->"] = function(compile, \%args, \%body)
        local lua = LuaCode("(function(")
        if SyntaxTree:is_instance(\%args) and \%args.type == "Action" then \%args = \%args:get_args() end
        local lua_args = table.map(\%args, function(a) return SyntaxTree:is_instance(a) and compile(a):text() or a end)
        lua:concat_append(lua_args, ", ")
        local body_lua = SyntaxTree:is_instance(\%body) and compile(\%body) or \%body
        if SyntaxTree:is_instance(\%body) and \%body.type ~= "Block" then body_lua:prepend("return ") end
        body_lua:remove_free_vars(lua_args)
        body_lua:declare_locals()
        lua:append(")\\n    ", body_lua, "\\nend)")
        return lua
    end"

lua> "\
    ..compile.action["what 1 compiles to"] = function(compile, \%action)
        local lua = LuaCode("compile.action[", \%action.stub:as_lua(), "](")
        local lua_args = table.map(\%action:get_args(), function(a) return compile(a) end)
        table.insert(lua_args, 1, "compile")
        lua:concat_append(lua_args, ", ")
        lua:append(")")
        return lua
    end"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

test:
    (five) compiles to "5"
test:
    assume ((five) == 5) or barf "Compile to expression failed."
    (loc x) compiles to "local x = 99;"
test:
    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."
    (asdf) compiles to:
        %tmp = ""
        return (Lua %tmp)
test:
    asdf
    assume (%tmp is (nil)) or barf "compile to is leaking variables"
lua> "\
    ..compile.action["1 compiles to"] = function(compile, \%action, \%body)
        local \%args = List{\(\%compile), unpack(\%action:get_args())}
        if \%body.type == "Text" then
            \%body = SyntaxTree{source=\%body.source, type="Action", "Lua", \%body}
        end
        return LuaCode("compile.action[", \%action.stub:as_lua(),
            "] = ", \(what (%args -> %body) compiles to))
    end"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(%actions all compile to %body) compiles to:
    lua> "\
        ..if \%actions.type ~= "List" then
            compile_error(\%actions, "This should be a list of actions.")
        end
        local lua = \(what (%actions.1 compiles to %body) compiles to)
        local \%args = List{\(\%compile), unpack(\%actions[1]:get_args())}
        for i=2,#\%actions do
            local alias = \%actions[i]
            local \%alias_args = List{\(\%compile), unpack(alias:get_args())}
            lua:append("\\ncompile.action[", alias.stub:as_lua(), "] = ")
            if \%alias_args == \%args then
                lua:append("compile.action[", \%actions[1].stub:as_lua(), "]")
            else
                lua:append(\(what (%alias_args -> \(what %actions.1 compiles to)) compiles to))
            end
        end
        return lua"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(call %fn with %args) compiles to:
    lua> "\
        ..local lua = LuaCode(compile(\%fn), "(")
        if \%args.type == 'List' then
            lua:concat_append(table.map(\%args, function(a) return compile(a) end), ", ")
        else
            lua:append('unpack(', compile(\%args), ')')
        end
        lua:append(")")
        return lua"

test:
    (foo %x) means "outer"
    with local [(foo %)'s meaning]:
        (foo %x) means:
            %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."
        (baz %) parses as (foo %)
    
    assume ((foo 1) == "outer")

(%action means %body) compiles to:
    lua> "\
        ..local fn_name = \%action.stub:as_lua_id()
        local \%args = \%action:get_args()
        local lua = LuaCode(fn_name, " = ", \(what (%args -> %body) compiles to))
        lua:add_free_vars({fn_name})
        return lua"
(%actions all mean %body) compiles to:
    lua> "\
        ..local fn_name = \%actions[1].stub:as_lua_id()
        local \%args = List(\%actions[1]:get_args())
        local lua = \(what (%actions.1 means %body) compiles to)
        for i=2,#\%actions do
            local alias = \%actions[i]
            local alias_name = alias.stub:as_lua_id()
            lua:add_free_vars({alias_name})
            local \%alias_args = List(alias:get_args())
            lua:append("\\n", alias_name, " = ")
            if \%args == \%alias_args then
                lua:append(fn_name)
            else
                lua:append(\(what (%alias_args -> %actions.1) compiles to))
            end
        end
        return lua"

test:
    externally (baz1) means: return "baz1"
    externally (baz2) means "baz2"
test:
    assume ((baz1) == "baz1")
    assume ((baz2) == "baz2")
(externally %action means %body) compiles to:
    lua> "\
        ..local lua = \(what (%action means %body) compiles to)
        lua:remove_free_vars({\%action.stub:as_lua_id()})
        return lua"
(externally %actions all mean %body) compiles to:
    lua> "\
        ..local lua = \(what (%actions all mean %body) compiles to)
        lua:remove_free_vars(table.map(\%actions, function(a) return a.stub:as_lua_id() end))
        return lua"

test:
    assume (((say %)'s meaning) == (=lua "say"))
(%action's meaning) compiles to (Lua (%action.stub as lua id))

test:
    (swap %x and %y) parses as (..)
        do:
            %tmp = %x
            %x = %y
            %y = %tmp
test:
    set {%1:1, %2:2}
    swap %1 and %2
    assume ((%1 == 2) and (%2 == 1)) or barf "\
        ..'parse % as %' failed on 'swap % and %'"
    set {%tmp:1, %tmp2:2}
    swap %tmp and %tmp2
    assume ((%tmp == 2) and (%tmp2 == 1)) or barf "\
        ..'parse % as %' variable mangling failed."
(%actions all parse as %body) compiles to:
    lua> "\
        ..local replacements = {}
        if \%actions.type ~= "List" then
            compile_error(\%actions, "This should be a list.")
        end
        for i,arg in ipairs(\%actions[1]:get_args()) do
            replacements[arg[1]] = compile(arg):text()
        end
        local function make_tree(t)
            if SyntaxTree:is_instance(t) and t.type == "Var" then
                if replacements[t[1]] then
                    return replacements[t[1]]
                else
                    return "SyntaxTree{mangle("..t[1]:as_lua().."), type="..t.type:as_lua()..", source="..tostring(t.source):as_lua().."}"
                end
            elseif SyntaxTree:is_instance(t) then
                local ret = {}
                local i = 1
                for k, v in pairs(t) do
                    if k == i then
                        ret[#ret+1] = make_tree(t[i])
                        i = i + 1
                    elseif k == "source" then
                        ret[#ret+1] = k.."= "..tostring(v):as_lua()
                    elseif lua_type_of(k) == 'string' and k:match("[_a-zA-Z][_a-zA-Z0-9]*") then
                        ret[#ret+1] = k.."= "..make_tree(v)
                    else
                        ret[#ret+1] = "["..make_tree(k).."]= "..make_tree(v)
                    end
                end
                return "SyntaxTree{"..table.concat(ret, ", ").."}"
            elseif lua_type_of(t) == 'number' then
                return tostring(t)
            else
                return t:as_lua()
            end
        end
        local \%new_body = LuaCode:from(\%body.source,
            "local mangle = mangler()",
            "\\nreturn ", make_tree(\%body))
        local ret = \(what (%actions all compile to %new_body) compiles to)
        return ret"

~~~~~~~~~~~~~~~~~~~~~~~~~~

[%action parses as %body] all parse as ([%action] all parse as %body)

(%tree as lua expr) compiles to "\
    ..compile(\(=lua "compile(\%tree, true)"), true)"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

externally [%var as lua identifier, %var as lua id] all mean:
    lua> "\
        ..if lua_type_of(\%var) == 'string' then return \%var:as_lua_id()
        elseif SyntaxTree:is_instance(\%var, 'Var') then return \%var[1]:as_lua_id()
        elseif SyntaxTree:is_instance(\%var) then
            local lua = \(%var as lua expr)
            if not lua:text():match("^[_a-zA-Z][_a-zA-Z0-9]*$") then
                compile_error(\%var, "This is not a valid Lua identifier.")
            end
            return lua
        else error("Unknown type: "..tostring(\%var))
        end"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(% is syntax tree) compiles to "SyntaxTree:is_instance(\(% as lua expr))"
externally (% is %kind syntax tree) means (..)
    =lua "SyntaxTree:is_instance(\%) and \%.type == \%kind"

(%tree with %t -> %replacement) compiles to "\
    ..\(%tree as lua expr):map(function(\(%t as lua expr))
        \((%replacement as lua) if (%replacement.type == "Block") else ("return \(%replacement as lua expr)"))
    end)"

externally (%tree with vars %replacements) means (..)
    =lua "\
        ..\%tree:map(function(\%t)
            if \%t.type == "Var" then
                return \%replacements[\%t[1]]
            end
        end)"

(tree %tree with vars %replacements) compiles to "\
    ..\(=lua "(\%tree):as_lua()"):map(function(t)
        if t.type == "Var" then
            return \(%replacements as lua expr)[t[1]]
        end
    end)"

(%tree has subtree %match_tree) compiles to "\
    ..(function()
        local match_tree = \(%match_tree as lua expr)
        for subtree in coroutine.wrap(function() \(%tree as lua expr):map(coroutine.yield) end) do
            if subtree == match_tree then return true end
        end
    end)()"

externally (match %tree with %patt) means:
    lua> "\
        ..if \%patt.type == "Var" then return Dict{[\%patt[1]]=\%tree} end
        if \%patt.type == "Action" and \%patt.stub ~= \%tree.stub then return nil end
        if #\%patt ~= #\%tree then return nil end
        local matches = Dict{}
        for \%i=1,#\%patt do
            if SyntaxTree:is_instance(\%tree[\%i]) then
                local submatch = \(match %tree.%i with %patt.%i)
                if not submatch then return nil end
                for k,v in pairs(submatch) do
                    if matches[k] and matches[k] ~= v then return nil end
                    matches[k] = v
                end
            end
        end
        return matches"

externally (%tree with %patt ~> %replacement) means:
    lua> "\
        ..return \%tree:map(function(\%t)
            local \%vars = \(match %t with %patt)
            if not \%vars then return nil end
            for \%k,\%v in pairs(\%vars) do
                \%vars[\%k] = \(%v with %patt ~> %replacement)
            end
            return \%replacement:map(function(\%t)
                if \%t.type == "Var" then
                    return \%vars[\%t[1]]
                end
            end)
        end)"

test:
    assume (..)
        (..)
            quote "\
                ..one
                "two""
        ..== "\"one\\n\\\"two\\\"\""
(quote %s) compiles to "tostring(\(%s as lua expr)):as_lua()"

test:
    assume (lua type of {}) == "table"
    assume (type of {}) == "Dict"
    assume ({} is a "Dict")
    assume ("" is text)
    assume ("" isn't a "Dict")
externally (% is text) means (=lua "\(lua type of %) == 'string'")
externally [% is not text, % isn't text] all mean (..)
    =lua "\(lua type of %) ~= 'string'"

externally (type of %) means:
    lua> "\
        ..local lua_type = \(lua type of %)
        if lua_type == 'string' then return 'Text'
        elseif lua_type == 'table' or lua_type == 'userdata' then
            local mt = getmetatable(\%)
            if mt and mt.__type then return mt.__type end
        end
        return lua_type"

[% is a %type, % is an %type] all parse as ((type of %) == %type)
[% isn't a %type, % isn't an %type, % is not a %type, % is not an %type] all parse as (..)
    (type of %) != %type

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

test:
    assume ((run "return (2 + 99)") == 101)
    external %passed = (no)
    run "external %passed = (yes)"
    assume %passed
    assume (run \(return \(\(5) + \(5)))) == 10
(run %nomsu_code) compiles to "\
    ..run_1_in(\(%nomsu_code as lua expr), _ENV)"

[compile %block, compiled %block, %block compiled] all compile to "\
    ..compile(\(%block as lua))"

# 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.
(return %return_value) compiles to "\
    ..do return \(..)
        =lua "\%return_value and \(%return_value as lua expr) or ''"
    .. end"

# Literals
(yes) compiles to "true"
(no) compiles to "false"
[nothing, nil, null] all compile to "nil"
(Nomsu syntax version) compiles to "NOMSU_SYNTAX_VERSION"
(Nomsu compiler version) compiles to "NOMSU_COMPILER_VERSION"
(core version) compiles to "NOMSU_CORE_VERSION"
(lib version) compiles to "NOMSU_LIB_VERSION"
(command line args) compiles to "command_line_args"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(with local compile actions %body) compiles to "\
    ..do
        --local compile = _1_forked(compile)
        local old_action = compile.action
        compile.action = _1_forked(old_action)
        \(%body as lua)
        compile.action = old_action
    end"

externally (Nomsu version) means:
    return "\
        ..\(Nomsu syntax version).\(core version).\(Nomsu compiler version).\(lib version)"