]> git.lizzy.rs Git - metalua.git/blob - src/compiler/bootstrap.lua
pulled from repo
[metalua.git] / src / compiler / bootstrap.lua
1 -- This only serves in the bootstrapping process, it isn't
2 -- included in the final compiler. When compiled with std.lua,
3 -- mlp and bytecode modules, it is able to compile metalua
4 -- sources into .luac bytecode files.
5 -- It allows to precompile files such as
6
7 print ' *** LOAD BOOTSTRAP with fake MLC'
8
9 require 'std'
10 require 'bytecode'
11 require 'mlp'
12
13 mlc = {}
14 mlc.metabugs = false
15
16 function mlc.function_of_ast (ast)
17    local proto        = bytecode.metalua_compile (ast)
18    local dump         = bytecode.dump_string (proto)
19    local func         = undump(dump)
20    return func
21 end
22
23 local function compile_file (src_filename)
24    local src_file     = io.open (src_filename, 'r')
25    local src          = src_file:read '*a'; src_file:close()
26    local lx           = mlp.lexer:newstream (src)
27    local ast          = mlp.chunk (lx)
28    local proto        = bytecode.metalua_compile (ast)
29    local dump         = bytecode.dump_string (proto)
30    local dst_filename = src_filename:gsub ("%.mlua$", ".luac")
31    local dst_file     = io.open (dst_filename, 'wb')
32    dst_file:write(dump)
33    dst_file:close()
34 end
35
36
37 for _, x in ipairs{...} do compile_file (x) end
38