-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
102 lines (77 loc) · 2.86 KB
/
main.ts
File metadata and controls
102 lines (77 loc) · 2.86 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
import { Lex } from "./Lex.ts";
import { Parse } from "./Parse.ts";
import { Resolve } from "./Resolve.ts";
import { LabelLoops } from "./LabelLoops.ts";
import { SymbolTable } from "./Symbols.ts";
import { TypeCheck } from "./TypeCheck.ts";
import { TackyGen } from "./TackyGen.ts";
import { CodeGen } from "./CodeGen.ts";
import { AssemblySymbolTable } from "./AssemblySymbols.ts";
import { ReplacePseudoes } from "./ReplacePseudos.ts";
import { InstructionFixup } from "./InstructionFixup.ts";
import { Emit } from "./Emit.ts";
import { TypeTable } from "./TypeTable.ts";
if (import.meta.main) {
// Compiler test suite passes the flag in first (if present) and then the
// file path. If there is no flag then the first element is the filepath
const inputFilePath = Deno.args.length == 1 ? Deno.args[0] : Deno.args[1];
const preprocessorOutputFilePath = inputFilePath.replace(".c", ".i");
await new Deno.Command("gcc", {
args: [
"-E",
"-P",
inputFilePath,
"-o",
preprocessorOutputFilePath,
],
}).output();
const source = await Deno.readTextFile(preprocessorOutputFilePath);
let result;
const lex = new Lex();
result = lex.lex(source);
if (Deno.args[0] === "--lex") Deno.exit();
const parse = new Parse();
result = parse.parse(result);
if (Deno.args[0] === "--parse") Deno.exit();
const resolve = new Resolve();
result = resolve.resolve(result);
const labelLoops = new LabelLoops();
result = labelLoops.labelLoops(result);
const symbolTable = new SymbolTable();
const typeTable = new TypeTable();
const typecheck = new TypeCheck(symbolTable, typeTable);
result = typecheck.typeCheck(result);
if (Deno.args[0] === "--validate") Deno.exit();
const tackyGen = new TackyGen(symbolTable, typeTable);
result = tackyGen.generate(result);
if (Deno.args[0] === "--tacky") Deno.exit();
const assemblySymbolTable = new AssemblySymbolTable();
const codeGen = new CodeGen(symbolTable, assemblySymbolTable, typeTable);
result = codeGen.generate(result);
const replacePseudoes = new ReplacePseudoes(assemblySymbolTable);
result = replacePseudoes.replacePseudos(result);
const instructionFixup = new InstructionFixup(assemblySymbolTable);
result = instructionFixup.fixupProgram(result);
if (Deno.args[0] === "--codegen") Deno.exit();
const emit = new Emit(assemblySymbolTable);
result = emit.emit(result);
const assemblyFilePath = inputFilePath.replace(".c", ".s");
await Deno.writeTextFile(assemblyFilePath, result);
const objectFilePath = inputFilePath.replace(".c", ".o");
await new Deno.Command("gcc", {
args: [
"-c",
assemblyFilePath,
"-o",
objectFilePath,
],
}).output();
const binaryFilePath = inputFilePath.replace(".c", "");
await new Deno.Command("gcc", {
args: [
objectFilePath,
"-o",
binaryFilePath,
],
}).output();
}