-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfc.cpp
611 lines (566 loc) · 10.1 KB
/
sfc.cpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*just a basic recursive descent parser for now...
* a lot of stuff is boilerplate that we should
* generate when we know how it's going to finally be laid out
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "ast.h"
/*BOILERPLATE*/
enum token_type {
_err_tk = 0,
num_tk,
name_tk,
add_tk,
sub_tk,
mult_tk,
div_tk,
eof_tk,
oparen_tk,
cparen_tk,
asg_tk,
semi_tk,
obrace_tk,
cbrace_tk,
if_tk,
while_tk,
for_tk,
gt_tk,
lt_tk,
gte_tk,
lte_tk,
};
token_type look;
char *lookstr;
int max_lookstr_sz;
FILE *file;
struct keyword_token_map {
const char *str;
const token_type token;
};
const keyword_token_map keywords[] = {
{"if", if_tk},
{"while", while_tk},
{"for", for_tk},
};
static
int
isdelim(char c)
{
if (c == '+'
|| c == '-'
|| c == '*'
|| c == '/'
|| c == '('
|| c == ')'
|| c == EOF
|| isspace(c)
|| c == '='
|| c == ';'
|| c == '{'
|| c == '}'
|| c == '<'
|| c == '>')
return 1;
return 0;
}
/*resize lookstr if new char goes past max_lookstr_sz*/
static
void
lookstr_append(char c)
{
int new_len = strlen(lookstr)+1;
if (new_len >= max_lookstr_sz) {
max_lookstr_sz *= 2;
lookstr = (char *)realloc(lookstr, max_lookstr_sz);
}
lookstr[new_len-1] = c;
lookstr[new_len] = '\0';
}
/*eat chars till delim and make sure validchar_func is true for all*/
static
int
try_till_delim(int (*validchar_func)(int))
{
char c;
int is_tok = 1;
while (!isdelim((c = getc(file)))) {
lookstr_append(c);
if (!(*validchar_func)(c)) is_tok = 0;
}
ungetc(c, file);
return is_tok;
}
/*check if character is valid variable character*/
static
int
isvarc(int c)
{
return (isalnum(c) || c == '_');
}
/*eat chars till delim. add them to lookstr. set look to corresponding token*/
static
void
nexttok()
{
char c;
look = _err_tk;
while (isspace(c = getc(file)))
;
lookstr[0] = '\0';
lookstr_append(c);
switch (c) {
case '+':
look = add_tk;
return;
case '-':
look = sub_tk;
return;
case '*':
look = mult_tk;
return;
case '/':
look = div_tk;
return;
case EOF:
look = eof_tk;
return;
case ')':
look = cparen_tk;
return;
case '(':
look = oparen_tk;
return;
case ':':
lookstr_append((c = getc(file)));
if (c == '=')
look = asg_tk;
return;
case ';':
look = semi_tk;
return;
case '{':
look = obrace_tk;
return;
case '}':
look = cbrace_tk;
return;
case '>':
look = gt_tk;
c = getc(file);
if (c == '=') {
lookstr_append(c);
look = gte_tk;
} else
ungetc(c, file);
return;
case '<':
look = lt_tk;
c = getc(file);
if (c == '=') {
lookstr_append(c);
look = lte_tk;
} else
ungetc(c, file);
return;
}
if (isdigit(c)) {
look = num_tk;
if (!try_till_delim(&isdigit))
look = _err_tk;
} else if (isalpha(c)) {
int i, num_keywords = (sizeof(keywords)/sizeof(keywords[0]));
if (!try_till_delim(&isvarc)) {
look = _err_tk;
return;
}
for (i = 0; i < num_keywords; i++) {
if (!strcmp(keywords[i].str, lookstr)) {
look = keywords[i].token;
return;
}
}
look = name_tk;
}
return;
}
#define printerr(...)\
{\
fprintf(stderr, "\n");\
fprintf(stderr, __VA_ARGS__);\
fprintf(stderr, "\n");\
}
#define abortc(...)\
{\
printerr(__VA_ARGS__);\
fprintf(stderr, "Aborting...\n\n");\
exit(1);\
}
static
void
expected(const char *lhs)
{
abortc("Expected %s.", lhs);
}
static
void
expect(token_type expected_type)
{
if (look == expected_type) {
nexttok();
return;
}
switch(expected_type) {
case num_tk:
expected("integer");
break;
case eof_tk:
expected("end of file");
case name_tk:
expected("variable name");
case add_tk:
expected("plus sign");
case sub_tk:
expected("minus sign");
case mult_tk:
expected("multiplication sign");
case div_tk:
expected("division sign");
case oparen_tk:
expected("open parenthesis");
case cparen_tk:
expected("closing parenthesis");
case asg_tk:
expected("assignment operator");
case semi_tk:
expected("semicolon");
case obrace_tk:
expected("opening brace");
case cbrace_tk:
expected("closing brace");
default:
expected("default");
}
}
static
ast_node *
create_node(ast_type t)
{
ast_node *n = (ast_node *)malloc(sizeof(ast_node));
n->type = t;
return n;
}
static ast_node *eprime(ast_node *);
static
ast_node *
num()
{
ast_node *n = create_node(type_num);
n->num = atoi(lookstr);
expect(num_tk);
return n;
}
static
ast_node *
name(ast_type t) //get a generic name (could be type, var, function, etc.)
{
ast_node *n = create_node(t);
n->name = (char *)malloc(strlen(lookstr)+1);
strcpy(n->name, lookstr);
expect(name_tk);
return n;
}
static ast_node * expr();
//variable name occuring in an expression
static
ast_node *
exprvar()
{
return name(type_exprvar);
}
//variable name being assigned to
static
ast_node *
asmtvar()
{
return name(type_asmtvar);
}
static
ast_node *
factor()
{
if (look == oparen_tk) {
ast_node *n;
nexttok();
if (!(n = expr()))
return NULL;
expect(cparen_tk);
return n;
} else if (look == num_tk)
return num();
else if (look == name_tk)
return exprvar();
return NULL;
}
static
ast_node *
tprime(ast_node *prev_factorn)
{
ast_node *n, *tprimen, *factorn;
if (look == semi_tk
|| look == add_tk
|| look == sub_tk
|| look == cparen_tk
|| look == gt_tk
|| look == lt_tk
|| look == gte_tk
|| look == lte_tk)
return prev_factorn;
n = create_node(type_expr);
if (look == mult_tk) {
nexttok();
n->expr.op = "imul";
} else if (look == div_tk) {
nexttok();
n->expr.op = "idiv";
} else {
expected("multiplication or division sign");
}
if (!(factorn = factor()))
return NULL;
if (!(tprimen = tprime(factorn)))
return NULL;
n->expr.left = prev_factorn;
n->expr.right = tprimen;
return n;
}
static
ast_node *
term()
{
ast_node *fnode;
if ((fnode = factor()))
return tprime(fnode);
return NULL;
}
static
ast_node *
cond(ast_node *left_expr)
{
ast_node *n;
if (look == semi_tk || look == cparen_tk)
return left_expr;
n = create_node(type_expr);
n->expr.left = left_expr;
if (look == gt_tk)
n->expr.op = "GT";
else if (look == lt_tk)
n->expr.op = "LT";
else if (look == gte_tk)
n->expr.op = "GTE";
else if (look == lte_tk)
n->expr.op = "LTE";
else
expected("conditional operator");
nexttok();
if (!(n->expr.right = expr()))
return NULL;
return n;
}
static
ast_node *
expr()
{
ast_node *termn, *eprimen;
if (!(termn = term()))
return NULL;
if ((eprimen = eprime(termn)))
return cond(eprimen);
return NULL;
}
/* combine eprime and tprime? */
static
ast_node *
eprime(ast_node *prev_termn)
{
ast_node *n, *eprimen, *termn;
if (look == semi_tk
|| look == cparen_tk
|| look == gt_tk
|| look == lt_tk
|| look == gte_tk
|| look == lte_tk)
return prev_termn;
n = create_node(type_expr);
if (look == add_tk) {
nexttok();
n->expr.op = "add";
} else if (look == sub_tk) {
nexttok();
n->expr.op = "sub";
} else {
expected("plus or minus sign");
}
if (!(termn = term()))
return NULL;
if (!(eprimen = eprime(termn)))
return NULL;
n->expr.left = prev_termn;
n->expr.right = eprimen;
return n;
}
static
ast_node *
astmt()
{
ast_node *n = create_node(type_astmt), *namen, *exprn;
if (!(namen = asmtvar()))
return NULL;
n->astmt.lval = namen;
expect(asg_tk);
if (!(exprn = expr()))
return NULL;
n->astmt.rval = exprn;
return n;
}
static
ast_node *
condexpr()
{
/*just an expr for now...*/
return expr();
}
static ast_node *ifstmt();
static ast_node *astmt();
static ast_node *wstmt();
static ast_node *fstmt();
static
ast_node *
stmt()
{
if (look == if_tk) {
return ifstmt();
} else if (look == while_tk) {
return wstmt();
} else if (look == for_tk) {
return fstmt();
} else if (look == name_tk) {
ast_node *n = astmt();
expect(semi_tk);
return n;
}
return NULL;
}
ast_node *
stmtlist()
{
ast_node *stmtlistn = create_node(type_stmtlist);
if (look == eof_tk || look == cbrace_tk)
return END_STMT_LIST;
if (!(stmtlistn->stmtlist.stmt = stmt()))
return NULL;
if(!(stmtlistn->stmtlist.next = stmtlist()))
return NULL;
return stmtlistn;
}
static
ast_node *
ifstmt()
{
ast_node *n = create_node(type_ifstmt);
expect(if_tk);
expect(oparen_tk);
if (!(n->ifstmt.condexpr = condexpr()))
return NULL;
expect(cparen_tk);
expect(obrace_tk);
if (!(n->ifstmt.stmtlist = stmtlist()))
return NULL;
expect(cbrace_tk);
return n;
}
static
ast_node *
wstmt()
{
ast_node *n = create_node(type_wstmt);
expect(while_tk);
expect(oparen_tk);
if (!(n->wstmt.condexpr = condexpr()))
return NULL;
expect(cparen_tk);
expect(obrace_tk);
if (!(n->wstmt.stmtlist = stmtlist()))
return NULL;
expect(cbrace_tk);
return n;
}
static
ast_node *
fstmt()
{
ast_node *n = create_node(type_fstmt);
expect(for_tk);
expect(oparen_tk);
if (!(n->fstmt.init = astmt()))
return NULL;
expect(semi_tk);
if (!(n->fstmt.condexpr = condexpr()))
return NULL;
expect(semi_tk);
if (!(n->fstmt.onloop = astmt()))
return NULL;
expect(cparen_tk);
expect(obrace_tk);
if (!(n->fstmt.stmtlist = stmtlist()))
return NULL;
expect(cbrace_tk);
return n;
}
void gen_code(ast_node *ast);
int
main(int argc, char **argv)
{
/* grammar:
stmtlist -> stmt stmtlist
| eps
stmt -> astmt;
| ifstmt
| wstmt
| fstmt
astmt -> lvar := expr
ifstmt -> if (condexpr) { stmtlist }
wstmt -> while (condexpr) { stmtlist }
fstmt -> for (init; condexpr; onloop) { stmtlist }
expr -> term eprime cond
cond -> > expr
| < expr
| >= expr
| <= expr
| eps
eprime -> + term eprime
| - term eprime
| eps
term -> factor tprime
tprime -> * factor tprime
| / factor tprime
| eps
factor -> ( expr )
| num
| rvar
*/
if (argc == 1)
abortc("no filename given");
file = fopen(argv[1], "r");
lookstr = (char *)malloc(1);
lookstr[0] = '\0';
max_lookstr_sz = 1;
nexttok();
ast_node *ast = stmtlist();
if (ast)
gen_code(ast);
else
printerr("ast err\n");
return 0;
}