-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
48 lines (43 loc) · 1.33 KB
/
index.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
/**
* Helpers for converting AST nodes to Quads
*/
import type { RA } from '../utils/types.js';
import type { AstNode } from '../ast/definitions/AstNode.js';
import { TempVariable } from './definitions/IdQuad.js';
import type { Quad } from './definitions/index.js';
import type { Register } from './definitions/Register.js';
export type QuadsContext = {
readonly optimize: boolean;
readonly requestLabel: () => string;
readonly requestString: (value: string) => string;
readonly requestTemp: () => TempVariable;
readonly getTempCount: () => number;
readonly declareVar: (name: string) => TempVariable;
readonly returnLabel: string;
readonly requestTempRegister: () => Register;
};
export function toQuads(ast: AstNode, optimize = true): RA<Quad> {
let labelIndex = -1;
return ast.toQuads({
optimize,
requestLabel() {
labelIndex += 1;
return `lbl_${labelIndex}`;
},
requestTemp() {
throw new Error('not Implemented');
},
getTempCount: () => 0,
requestString() {
throw new Error('Not Implemented');
},
requestTempRegister() {
throw new Error('Not Implemented');
},
declareVar(name: string) {
return new TempVariable(name);
},
returnLabel: 'ERROR: Return label is not defined',
});
}
export const formatTemp = (index: number) => `tmp${index}`;