This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from fab13n/metalua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetalua.mlua
More file actions
257 lines (224 loc) · 8.73 KB
/
metalua.mlua
File metadata and controls
257 lines (224 loc) · 8.73 KB
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
--*-lua-*- Set as a metalua file because it requires some metalua libs
--require 'verbose_require'
require 'metalua.compiler'
require 'metalua.clopts'
require 'metalua.mlc_xcall'
AST_COMPILE_ERROR_NUMBER = -1
RUNTIME_ERROR_NUMBER = -3
BYTECODE_SYNTHESE_ERROR_NUMBER = -100
-{ extension 'match' }
local chunks = { }
local runargs = { }
local acc_chunk = |kind| |arg| table.insert (chunks, { tag=kind, arg })
parser = clopts {
-- Chunk loading
{ short = 'f', long = 'file', type = 'string', action = acc_chunk 'File',
usage = 'load a file to compile and/or run'
},
{ short = 'l', long = 'library', type = 'string', action = acc_chunk 'Library',
usage = 'load a libary from the standard paths'
},
{ short = 'e', long = 'literal', type = 'string', action = acc_chunk 'Literal',
usage = 'load a literal piece of source code'
},
-- What to do with chunks
{ short = 'o', long = 'output', type = 'string',
usage = 'set the target name of the next compiled file'
},
{ short = 'x', long = 'run', type = 'boolean',
usage = 'execute the compiled file instead of saving it (unless -o is also used)'
},
{ short = 'i', long = 'interactive', type = 'boolean',
usage = 'run an interactive loop after having run other files'
},
-- Advanced stuff
{ short = 'v', long = 'verbose', type = 'boolean',
usage = 'verbose mode'
},
{ short = 'a', long = 'print-ast', type = 'boolean',
usage = 'print the AST resulting from file compilation'
},
{ short = 'A', long = 'print-ast-lineinfo', type = 'boolean',
usage = 'print the AST resulting from file compilation, including lineinfo data'
},
{ short = 'S', long = 'print-src', type = 'boolean',
usage = 'print the AST resulting from file compilation, as re-gerenerated sources'
},
{ short = 'b', long = 'metabugs', type = 'boolean',
usage = 'show syntax errors as compile-time execution errors'
},
{ short = 's', long = 'sharpbang', type = 'string',
usage = 'set a first line to add to compiled file, typically "#!/bin/env mlr"'
},
{ long = 'no-runtime', type = 'boolean',
usage = "prevent the automatic requirement of metalua runtime"
},
{ long = '', short = 'p', type = '*',
action= function (newargs) runargs=table.icat(runargs, newargs) end,
usage = "pass all remaining arguments to the program"
},
usage=[[
Compile and/or execute metalua programs. Parameters passed to the
compiler should be prefixed with an option flag, hinting what must be
done with them: take tham as file names to compile, as library names
to load, as parameters passed to the running program... When option
flags are absent, metalua tries to adopt a "Do What I Mean" approach:
- if no code (no library, no literal expression and no file) is
specified, the first flag-less parameter is taken as a file name to
load.
- if no code and no parameter is passed, an interactive loop is
started.
- if a target file is specified with --output, the program is not
executed by default, unless a --run flag forces it to. Conversely,
if no --output target is specified, the code is run unless ++run
forbids it.
]]}
local function main (...)
local cfg = parser(...)
-------------------------------------------------------------------
-- Print messages if in verbose mode
-------------------------------------------------------------------
local function verb_print (fmt, ...)
if cfg.verbose then
return printf ("[ "..fmt.." ]", ...)
end
end
if cfg.verbose then
verb_print("raw options: %s", table.tostring(cfg))
end
-------------------------------------------------------------------
-- If there's no chunk but there are params, interpret the first
-- param as a file name.
if #chunks==0 and cfg.params then
local the_file = table.remove(cfg.params, 1)
verb_print("Param %q considered as a source file", the_file)
chunks = { `File{ the_file } }
end
-------------------------------------------------------------------
-- If nothing to do, run REPL loop
if #chunks==0 and cfg.interactive==nil then
verb_print "Nothing to compile nor run, force interactive loop"
cfg.interactive=true
end
-------------------------------------------------------------------
-- Run if asked to, or if no --output has been given
-- if cfg.run==false it's been *forced* to false, don't override.
if cfg.run==nil and not cfg.output then
verb_print("No output file specified; I'll run the program")
cfg.run = true
end
local code = { }
-------------------------------------------------------------------
-- Get ASTs from sources
mlc.metabugs = cfg.metabugs
local last_file
for x in values(chunks) do
verb_print("Compiling %s", table.tostring(x))
local st, ast
match x with
| `Library{ l } -> st, ast = true, `Call{ `Id 'require', `String{ l } }
| `Literal{ e } -> st, ast = mlc_xcall.client_literal (e)
| `File{ f } ->
st, ast = mlc_xcall.client_file (f)
-- Isolate each file in a separate fenv
if st then
ast = +{ function (...) -{ast} end (...) }
ast.source = '@'..f -- TODO [EVE]
code.source = '@'..f -- TODO [EVE]
last_file = ast
end
end
if not st then
printf ("Cannot compile %s:\n%s", table.tostring(x), ast or "no msg")
os.exit (AST_COMPILE_ERROR_NUMBER)
end
ast.origin = x
table.insert(code, ast)
end
-- The last file returns the whole chunk's result
if last_file then
local c = table.shallow_copy(last_file)
last_file <- `Return{ source = c.source, c }
end
-------------------------------------------------------------------
-- AST printing
if cfg['print-ast'] or cfg['print-ast-lineinfo'] then
verb_print "Resulting AST:"
for x in ivalues(code) do
printf("--- AST From %s: ---", table.tostring(x.source, 'nohash'))
if x.origin and x.origin.tag=='File' then x=x[1][1][2][1] end
if cfg['print-ast-lineinfo'] then table.print(x, 80, "indent1")
else table.print(x, 80, 'nohash') end
end
end
-------------------------------------------------------------------
-- Source printing
if cfg['print-src'] then
verb_print "Resulting sources:"
require 'metalua.ast_to_string'
for x in ivalues(code) do
printf("--- Source From %s: ---", table.tostring(x.source, 'nohash'))
if x.origin and x.origin.tag=='File' then x=x[1][1][2][1] end
print (ast_to_string (x))
end
end
-- FIXME: canonize/check AST
-------------------------------------------------------------------
-- Insert runtime loader
if cfg['no-runtime'] then
verb_print "Prevent insertion of command \"require 'metalua.runtime'\""
else
table.insert(code, 1, +{require'metalua.runtime'})
end
local bytecode = mlc.luacstring_of_ast (code)
code = nil
-------------------------------------------------------------------
-- Insert #!... command
if cfg.sharpbang then
local shbang = cfg.sharpbang
verb_print ("Adding sharp-bang directive %q", shbang)
if not shbang :strmatch'^#!' then shbang = '#!' .. shbang end
if not shbang :strmatch'\n$' then shbang = shbang .. '\n' end
bytecode = shbang .. bytecode
end
-------------------------------------------------------------------
-- Save to file
if cfg.output then
-- FIXME: handle '-'
verb_print ("Saving to file %q", cfg.output)
local file, err_msg = io.open(cfg.output, 'wb')
if not file then error("can't open output file: "..err_msg) end
file:write(bytecode)
file:close()
if cfg.sharpbang and os.getenv "OS" ~= "Windows_NT" then
pcall(os.execute, 'chmod a+x "'..cfg.output..'"')
end
end
-------------------------------------------------------------------
-- Run compiled code
if cfg.run then
verb_print "Running"
local f = mlc.function_of_luacstring (bytecode)
bytecode = nil
-- FIXME: isolate execution in a ring
-- FIXME: check for failures
runargs = table.icat(cfg.params or { }, runargs)
local function print_traceback (errmsg)
return errmsg .. '\n' .. debug.traceback ('',2) .. '\n'
end
local st, msg = xpcall(|| f(unpack (runargs)), print_traceback)
if not st then
io.stderr:write(msg)
os.exit(RUNTIME_ERROR_NUMBER)
end
end
-------------------------------------------------------------------
-- Run REPL loop
if cfg.interactive then
verb_print "Starting REPL loop"
require 'metalua.metaloop'
metaloop.run()
end
verb_print "Done"
end
main(...)