This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mCExecutinTreeGenerator.ts
245 lines (218 loc) · 6.63 KB
/
mCExecutinTreeGenerator.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
import antlr4 = require("antlr4");
import { InputStream, CommonTokenStream } from "antlr4";
import { mCLexer } from "./parser/mCLexer";
import { mCParser } from "./parser/mCParser";
import { mCListener } from "./parser/mCListener";
import * as fs from "fs";
import { ENodeType, TNode, EOperationType } from "./ExecutionTree";
// tslint:disable-next-line: class-name
export class mCExecutinTreeGenerator {
private static readonly Listener = (class extends mCListener {
public tree: TNode;
public exitStatement(ctx) {
ctx.res = ctx.children.map(x => x.res).filter(x => x !== undefined);
if (ctx.res.length === 1)
ctx.res = ctx.res[0];
}
public exitStFor(ctx) {
const then = ctx.statement().res;
const node: TNode = {
type: ENodeType.for,
then,
};
// removing `for` `(` and `)` `statement`
let c = ctx.children.slice(2, -2);
const pre = c[0].res;
c = c.slice(pre ? 2 : 1);
const cond = c[0].res;
c = c.slice(cond ? 2 : 1);
const step = c[0]?.res;
if (pre) node.pre = pre;
if (cond) node.cond = cond;
if (step) node.step = step;
ctx.res = node;
}
public exitStIf(ctx) {
const cond = ctx.expression().res;
const [then, el] = ctx.statement().map(x => x.res);
ctx.res = {
type: ENodeType.if,
cond,
then,
} as TNode;
if (el)
ctx.res.el = el;
}
public exitStDoWhile(ctx) {
this._exitWhile(ctx, true);
}
public exitStWhile(ctx) {
this._exitWhile(ctx);
}
public _exitWhile(ctx, condAfter?: true) {
const cond = ctx.expression().res;
const then = ctx.statement().res;
ctx.res = {
type: ENodeType.while,
cond,
then,
} as TNode;
if (condAfter)
ctx.res.condAfter = true;
}
public exitStart(ctx) {
ctx.res = ctx.children.map(x => x.res).filter(x => x !== undefined);
if (ctx.res.length === 1)
ctx.res = ctx.res[0];
this.tree = ctx.res;
}
public exitFncCall(ctx) {
const [{ res: fnc }, , { res: args }] = ctx.children;
ctx.res = {
type: ENodeType.operation,
operation: EOperationType.function,
args,
fnc,
} as TNode;
}
public exitArguments(ctx) {
// filtering `,` between arguments
ctx.res = ctx.children.map(x => x.res).filter(x => x !== undefined);
}
public exitOpAssignSpecial(ctx) {
ctx.res = ctx.children[0].symbol.text;
}
public exitAssign(ctx) {
const [{ res: target }, , { res: exprMain }] = ctx.children;
let expr: TNode = exprMain;
if (ctx.opAssignSpecial()) {
const op = ctx.opAssignSpecial().res;
expr = {
type: ENodeType.operation,
args: [target, expr],
operation: op,
};
}
ctx.res = {
type: ENodeType.assign,
expr,
target,
} as TNode;
}
public exitExpression(ctx) {
let node: TNode;
switch (ctx.children.length) {
case 1:
node = ctx.children[0].res;
break;
case 2:
const target = ctx.children[1].res;
if (ctx.OpInc() || ctx.OpDec())
node = {
type: ENodeType.assign,
target,
expr: {
type: ENodeType.operation,
operation: ctx.OpInc() && EOperationType.Plus || EOperationType.Minus,
args: [target, { type: ENodeType.constant, value: 1 }],
},
};
else
node = {
type: ENodeType.operation,
operation: ctx.children[0].symbol.text,
args: [target],
};
break;
case 3:
if (ctx.children[0]?.symbol?.type === mCParser.ParRoundBeg) {
node = ctx.children[1].res;
break;
}
const [{ res: arg1 }, { symbol: { text: operation } }, { res: arg2 }] = ctx.children;
node = {
type: ENodeType.operation,
operation,
args: [arg1, arg2],
};
break;
}
ctx.res = node;
}
public exitNumber(ctx) {
const radix
= (ctx.NumberPrefHex() && 16)
|| (ctx.NumberPrefBin() && 2)
|| 10
;
ctx.res = {
type: ENodeType.constant,
value: Number.parseInt(ctx.Digits()[0].getText(), radix),
} as TNode;
}
public visitTerminal(ctx) {
const typeCode = ctx.symbol.type;
let text = ctx.getText() as string;
let node: TNode;
switch (typeCode) {
case mCParser.Char:
// removing ' symbols
text = text.slice(1, -1);
node = {
type: ENodeType.constant,
value: text.charCodeAt(0),
};
break;
case mCParser.String:
// todo: parser by měl vracet string a char bez "/'
// removing " symbols
text = text.slice(1, -1);
node = {
type: ENodeType.constant,
value: text,
};
break;
case mCParser.Variable:
node = {
type: ENodeType.variable,
name: text,
};
break;
case mCParser.Boolean:
node = {
type: ENodeType.constant,
value: text.toLowerCase() === "true",
};
break;
default: break;
}
ctx.res = node;
}
});
private static readonly ErrorListener = (class extends (antlr4 as any).error.ErrorListener {
public syntaxError(recognizer, offendingSymbol, line, column, msg, err) {
this.errors.push({ column, line, msg });
}
public errors: { msg: string, line: number, column: number }[] = [];
});
public static run(inputFile: string): TNode {
const input = fs.readFileSync(inputFile).toString();
const chars = new InputStream(input);
const lexer = new mCLexer(chars);
const tokens = new CommonTokenStream(lexer as any);
const parser = new mCParser(tokens);
(parser as any).buildParseTrees = true;
const listenerError = new mCExecutinTreeGenerator.ErrorListener();
(parser as any).removeErrorListeners();
(parser as any).addErrorListener(listenerError);
const tree = parser.start();
if (listenerError.errors.length) {
throw new Error(`Syntax error:\n${listenerError.errors
.map((x, i) => `Error ${i}:\n${inputFile}:${x.line}:${x.column} ${x.msg}\n`)
.join("\n")}`);
}
const listener = new mCExecutinTreeGenerator.Listener();
(antlr4 as any).tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
return listener.tree;
}
}