-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgrammar.y
717 lines (641 loc) · 21.3 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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Jass2 parser for bison/yacc
// by Rudi Cilibrasi
// Sun Jun 8 00:51:53 CEST 2003
// thanks to Jeff Pang for the handy documentation that this was based
// on at http://jass.sourceforge.net
%{
#include <stdio.h>
#include <string.h>
#include "token.yy.h"
#include "misc.h"
#include "blocks.h"
#define YYMAXDEPTH 100000
#define YYDEBUG 1
%}
%define api.value.type {union node}
%token IF
%token THEN
%token TYPE
%token EXTENDS
%token HANDLE
%token NEWLINE
%token GLOBALS
%token ENDGLOBALS
%token CONSTANT
%token NATIVE
%token TAKES
%token RETURNS
%token FUNCTION
%token ENDFUNCTION
%token LOCAL
%token ARRAY
%token SET
%token CALL
%token ELSE
%token ELSEIF
%token ENDIF
%token LOOP
%token EXITWHEN
%token RETURN
%token DEBUG
%token ENDLOOP
%token NOT
%token TNULL
%token TTRUE
%token TFALSE
%token CODE
%token STRING
%token INTEGER
%token REAL
%token BOOLEAN
%token NOTHING
%token ID
%token COMMA
%token AND
%token OR
%token EQUALS
%token TIMES
%token DIV
%token MOD
%token PLUS
%token MINUS
%token LPAREN
%token RPAREN
%token LBRACKET
%token RBRACKET
%token LESS
%token GREATER
%token LEQ
%token GEQ
%token EQCOMP
%token NEQ
%token STRINGLIT
%token INTLIT
%token REALLIT
%token UNITTYPEINT
%token ANNOTATION
%token ALIAS
%right EQUALS
%left AND
%left OR
%left EQCOMP NEQ
%left LESS GREATER LEQ GEQ
%left NOT
%left MINUS PLUS
%left TIMES DIV MOD
%%
program: topscopes globdefs topscopes funcdefns
;
topscopes: topscope
| topscopes topscope
;
topscope: typedefs
| funcdecls
;
funcdefns: /* empty */
| funcdefns funcdefn
;
globdefs: /* empty */
| globals newline vardecls endglobals
| globals vardecls endglobals {yyerrorline(syntaxerror, lineno - 1, "Missing linebreak before global declaration");}
;
globals: GLOBALS { inglobals = 1; };
endglobals: ENDGLOBALS { inglobals = 0; };
vardecls: /* empty */
| vd vardecls
;
vd: newline
| vardecl
;
funcdecls: /* empty */
| fd funcdecls
;
fd: newline
| funcdecl
;
typedefs: /* empty */
| td typedefs
;
td: newline
| typedef
;
// Returns a typenode
expr: intexpr { $$.ty = gInteger; }
| realexpr { $$.ty = gReal; }
| stringexpr { $$.ty = $1.ty; }
| boolexpr { $$.ty = gBoolean; }
| FUNCTION rid LPAREN exprlistcompl RPAREN {
struct funcdecl *fd = ht_lookup(&functions, $2.str);
if (fd == NULL) {
char ebuf[1024];
snprintf(ebuf, 1024, "Undefined function %s", $2.str);
getsuggestions($2.str, ebuf, 1024, 1, &functions);
yyerrorex(semanticerror, ebuf);
$$.ty = gCode;
} else {
char ebuf[1024];
if (fd->p->head != NULL) {
snprintf(ebuf, 1024, "Function %s must not take any arguments when used as code", $2.str);
}else{
snprintf(ebuf, 1024, "Function must not take any arguments when used as code");
}
yyerrorex(semanticerror, ebuf);
if( fd->ret == gBoolean) {
$$.ty = gCodeReturnsBoolean;
} else {
$$.ty = gCodeReturnsNoBoolean;
}
}
}
| FUNCTION rid {
struct funcdecl *fd = ht_lookup(&functions, $2.str);
if (fd == NULL) {
char ebuf[1024];
snprintf(ebuf, 1024, "Undefined function %s", $2.str);
getsuggestions($2.str, ebuf, 1024, 1, &functions);
yyerrorex(semanticerror, ebuf);
$$.ty = gCode;
} else {
if (fd->p->head != NULL) {
char ebuf[1024];
snprintf(ebuf, 1024, "Function %s must not take any arguments when used as code", $2.str);
yyerrorex(semanticerror, ebuf);
}
if( fd->isnative ) {
char ebuf[1024];
snprintf(ebuf, 1024, "Cannot use native '%s' as code", $2.str);
yyerrorline(runtimeerror, islinebreak ? lineno - 1 : lineno, ebuf);
}
if( fd->ret == gBoolean) {
$$.ty = gCodeReturnsBoolean;
} else {
$$.ty = gCodeReturnsNoBoolean;
}
}
}
| TNULL { $$.ty = gNull; }
| expr LEQ expr { checkcomparison($1.ty, $3.ty); $$.ty = gBoolean; }
| expr GEQ expr { checkcomparison($1.ty, $3.ty); $$.ty = gBoolean; }
| expr LESS expr { checkcomparison($1.ty, $3.ty); $$.ty = gBoolean; }
| expr GREATER expr { checkcomparison($1.ty, $3.ty); $$.ty = gBoolean; }
| expr EQCOMP expr { checkeqtest($1.ty, $3.ty); $$.ty = gBoolean; }
| expr NEQ expr { checkeqtest($1.ty, $3.ty); $$.ty = gBoolean; }
| expr AND expr { canconvert($1.ty, gBoolean, 0); canconvert($3.ty, gBoolean, 0); $$.ty = gBoolean; }
| expr OR expr { canconvert($1.ty, gBoolean, 0); canconvert($3.ty, gBoolean, 0); $$.ty = gBoolean; }
| NOT expr { canconvert($2.ty, gBoolean, 0); $$.ty = gBoolean; }
| expr TIMES expr { $$.ty = binop($1.ty, $3.ty); }
| expr DIV expr { $$.ty = binop($1.ty, $3.ty); }
| expr MOD expr {
checkmodulo($1.ty, $3.ty);
$$.ty = gInteger;
}
| expr MINUS expr { $$.ty = binop($1.ty, $3.ty); }
| expr PLUS expr {
if ($1.ty == gString && $3.ty == gString)
$$.ty = gString;
else
$$.ty = binop($1.ty, $3.ty);
}
| MINUS expr { isnumeric($2.ty); $$.ty = $2.ty; }
| PLUS expr { isnumeric($2.ty); $$.ty = $2.ty; }
| LPAREN expr RPAREN { $$.ty = $2.ty; }
| funccall { $$.ty = $1.ty; }
| rid LBRACKET expr RBRACKET {
const struct typeandname *tan = getVariable($1.str);
if (!typeeq(tan->ty, gAny)) {
checkarrayindex(tan->name, $3.ty, lineno);
if (!tan->isarray) {
char ebuf[1024];
snprintf(ebuf, 1024, "%s not an array", $1.str);
yyerrorex(semanticerror, ebuf);
}
}
$$.ty = tan->ty;
}
| rid {
const struct typeandname *tan = getVariable($1.str);
if (tan->isarray) {
char ebuf[1024];
snprintf(ebuf, 1024, "Index missing for array variable %s", $1.str);
yyerrorex(semanticerror, ebuf);
}
if( !tan->isarray && !ht_lookup(&initialized, $1.str) ){
char buf[1024];
if( ht_lookup(&locals, $1.str)){
snprintf(buf, 1024, "Variable %s is uninitialized", $1.str);
yyerrorline(semanticerror, lineno, buf);
}else if(ht_lookup(&uninitialized_globals, $1.str)){
if(infunction ){
if( flagenabled(flag_checkglobalsinit) ){
snprintf(buf, 1024, "Variable %s might be uninitalized", $1.str);
yyerrorline(semanticerror, lineno, buf);
}
}else{
snprintf(buf, 1024, "Variable %s is uninitalized", $1.str);
yyerrorline(semanticerror, lineno, buf);
}
}
}
$$.ty = tan->ty;
}
| expr EQUALS expr {yyerrorex(syntaxerror, "Single = in expression, should probably be =="); checkeqtest($1.ty, $3.ty); $$.ty = gBoolean;}
| LPAREN expr {yyerrorex(syntaxerror, "Mssing ')'"); $$.ty = $2.ty;}
// incomplete expressions
| expr LEQ { checkcomparisonsimple($1.ty); yyerrorex(syntaxerror, "Missing expression for comparison"); $$.ty = gBoolean; }
| expr GEQ { checkcomparisonsimple($1.ty); yyerrorex(syntaxerror, "Missing expression for comparison"); $$.ty = gBoolean; }
| expr LESS { checkcomparisonsimple($1.ty); yyerrorex(syntaxerror, "Missing expression for comparison"); $$.ty = gBoolean; }
| expr GREATER { checkcomparisonsimple($1.ty); yyerrorex(syntaxerror, "Missing expression for comparison"); $$.ty = gBoolean; }
| expr EQCOMP { yyerrorex(syntaxerror, "Missing expression for comparison"); $$.ty = gBoolean; }
| expr NEQ { yyerrorex(syntaxerror, "Missing expression for comparison"); $$.ty = gBoolean; }
| expr AND { canconvert($1.ty, gBoolean, 0); yyerrorex(syntaxerror, "Missing expression for logical and"); $$.ty = gBoolean; }
| expr OR { canconvert($1.ty, gBoolean, 0); yyerrorex(syntaxerror, "Missing expression for logical or"); $$.ty = gBoolean; }
| NOT { yyerrorex(syntaxerror, "Missing expression for logical negation"); $$.ty = gBoolean; }
;
funccall: rid LPAREN exprlistcompl RPAREN {
$$ = checkfunccall($1.str, $3.pl);
}
| rid LPAREN exprlistcompl newline {
yyerrorex(syntaxerror, "Missing ')'");
$$ = checkfunccall($1.str, $3.pl);
}
;
exprlistcompl: /* empty */ { $$.pl = newparamlist(); }
| exprlist { $$.pl = $1.pl; }
;
exprlist: expr { $$.pl = newparamlist(); addParam($$.pl, newtypeandname($1.ty, "")); }
| expr COMMA exprlist { $$.pl = $3.pl; addParam($$.pl, newtypeandname($1.ty, "")); }
;
stringexpr: STRINGLIT {
int len = strlen(stringlit_buff);
if( len > 1023 ){
yyerrorex(semanticerror, "String literals over 1023 chars long crash the game upon loading a saved game.");
}
if(flagenabled(flag_checkstringhash)){
$$.ty = ht_lookup(&string_literals, stringlit_buff);
if( $$.ty == NULL ) {
$$.ty = newtypenode(stringlit_buff, gString);
ht_put(&string_literals, $$.ty->typename, $$.ty);
}
}else{
$$.ty = gString;
}
};
realexpr: REALLIT {
$$.ty = gReal;
if(flagenabled(flag_checknumberliterals))
checkreallit(yytext);
};
boolexpr: boollit { $$.ty = gBoolean; }
;
boollit: TTRUE
| TFALSE
;
intexpr: INTLIT {
$$.ty = gInteger;
if(flagenabled(flag_checknumberliterals))
checkintlit(yytext);
} | UNITTYPEINT { $$.ty = gInteger; }
;
funcdecl: nativefuncdecl { $$.fd = $1.fd; }
| CONSTANT nativefuncdecl { $$.fd = $2.fd; }
| funcdefncore { $$.fd = $1.fd; }
;
nativefuncdecl: NATIVE rid TAKES optparam_list RETURNS opttype
{
infunction = 1;
if(encoutered_first_function){
yyerrorex(semanticerror, "Native declared after functions");
}
$$ = checkfunctionheader($2.str, $4.pl, $6.ty);
$$.fd->isconst = isconstant;
$$.fd->isnative = true;
if( !strcmp("Filter", $$.fd->name) ){
fFilter = $$.fd;
}else if( !strcmp("Condition", $$.fd->name) ){
fCondition = $$.fd;
}else if( !strcmp("StringHash", $$.fd->name) ){
fStringHash = $$.fd;
}
ht_clear(&locals);
ht_clear(&initialized);
infunction = 0;
fCurrent = NULL;
}
;
funcdefn: newline
| funcdefncore
| statement { yyerrorex(syntaxerror, "Statement outside of function"); }
;
funcdefncore: funcbegin localblock codeblock funcend {
if(retval != gNothing) {
if(!getTypeTag($3.ty))
yyerrorline(semanticerror, lineno - 1, "Missing return");
else if ( flagenabled(flag_rb) )
canconvertreturn($3.ty, retval, -1);
}
fnannotations = pjass_flags;
}
| funcbegin localblock codeblock {
char msg[1024];
block_missing_error(msg, 1024);
yyerrorex(syntaxerror, msg);
ht_clear(&locals);
ht_clear(&initialized);
fnannotations = pjass_flags;
}
;
funcend: ENDFUNCTION {
ht_clear(&locals);
ht_clear(&initialized);
inblock = 0;
inconstant = 0;
infunction = 0;
char msg[1024];
if(! block_pop(Function, msg, 1024)){
yyerrorex(syntaxerror, msg);
}
}
;
returnorreturns: RETURNS
| RETURN {yyerrorex(syntaxerror,"Expected \"returns\" instead of \"return\"");}
;
funcbegin: FUNCTION rid TAKES optparam_list returnorreturns opttype {
inconstant = 0;
infunction = 1;
encoutered_first_function = 1;
$$ = checkfunctionheader($2.str, $4.pl, $6.ty);
$$.fd->isconst = 0;
block_push(lineno, Function);
}
| CONSTANT FUNCTION rid TAKES optparam_list returnorreturns opttype {
inconstant = 1;
infunction = 1;
encoutered_first_function = 1;
$$ = checkfunctionheader($3.str, $5.pl, $7.ty);
$$.fd->isconst = 1;
block_push(lineno, Function);
}
;
codeblock: /* empty */ { $$.ty = gEmpty; }
| statement codeblock {
if(typeeq($2.ty, gEmpty))
$$.ty = $1.ty;
else
$$.ty = mkretty($2.ty, getTypeTag($1.ty) || getTypeTag($2.ty) );
}
;
statement:
newline { $$.ty = gEmpty; }
| CALL funccall newline { $$.ty = gAny; }
/*1 2 3 4 5 6 7 8 9 */
| ifstart expr THEN newline codeblock elsifseq elseseq ifend newline {
canconvert($2.ty, gBoolean, -1);
$$.ty = combinetype($5.ty, combinetype($6.ty, $7.ty));
}
| SET rid EQUALS expr newline {
const struct typeandname *tan = getVariable($2.str);
if (tan->isarray) {
char ebuf[1024];
snprintf(ebuf, 1024, "Index missing for array variable %s", $2.str);
yyerrorline(semanticerror, lineno - 1, ebuf);
}
canconvert($4.ty, tan->ty, -1);
$$.ty = gAny;
if (tan->isconst) {
char ebuf[1024];
snprintf(ebuf, 1024, "Cannot assign to constant %s", $2.str);
yyerrorline(semanticerror, lineno - 1, ebuf);
}
if (inconstant)
validateGlobalAssignment($2.str);
if(infunction && !ht_lookup(&initialized, $2.str)){
ht_put(&initialized, $2.str, (void*)1);
}
$$.ty = gAny;
}
| SET rid rid EQUALS expr newline {
char ebuf[1024];
if(ht_lookup(&types, $2.str) || ht_lookup(&builtin_types, $3.str) ){
snprintf(ebuf, 1024, ">%s< %s is an error here. The type only needs to be stated at declartion time", $2.str, $3.str);
}else if(ht_lookup(&types, $3.str) || ht_lookup(&builtin_types, $3.str) ){
snprintf(ebuf, 1024, "%s >%s< is an error here. The type only needs to be stated at declartion time", $2.str, $3.str);
}else{
snprintf(ebuf, 1024, "Unexpected '%s'", $3.str);
}
yyerrorline(syntaxerror, lineno -1, ebuf);
$$.ty = gAny;
}
| SET rid LBRACKET expr RBRACKET EQUALS expr newline{
const struct typeandname *tan = getVariable($2.str);
$$.ty = gAny;
if (tan->ty != gAny) {
checkarrayindex(tan->name, $4.ty, lineno -1);
if (!tan->isarray) {
char ebuf[1024];
snprintf(ebuf, 1024, "%s is not an array", $2.str);
yyerrorline(semanticerror, lineno - 1, ebuf);
}
canconvert($7.ty, tan->ty, -1);
if (inconstant)
validateGlobalAssignment($2.str);
}
$$.ty = gAny;
}
| loopstart newline codeblock loopend newline { $$.ty = $3.ty; }
| loopstart newline codeblock {
char msg[1024];
block_missing_error(msg, 1024);
yyerrorex(syntaxerror, msg);
$$.ty = $3.ty;
}
| EXITWHEN expr newline {
canconvert($2.ty, gBoolean, -1);
if (!inloop)
yyerrorline(syntaxerror, lineno - 1, "Exitwhen outside of loop");
$$.ty = gAny;
}
| RETURN expr newline {
if(retval == gNothing)
yyerrorline(semanticerror, lineno - 1, "Cannot return value from function that returns nothing");
else if (! flagenabled(flag_rb) )
canconvertreturn($2.ty, retval, 0);
$$.ty = mkretty($2.ty, 1);
}
| RETURN newline {
if (retval != gNothing)
yyerrorline(semanticerror, lineno - 1, "Return nothing in function that should return value");
$$.ty = mkretty(gAny, 1);
}
| DEBUG statement { $$.ty = gAny; }
/*1 2 3 4 5 6 7 */
| ifstart expr THEN newline codeblock elsifseq elseseq {
canconvert($2.ty, gBoolean, -1);
$$.ty = combinetype($5.ty, combinetype($6.ty, $7.ty));
char msg[1024];
block_missing_error(msg, 1024);
yyerrorex(syntaxerror, msg);
}
| ifstart expr newline {
canconvert($2.ty, gBoolean, -1);
$$.ty = gAny;
yyerrorex(syntaxerror, "Missing then or non valid expression");
}
| SET funccall newline {
$$.ty = gAny;
yyerrorline(semanticerror, lineno - 1, "Call expected instead of set");
}
| lvardecl {
$$.ty = gAny;
yyerrorex(semanticerror, "Local declaration after first statement");
}
| funccall newline {
yyerrorline(syntaxerror, lineno-1, "Missing 'call'");
$$.ty = gAny;
}
| error { $$.ty = gAny; }
;
loopstart: LOOP {
inloop++;
block_push(lineno, Loop);
};
loopend: ENDLOOP {
inloop--;
char msg[1024];
if(! block_pop(Loop, msg, 1024)){
yyerrorex(syntaxerror, msg);
}
};
ifstart: IF {
block_push(lineno, If);
};
ifend: ENDIF {
char msg[1024];
if(! block_pop(If, msg, 1024)){
yyerrorex(syntaxerror, msg);
}
};
elseseq: /* empty */ { $$.ty = gAny; }
| ELSE newline codeblock {
$$.ty = $3.ty;
}
;
elsifseq: /* empty */ { $$.ty = mkretty(gEmpty, 1); }
/* 1 2 3 4 5 6 */
| ELSEIF expr THEN newline codeblock elsifseq {
canconvert($2.ty, gBoolean, -1);
if(typeeq($6.ty, gEmpty)){
if(typeeq($5.ty, gEmpty)){
$$.ty = mkretty(gAny, 0);
}else{
$$.ty = $5.ty;
}
}else{
$$.ty = combinetype($5.ty, $6.ty);
}
}
;
optparam_list: param_list { $$.pl = $1.pl; }
| NOTHING { $$.pl = newparamlist(); }
;
opttype: NOTHING { $$.ty = gNothing; }
| type { $$.ty = $1.ty; }
;
param_list: typeandname { $$.pl = newparamlist(); addParam($$.pl, $1.tan); }
| typeandname COMMA param_list { addParam($3.pl, $1.tan); $$.pl = $3.pl; }
;
rid: ID {
$$.str = strdup(yytext);
checkidlength($$.str);
}
| ALIAS {
$$.str = strdup("alias");
yyerrorex(syntaxerror, "Invalid name \"alias\"");
}
| TYPE {
$$.str = strdup("type");
yyerrorex(syntaxerror, "Invalid name \"type\"");
}
;
vartypedecl: type rid {
struct typeandname *tan = newtypeandname($1.ty, $2.str);
tan->lineno = lineno;
$$ = checkvartypedecl(tan);
}
| CONSTANT type rid {
if (infunction) {
yyerrorex(semanticerror, "Local constants are not allowed");
}
struct typeandname *tan = newtypeandname($2.ty, $3.str);
tan->isconst = 1;
$$ = checkvartypedecl(tan);
}
| type ARRAY rid {
struct typeandname *tan = newtypeandname($1.ty, $3.str);
tan->isarray = 1;
$$ = checkarraydecl(tan);
}
;
localblock: endlocalsmarker
| lvardecl localblock
| newline localblock
;
endlocalsmarker: /* empty */ { fCurrent = NULL; }
;
lvardecl: LOCAL vardecl { }
| vardecl { yyerrorex(syntaxerror, "Missing 'local'"); }
| CONSTANT LOCAL vardecl { yyerrorex(syntaxerror, "Local variables can not be declared constant"); }
| typedef { yyerrorex(syntaxerror,"Types can not be extended inside functions"); }
| funccall newline {
yyerrorline(syntaxerror, lineno-1, "Missing 'call'");
$$.ty = gAny;
}
;
vardecl: vartypedecl newline {
const struct typeandname *tan = getVariable($1.str);
if (tan->isconst) {
yyerrorline(syntaxerror, lineno - 1, "Constants must be initialized");
}
if(inglobals){
ht_put(&uninitialized_globals, $1.str, (void*)1);
}
$$.ty = gNothing;
}
| vartypedecl EQUALS expr newline {
const struct typeandname *tan = getVariable($1.str);
if (tan->isarray) {
yyerrorex(syntaxerror, "Arrays cannot be directly initialized");
}
if(infunction ){
ht_put(&initialized, tan->name, (void*)1);
}
canconvert($3.ty, tan->ty, -1);
$$.ty = gNothing;
}
| error
;
// TODO: add some tests for this
typedef: TYPE rid EXTENDS type {
check_name_allready_defined(&functions, $2.str, "%s already defined as function");
check_name_allready_defined(&globals, $2.str, "%s already defined as global");
check_name_allready_defined(&builtin_types, $2.str, "%s already defined as type");
check_name_allready_defined(&types, $2.str, "%s already defined as type");
ht_put(&types, $2.str, newtypenode($2.str, $4.ty));
}
;
typeandname: type rid { $$.tan = newtypeandname($1.ty, $2.str); }
;
type: rid {
$$.ty = ht_lookup(&types, $1.str);
if ($$.ty == NULL) {
$$.ty = ht_lookup(&builtin_types, $1.str);
if( $$.ty == NULL) {
char buf[1024];
snprintf(buf, 1024, "Undefined type %s", $1.str);
getsuggestions($1.str, buf, 1024, 1, &types);
yyerrorex(semanticerror, buf);
$$.ty = gAny;
}
}
}
;
newline: NEWLINE;