1 --*-lua-*- Set as a metalua file because it requires some metalua libs
3 --require 'verbose_require'
5 require 'metalua.compiler'
9 AST_COMPILE_ERROR_NUMBER = -1
10 RUNTIME_ERROR_NUMBER = -3
11 BYTECODE_SYNTHESE_ERROR_NUMBER = -100
13 -{ extension 'match' }
18 local acc_chunk = |kind| function (arg)
19 table.insert (chunks, { tag=kind, arg })
24 { short = 'f', long = 'file', type = 'string', action = acc_chunk 'File',
25 usage = 'load a file to compile and/or run'
27 { short = 'l', long = 'library', type = 'string', action = acc_chunk 'Library',
28 usage = 'load a libary from the standard paths'
30 { short = 'e', long = 'literal', type = 'string', action = acc_chunk 'Literal',
31 usage = 'load a literal piece of source code'
33 -- What to do with chunks
34 { short = 'o', long = 'output', type = 'string',
35 usage = 'set the target name of the next compiled file'
37 { short = 'x', long = 'run', type = 'boolean',
38 usage = 'execute the compiled file instead of saving it (unless -o is also used)'
40 { short = 'i', long = 'interactive', type = 'boolean',
41 usage = 'run an interactive loop after having run other files'
44 { short = 'v', long = 'verbose', type = 'boolean',
45 usage = 'verbose mode'
47 { short = 'a', long = 'print-ast', type = 'boolean',
48 usage = 'print the AST resulting from file compilation'
50 { short = 'b', long = 'metabugs', type = 'boolean',
51 usage = 'show syntax errors as compile-time execution errors'
53 { short = 's', long = 'sharpbang', type = 'string',
54 usage = 'set a first line to add to compiled file, typically "#!/bin/env mlr"'
56 { long = 'no-runtime', type = 'boolean',
57 usage = "prevent the automatic requirement of metalua runtime"
59 { long = '', short = 'p', type = '*',
60 action= function (newargs) runargs=table.icat(runargs, newargs) end,
61 usage = "pass all remaining arguments to the program"
65 Compile and/or execute metalua programs. Parameters passed to the
66 compiler should be prefixed with an option flag, hinting what must be
67 done with them: take tham as file names to compile, as library names
68 to load, as parameters passed to the running program... When option
69 flags lack, metalua tries to adopt a "Do What I Mean" approach:
71 - if no code (no library, no literal expression and no file) is
72 specified, the first flag-less parameter is taken as a file name to
75 - if no code and no parameter is passed, an interactive loop is
78 - if a target file is specified with --output, the program is not
79 executed by default, unless a --run flag forces it to. Conversely,
80 if no --output target is specified, the code is run unless ++run
85 INIT_COMPILATION_RING = [[require 'metalua.compiler']]
87 local function main (...)
89 local cfg = parser(...)
91 -------------------------------------------------------------------
92 -- Print messages if in verbose mode
93 -------------------------------------------------------------------
94 local function verb_print (fmt, ...)
96 return printf ("[ "..fmt.." ]", ...)
100 -------------------------------------------------------------------
101 -- If there's no chunk but there are params, interpret the first
102 -- param as a file name.
103 if #chunks==0 and cfg.params then
104 local the_file = table.remove(cfg.params, 1)
105 verb_print("Param %q considered as a source file", the_file)
106 chunks = { `File{ the_file } }
109 -------------------------------------------------------------------
110 -- If nothing to do, run REPL loop
111 if #chunks==0 and cfg.interactive==nil then
112 verb_print "Nothing to compile nor run, force interactive loop"
117 -------------------------------------------------------------------
118 -- Run if asked to, or if no --output has been given
119 -- if cfg.run==false it's been *forced* to false, don't override.
120 if cfg.run==nil and not cfg.output then
121 verb_print("No output file specified; I'll run the program")
127 -------------------------------------------------------------------
128 -- Get ASTs from sources
130 for x in values(chunks) do
131 verb_print("Compiling %s", table.tostring(x))
134 | `Library{ l } -> st, ast = true, `Call{ `Id 'require', `String{ l } }
136 local ring = springs.new()
137 ring:dostring (INIT_COMPILATION_RING)
138 st, ast = ring:pcall('mlc.ast_of_luastring', e, 'literal')
140 local ring = springs.new()
141 ring:dostring (INIT_COMPILATION_RING)
142 st, ast = ring:pcall('mlc.ast_of_luafile', f, '@'..f) -- FIXME: handle '-'
143 -- Isolate each file in a separate fenv
144 ast = +{ function (...) -{ast} end (...) }
147 if not st then os.exit (AST_COMPILE_ERROR_NUMBER) end
149 table.insert(code, ast)
151 -- The last file returns the whole chunk's result
153 local c = table.shallow_copy(last_file)
154 last_file <- `Return{ origin = c.origin, c }
157 -------------------------------------------------------------------
159 if cfg['print-ast'] then
160 verb_print "Resulting AST:"
161 for x in values(code) do
162 printf("--- AST From %s: ---", table.tostring(x.origin, 'nohash'))
163 --if x.origin.tag=='File' then x=x[1][2][1] end
164 table.print(x, 80, 'nohash')
168 -------------------------------------------------------------------
169 -- Insert runtime loader
170 if cfg['no-runtime'] then
171 verb_print "Prevent insertion of command \"require 'metalua.runtime'\""
173 table.insert(code, 1, +{require'metalua.runtime'})
176 -- FIXME: check for failures
177 -- FIXME: handle metabugs
178 local bytecode = mlc.luacstring_of_ast (code)
181 -------------------------------------------------------------------
182 -- Insert #!... command
183 if cfg.sharpbang then
184 verb_print ("Adding sharp-bang directive %q", cfg.sharpbang)
185 if not cfg.sharpbang:strmatch'\n$' then cfg.sharpbang=cfg.sharpbang..'\n' end
186 bytecode = cfg.sharpbang..bytecode
189 -------------------------------------------------------------------
193 verb_print ("Saving to file %q", cfg.output)
194 local file, err_msg = io.open(cfg.output, 'wb')
195 if not file then error("can't open output file: "..err_msg) end
198 pcall(os.execute, 'chmod a+x "'..cfg.output..'"')
201 -------------------------------------------------------------------
205 local f = mlc.function_of_luacstring (bytecode)
207 -- FIXME: isolate execution in a ring
208 -- FIXME: check for failures
209 local st, msg = pcall(f, unpack (runargs))
212 os.exit(RUNTIME_ERROR_NUMBER)
216 -------------------------------------------------------------------
218 if cfg.interactive then
220 print "*** !!! Interactive loop not implemented !!!"