aboutsummaryrefslogtreecommitdiff
path: root/nomsu_compiler.moon
blob: 497d2101274a8dc794bd00f748b6cf9a51c56ed6 (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
-- This file contains the source code of the Nomsu compiler.
-- Nomsu is a programming language that cross-compiles to Lua. It was designed to be good
-- at natural-language-like code that is highly self-modifying and flexible.
-- The only dependency is LPEG, which can be installed using "luarocks install lpeg"
-- File usage:
--    Either, in a lua/moonscript file:
--        Nomsu = require "nomsu"
--        nomsu = Nomsu()
--        nomsu:run(your_nomsu_code)
--    Or from the command line:
--        lua nomsu.lua your_file.nom
lpeg = require 'lpeg'
{:R,:P,:S} = lpeg
re = require 're'
{:List, :Dict, :Text} = require 'containers'
{:insert, :remove, :concat} = table
unpack or= table.unpack
{:match, :sub, :gsub, :format, :byte, :find} = string
{:LuaCode, :Source} = require "code_obj"
SyntaxTree = require "syntax_tree"
{:Importer, :import_to_1_from, :_1_forked} = require 'importer'

table.map = (t, fn)-> setmetatable([fn(v) for _,v in ipairs(t)], getmetatable(t))

-- TODO:
-- Re-implement nomsu-to-lua comment translation?

-- TODO: de-duplicate this
pretty_error = require("pretty_errors")
compile_error = (tree, err_msg, hint=nil)->
    err_str = pretty_error{
        title: "Compile error"
        error:err_msg, hint:hint, source:tree\get_source_file!
        start:tree.source.start, stop:tree.source.stop, filename:tree.source.filename
    }
    error(err_str, 0)
{:tree_to_nomsu, :tree_to_inline_nomsu} = require "nomsu_decompiler"

-- This is a bit of a hack, but this code handles arbitrarily complex
-- math expressions like 2*x + 3^2 without having to define a single
-- action for every possibility.
math_expression = re.compile [[ (([*/^+-] / [0-9]+) " ")* [*/^+-] !. ]]

MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
compile = setmetatable({
    action: Importer{
        [""]: (compile, fn, ...)->
            lua = LuaCode!
            fn_lua = compile(fn)
            lua\append fn_lua
            unless fn_lua\text!\match("^%(.*%)$") or fn_lua\text!\match("^[_a-zA-Z][_a-zA-Z0-9.]*$")
                lua\parenthesize!
            lua\append "("
            for i=1,select('#',...)
                lua\append(", ") if i > 1
                lua\append compile(select(i, ...))
            lua\append ")"
            return lua

        ["Lua"]: (compile, code)->
            if code.type != "Text"
                return LuaCode("LuaCode:from(", tostring(code.source)\as_lua!, ", ", compile(code), ")")
            add_bit_lua = (lua, bit_lua)->
                bit_leading_len = #(bit_lua\match("^[^\n]*"))
                lua\append(lua\trailing_line_len! + bit_leading_len > MAX_LINE and ",\n    " or ", ")
                lua\append(bit_lua)
            operate_on_text = (text)->
                lua = LuaCode\from(text.source, "LuaCode:from(", tostring(text.source)\as_lua!)
                for bit in *text
                    if type(bit) == "string"
                        add_bit_lua(lua, bit\as_lua!)
                    elseif bit.type == "Text"
                        add_bit_lua(lua, operate_on_text(bit))
                    else
                        add_bit_lua(lua, compile(bit))
                lua\append ")"
                return lua
            return operate_on_text code

        ["lua >"]: (compile, code)->
            if code.type != "Text"
                return code
            operate_on_text = (text)->
                lua = LuaCode\from(text.source)
                for bit in *text
                    if type(bit) == "string"
                        lua\append bit
                    elseif bit.type == "Text"
                        lua\append(operate_on_text(bit))
                    else
                        lua\append compile(bit)
                return lua
            return operate_on_text code

        ["= lua"]: (compile, code)-> compile.action["lua >"](compile, code)

        ["use"]: (compile, path)->
            --if path.type == 'Text' and #path == 1 and type(path[1]) == 'string'
            --    unless import_to_1_from(compile, path[1])
            --        compile_error tree, "Could not find anything to import for #{path}"
            return LuaCode("run_file_1_in(#{compile(path)}, _ENV, OPTIMIZATION)")

        ["tests"]: (compile)-> LuaCode("TESTS")
        ["test"]: (compile, body)->
            unless body.type == 'Block'
                compile_error(body, "This should be a Block")
            test_nomsu = body\get_source_code!\match(":[ ]*(.*)")
            if indent = test_nomsu\match("\n([ ]*)")
                test_nomsu = test_nomsu\gsub("\n"..indent, "\n")
            return LuaCode "TESTS[#{tostring(body.source)\as_lua!}] = ", test_nomsu\as_lua!

        ["is jit"]: (compile, code)-> LuaCode("jit")
        ["Lua version"]: (compile, code)-> LuaCode("_VERSION")
        ["nomsu environment"]: (compile)-> LuaCode("_ENV")
    }
}, {
    __import: (other)=>
        import_to_1_from(@action, other.action)
        return
    __call: (compile, tree, force_value=false)->
        switch tree.type
            when "Action"
                stub = tree.stub
                compile_action = compile.action[stub]
                if not compile_action and math_expression\match(stub)
                    lua = LuaCode\from(tree.source)
                    for i,tok in ipairs tree
                        if type(tok) == 'string'
                            lua\append tok
                        else
                            tok_lua = compile(tok)
                            tok_lua\parenthesize! if tok.type == "Action"
                            lua\append tok_lua
                        lua\append " " if i < #tree
                    return lua

                if compile_action and not tree.target
                    args = [arg for arg in *tree when type(arg) != "string"]
                    -- Force Lua to avoid tail call optimization for debugging purposes
                    -- TODO: use tail call?
                    ret = compile_action(compile, unpack(args))
                    if ret == nil
                        info = debug.getinfo(compile_action, "S")
                        filename = Source\from_string(info.source).filename
                        compile_error tree,
                            "The compile-time action here (#{stub}) failed to return any value.",
                            "Look at the implementation of (#{stub}) in #{filename}:#{info.linedefined} and make sure it's returning something."
                    unless SyntaxTree\is_instance(ret)
                        ret.source or= tree.source
                        return ret
                    if ret != tree
                        return compile(ret)

                lua = LuaCode\from(tree.source)
                if tree.target -- Method call
                    target_lua = compile tree.target
                    target_text = target_lua\text!
                    if target_text\match("^%(.*%)$") or target_text\match("^[_a-zA-Z][_a-zA-Z0-9.]*$")
                        lua\append target_lua, ":"
                    else
                        lua\append "(", target_lua, "):"
                lua\append((stub)\as_lua_id!,"(")
                args = {}
                for i, tok in ipairs tree
                    if type(tok) == "string" then continue
                    arg_lua = compile(tok, true)
                    insert args, arg_lua
                lua\concat_append args, ", "
                lua\append ")"
                return lua

            when "EscapedNomsu"
                lua = LuaCode\from tree.source, "SyntaxTree{"
                needs_comma, i = false, 1
                as_lua = (x)->
                    if type(x) == 'number'
                        tostring(x)
                    elseif SyntaxTree\is_instance(x)
                        compile(x)
                    else x\as_lua!

                for k,v in pairs((SyntaxTree\is_instance(tree[1]) and tree[1].type == "EscapedNomsu" and tree) or tree[1])
                    if needs_comma then lua\append ", "
                    else needs_comma = true
                    if k == i
                        i += 1
                    elseif type(k) == 'string' and match(k,"[_a-zA-Z][_a-zA-Z0-9]*")
                        lua\append(k, "= ")
                    else
                        lua\append("[", as_lua(k), "]= ")
                    if k == "source"
                        lua\append tostring(v)\as_lua!
                    else
                        lua\append as_lua(v)
                lua\append "}"
                return lua
            
            when "Block"
                if not force_value
                    lua = LuaCode\from(tree.source)
                    lua\concat_append([compile(line) for line in *tree], "\n")
                    return lua
                else
                    lua = LuaCode\from(tree.source)
                    lua\append("((function()")
                    for i, line in ipairs(tree)
                        lua\append "\n    ", compile(line)
                    lua\append("\nend)())")
                    return lua

            when "Text"
                lua = LuaCode\from(tree.source)
                string_buffer = ""
                for i, bit in ipairs tree
                    if type(bit) == "string"
                        string_buffer ..= bit
                        continue
                    if string_buffer != ""
                        if #lua.bits > 0 then lua\append ".."
                        lua\append string_buffer\as_lua!
                        string_buffer = ""
                    bit_lua = compile(bit)
                    if #lua.bits > 0 then lua\append ".."
                    if bit.type != "Text"
                        bit_lua = LuaCode\from(bit.source, "tostring(",bit_lua,")")
                    lua\append bit_lua

                if string_buffer ~= "" or #lua.bits == 0
                    if #lua.bits > 0 then lua\append ".."
                    lua\append string_buffer\as_lua!

                if #lua.bits > 1
                    lua\parenthesize!
                return lua

            when "List", "Dict"
                lua = LuaCode\from tree.source, "#{tree.type}{"
                i = 1
                sep = ''
                while i <= #tree
                    item = tree[i]
                    if item.type == "Block"
                        break
                    lua\append sep
                    if item.type == "Comment"
                        lua\append compile(item), "\n"
                        sep = ''
                    else
                        item_lua = compile(item)
                        lua\append item_lua
                        sep = ', '
                    i += 1
                lua\append "}"
                -- List/dict comprehenstion
                if i <= #tree
                    lua = LuaCode\from tree.source, "(function()\n    local comprehension = ", lua
                    if tree.type == "List"
                        lua\append "\n    local function add(x) comprehension[#comprehension+1] = x end"
                    else
                        lua\append "\n    local function #{("add 1 =")\as_lua_id!}(k, v) comprehension[k] = v end"
                    while i <= #tree
                        lua\append "\n    "
                        if tree[i].type == 'Block' or tree[i].type == 'Comment'
                            lua\append compile(tree[i])
                        elseif tree[i].type == "DictEntry"
                            entry_lua = compile(tree[i])
                            lua\append (entry_lua\text!\sub(1,1) == '[' and "comprehension" or "comprehension."), entry_lua
                        else
                            lua\append "comprehension[#comprehension+1] = ", compile(tree[i])
                        i += 1
                    lua\append "\n    return comprehension\nend)()"
                return lua

            when "DictEntry"
                key, value = tree[1], tree[2]
                key_lua = compile(key)
                value_lua = value and compile(value) or LuaCode\from(key.source, "true")
                key_str = match(key_lua\text!, [=[^["']([a-zA-Z_][a-zA-Z0-9_]*)['"]$]=])
                return if key_str and key_str\is_lua_id!
                    LuaCode\from tree.source, key_str,"=",value_lua
                elseif sub(key_lua\text!,1,1) == "["
                    -- NOTE: this *must* use a space after the [ to avoid freaking out
                    -- Lua's parser if the inner expression is a long string. Lua
                    -- parses x[[[y]]] as x("[y]"), not as x["y"]
                    LuaCode\from tree.source, "[ ",key_lua,"]=",value_lua
                else
                    LuaCode\from tree.source, "[",key_lua,"]=",value_lua
            
            when "IndexChain"
                lua = compile(tree[1])
                first_char = sub(lua\text!,1,1)
                if first_char == "{" or first_char == '"' or first_char == "["
                    lua\parenthesize!

                for i=2,#tree
                    key = tree[i]
                    key_lua = compile(key)
                    key_lua_str = key_lua\text!
                    lua_id = match(key_lua_str, "^['\"]([a-zA-Z_][a-zA-Z0-9_]*)['\"]$")
                    if lua_id and lua_id\is_lua_id!
                        lua\append ".#{lua_id}"
                    elseif sub(key_lua_str,1,1) == '['
                        -- NOTE: this *must* use a space after the [ to avoid freaking out
                        -- Lua's parser if the inner expression is a long string. Lua
                        -- parses x[[[y]]] as x("[y]"), not as x["y"]
                        lua\append "[ ",key_lua," ]"
                    else
                        lua\append "[",key_lua,"]"
                return lua

            when "Number"
                return LuaCode\from(tree.source, tostring(tree[1]))

            when "Var"
                return LuaCode\from(tree.source, (tree[1])\as_lua_id!)

            when "FileChunks"
                error("Can't convert FileChunks to a single block of lua, since each chunk's "..
                    "compilation depends on the earlier chunks")
            
            when "Comment"
                return LuaCode\from(tree.source, "-- ", (tree[1]\gsub('\n', '\n-- ')))
            
            when "Error"
                error("Can't compile errors")

            else
                error("Unknown type: #{tree.type}")

})

return compile