-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.y
338 lines (316 loc) · 9.97 KB
/
grammar.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
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ast.h"
#include "symbols.h"
#include "types_and_vars.h"
#include "codegen.h"
extern int yylex();
static void yyerror(const char* msg);
/* Line number from flex lexer */
extern int yylineno;
/* Input file used by flex lexer */
extern FILE* yyin;
static DeclarationList* tree = NULL;
#define YYERROR_VERBOSE
%}
%union {
/* AST */
DeclarationList* declarations;
Declaration* declaration;
Expr* expr;
ExprList* exprs;
Pattern* pattern;
PatternList* patterns;
TPat* tpat;
ParamList* params;
Param* param;
TypeExpr* typexpr;
TypeExprList* types;
Case* kase;
CaseList* kases;
Ctor* ctor;
CtorList* ctors;
/* terminals */
int intval; // we should probably store these
// as strings/symbols as well...
_Bool boolval;
Symbol identifier;
Symbol text;
const char* error_msg;
}
%token LET REC TYPE IN IF THEN ELSE MATCH WITH FUNCTION FUN
%token <intval> INT
%token <boolval> BOOL
%token <text> STR_LIT
%token <identifier> ID CTORID
%token <error> ERROR
%token EOFTOK
%type <declarations> program declarations
%type <declaration> declaration letdecl
%type <pattern> pattern
%type <tpat> tpattern
%type <params> params
%type <param> param
%type <exprs> exprlist nonemptylist tuplexpr
%type <expr> expr letexpr exprterm
%type <typexpr> typexpr typeterm optionaltype
%type <types> typetuple
%type <kases> matchings
%type <kase> matching
%type <ctors> constructors
%type <ctor> constructor
%nonassoc LET IN TYPE EXTERNAL MATCH WITH FUN FUNCTION AND
%right ARROW /* function typexprs */
%nonassoc '[' ']' VSTART VEND
%right ';'
%nonassoc IF THEN ELSE
%left ',' /* not really, but makes list-building easier */
%left '=' '|' /* right in assignments but left in expressions */
%nonassoc '<' LE
%right '^' '@'
%nonassoc OF
%right CONS
%left '+' '-'
%left '*' '/'
/* function application -note, typecheck will stop strings and ints being
functions, rather than the grammar */
%left ID CTORID INT BOOL STR_LIT '('
%%
program:
declarations { tree = $$ = reverse_declarations($1); }
;
declarations:
declarations declaration { $$ = add_declaration($1, $2); }
| declaration { $$ = declaration_list($1); }
;
/* we will add types to this*/
declaration:
letdecl { $$ = $1; }
| TYPE ID '=' typexpr { $$ = type($2, $4); }
| TYPE ID '=' constructors { $$ = type_ctor($2, reverse_ctors($4)); }
| EXTERNAL ID ':' typexpr '=' STR_LIT
{
$$ = externdecl($2, $4, $6);
}
;
letdecl:
LET pattern '=' expr { $$ = binding($2, $4); }
| LET ID params optionaltype '=' expr
{ $$ = func_w_type($2, reverse_params($3), $4, $6); }
| LET REC ID params optionaltype '=' expr
{ $$ = recfunc_w_type($3, reverse_params($4), $5, $7); }
;
pattern:
tpattern { $$ = tpat_to_pat($1); }
;
tpattern:
ID { $$ = tpat_var($1); }
| '(' pattern ')' { $$ = tpat_pattern($2); }
| '_' { $$ = tpat_discard(); }
| tpattern CONS tpattern { $$ = tpat_cons($1, $3); }
| tpattern ',' tpattern { $$ = tpat_tuple($1, $3); }
| CTORID tpattern { $$ = tpat_constr_warg($1, $2); }
| CTORID { $$ = tpat_constr_noarg($1); }
| INT { $$ = tpat_int($1); }
| STR_LIT { $$ = tpat_str($1); }
| '[' ']' { $$ = tpat_nil(); }
;
params:
params param { $$ = add_param($1, $2); }
| param { $$ = param_list($1); }
;
param:
ID { $$ = param($1); }
| '(' ID ':' typexpr ')' { $$ = param_with_type($2, $4); }
| '(' ')' { $$ = param(symbol("()")); }
;
expr:
letexpr { $$ = $1; }
| IF expr THEN expr ELSE expr { $$ = ifexpr($2,$4,$6); }
/* TODO add '^' string concatenation */
| expr '+' expr { $$ = plus($1, $3); }
| expr '-' expr { $$ = minus($1, $3); }
| expr '*' expr { $$ = multiply($1, $3); }
| expr '/' expr { $$ = divide($1, $3); }
| expr '=' expr { $$ = equal($1, $3); }
| expr '<' expr { $$ = lessthan($1, $3); }
| expr LE expr { $$ = lessequal($1, $3); }
/* in real ML this should be "expr expr" */
/* but we need to disambiguate */
| expr exprterm { $$ = apply($1, $2); }
| exprterm { $$ = $1; }
| MATCH expr WITH matchings { $$ = match($2, reverse_cases($4)); }
| tuplexpr %prec ';' { $$ = tuple(reverse_list($1)); }
;
tuplexpr:
expr ',' expr { $$ = add_expr(add_expr(exprlist(), $1), $3); }
| tuplexpr ',' expr { $$ = add_expr($1, $3); }
;
exprterm:
'(' expr ')' { $$ = $2; }
| '(' expr ':' typexpr ')' { $$ = $2; $2->type = $4; }
| '[' exprlist ']' { $$ = list($2); }
| VSTART exprlist VEND { $$ = vector($2); }
| ID { $$ = var($1); }
| CTORID { $$ = var($1); }
| '(' ')' { $$ = unit_expr(); }
| INT { $$ = intval($1); }
| STR_LIT { $$ = strval($1); }
;
letexpr:
LET ID params optionaltype '=' expr IN expr
{
$$ = local_func_w_type($2, reverse_params($3), $4, $6, $8);
}
| LET REC ID params optionaltype '=' expr IN expr
{
$$ = local_recfunc_w_type($3, reverse_params($4), $5, $7, $9);
}
| LET pattern '=' expr IN expr
{
$$ = local_binding($2, $4, $6);
}
;
optionaltype:
/* empty */ { $$ = NULL; /* will become type constraint in TC*/ }
| ':' typexpr { $$ = $2; }
;
exprlist:
/* empty */ { $$ = exprlist(); }
| nonemptylist { $$ = $1; }
;
nonemptylist:
expr { $$ = add_expr(exprlist(), $1); }
| expr ';' nonemptylist { $$ = add_expr($3, $1); }
;
matchings:
matchings '|' matching { $$ = case_add($1, $3); }
| '|' matching { $$ = caselist($2); } /* optional first | */
| matching { $$ = caselist($1); }
;
matching:
pattern ARROW expr { $$ = matchcase($1, $3); }
;
typexpr:
typexpr ARROW typexpr { $$ = typearrow($1, $3); }
| typetuple { $$ = typetuple(reversed_types($1)); }
| typeterm { $$ = $1; }
;
typeterm:
'(' typexpr ')' { $$ = $2; }
| typeterm ID { $$ = typeconstructor($1, $2); }
| ID { $$ = typename($1); }
;
typetuple:
/* these need to be a list as (1,2,3) != ((1,2),3) and != (1,(2,3)) */
typeterm '*' typeterm { $$ = type_add(type_list($1), $3); }
| typetuple '*' typeterm { $$ = type_add($1, $3); }
;
constructors:
constructor { $$ = ctor_list($1); }
| constructors '|' constructor { $$ = add_ctor($1, $3); }
;
constructor:
CTORID { $$ = ctor_noarg($1); }
| CTORID OF typexpr { $$ = ctor_warg($1, $3); }
;
%%
int yywrap() { return 1; }
void yyerror(const char* msg)
{
fprintf(stderr, "line:%d: error: %s\n", yylineno, msg);
}
int main(int argc, char* argv[])
{
int debug = 0;
int parse_only = 0;
int stop_after_type_check = 0;
char* inarg = NULL;
char* outarg = NULL;
for (int i = 1; i < argc; i++) {
char* c = argv[i];
if (strcmp(c, "-v") == 0) {
debug = debug_type_checker = 1;
} else if (strcmp(c, "-p") == 0) {
parse_only = 1;
} else if (strcmp(c, "-t") == 0) {
stop_after_type_check = 1;
} else if (i + 1 < argc && strcmp(c, "-o") == 0) {
if (outarg) {
fprintf(stderr, "intml: error: too many output files\n");
exit(EXIT_FAILURE);
}
outarg = argv[++i];
} else {
if (inarg) {
fprintf(stderr, "intml: error: too many input files\n");
exit(EXIT_FAILURE);
}
inarg = c;
}
}
// want to check we have input before potentially destroying an output file
// which was supposed to be input
if (inarg) {
if (strcmp(inarg, "-") == 0) {
yyin = stdin; // default anyway
} else {
if (!(yyin = fopen(inarg, "r"))) {
perror(inarg);
exit(EXIT_FAILURE);
}
}
} else {
fprintf(stderr, "intml: error: no input files\n");
exit(EXIT_FAILURE);
}
if (outarg) {
if (!(cgenout = fopen(outarg, "w"))) {
perror(outarg);
exit(EXIT_FAILURE);
}
} else {
cgenout = stdout;
}
debug_type_checker = debug;
debug_codegen = debug;
yyparse();
if (!tree) {
exit(EXIT_FAILURE);
}
if (debug || parse_only) {
print_tree(stdout, tree);
printf("\n");
}
if (parse_only)
return 0;
// type check!
type_check_tree(tree);
if (stop_after_type_check)
return 0;
// Check we have a main function <- entry point
check_runtime_properties(tree);
if (debug) {
fprintf(stderr, "found enough components of a runnable program (e.g. main)\n");
}
{
/* ideal target: */
// High level tree optimization
//optimize1(tree);
// Transform to 3AC (Three address code)
//ir_tree ir = transform();
//optimize2(ir);
// Generate code?
}
{
/* idea for now */
// Generate stack machine code
codegen(tree);
}
if (cgenout != stdout)
fclose(cgenout);
if (yyin != stdin)
fclose(yyin);
}