-
Notifications
You must be signed in to change notification settings - Fork 1
/
mab.lua
executable file
·299 lines (261 loc) · 8.64 KB
/
mab.lua
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
#!/usr/bin/env lua
package.path = package.path .. ';1 Parser/?.lua'
package.path = package.path .. ';1 Parser/Components/?.lua'
package.path = package.path .. ';2 Type Checker/?.lua'
package.path = package.path .. ';3 Translators/?.lua'
package.path = package.path .. ';4 Interpreter/?.lua'
package.path = package.path .. ';Text/?.lua'
local parser = require 'parser'
local typeChecker = require 'typechecker'
local toStackVM = require 'toStackVM'
local toGraphviz = require 'toGraphviz'
local interpreter = require 'stackVM'
local pt = require 'External.pt'
local common = require 'common'
local phases = {
parser = {
action = parser.parse,
name = 'Parser',
actionName = 'parsing',
inputName = 'source code',
abortOnFailure = 'Failed to generate AST from input.',
},
typeChecker = {
action = typeChecker.check,
name = 'Type Checker',
actionName = 'type checking',
inputName = 'AST',
version = 1052957823,
},
graphviz = {
action = toGraphviz.translate,
name = 'Graphviz AST',
actionName = 'generating GraphViz file',
inputName = 'AST',
version = 1052957823,
},
toStackVM = {
action = toStackVM.translate,
name = 'Stack VM',
actionName = 'generating Stack VM code',
inputName = 'AST',
version = 1052957823,
abortOnFailure = 'Failed to generate StackVM code from AST.',
},
interpreter = {
action = interpreter.execute,
name = 'Interpreter',
actionName = 'interpreting',
inputName = 'Stack VM code',
-- Prints 'starting' and then leaves space
-- for the program to output, then prints
-- the completion message.
separatedOutput = true,
version = 246844683,
},
}
function runPhase(phaseTable, phaseInput, parameters)
if not phaseInput then
io.stderr:write('\n'..phaseTable.name..' cannot start, no input.')
if parameters.abortOnFailure then
io.stderr:write(' Unable to continue.\n')
os.exit(1)
else
io.stderr:write '\n'
return
end
end
if parameters.verbose then
if not phaseTable.separatedOutput then
local extraBuffer = 14 - #phaseTable.name
io.write('\n'..phaseTable.name..'...'..(' '):rep(extraBuffer))
else
io.write('\n'..phaseTable.name..' starting...\n\n')
end
end
local start = os.clock()
local errorReporter, result, extra = phaseTable.action(phaseInput, parameters)
local success = result and errorReporter:count() == 0
local message
-- We allow only allow unified output in verbose mode.
if not phaseTable.separatedOutput and parameters.verbose then
message = (string.format('%s: %7.2f milliseconds.\n',
success and 'complete' or ' FAILED',
(os.clock() - start) * 1000))
-- If not in verbose mode, we only print the message if the phase failed,
-- and we print the whole thing at once, never in two pieces.
else
local extraBuffer = 12 - #phaseTable.name
message = string.format('\n'..(' '):rep(extraBuffer)..phaseTable.name..
' %s: %7.2f milliseconds.\n',
success and 'completed in' or 'FAILED after',
(os.clock() - start) * 1000)
end
if success and parameters.verbose then
io.write(message)
elseif not success then
io.stderr:write(message)
end
local mismatchAndFailure, mismatchAndSuccess = common.maybeCreateMismatchMessages(phaseInput, phaseTable)
if not success then
if mismatchAndFailure then
io.stderr:write(mismatchAndFailure..'\n\n')
end
errorReporter:outputErrors(parameters.subject, parameters.inputFile)
if parameters.abortOnFailure then
io.stderr:write('Unable to continue. '..parameters.abortOnFailure..'\n')
os.exit(1)
end
elseif mismatchAndSuccess then
io.stderr:write(mismatchAndSuccess..'\n\n')
end
return success, result, extra
end
if arg[1] ~= nil and (string.lower(arg[1]) == '--tests') then
print(common.poem(true))
arg[1] = nil
local lu = require 'External.luaunit'
testFrontend = require 'tests':init(parser.parse, typeChecker, toGraphviz, toStackVM, interpreter)
os.exit(lu.LuaUnit.run())
end
local parameters = { show = {}, poetic = true }
local awaiting_filename = false
local function readOption(argument)
if awaiting_filename then
local defaultInput = io.input()
local status, err = pcall(io.input, argument)
if not status then
io.stderr:write('Could not open file "' .. argument .. '"\n\tError: ' .. err..'\n')
os.exit(1)
else
parameters.inputFile = argument
parameters.subject = io.read 'a'
io.close()
end
io.input(defaultInput)
awaiting_filename = false
elseif argument == '--input' or argument == '-i' then
awaiting_filename = true
elseif argument == '--tests' then
io.stderr:write('--tests must be the first argument if it is being sent in.\n')
os.exit(1)
elseif argument == '--ast' or argument == '-a' then
parameters.show.AST = true
elseif argument == '--unpoetic' or argument == '-u' then
parameters.poetic = false
elseif argument == '--verbose' or argument == '-v' then
parameters.verbose = true
elseif argument == '--code' or argument == '-c' then
parameters.show.code = true
elseif argument == '--trace' or argument == '-t' then
parameters.show.trace = true
elseif argument == '--result' or argument == '-r' then
parameters.show.result = true
elseif argument == '--echo-input' or argument == '-e' then
parameters.show.input = true
elseif argument == '--graphviz' or argument == '-g' then
parameters.show.graphviz = true
elseif argument == '--pegdebug' or argument == '-p' then
parameters.pegdebug = true
elseif argument == '--stop-at-first-error' or argument == '-s' then
parameters.stopAtFirstError = true
else
io.stderr:write('Unknown argument ' .. argument .. '.\n')
os.exit(1)
end
end
for _, argument in ipairs(arg) do
if awaiting_filename then
readOption(argument)
elseif argument:find('^[-][-]') then
readOption(argument)
elseif argument:find('^[-]') then
local numOptions = #argument - 1
for i = 1, numOptions do
readOption('-'..argument:sub(i+1,i+1))
end
-- Let's assume this is a filename.
else
awaiting_filename = true
readOption(argument)
end
end
if awaiting_filename then
io.stderr:write('Specified -i, but no input file found.\n')
os.exit(1)
end
if parameters.verbose or parameters.poetic then
print(common.poem())
end
local subject = parameters.subject or io.read 'a'
if parameters.show.input then
print 'Input:'
print(subject)
end
local _, ast = runPhase(phases.parser, subject, parameters)
if parameters.show.AST then
print '\nAST:'
print(pt.pt(ast, {'version', 'tag', 'scope', 'parameters', 'type_', 'name', 'identifier', 'value', 'assignment', 'firstChild', 'op', 'child', 'secondChild', 'body', 'sentence', 'position'}))
end
local typeCheckerSuccess = runPhase(phases.typeChecker, ast, parameters)
if parameters.show.graphviz then
local success, graphviz = runPhase(phases.graphviz, ast, parameters)
if success then
local prefix = parameters.inputFile or 'temp'
local dotFileName = prefix .. '.dot'
local dotFile = io.open(dotFileName, 'w')
dotFile:write(graphviz)
dotFile:close()
local svgFileName = prefix .. '.svg'
local retries = 5
for i=1,retries do
local result = os.execute('dot ' .. '"' .. dotFileName .. '" -Tsvg -o "' .. svgFileName .. '"')
if result then
break
else
-- GraphViz has been known to crash on valid input.
io.stderr:write('GraphViz failure. Retry attempt '..i..' of '..retries..'...\n')
end
end
os.execute('"'.. svgFileName .. '" &')
else
io.stderr:write('GraphViz failed.')
if typeCheckerSuccess then
io.stderr:write ' Continuing...'
end
io.stderr:write '\n'
end
end
-- Bail out from type checking here, so we can run things in order,
-- but still show GraphViz.
if not typeCheckerSuccess then
return 1
end
local _, code = runPhase(phases.toStackVM, ast, parameters)
if parameters.show.code then
print '\nGenerated code:'
print(pt.pt(code))
end
local success, result, trace = runPhase(phases.interpreter, code, parameters)
if trace then
print '\nExecution trace:'
for k, v in ipairs(trace) do
print(k, v)
if trace.stack[k] then
for i = #trace.stack[k],1,-1 do
print('\t\t\t' .. tostring(trace.stack[k][i].value)..(trace.stack[k][i].base==i and ' -' or ''))
end
if #trace.stack[k] == 0 then
print '\t\t\t(empty)'
end
end
end
end
if parameters.show.result and result ~= nil then
print '\nResult:'
print(result)
end
-- Return 1 to indicate failure.
if not success then
return 1
end