-
Notifications
You must be signed in to change notification settings - Fork 1
/
mini-javascript.y
417 lines (370 loc) · 17.5 KB
/
mini-javascript.y
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
%{
#include <stdio.h>
#include <string.h>
#include "parser.h"
%}
%union {
char *val;
node *node;
}
%type <val> value_literal object_literal array_literal type_specifier
%type <node> script statements statement scope
if_statement for_statement jump_statement while_statement do_while_statement switch_statement expression_statement
switch_body case_statements case_statement
expression primary_expression assignment_expression conditional_expression postfix_expression
equality_expression relational_expression additive_expression multiplicative_expression unary_expression
logical_or_expression logical_and_expression
variable_declaration function_declaration parameters arguments
%token VAR
%token LET
%token CONST
%token FUNCTION
%token ARROW_FUNCTION // =>
%token SEMICOLON // ;
%token COLON // :
%token FOR
%token WHILE
%token DO
%token IF
%token ELSE
%token RETURN
%token SWITCH
%token CASE
%token DEFAULT
%token BREAK
%token CONTINUE
%token NEW
%token DELETE
%token TYPEOF
%token INSTANCEOF
%token ASYNC
%token AWAIT
%token LPAREN // (
%token RPAREN // )
%token LBRACE // {
%token RBRACE // }
%token LBRACKET // [
%token RBRACKET // ]
%token <val> LITERAL_TRUE // true
%token <val> LITERAL_FALSE // false
%token <val> LITERAL_NAN // NaN
%token <val> LITERAL_INFINITY // Infinity
%token <val> LITERAL_NULL // null
%token <val> LITERAL_UNDEFINED // undefined
%token PLUS
%token MINUS
%token MULTIPLY
%token DIVIDE
%token ASSIGN // =
%token EQ // ==
%token EXACTLY_EQ // ===
%token NOT_EQ // !=
%token EXACTLY_NOT_EQ // !==
%token NOT // !
%token DOT // .
%token AND // &
%token OR // |
%token COMMA // ,
%token MODULO // %
%token TERNARY // ?
%token INCREASE // ++
%token DECREASE // --
%token ADD_ASSIGN // +=
%token SUBTRACT_ASSIGN // -=
%token MULTIPLY_ASSIGN // *=
%token DIVIDE_ASSIGN // /=
%token MODULO_ASSIGN // %=
%token LT // <
%token GT // >
%token LTE // <=
%token GTE // >=
%token STRICT_MODE // "use strict";
%token <val> NUMBER
%token <val> STRING
%token <val> IDENTIFIER
/* %parse-param {node *root} */
%error-verbose
%%
script
: statements { root = put_node(Program, NULL, NULL, NULL, $1); }
;
statements
: statement
| statements statement { $$ = sibling_node($1, $2); }
;
value_literal
: STRING { debug("string literal", $$); }
| LITERAL_FALSE { debug("boolean: false literal", $$); }
| LITERAL_TRUE { debug("boolean: true literal", $$); }
| NUMBER { debug("number literal", $$); }
| LITERAL_NAN { debug("number: NaN literal", $$); }
| LITERAL_INFINITY { debug("number: Infinity literal", $$); }
| LITERAL_NULL { debug("object: null literal", $$); }
| LITERAL_UNDEFINED { debug("undefined literal", $$); }
;
array_literal
: LBRACKET RBRACKET { $$ = "[]"; debug("array: [] literal", $$); }
| LBRACKET array_elements RBRACKET {}
;
array_elements
: assignment_expression
| array_elements COMMA assignment_expression
;
object_literal
: LBRACE RBRACE { $$ = "{}"; }
| LBRACE object_pair RBRACE {}
;
object_pair
: object_key COLON assignment_expression
| object_pair COMMA object_key COLON assignment_expression
| object_pair COMMA
;
object_key
: STRING
| NUMBER
| IDENTIFIER
;
primary_expression
: IDENTIFIER { $$ = identifier_node($1, 1); }
| value_literal { $$ = literal_node($1); }
| array_literal { $$ = literal_node($1); }
| object_literal { $$ = literal_node($1); }
| LPAREN expression RPAREN { $$ = expression_node(NULL, $2); }
| function_declaration { debug("function declaration", ""); }
;
function_declaration
: FUNCTION IDENTIFIER LPAREN parameters RPAREN scope { $$ = function_node($2, 0, $4, $6); }
| FUNCTION LPAREN parameters RPAREN scope { $$ = function_node(NULL, 0, $3, $5); }
| LPAREN parameters RPAREN ARROW_FUNCTION scope { $$ = function_node(NULL, 0, $2, $5); }
| ASYNC FUNCTION IDENTIFIER LPAREN parameters RPAREN scope { $$ = function_node($3, 1, $5, $7); }
| ASYNC FUNCTION LPAREN parameters RPAREN scope { $$ = function_node(NULL, 1, $4, $6); }
| ASYNC LPAREN parameters RPAREN ARROW_FUNCTION scope { $$ = function_node(NULL, 1, $3, $6); }
;
postfix_expression
: primary_expression
| primary_expression INCREASE { debug("postfix increase", ""); }
| primary_expression DECREASE { debug("postfix decrease", ""); }
| postfix_expression DOT primary_expression
| postfix_expression LPAREN RPAREN { $$ = call_node($1, NULL); }
| postfix_expression LPAREN arguments RPAREN { $$ = call_node($1, $3); }
| postfix_expression LBRACKET expression RBRACKET { debug("member access", ""); }
;
unary_expression
: postfix_expression
| NOT unary_expression { $$ = expression_node("unary", $2); $$->val = strdup("!"); }
| MINUS unary_expression { $$ = expression_node("unary", $2); $$->val = strdup("-"); }
| NEW unary_expression { $$ = expression_node("unary", $2); $$->val = strdup("new"); }
| DELETE unary_expression { $$ = expression_node("unary", $2); $$->val = strdup("delete"); }
| INSTANCEOF unary_expression { $$ = expression_node("unary", $2); $$->val = strdup("instanceof"); }
| TYPEOF unary_expression { $$ = expression_node("unary", $2); $$->val = strdup("typeof"); }
| AWAIT unary_expression {
/* TODO async scope and strict mode check */
$$ = expression_node("unary", $2);
$$->val = strdup("await");
}
;
multiplicative_expression
: unary_expression
| multiplicative_expression MULTIPLY unary_expression { operator_node("*", $1, $3); debug("multiply expression", ""); }
| multiplicative_expression DIVIDE unary_expression { operator_node("/", $1, $3); debug("divide expression", ""); }
| multiplicative_expression MODULO unary_expression { operator_node("%", $1, $3); debug("modulo expression", ""); }
;
additive_expression
: multiplicative_expression
| additive_expression PLUS multiplicative_expression { $$ = operator_node("+", $1, $3); }
| additive_expression MINUS multiplicative_expression { $$ = operator_node("-", $1, $3); }
;
relational_expression
: additive_expression
| relational_expression LT additive_expression { $$ = operator_node("<", $1, $3); }
| relational_expression LTE additive_expression { $$ = operator_node("<=", $1, $3); }
| relational_expression GT additive_expression { $$ = operator_node(">", $1, $3); }
| relational_expression GTE additive_expression { $$ = operator_node(">=", $1, $3); }
| relational_expression INSTANCEOF additive_expression { $$ = operator_node("instanceof", $1, $3); }
;
equality_expression
: relational_expression
| equality_expression EQ relational_expression { $$ = operator_node("==", $1, $3); debug("equal expression", ""); }
| equality_expression NOT_EQ relational_expression { $$ = operator_node("!=", $1, $3); debug("not equal expression", ""); }
| equality_expression EXACTLY_EQ relational_expression { $$ = operator_node("===", $1, $3); debug("exactly equal expression", ""); }
| equality_expression EXACTLY_NOT_EQ relational_expression { $$ = operator_node("!==", $1, $3); debug("exactly not equal expression", ""); }
;
logical_and_expression
: equality_expression
| logical_and_expression AND equality_expression { $$ = operator_node("&&", $1, $3); debug("logical and expression", ""); }
;
logical_or_expression
: logical_and_expression
| logical_or_expression OR logical_and_expression { $$ = operator_node("||", $1, $3); debug("logical or expression", ""); }
;
conditional_expression
: logical_or_expression
| logical_or_expression TERNARY assignment_expression COLON conditional_expression { debug("conditional expression", ""); }
;
assignment_expression
: conditional_expression
| unary_expression assignment_operator assignment_expression { $$ = operator_node("=", $1, $3); debug("assignment expression", ""); }
;
expression
: assignment_expression
| expression COMMA assignment_expression { $$ = sibling_node($1, $3); }
;
statement
: expression_statement
| for_statement
| while_statement
| do_while_statement
| if_statement
| switch_statement
| jump_statement
| declaration
| STRICT_MODE {
strict_mode = 1;
$$ = statement_node("stric mode");
debug("strict mode enabled", "");
}
;
expression_statement
: expression
| expression SEMICOLON { $$ = $1; }
| SEMICOLON { $$ = NULL; }
;
if_statement
: IF LPAREN assignment_expression RPAREN scope ELSE scope {
$$ = statement_node("if-else");
node *condition = expression_node("condition", $3);
$$->child = condition;
sibling_node(condition, $5);
sibling_node($5, $7);
debug("if-else statement", "");
}
| IF LPAREN assignment_expression RPAREN scope {
$$ = statement_node("if");
node *condition = expression_node("condition", $3);
$$->child = condition;
sibling_node(condition, $5);
debug("if statement", "");
}
;
for_statement
: FOR LPAREN variable_declaration SEMICOLON assignment_expression SEMICOLON assignment_expression RPAREN scope {
$$ = statement_node("for");
debug("for statement", "");
}
| FOR LPAREN assignment_expression SEMICOLON assignment_expression SEMICOLON assignment_expression RPAREN scope { debug("for statement", ""); }
;
while_statement
: WHILE LPAREN assignment_expression RPAREN scope {
$$ = statement_node("while");
$$->child = $3;
sibling_node($$->child, $5);
debug("while statement", "");
}
;
do_while_statement
: DO scope WHILE LPAREN assignment_expression RPAREN SEMICOLON {
$$ = statement_node("do-while");
$$->child = $5;
sibling_node($$->child, $2);
debug("do-while statement", "");
}
;
switch_statement
: SWITCH LPAREN assignment_expression RPAREN switch_body {
$$ = statement_node("switch");
$$->child = $5;
debug("switch statement", "");
}
;
switch_body
: LBRACE RBRACE { $$ = put_node(Scope, NULL, NULL, NULL, NULL); }
| LBRACE case_statements RBRACE { $$ = put_node(Scope, NULL, NULL, NULL, $2); }
;
case_statements
: case_statement
| case_statements case_statement { $$ = sibling_node($1, $2); }
;
case_statement
: CASE assignment_expression COLON statements {
node *label = expression_node("label", $2);
node *statements = statement_node("statement");
statements->child = $4;
label->next_sibling = statements;
$$ = statement_node("case");
$$->next_sibling = label;
}
| CASE assignment_expression COLON case_statement {
node *label = expression_node("label", $2);
node *statements = statement_node("statement");
label->next_sibling = statements;
$$ = statement_node("case");
$$->next_sibling = label;
sibling_node($$, $4);
}
| DEFAULT COLON statements {
node *label = expression_node("label", NULL);
node *statements = statement_node("statement");
statements->child = $3;
label->next_sibling = statements;
$$ = statement_node("case");
$$->next_sibling = label;
}
;
jump_statement
: BREAK skippable_semicolon { $$ = jump_node("break", NULL); debug("break", ""); }
| CONTINUE skippable_semicolon { $$ = jump_node("continue", NULL); debug("continue", ""); }
| RETURN { $$ = jump_node("return", NULL); debug("return", ""); }
| RETURN expression_statement { $$ = jump_node("return", $2); debug("return", ""); }
;
scope
: LBRACE RBRACE { $$ = put_node(Scope, NULL, NULL, NULL, NULL); debug("empty scope", ""); }
| LBRACE statements RBRACE { $$ = put_node(Scope, NULL, NULL, NULL, $2); }
;
declaration
: variable_declaration skippable_semicolon
;
variable_declaration
: type_specifier IDENTIFIER {
if (!strict_mode && strcmp($1, "var") != 0) {
yyerror("Error: let / const keyword requires strict mode.");
}
$$ = identifier_node($2, strcmp($1, "const") == 0 ? 0 : 1); debug("variable declaration", $2);
}
| type_specifier IDENTIFIER ASSIGN expression {
if (!strict_mode && strcmp($1, "var") != 0) {
yyerror("Error: let / const keyword requires strict mode.");
}
$$ = identifier_node($2, strcmp($1, "const") == 0 ? 0 : 1);
debug("variable declaration with value", $2);
$$->child = $4;
}
;
type_specifier
: VAR { $$ = strdup("var"); }
| LET { $$ = strdup("let"); }
| CONST { $$ = strdup("const"); }
;
assignment_operator
: ASSIGN
| ADD_ASSIGN
| SUBTRACT_ASSIGN
| MULTIPLY_ASSIGN
| DIVIDE_ASSIGN
| MODULO_ASSIGN
;
parameters
: /* None */ { $$ = NULL; debug("empty function parameter", ""); }
| IDENTIFIER { $$ = identifier_node($1, 1); debug("function parameter", $1); }
| parameters COMMA IDENTIFIER {
$$ = sibling_node($1, identifier_node($3, 1));
}
;
arguments
: assignment_expression
| arguments COMMA assignment_expression { $$ = sibling_node($1, $3); }
;
skippable_semicolon
: /* Empty */
| SEMICOLON
;
%%