1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | function Identifier([{ value, text, offset, lineBreaks, line, col }]) {
|
9 | return {
|
10 | tag: "Identifier",
|
11 | name: text
|
12 | };
|
13 | }
|
14 | exports.Identifier = Identifier;
|
15 | function IntType([tok]) {
|
16 | return {
|
17 | tag: "IntType"
|
18 | };
|
19 | }
|
20 | exports.IntType = IntType;
|
21 | function BoolType([tok]) {
|
22 | return {
|
23 | tag: "BoolType"
|
24 | };
|
25 | }
|
26 | exports.BoolType = BoolType;
|
27 | function StringType([tok]) {
|
28 | return {
|
29 | tag: "StringType"
|
30 | };
|
31 | }
|
32 | exports.StringType = StringType;
|
33 | function CharType([tok]) {
|
34 | return {
|
35 | tag: "CharType"
|
36 | };
|
37 | }
|
38 | exports.CharType = CharType;
|
39 | function VoidType([tok]) {
|
40 | return {
|
41 | tag: "VoidType"
|
42 | };
|
43 | }
|
44 | exports.VoidType = VoidType;
|
45 | function PointerType([tp, s, tok]) {
|
46 | return {
|
47 | tag: "PointerType",
|
48 | argument: tp
|
49 | };
|
50 | }
|
51 | exports.PointerType = PointerType;
|
52 | function ArrayType([tp, s1, l, s2, r]) {
|
53 | return {
|
54 | tag: "ArrayType",
|
55 | argument: tp
|
56 | };
|
57 | }
|
58 | exports.ArrayType = ArrayType;
|
59 | function StructType([str, s, id]) {
|
60 | return {
|
61 | tag: "StructType",
|
62 | id: id
|
63 | };
|
64 | }
|
65 | exports.StructType = StructType;
|
66 | function IntLiteral([{ value, text, offset, lineBreaks, line, col }]) {
|
67 | return {
|
68 | tag: "IntLiteral",
|
69 | raw: text
|
70 | };
|
71 | }
|
72 | exports.IntLiteral = IntLiteral;
|
73 | function CharLiteral([[start, [tok], end]]) {
|
74 | return {
|
75 | tag: "CharLiteral",
|
76 | raw: tok.value
|
77 | };
|
78 | }
|
79 | exports.CharLiteral = CharLiteral;
|
80 | function StringLiteral([[start, toks, end]]) {
|
81 | return {
|
82 | tag: "StringLiteral",
|
83 | raw: toks.map(x => x[0].value)
|
84 | };
|
85 | }
|
86 | exports.StringLiteral = StringLiteral;
|
87 | function BoolLiteral([t]) {
|
88 | return {
|
89 | tag: "BoolLiteral",
|
90 | value: t.value === "true"
|
91 | };
|
92 | }
|
93 | exports.BoolLiteral = BoolLiteral;
|
94 | function NullLiteral() {
|
95 | return {
|
96 | tag: "NullLiteral"
|
97 | };
|
98 | }
|
99 | exports.NullLiteral = NullLiteral;
|
100 | function ArrayMemberExpression([object, s1, l, s2, index, s3, r]) {
|
101 | return {
|
102 | tag: "ArrayMemberExpression",
|
103 | object: object,
|
104 | index: index
|
105 | };
|
106 | }
|
107 | exports.ArrayMemberExpression = ArrayMemberExpression;
|
108 | function Arguments([l, s1, args, r]) {
|
109 | if (args === null)
|
110 | return [];
|
111 | return [args[0]].concat(args[2].map(x => x[2]));
|
112 | }
|
113 | exports.Arguments = Arguments;
|
114 | function StructMemberExpression([object, s1, deref, s2, field]) {
|
115 | return {
|
116 | tag: "StructMemberExpression",
|
117 | deref: deref instanceof Array,
|
118 | object: object,
|
119 | field: field
|
120 | };
|
121 | }
|
122 | exports.StructMemberExpression = StructMemberExpression;
|
123 | function CallExpression([f, ws, args]) {
|
124 | return {
|
125 | tag: "CallExpression",
|
126 | callee: f,
|
127 | arguments: Arguments(args)
|
128 | };
|
129 | }
|
130 | exports.CallExpression = CallExpression;
|
131 | function IndirectCallExpression([l, s1, s, s2, f, s3, r, s4, args]) {
|
132 | return {
|
133 | tag: "IndirectCallExpression",
|
134 | callee: f,
|
135 | arguments: Arguments(args)
|
136 | };
|
137 | }
|
138 | exports.IndirectCallExpression = IndirectCallExpression;
|
139 | function UnaryExpression([operator, s, argument]) {
|
140 | if (operator.length == 1) {
|
141 | switch (operator[0].value) {
|
142 | case "&":
|
143 | case "!":
|
144 | case "~":
|
145 | case "-":
|
146 | case "*":
|
147 | return {
|
148 | tag: "UnaryExpression",
|
149 | operator: operator[0].value,
|
150 | argument: argument
|
151 | };
|
152 | default:
|
153 | throw new Error(operator[0].value);
|
154 | }
|
155 | }
|
156 | else {
|
157 | return {
|
158 | tag: "CastExpression",
|
159 | kind: operator[2],
|
160 | argument: argument
|
161 | };
|
162 | }
|
163 | }
|
164 | exports.UnaryExpression = UnaryExpression;
|
165 | function BinaryExpression([left, s1, opertoks, s2, right]) {
|
166 | const operator = opertoks.map((tok) => tok.text).join("");
|
167 | switch (operator) {
|
168 | case "*":
|
169 | case "/":
|
170 | case "%":
|
171 | case "+":
|
172 | case "-":
|
173 | case "<<":
|
174 | case ">>":
|
175 | case "<":
|
176 | case "<=":
|
177 | case ">=":
|
178 | case ">":
|
179 | case "==":
|
180 | case "!=":
|
181 | case "&":
|
182 | case "^":
|
183 | case "|":
|
184 | return {
|
185 | tag: "BinaryExpression",
|
186 | operator: operator,
|
187 | left: left,
|
188 | right: right
|
189 | };
|
190 | case "&&":
|
191 | case "||":
|
192 | return {
|
193 | tag: "LogicalExpression",
|
194 | operator: operator,
|
195 | left: left,
|
196 | right: right
|
197 | };
|
198 | case "=":
|
199 | case "+=":
|
200 | case "-=":
|
201 | case "*=":
|
202 | case "/=":
|
203 | case "%=":
|
204 | case "&=":
|
205 | case "^=":
|
206 | case "|=":
|
207 | case "<<=":
|
208 | case ">>=":
|
209 | return {
|
210 | tag: "AssignmentExpression",
|
211 | operator: operator,
|
212 | left: left,
|
213 | right: right
|
214 | };
|
215 | default:
|
216 | throw new Error(operator);
|
217 | }
|
218 | }
|
219 | exports.BinaryExpression = BinaryExpression;
|
220 | function ConditionalExpression([test, s1, op1, s2, consequent, s3, op2, s4, alternate]) {
|
221 | return {
|
222 | tag: "ConditionalExpression",
|
223 | test: test,
|
224 | consequent: consequent,
|
225 | alternate: alternate
|
226 | };
|
227 | }
|
228 | exports.ConditionalExpression = ConditionalExpression;
|
229 | function AllocExpression([alloc, s1, l, s2, typ, s3, r]) {
|
230 | return {
|
231 | tag: "AllocExpression",
|
232 | kind: typ
|
233 | };
|
234 | }
|
235 | exports.AllocExpression = AllocExpression;
|
236 | function AllocArrayExpression([alloc, s1, l, s2, typ, s3, c, s4, size, sp, r]) {
|
237 | return {
|
238 | tag: "AllocArrayExpression",
|
239 | kind: typ,
|
240 | size: size
|
241 | };
|
242 | }
|
243 | exports.AllocArrayExpression = AllocArrayExpression;
|
244 | function ResultExpression([b, res]) {
|
245 | return {
|
246 | tag: "ResultExpression"
|
247 | };
|
248 | }
|
249 | exports.ResultExpression = ResultExpression;
|
250 | function LengthExpression([b, length, s1, l, s2, argument, s3, r]) {
|
251 | return {
|
252 | tag: "LengthExpression",
|
253 | argument: argument
|
254 | };
|
255 | }
|
256 | exports.LengthExpression = LengthExpression;
|
257 | function HasTagExpression([b, hastag, s1, l, s2, typ, s3, c, s4, argument, s5, r]) {
|
258 | return {
|
259 | tag: "HasTagExpression",
|
260 | kind: typ,
|
261 | argument: argument
|
262 | };
|
263 | }
|
264 | exports.HasTagExpression = HasTagExpression;
|
265 | function UpdateExpression([argument, s1, op1, op2]) {
|
266 | return {
|
267 | tag: "UpdateExpression",
|
268 | argument: argument,
|
269 | operator: op1.value === "+" ? "++" : "--"
|
270 | };
|
271 | }
|
272 | exports.UpdateExpression = UpdateExpression;
|
273 | function AssertExpression([assert, s1, l, s2, test, s3, r]) {
|
274 | return {
|
275 | tag: "AssertExpression",
|
276 | test: test
|
277 | };
|
278 | }
|
279 | exports.AssertExpression = AssertExpression;
|
280 | function ErrorExpression([error, s1, l, s2, argument, s3, r]) {
|
281 | return {
|
282 | tag: "ErrorExpression",
|
283 | argument: argument
|
284 | };
|
285 | }
|
286 | exports.ErrorExpression = ErrorExpression;
|
287 | function SimpleStatement([stm, s1, semi]) {
|
288 | if (stm instanceof Array) {
|
289 | const init = stm[3];
|
290 | return {
|
291 | tag: "VariableDeclaration",
|
292 | kind: stm[0],
|
293 | id: stm[2],
|
294 | init: init === null ? null : init[3]
|
295 | };
|
296 | }
|
297 | else {
|
298 | return {
|
299 | tag: "ExpressionStatement",
|
300 | expression: stm
|
301 | };
|
302 | }
|
303 | }
|
304 | exports.SimpleStatement = SimpleStatement;
|
305 | function IfStatement([i, s1, l, s2, test, s3, r, s4, [annos, consequent]]) {
|
306 | return {
|
307 | tag: "IfStatement",
|
308 | test: test,
|
309 | consequent: [annos, consequent]
|
310 | };
|
311 | }
|
312 | exports.IfStatement = IfStatement;
|
313 | function IfElseStatement([i, s1, l1, s2, test, s3, r, annos1, s4, consequent, s5, e, annos2, s6, alternate]) {
|
314 | return {
|
315 | tag: "IfStatement",
|
316 | test: test,
|
317 | consequent: [annos1, consequent],
|
318 | alternate: [annos2, alternate]
|
319 | };
|
320 | }
|
321 | exports.IfElseStatement = IfElseStatement;
|
322 | function WhileStatement([w, s1, l, s2, test, s3, r, annos, s4, body]) {
|
323 | return {
|
324 | tag: "WhileStatement",
|
325 | test: test,
|
326 | annos: annos,
|
327 | body: body
|
328 | };
|
329 | }
|
330 | exports.WhileStatement = WhileStatement;
|
331 | function ForStatement([f, s1, l, init, s2, semi1, s3, test, s4, semi2, update, s5, r, annos, s6, body]) {
|
332 | return {
|
333 | tag: "ForStatement",
|
334 | init: init === null ? null : SimpleStatement([init[1], s2, semi1]),
|
335 | test: test,
|
336 | update: update === null ? null : update[1],
|
337 | annos: annos,
|
338 | body: body
|
339 | };
|
340 | }
|
341 | exports.ForStatement = ForStatement;
|
342 | function ReturnStatement([r, argument, s1, semi]) {
|
343 | return {
|
344 | tag: "ReturnStatement",
|
345 | argument: argument === null ? null : argument[1]
|
346 | };
|
347 | }
|
348 | exports.ReturnStatement = ReturnStatement;
|
349 | function BlockStatement([l, stms, annos, s, r]) {
|
350 | const stms1 = stms.map(x => x[1][0].map((y) => ({ tag: "AnnoStatement", anno: y })).concat([x[1][1]]));
|
351 | const stms2 = annos.map((x) => ({
|
352 | tag: "AnnoStatement",
|
353 | anno: x[1][0]
|
354 | }));
|
355 | const stmsAll = stms1
|
356 | .concat([stms2])
|
357 | .reduce((collect, stms) => collect.concat(stms), []);
|
358 | return {
|
359 | tag: "BlockStatement",
|
360 | body: stmsAll
|
361 | };
|
362 | }
|
363 | exports.BlockStatement = BlockStatement;
|
364 | function BreakStatement([stm, s1, semi]) {
|
365 | return { tag: "BreakStatement" };
|
366 | }
|
367 | exports.BreakStatement = BreakStatement;
|
368 | function ContinueStatement([stm, s1, semi]) {
|
369 | return { tag: "ContinueStatement" };
|
370 | }
|
371 | exports.ContinueStatement = ContinueStatement;
|
372 | function Anno1(annos) {
|
373 | const start = annos[0];
|
374 | const end = annos[6] ? annos[6] : annos[4];
|
375 | if (start.type === "anno_line_start" && start.line !== end.line)
|
376 | throw new Error(`Single-line annotations cannot be extended onto multiple lines with multiline comments.`);
|
377 | return annos[2];
|
378 | }
|
379 | exports.Anno1 = Anno1;
|
380 | function FunctionDeclarationArgs([l, s1, params, r]) {
|
381 | if (params === null)
|
382 | return [];
|
383 | const first = {
|
384 | tag: "VariableDeclaration",
|
385 | kind: params[0],
|
386 | id: params[2]
|
387 | };
|
388 | return [first].concat(params[4].map(x => ({
|
389 | tag: "VariableDeclaration",
|
390 | kind: x[2],
|
391 | id: x[4]
|
392 | })));
|
393 | }
|
394 | exports.FunctionDeclarationArgs = FunctionDeclarationArgs;
|
395 | function FunctionDeclaration([ty, s1, f, s2, args, annos, s3, def]) {
|
396 | return {
|
397 | tag: "FunctionDeclaration",
|
398 | returns: ty,
|
399 | id: f,
|
400 | params: args,
|
401 | annos: annos,
|
402 | body: def
|
403 | };
|
404 | }
|
405 | exports.FunctionDeclaration = FunctionDeclaration;
|
406 |
|
\ | No newline at end of file |