-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompIR0Translator.ts
199 lines (153 loc) · 5.04 KB
/
CompIR0Translator.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
import { CompIR1 } from "./CompIR1.ts";
import { Expression, JsonLang, Statement } from "./CompIR0.ts";
class LabelFactory {
private next = 0;
createLabel() {
const ret = "l" + this.next;
this.next += 1;
return ret;
}
}
interface JumpLabels {
continueLbl: string;
breakLbl: string;
}
function translateGeneral(stmt: Statement<Expression>,
labelFactory: LabelFactory,
jumps: JumpLabels | null ): CompIR1[] {
if (stmt === "break") { // dejar como ejercicio 00
if (!jumps?.breakLbl) {
throw new Error("Cant break from this context");
}
return [{ jmp: jumps?.breakLbl }];
}
else if (stmt === "continue") { // dejar como ejercicio 00
if (!jumps?.continueLbl) {
throw new Error("Cant break from this context");
}
return [{ jmp: jumps?.continueLbl }];
}
else if (stmt instanceof Array) {
const ir1 = new Array<CompIR1>();
ir1.push("enterBlock");
for (const individual_stmt of stmt) {
ir1.push(...translateGeneral(individual_stmt, labelFactory, jumps))
}
ir1.push("exitBlock")
return ir1;
}
else if ("if" in stmt) {
const if_list = stmt.if;
const ir1 = new Array<CompIR1>();
const labels = new Array<string>;
ir1.push("enterBlock");
if (if_list.length < 1) {
throw Error("If incorrecto")
}
for (let i = 0; i < if_list.length; i++) {
labels.push(labelFactory.createLabel());
}
if (stmt.else) {
labels.push(labelFactory.createLabel());
}
for (let i = 0; i < if_list.length; i++) {
const parte_if = if_list[i]
const operadores: [Expression, Expression] = [0, parte_if.cond];
const content = [
{cmpq: operadores},
{je: labels[i]},
...remove_external_block(translateGeneral(parte_if.then, labelFactory, jumps)),
{jmp: labels[labels.length-1]},
{lbl: labels[i]}
]
ir1.push(...content)
}
if (stmt.else) {
ir1.push(...remove_external_block(translateGeneral(stmt.else, labelFactory, jumps)))
ir1.push({lbl: labels[labels.length-1]})
} else {
const lbl_temp = ir1.pop()
ir1.pop()
ir1.push(lbl_temp?lbl_temp:{lbl: "ERROR"});
}
ir1.push("exitBlock");
return ir1;
} else if ("iterator" in stmt) {
const ir1 = new Array<CompIR1>();
ir1.push("enterBlock");
ir1.push({declare: stmt.iterator, value: stmt.from});
const cond:Expression = {binop: "<=", argl: stmt.iterator, argr: stmt.to};
let step_value:Expression = 1;
if (stmt.step) {
step_value = stmt.step;
if (stmt.step < 0) {
cond.binop = ">=";
}
}
const doOfWhile = (stmt.do instanceof Array)?stmt.do:[stmt.do];
doOfWhile.push({set: stmt.iterator, value: {binop: "+", argl: stmt.iterator, argr: step_value}})
const while_constr = {while: cond, do: doOfWhile};
const while_instructions = remove_external_block(translateGeneral(while_constr, labelFactory, jumps));
ir1.push(...while_instructions)
ir1.push("exitBlock");
return ir1;
}
else if ("until" in stmt) {
const ir1 = new Array<CompIR1>();
ir1.push("enterBlock")
ir1.push(...remove_external_block(translateGeneral(stmt.do, labelFactory, jumps)));
const doOfWhile = (stmt.do instanceof Array)?stmt.do:[stmt.do];
const expresion: Expression = {unop: "!", arg:stmt.until}
const while_constr = {while: expresion, do: doOfWhile};
const while_instructions = remove_external_block(translateGeneral(while_constr, labelFactory, jumps));
ir1.push(...while_instructions);
ir1.push("exitBlock");
return ir1;
}
else if ("while" in stmt) { // dejar como ejercicio 00
const beginWhile = labelFactory.createLabel();
const endWhile = labelFactory.createLabel();
const jump_lbls = {breakLbl: endWhile,
continueLbl: beginWhile};
const bloque_while = translateGeneral(stmt.do, labelFactory, jump_lbls);
const contenidoWhile = remove_external_block(bloque_while);
return [
"enterBlock",
{ lbl: beginWhile },
{ cmpq: [0,stmt.while] },
{ je: endWhile },
...contenidoWhile,
{ jmp: beginWhile },
{ lbl: endWhile },
"exitBlock"
];
}
else {
return [stmt];
}
}
export function translate(code: JsonLang): CompIR1[] {
const labelFactory = new LabelFactory();
return code.flatMap((c) => {
if (typeof c === "object" && "function" in c) {
const afterFunction = labelFactory.createLabel();
return [
{ jmp: afterFunction },
{ lbl: c.function },
{ functionIntro: c.args },
...remove_external_block(translateGeneral(c.block, labelFactory, null)),
"functionEnd",
{ lbl: afterFunction },
];
} else {
return translateGeneral(c, labelFactory, null);
}
});
}
function remove_external_block(stmt: CompIR1[]): CompIR1[] {
if (stmt[0] == "enterBlock" && stmt[stmt.length-1] == "exitBlock") {
stmt.shift()
stmt.pop()
}
return stmt;
}