Skip to content

Commit 1b33768

Browse files
committed
added separate command line functions
1 parent 8f7d405 commit 1b33768

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/funktion.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,43 @@ const help = `Funktion compiler
88
99
Syntax: funktion <filename> <outputType>
1010
11-
Prints to stdout according to <outputType>, which must be one of:
11+
Available compiler phases (outputType):
12+
parsed - Show raw parser output
13+
analyzed - Display analyzed AST
14+
optimized - Show optimized AST
15+
js - Generate JavaScript code
1216
13-
parsed a message that the program was matched ok by the grammar
14-
analyzed the statically analyzed representation
15-
optimized the optimized semantically analyzed representation
16-
js the translation to JavaScript
17+
Or run individual phases:
18+
parse - Only run the parser
19+
analyze - Run parser + analyzer
20+
optimize - Run up to optimizer
21+
generate - Full compilation pipeline
1722
`;
1823

24+
// Modify the compileFromFile function
1925
async function compileFromFile(filename, outputType) {
2026
try {
2127
const buffer = await fs.readFile(filename);
22-
const compiled = compile(buffer.toString(), outputType);
23-
console.log(stringify(compiled, "kind") || compiled);
28+
let result;
29+
30+
switch(outputType.toLowerCase()) {
31+
case 'parse':
32+
result = parse(buffer.toString());
33+
break;
34+
case 'analyze':
35+
result = analyze(parse(buffer.toString()));
36+
break;
37+
case 'optimize':
38+
result = optimize(analyze(parse(buffer.toString())));
39+
break;
40+
case 'generate':
41+
result = generate(optimize(analyze(parse(buffer.toString()))));
42+
break;
43+
default: // Maintain original output types
44+
result = compile(buffer.toString(), outputType);
45+
}
46+
47+
console.log(stringify(result, "kind") || result);
2448
} catch (e) {
2549
console.error(`\u001b[31m${e}\u001b[39m`);
2650
process.exitCode = 1;

0 commit comments

Comments
 (0)