-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug-test-case.ts
223 lines (208 loc) · 8.04 KB
/
debug-test-case.ts
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
import { file as tmpFile } from 'tmp-promise';
import { testPrograms, testModules } from './test-cases';
import { passed } from './test-cases';
import produceProgramInfo from './produceProgramInfo';
import writeSvg from './util/graph/writeSvg';
import writeTempFile from './util/writeTempFile';
import { prompt } from 'inquirer';
import * as dot from 'graphlib-dot';
import { toDotFile } from './parser-lib/parse';
import { toString as typeErrorToString } from './TypeError';
import * as chalk from 'chalk';
import * as commander from 'commander';
import annotateSource from './annotateSource';
import * as deepEqual from 'deep-equal';
import renderParseError from './parser-lib/renderParseError';
import { mplLoader } from './mpl-loader';
(async () => {
// Commander is dumb
let args = process.argv;
const buildBinaries = !args.includes('--no-build-binaries');
args = args.filter(arg => arg != '--no-build-binaries');
commander
.arguments('<test_name>')
.allowUnknownOption()
.option('--no-execute', "Only produce binaries, don't execute them")
.option(
'--skip-backends [backends]',
"Don't run [backend]",
(val, memo) => {
(memo as any).push(val);
return memo;
},
[]
)
.parse(args);
const testCase = testPrograms.find(c => c.name == commander.args[0]);
if (!testCase) {
const testModule = testModules.find(c => c.name == commander.args[0]);
if (!testModule) {
console.log(`Could not find a test case named "${commander.args[0]}"`);
return;
}
// TODO: Refactor to produceModuleInfo?
const errors: any[] = [];
const jsSource = await mplLoader(testModule.source, {
emitError: e => {
debugger;
errors.push(e);
},
});
if (errors.length != 0) {
console.log(`Errors in module:`);
errors.forEach(e => {
console.log(e.stack);
});
return;
}
console.log(jsSource);
return;
}
const programInfo = await produceProgramInfo(
testCase.source,
testCase.stdin ? testCase.stdin : '',
{
includeExecutionResult: commander.execute,
buildBinaries,
skipBackends: commander.skipBackends,
}
);
// TODO: Unify and improve error printing logic with test-utils and produceProgramInfo
if ('kind' in programInfo) {
console.log('Failed to lex');
return;
}
if ('parseErrors' in programInfo) {
console.log(`Failed to parse:`);
let errorString: string = '';
programInfo.parseErrors.forEach(e => {
errorString += renderParseError(e, testCase.source);
});
console.log(errorString);
return;
}
if ('typeErrors' in programInfo) {
let errorString: string = '';
programInfo.typeErrors.forEach(e => {
errorString += annotateSource(
testCase.source,
(e as any).sourceLocation,
typeErrorToString(e as any)
);
});
console.log(errorString);
return;
}
console.log(`Mpl: ${(await writeTempFile(testCase.source, 'mpl', 'mpl')).path}`);
console.log(
`Tokens: ${
(await writeTempFile(JSON.stringify(programInfo.tokens, null, 2), 'tokens', 'json'))
.path
}`
);
const astFile = await writeTempFile(JSON.stringify(programInfo.ast, null, 2), 'ast', 'json');
const astInfo = `Ast: ${astFile.path}`;
const astMismatch = 'ast' in testCase && !deepEqual(testCase.ast, programInfo.ast);
if (astMismatch) {
console.log(chalk.red(astInfo));
} else {
console.log(astInfo);
}
const dotText = dot.write(toDotFile(programInfo.ast));
const svgFile = await tmpFile({ template: 'ast-XXXXXX.svg', dir: '/tmp' });
await writeSvg(dotText, svgFile.path);
console.log(`Ast SVG: ${svgFile.path}`);
console.log(
`Structure: ${(await writeTempFile(programInfo.structure, 'structure', 'txt')).path}`
);
console.log(
`Three Address Code: ${
(await writeTempFile(programInfo.threeAddressCode, 'three-address-code', 'txt')).path
}`
);
const roundTripParsedPath = (
await writeTempFile(
JSON.stringify(programInfo.threeAddressRoundTrip, null, 2),
'round-trip-parsed',
'txt'
)
).path;
const roundTripSuccess =
!Array.isArray(programInfo.threeAddressRoundTrip) &&
!('kind' in programInfo.threeAddressRoundTrip);
if (roundTripSuccess) {
console.log(`Three Address Code Round Trip Parse: ${roundTripParsedPath}`);
} else {
console.log(chalk.red(`Three Address Code Round Trip Parse: ${roundTripParsedPath}`));
}
console.log('\nBackends:');
programInfo.backendResults.forEach(({ name, compilationResult, executionResults }) => {
const testPassed = executionResults.every(r => passed(testCase, r));
if (testPassed) {
console.log(` ${name}:`);
} else {
console.log(chalk.red(` ${name}:`));
}
if ('error' in compilationResult) {
console.log(chalk.red(` Compilation Failed: ${compilationResult.error}`));
if ('intermediateFile' in compilationResult) {
console.log(
chalk.red(
` Intermediate File: ${compilationResult.intermediateFile.path}`
)
);
}
} else {
console.log(` Source: ${compilationResult.sourceFile.path}`);
console.log(` Binary: ${compilationResult.binaryFile.path}`);
if (compilationResult.threeAddressCodeFile) {
console.log(
` Three Address Code: ${compilationResult.threeAddressCodeFile.path}`
);
}
executionResults.forEach(r => {
console.log(` Executor: ${r.executorName}`);
if ('error' in r) {
// TODO: put debug instructions too
console.log(chalk.red(` Execution Failed: ${r.error}`));
} else {
let lg =
testCase.exitCode == r.exitCode
? s => console.log(s)
: s => console.log(chalk.red(s));
lg(` Expected Exit Code: ${testCase.exitCode}`);
lg(` Actual Exit Code: ${r.exitCode}`);
if (testCase.stdout) {
lg =
testCase.stdout == r.stdout
? s => console.log(s)
: s => console.log(chalk.red(s));
lg(` Expected stdout: ${testCase.stdout}`);
lg(` Actual stdout: ${r.stdout}`);
}
console.log(` Run: ${r.runInstructions}`);
console.log(` Debug: ${r.debugInstructions}`);
console.log('');
}
});
}
});
console.log('Interpreter:');
const log =
'error' in programInfo.interpreterResults ||
testCase.exitCode == programInfo.interpreterResults.exitCode
? s => console.log(s)
: s => console.log(chalk.red(s));
if ('error' in programInfo.interpreterResults) {
log(` Error: ${programInfo.interpreterResults.error}`);
} else {
log(` Expected Exit Code: ${testCase.exitCode}`);
log(` Actual Exit Code: ${programInfo.interpreterResults.exitCode}`);
}
await prompt({
type: 'confirm',
message:
'Holding temporary files. Press Enter when you are done to exit. Temporary files may be removed.',
name: 'unused',
});
})();