-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
392 lines (332 loc) · 11 KB
/
main.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { Lexer, Token, colors } from "./token";
import { Parser } from "./parser";
import { genStart } from "./codegen";
import { Statement } from "./stmt";
import { Expression } from "./expr";
import { createWriteStream, readFile, truncate } from "fs";
import { spawn } from "child_process";
import { relative, resolve } from "path"
import { Type, alignTo, beginTagScope, getEnum, getPresentModule, i32, i64, myType, searchStruct, voidtype } from "./type";
import { cwd } from "process";
export enum fnType {
extern,
native,
}
var anon_count = 0;
//var localSize = 0;
var scopeDepth = 0;
//var locals: { name:string, offset:number, scope: number }[] = [];
class VariableScope {
variables: Variable[] = [];
constructor() {
this.variables = [];
}
}
var variable_scopes: VariableScope[] = [];
var globalstrings: { value: string }[] = [];
var globals: Variable[] = [];
var anon_strings: { value: Expression }[] = [];
var resolution_pass = true;
export function addAnonString(val: Expression) {
if (resolution_pass) return;
anon_strings.push({ value: val });
return anon_strings.length - 1;
}
var labels:Token[] = [];
export function pushLabel(name:Token) {
labels.push(name);
}
export function searchlabel(name:string) {
return labels.find((l)=> l.value === name );
}
export class Function {
name: string;
arity: number;
impilicit_arity: number;
type: fnType;
locals: Variable[];
localOffset: number;
body: Statement;
module_name: string;
data_type: Type;
constructor(
name: string,
type: fnType,
params: { name: string, datatype: Type }[],
locals: Variable[],
retType: Type
) {
this.name = name;
this.type = type;
this.locals = locals;
if (type === fnType.native) {
var i = 0;
params.forEach((p) => {
var arg_data_type:Type;
var pointerised = false;
if(p.datatype.size > 8) {
arg_data_type = new Type().newPointer(p.datatype);
pointerised = true;
} else {
arg_data_type = p.datatype
}
this.locals.splice(i, 0, new Variable().local(p.name, arg_data_type, pointerised));
i++;
})
}
if (retType.size > 8) {
this.locals.splice(0, 0, new Variable().local("", new Type().newPointer(retType)))
}
this.arity = params.length;
this.impilicit_arity = retType.size > 8 ? params.length + 1 : params.length;
this.data_type = new Type().newFunction(retType, params, type);
}
}
// export class Struct {
// name: string;
// size: number;
// members: { name: string, datatype: Type, default: Expression | undefined }[];
// is_union: boolean;
// module_name: string;
//
// constructor(name: string, isunion: boolean, members: { name: string, datatype: Type, default: Expression | undefined }[]) {
// this.name = name;
// this.size = 0;
// this.members = members;
// this.is_union = isunion;
// this.module_name = getPresentModule() as string;
// }
// }
export class Variable {
name: string;
scope: number;
offset: number;
datatype: Type;
is_global: boolean;
initializer?: Expression;
token:Token|undefined;
pointerised:boolean;
global(name: string, datatype: Type, initializer: Expression|undefined = undefined) {
this.name = name;
this.initializer = initializer;
this.datatype = datatype
return this;
}
local(name: string, datatype: Type, pointerised = false) {
this.name = name;
this.datatype = datatype;
this.pointerised = pointerised;
return this;
}
constructor(token:Token|undefined = undefined) {
if (currentFn === -1) this.is_global = true;
this.token = token;
this.scope = scopeDepth;
}
}
var functions: Function[] = [];
function checkVariableInCurrScope(name:string, tok: Token) {
for (let v of variable_scopes[0].variables) {
if (v.name == name) {
console.error(`${colors.yellow + relative(cwd(), tok.file_name) + colors.green}:${tok.line}:${tok.col} ${colors.red + `redeclaration variable ${name} previously declared at ${relative(cwd(), (v.token as Token).file_name)}:${v.token?.line}:${v.token?.col}`} ${colors.reset} `);
process.exit();
}
}
}
function addVariableInFn(name: string, type: Type, token:Token|undefined):Variable {
var variab = new Variable(token).local(name, type)
functions[currentFn].locals.push(variab);
variable_scopes[0].variables.push(variab);
return variab;
}
export function incLocalOffset(name: string, type: Type, token:undefined|Token, initializer?: Expression): Variable {
var dummy = new Variable();
dummy.datatype = type;
if (resolution_pass) return dummy;
if (name === "" || name === "_") {
if (resolution_pass && currentFn != -1) { } else {
name = "anon" + anon_count.toString(2);
anon_count++;
}
}
if (scopeDepth === 0) {
globals.push(new Variable(token).global(name, type, initializer));
return globals[globals.length - 1];
}
checkVariableInCurrScope(name, token as Token);
var added = addVariableInFn(name, type,token);
//console.error(added);
return added;
}
export function addGlobalString(value: string): number {
globalstrings.push({ value: value });
return globalstrings.length - 1;
}
// export function addGlobal(name: string, value: Expression | undefined, datatype: Type): void {
// globals.push(new );
// }
export function isResolutionPass() {
return resolution_pass;
}
export function getLocalOffset(name: string, tok: Token): { offset: number, datatype: Type, variable: Variable | undefined } {
var dummy = new Variable();
dummy.datatype = voidtype;
dummy.datatype.return_type = voidtype;
if (resolution_pass) return { offset: -10, datatype: voidtype, variable: dummy }
if (getEnum(name)) {
return { offset: -3, datatype: getEnum(name) as Type, variable: undefined }
}
if (searchStruct(name)) {
return { offset: -4, datatype: i64, variable: undefined };
}
for (let scope of variable_scopes) {
//console.error(scope.variables[0]);
for (let v of scope.variables) {
if (v.name === name) {
//console.error(v, "**", scope.variables.length, v.name === name, v.scope);
return { offset: 0, datatype: v.datatype, variable: v };
}
}
}
if (scopeDepth > 0) {
for (let v of functions[currentFn].locals.slice(0, functions[currentFn].impilicit_arity)) {
if (v.name === name) {
return { offset: 0, datatype: v.datatype, variable: v };
}
}
}
for (let i = 0; i < globals.length; i++) {
if (globals[i].name === name) {
return { offset: -2, datatype: globals[i].datatype, variable: globals[i] }
}
}
for (let i = functions.length - 1; i >= 0; i--) {
if (functions[i].name === name) {
return { offset: -1, datatype: functions[i].data_type, variable: undefined }
}
}
console.error(`${colors.yellow + relative(cwd(), tok.file_name) + colors.green}:${tok.line}:${tok.col} ${colors.red + `undefined variable ${name}`} ${colors.reset} `);
process.exit();
}
export function getFn(name: string): Function {
for (let i = functions.length - 1; i >= 0; i--) {
if (functions[i].name === name) {
return functions[i];
}
}
console.error(`undefined function ${name}`);
//console.error(functions);
process.exit(1);
}
export function fnExists(name: string): number {
var index = 0;
for (let fun of functions) {
if (fun.name === name) {
return index;
}
index++;
}
return -1;
}
export function pushFunction(name: string, params: { name: string, datatype: Type }[], type: fnType, retType: any): number {
var f = fnExists(name);
if (f >= 0) {
return f;
}
functions.push(new Function(name, type, params, [], retType));
return functions.length - 1;
}
var currentFn: number = -1;
export function setCurrentFuction(n: number) {
currentFn = n;
}
export function getcurrFn() {
return currentFn;
}
export function restoreFn(num: number) {
currentFn = num;
}
export function resetCurrentFunction(name: string, body: Statement, tokens?: Token[]) {
if (!resolution_pass) {
functions[currentFn].body = body;
}
//console.error(functions[currentFn].locals)
currentFn = -1;
}
export function beginScope() {
variable_scopes.splice(0, 0, new VariableScope());
scopeDepth++;
}
export function endScope() {
variable_scopes.splice(0, 1);
scopeDepth--;
}
var compiled_files: string[] = [];
function offsetLocalVariables(fn: Function) {
fn.localOffset = 0;
fn.locals.forEach((p) => {
fn.localOffset = alignTo(p.datatype.align, fn.localOffset);
fn.localOffset += p.datatype.size;
p.offset = fn.localOffset;
p.is_global = false;
})
//console.error(fn.name,fn.locals);
}
var parsers: Parser[] = [];
export function compile(path: string) {
return new Promise((resolv, reject) => {
var abs_path = resolve(path);
if (compiled_files.find((p) => p === abs_path)) {
resolv(true);
return;
}
readFile(path, { encoding: "utf-8" }, async (err, data) => {
if (err) {
throw err
} else {
compiled_files.push(abs_path);
if (err) {
console.error("failed to open file");
process.exit(1);
}
var lexer = new Lexer(data, abs_path);
var tokens = lexer.lex();
var parser = new Parser(tokens);
parsers.push(parser);
await parser.parse();
resolv(true);
}
})
})
}
if (process.argv.length < 3) {
console.error("Usage: make FILE=<file name>");
} else {
if (process.argv[2] === "") {
console.error("Usage: make FILE=<file name>");
process.exit();
}
truncate("./tmp.s", async () => {
resolution_pass = true;
beginTagScope();
await compile(process.argv[2]);
resolution_pass = false;
for (let p of parsers) {
p.reset();
await p.parse();
}
var bitstream = createWriteStream("./tmp.s", { flags: "a" });
var orig = console.log;
console.log = (data) => { bitstream.write(`${data}\n`); }
functions.forEach((fn) => {
offsetLocalVariables(fn);
})
genStart(globalstrings, globals, anon_strings, functions);
bitstream.end();
console.log = orig;
var proc = spawn("make", ["bin"]);
proc.on("exit", ()=>{
process.exit();
})
})
}