-
Notifications
You must be signed in to change notification settings - Fork 11
/
analysis.c
1070 lines (831 loc) · 25.8 KB
/
analysis.c
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Project: Implementace překladače imperativního jazyka IFJ17.
*
* @brief Syntactical and semantical analysis imeplementation.
*
* @author Timotej Halás <xhalas10@stud.fit.vutbr.cz>
* @author Matej Karas <xkaras34@stud.fit.vutbr.cz>
* @author Dominik Harmim <xharmi00@stud.fit.vutbr.cz>
*/
#include "dynamic_string.h"
#include "error.h"
#include "symtable.h"
#include "analysis.h"
#include "code_generator.h"
#define IS_VALUE(token) \
(token).type == TOKEN_TYPE_DOUBLE_NUMBER \
|| (token).type == TOKEN_TYPE_INT_NUMBER \
|| (token).type == TOKEN_TYPE_STRING \
|| (token).type == TOKEN_TYPE_IDENTIFIER
#define GET_TOKEN() \
if ((result = get_next_token(&data->token)) != SCANNER_TOKEN_OK)\
return result
#define CHECK_TYPE(_type) \
if (data->token.type != (_type)) return SYNTAX_ERR
#define CHECK_RULE(rule) \
if ((result = rule(data))) return result
#define CHECK_KEYWORD(_keyword) \
if ( \
data->token.type != TOKEN_TYPE_KEYWORD \
|| data->token.attribute.keyword != (_keyword) \
) return SYNTAX_ERR
#define GET_TOKEN_AND_CHECK_TYPE(_type) \
do { \
GET_TOKEN(); \
CHECK_TYPE(_type); \
} while(0)
#define GET_TOKEN_AND_CHECK_RULE(rule) \
do { \
GET_TOKEN(); \
CHECK_RULE(rule); \
} while(0)
#define GET_TOKEN_AND_CHECK_KEYWORD(_keyword) \
do { \
GET_TOKEN(); \
CHECK_KEYWORD(_keyword); \
} while(0)
// forward declarations
static int scope(PData* data);
static int end(PData* data);
static int params(PData* data);
static int param_n(PData* data);
static int statement(PData* data);
static int def_var(PData* data);
static int def_value(PData* data);
static int arg(PData* data);
static int arg_n(PData* data);
static int value(PData* data);
static int print(PData* data);
/**
* Implementation of <prog> rule.
*
* @return Given exit code.
*/
static int prog(PData* data)
{
int result;
// <prog> -> DECLARE FUNCTION ID ( <params> ) AS TYPE <prog>
if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_DECLARE)
{
data->in_declaration = true;
data->non_declared_function = true;
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_FUNCTION);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_IDENTIFIER);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_LEFT_BRACKET);
// add id to the global symbol table
bool internal_error;
data->current_id = sym_table_add_symbol(&data->global_table, data->token.attribute.string->str, &internal_error);
if (!data->current_id)
{
if (internal_error) return ERROR_INTERNAL;
else return SEM_ERR_UNDEFINED_VAR;
}
GET_TOKEN_AND_CHECK_RULE(params);
CHECK_TYPE(TOKEN_TYPE_RIGHT_BRACKET);
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_AS);
// get next token
GET_TOKEN();
// check for <type> rule
if (data->token.type == TOKEN_TYPE_KEYWORD)
{
switch (data->token.attribute.keyword)
{
case KEYWORD_INTEGER:
data->current_id->type = TYPE_INT;
break;
case KEYWORD_DOUBLE:
data->current_id->type = TYPE_DOUBLE;
break;
case KEYWORD_STRING:
data->current_id->type = TYPE_STRING;
break;
default:
return SYNTAX_ERR;
}
}
else
return SYNTAX_ERR;
// get next token and execute <prog> rule
GET_TOKEN();
return prog(data);
}
// <prog> -> FUNCTION ID ( <params> ) AS TYPE EOL <statement> END FUNCTION <prog>
else if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_FUNCTION)
{
data->in_function = true;
data->in_declaration = false;
data->label_index = 0;
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_IDENTIFIER);
// if function wasn't declared add it to the global symbol table
bool internal_error;
data->current_id = sym_table_add_symbol(&data->global_table, data->token.attribute.string->str, &internal_error);
if (data->current_id)
data->non_declared_function = true;
else
{
if (internal_error) return ERROR_INTERNAL;
data->non_declared_function = false;
data->current_id = sym_table_search(&data->global_table, data->token.attribute.string->str);
if (data->current_id->defined)
return SEM_ERR_UNDEFINED_VAR;
}
GENERATE_CODE(generate_function_start, data->current_id->identifier);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_LEFT_BRACKET);
GET_TOKEN_AND_CHECK_RULE(params);
CHECK_TYPE(TOKEN_TYPE_RIGHT_BRACKET);
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_AS);
// get next token
GET_TOKEN();
// check for <type> rule
if (data->token.type == TOKEN_TYPE_KEYWORD)
{
switch (data->token.attribute.keyword)
{
case KEYWORD_INTEGER:
if (!data->non_declared_function && data->current_id->type != TYPE_INT)
return SEM_ERR_UNDEFINED_VAR;
data->current_id->type = TYPE_INT;
break;
case KEYWORD_DOUBLE:
if (!data->non_declared_function && data->current_id->type != TYPE_DOUBLE)
return SEM_ERR_UNDEFINED_VAR;
data->current_id->type = TYPE_DOUBLE;
break;
case KEYWORD_STRING:
if (!data->non_declared_function && data->current_id->type != TYPE_STRING)
return SEM_ERR_UNDEFINED_VAR;
data->current_id->type = TYPE_STRING;
break;
default:
return SYNTAX_ERR;
}
}
else
return SYNTAX_ERR;
GENERATE_CODE(generate_function_retval, data->current_id->type);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
GET_TOKEN_AND_CHECK_RULE(statement);
CHECK_KEYWORD(KEYWORD_END);
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_FUNCTION);
GENERATE_CODE(generate_function_end, data->current_id->identifier);
// Current function is defined
data->current_id->defined = true;
// clear local symtable
sym_table_free(&data->local_table);
// get next token and execute <prog> rule
GET_TOKEN();
return prog(data);
}
// <prog> -> EOL <prog>
else if (data->token.type == TOKEN_TYPE_EOL)
{
GET_TOKEN();
return prog(data);
}
// <prog> -> <scope>
else
{
return scope(data);
}
return SYNTAX_ERR;
}
/**
* Implementation of <scope> rule.
*
* @return Given exit code.
*/
static int scope(PData* data)
{
int result;
// <prog> -> SCOPE EOL <statement> END SCOPE <end>
if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_SCOPE)
{
// check if all functions are defined
for (int i = 0; i < MAX_SYMTABLE_SIZE; i++)
for (Sym_table_item* it = data->global_table[i]; it != NULL; it = it->next)
if (!it->data.defined) return SEM_ERR_UNDEFINED_VAR;
// we are in scope
data->in_function = false;
data->current_id = NULL;
data->label_index = 0;
GENERATE_CODE(generate_main_scope_start);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
GET_TOKEN_AND_CHECK_RULE(statement);
CHECK_KEYWORD(KEYWORD_END);
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_SCOPE);
GENERATE_CODE(generate_main_scope_end);
// clear local symbol table
sym_table_free(&data->local_table);
// get next token and execute <prog> rule
GET_TOKEN();
return end(data);
}
return SYNTAX_ERR;
}
/**
* Implementation of <end> rule.
*
* @return Given exit code.
*/
static int end(PData* data)
{
int result;
// <prog> -> EOL <end>
if (data->token.type == TOKEN_TYPE_EOL)
{
GET_TOKEN();
return end(data);
}
// <end> -> EOF
if (data->token.type == TOKEN_TYPE_EOF)
{
return SYNTAX_OK;
}
return SYNTAX_ERR;
}
/**
* Implementation of <type> rule. Only for variables.
*
* @return Given exit code.
*/
static int type(PData* data)
{
if (data->token.type == TOKEN_TYPE_KEYWORD)
{
switch (data->token.attribute.keyword)
{
case KEYWORD_INTEGER:
if (data->non_declared_function)
{
if (!sym_table_add_param(data->current_id, TYPE_INT)) return ERROR_INTERNAL;
}
else if (data->current_id->params->str[data->param_index] != 'i') return SEM_ERR_UNDEFINED_VAR;
if (!data->in_declaration) data->rhs_id->type = TYPE_INT;
break;
case KEYWORD_DOUBLE:
if (data->non_declared_function)
{
if (!sym_table_add_param(data->current_id, TYPE_DOUBLE)) return ERROR_INTERNAL;
}
else if (data->current_id->params->str[data->param_index] != 'd') return SEM_ERR_UNDEFINED_VAR;
if (!data->in_declaration) data->rhs_id->type = TYPE_DOUBLE;
break;
case KEYWORD_STRING:
if (data->non_declared_function)
{
if (!sym_table_add_param(data->current_id, TYPE_STRING)) return ERROR_INTERNAL;
}
else if (data->current_id->params->str[data->param_index] != 's') return SEM_ERR_UNDEFINED_VAR;
if (!data->in_declaration) data->rhs_id->type = TYPE_STRING;
break;
default:
return SYNTAX_ERR;
}
}
else
return SYNTAX_ERR;
return SYNTAX_OK;
}
/**
* Implementation of <params> rule.
*
* @return Given exit code.
*/
static int params(PData* data)
{
int result;
data->param_index = 0;
// <params> -> ID AS <type> <param_n>
if (data->token.type == TOKEN_TYPE_IDENTIFIER)
{
// if there is function named as parameter
if (sym_table_search(&data->global_table, data->token.attribute.string->str))
return SEM_ERR_UNDEFINED_VAR;
// if we are in defintion, we need to add parameters to the local symbol table
if (!data->in_declaration)
{
bool internal_error;
if (!(data->rhs_id = sym_table_add_symbol(&data->local_table, data->token.attribute.string->str, &internal_error)))
{
if (internal_error) return ERROR_INTERNAL;
else return SEM_ERR_UNDEFINED_VAR;
}
GENERATE_CODE(generate_function_param_declare, data->rhs_id->identifier, data->param_index);
}
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_AS);
GET_TOKEN_AND_CHECK_RULE(type);
GET_TOKEN_AND_CHECK_RULE(param_n);
if (data->param_index + 1 != data->current_id->params->length)
return SEM_ERR_UNDEFINED_VAR;
}
else if (!data->in_declaration && data->current_id->params->length)
return SEM_ERR_UNDEFINED_VAR;
// <params> -> ε
return SYNTAX_OK;
}
/**
* Implementation of <param_n> rule.
*
* @return Given exit code.
*/
static int param_n(PData* data)
{
int result;
// <param_n> -> , ID AS <type> <param_n>
if (data->token.type == TOKEN_TYPE_COMMA)
{
data->param_index++;
if (!data->in_declaration && !data->non_declared_function && data->param_index == data->current_id->params->length)
return SEM_ERR_UNDEFINED_VAR;
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_IDENTIFIER);
// if we are in defintion, we need to add ID to the local symbol table
if (!data->in_declaration)
{
bool internal_error;
if (!(data->rhs_id = sym_table_add_symbol(&data->local_table, data->token.attribute.string->str, &internal_error)))
{
if (internal_error) return ERROR_INTERNAL;
else return SEM_ERR_UNDEFINED_VAR;
}
GENERATE_CODE(generate_function_param_declare, data->rhs_id->identifier, data->param_index);
}
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_AS);
GET_TOKEN_AND_CHECK_RULE(type);
// get next token and execute <param_n> rule
GET_TOKEN();
return param_n(data);
}
// <param_n> -> ε
return SYNTAX_OK;
}
/**
* Implementation of <statement> rule.
*
* @return Given exit code.
*/
static int statement(PData* data)
{
int result;
// <statement> -> DIM ID AS TYPE <def_var> EOL <statement>
if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_DIM)
{
if (data->in_while_or_if) return SYNTAX_ERR;
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_IDENTIFIER);
// add id to the local symbol table
bool internal_error;
data->lhs_id = sym_table_add_symbol(&data->local_table, data->token.attribute.string->str, &internal_error);
if (!data->lhs_id || sym_table_search(&data->global_table, data->token.attribute.string->str))
{
if (internal_error) return ERROR_INTERNAL;
else return SEM_ERR_UNDEFINED_VAR;
}
GENERATE_CODE(generate_var_declare, data->lhs_id->identifier);
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_AS);
// get next token and check for TYPE token
GET_TOKEN();
if (data->token.type == TOKEN_TYPE_KEYWORD)
{
switch (data->token.attribute.keyword)
{
case KEYWORD_INTEGER:
data->lhs_id->type = TYPE_INT;
break;
case KEYWORD_DOUBLE:
data->lhs_id->type = TYPE_DOUBLE;
break;
case KEYWORD_STRING:
data->lhs_id->type = TYPE_STRING;
break;
default:
return SYNTAX_ERR;
}
}
else
return SYNTAX_ERR;
GET_TOKEN_AND_CHECK_RULE(def_var);
CHECK_TYPE(TOKEN_TYPE_EOL);
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> IF <expression> THEN EOL <statement> ELSE EOL <statement> END IF EOL <statement>
else if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_IF)
{
data->label_deep++;
data->in_while_or_if = true;
data->lhs_id = sym_table_search(&data->global_table, "%exp_result");
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
data->lhs_id->type = TYPE_BOOL;
char *function_id = data->current_id ? data->current_id->identifier : "";
int current_label_index = data->label_index;
data->label_index += 2;
GENERATE_CODE(generate_if_head);
GET_TOKEN_AND_CHECK_RULE(expression);
CHECK_KEYWORD(KEYWORD_THEN);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
GENERATE_CODE(generate_if_start, function_id, current_label_index, data->label_deep);
GET_TOKEN_AND_CHECK_RULE(statement);
CHECK_KEYWORD(KEYWORD_ELSE);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
GENERATE_CODE(generate_if_else_part, function_id, current_label_index, data->label_deep);
GET_TOKEN_AND_CHECK_RULE(statement);
CHECK_KEYWORD(KEYWORD_END);
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_IF);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
GENERATE_CODE(generate_if_end, function_id, current_label_index + 1, data->label_deep);
data->label_deep--;
data->in_while_or_if = false;
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> DO WHILE <expression> EOL <statement> LOOP EOL <statement>
else if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_DO)
{
data->label_deep++;
data->in_while_or_if = true;
GET_TOKEN_AND_CHECK_KEYWORD(KEYWORD_WHILE);
data->lhs_id = sym_table_search(&data->global_table, "%exp_result");
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
data->lhs_id->type = TYPE_BOOL;
char *function_id = data->current_id ? data->current_id->identifier : "";
int current_label_index = data->label_index;
data->label_index += 2;
GENERATE_CODE(generate_while_head, function_id, current_label_index, data->label_deep);
GET_TOKEN_AND_CHECK_RULE(expression);
CHECK_TYPE(TOKEN_TYPE_EOL);
GENERATE_CODE(generate_while_start, function_id, current_label_index + 1, data->label_deep);
GET_TOKEN_AND_CHECK_RULE(statement);
CHECK_KEYWORD(KEYWORD_LOOP);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
GENERATE_CODE(generate_while_end, function_id, current_label_index + 1, data->label_deep);
data->label_deep--;
data->in_while_or_if = false;
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> ID = <def_value> EOL <statement>
else if (data->token.type == TOKEN_TYPE_IDENTIFIER)
{
// check for existence of variable
data->lhs_id = sym_table_search(&data->local_table, data->token.attribute.string->str);
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_ASSIGN);
GET_TOKEN_AND_CHECK_RULE(def_value);
CHECK_TYPE(TOKEN_TYPE_EOL);
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> INPUT ID EOL <statement>
else if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_INPUT)
{
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_IDENTIFIER);
data->lhs_id = sym_table_search(&data->local_table, data->token.attribute.string->str);
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
if (data->lhs_id->type != TYPE_INT && data->lhs_id->type != TYPE_DOUBLE && data->lhs_id->type != TYPE_STRING)
{
return SEM_ERR_OTHER;
}
GENERATE_CODE(generate_input, data->lhs_id->identifier, data->lhs_id->type);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_EOL);
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> PRINT <expression> ; <print> EOL <statement>
else if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_PRINT)
{
data->lhs_id = sym_table_search(&data->global_table, "%exp_result");
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
data->lhs_id->type = TYPE_UNDEFINED;
GET_TOKEN_AND_CHECK_RULE(expression);
CHECK_TYPE(TOKEN_TYPE_SEMICOLON);
GENERATE_CODE(generate_print);
GET_TOKEN_AND_CHECK_RULE(print);
CHECK_TYPE(TOKEN_TYPE_EOL);
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> RETURN <expression> EOL <statement>
else if (data->token.type == TOKEN_TYPE_KEYWORD && data->token.attribute.keyword == KEYWORD_RETURN)
{
// scope doesn't have this type of rule
if (!data->in_function) return SYNTAX_ERR;
data->lhs_id = sym_table_search(&data->global_table, "%exp_result");
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
data->lhs_id->type = data->current_id->type;
GET_TOKEN_AND_CHECK_RULE(expression);
GENERATE_CODE(generate_function_return, data->current_id->identifier);
CHECK_TYPE(TOKEN_TYPE_EOL);
// get next token and execute <statement> rule
GET_TOKEN();
return statement(data);
}
// <statement> -> ε
else if (data->token.type == TOKEN_TYPE_EOL)
{
GET_TOKEN();
return statement(data);
}
return SYNTAX_OK;
}
/**
* Implementation of <def_var> rule.
*
* @return Given exit code.
*/
static int def_var(PData* data)
{
int result;
GENERATE_CODE(generate_var_default_value, data->lhs_id->identifier, data->lhs_id->type);
// <def_var> -> = <expression>
if (data->token.type == TOKEN_TYPE_ASSIGN)
{
GET_TOKEN_AND_CHECK_RULE(expression);
}
// <def_var> -> ε
return SYNTAX_OK;
}
/**
* Implementation of <def_value> rule.
*
* @return Given exit code.
*/
static int def_value(PData* data)
{
int result;
if (data->token.type == TOKEN_TYPE_IDENTIFIER || data->token.type == TOKEN_TYPE_KEYWORD)
{
// <def_value> -> ID ( <arg> )
if (data->token.type == TOKEN_TYPE_IDENTIFIER)
{
data->rhs_id = sym_table_search(&data->global_table, data->token.attribute.string->str);
}
if (data->token.type == TOKEN_TYPE_KEYWORD)
{
switch (data->token.attribute.keyword)
{
// <def_value> -> ASC ( <arg> )
case KEYWORD_ASC:
data->rhs_id = sym_table_search(&data->global_table, "asc");
break;
// <def_value> -> CHR ( <arg> )
case KEYWORD_CHR:
data->rhs_id = sym_table_search(&data->global_table, "chr");
break;
// <def_value> -> LENGTH ( <arg> )
case KEYWORD_LENGTH:
data->rhs_id = sym_table_search(&data->global_table, "length");
break;
// <def_value> -> SUBSTR ( <arg> )
case KEYWORD_SUBSTR:
data->rhs_id = sym_table_search(&data->global_table, "substr");
break;
default:
return SYNTAX_ERR;
}
}
if (data->rhs_id)
{
// check type compatibilty
// if either one expression is string, we cannot implicitly convert
if (data->lhs_id->type != data->rhs_id->type)
if (data->lhs_id->type == TYPE_STRING || data->rhs_id->type == TYPE_STRING)
return SEM_ERR_TYPE_COMPAT;
GENERATE_CODE(generate_function_before_pass_params);
GET_TOKEN_AND_CHECK_TYPE(TOKEN_TYPE_LEFT_BRACKET);
GET_TOKEN_AND_CHECK_RULE(arg);
CHECK_TYPE(TOKEN_TYPE_RIGHT_BRACKET);
GET_TOKEN();
if (data->rhs_id->params->length != data->param_index) return SEM_ERR_TYPE_COMPAT;
GENERATE_CODE(generate_function_call, data->rhs_id->identifier);
GENERATE_CODE(generate_function_retval_assign, data->lhs_id->identifier, data->lhs_id->type, data->rhs_id->type);
return SYNTAX_OK;
}
data->rhs_id = sym_table_search(&data->local_table, data->token.attribute.string->str);
if (!data->rhs_id)
return SEM_ERR_UNDEFINED_VAR;
}
// <def_value> -> <expression>
CHECK_RULE(expression);
return SYNTAX_OK;
}
/**
* Implementation of <arg> rule.
*
* @return Given exit code.
*/
static int arg(PData* data)
{
int result;
// currently processed argument
data->param_index = 0;
// <arg> -> <value> <arg_n>
// if token is value
if (IS_VALUE(data->token))
{
CHECK_RULE(value);
GET_TOKEN_AND_CHECK_RULE(arg_n);
}
// <arg> -> ε
return SYNTAX_OK;
}
/**
* Implementation of <arg_n> rule.
*
* @return Given exit code.
*/
static int arg_n(PData* data)
{
int result;
// <arg_n> -> , <value> <arg_n>
if (data->token.type == TOKEN_TYPE_COMMA)
{
GET_TOKEN_AND_CHECK_RULE(value);
GET_TOKEN_AND_CHECK_RULE(arg_n);
}
// <arg_n> -> ε
return SYNTAX_OK;
}
/**
* Implementation of <value> rule.
*
* @return Given exit code.
*/
static int value(PData* data)
{
// check number of arguments
if (data->rhs_id->params->length == data->param_index)
return SEM_ERR_TYPE_COMPAT;
GENERATE_CODE(generate_function_pass_param, data->token, data->param_index);
switch (data->token.type)
{
// <value> -> DOUBLE_NUMBER
case TOKEN_TYPE_DOUBLE_NUMBER:
if (data->rhs_id->params->str[data->param_index] == 's')
return SEM_ERR_TYPE_COMPAT;
if (data->rhs_id->params->str[data->param_index] == 'i')
GENERATE_CODE(generate_function_convert_passed_param, TYPE_DOUBLE, TYPE_INT, data->param_index);
break;
// <value> -> INT_NUMBER
case TOKEN_TYPE_INT_NUMBER:
if (data->rhs_id->params->str[data->param_index] == 's')
return SEM_ERR_TYPE_COMPAT;
if (data->rhs_id->params->str[data->param_index] == 'd')
GENERATE_CODE(generate_function_convert_passed_param, TYPE_INT, TYPE_DOUBLE, data->param_index);
break;
// <value> -> STRING
case TOKEN_TYPE_STRING:
if (data->rhs_id->params->str[data->param_index] != 's')
return SEM_ERR_TYPE_COMPAT;
break;
// <value> -> IDENTIFIER
case TOKEN_TYPE_IDENTIFIER:; // ; C evil magic
TData* id = sym_table_search(&data->local_table, data->token.attribute.string->str);
if (!id) return SEM_ERR_UNDEFINED_VAR;
switch (id->type)
{
case TYPE_INT:
if (data->rhs_id->params->str[data->param_index] == 's')
return SEM_ERR_TYPE_COMPAT;
if (data->rhs_id->params->str[data->param_index] == 'd')
GENERATE_CODE(generate_function_convert_passed_param, TYPE_INT, TYPE_DOUBLE, data->param_index);
break;
case TYPE_DOUBLE:
if (data->rhs_id->params->str[data->param_index] == 's')
return SEM_ERR_TYPE_COMPAT;
if (data->rhs_id->params->str[data->param_index] == 'i')
GENERATE_CODE(generate_function_convert_passed_param, TYPE_DOUBLE, TYPE_INT, data->param_index);
break;
case TYPE_STRING:
if (data->rhs_id->params->str[data->param_index] != 's')
return SEM_ERR_TYPE_COMPAT;
break;
default: // shouldn't get here
return ERROR_INTERNAL;
}
break;
default:
return SYNTAX_ERR;
}
// increment argument position
data->param_index++;
return SYNTAX_OK;
}
/**
* Implementation of <print> rule.
*
* @return Given exit code.
*/
static int print(PData* data)
{
int result;
// <print> -> ε
if (data->token.type == TOKEN_TYPE_EOL)
{
return SYNTAX_OK;
}
data->lhs_id = sym_table_search(&data->global_table, "%exp_result");
if (!data->lhs_id) return SEM_ERR_UNDEFINED_VAR;
data->lhs_id->type = TYPE_UNDEFINED;
// <print> -> <expression> ; <print>
CHECK_RULE(expression);
CHECK_TYPE(TOKEN_TYPE_SEMICOLON);
GENERATE_CODE(generate_print);
GET_TOKEN_AND_CHECK_RULE(print);
return SYNTAX_OK;
}
/**
* Initialize variable needed for analysis.
*
* @return True if inicialization was successful, false otherwise.
*/
static bool init_variables(PData* data)
{
sym_table_init(&data->global_table);
sym_table_init(&data->local_table);
data->current_id = NULL;
data->lhs_id = NULL;
data->rhs_id = NULL;
data->param_index = 0;
data->label_index = 0;
data->label_deep = -1;
data->in_function = false;
data->in_declaration = false;
data->in_while_or_if = false;
data->non_declared_function = false;
// init default functions
bool internal_error;
TData* id;
// Length(s As String) As Integer
id = sym_table_add_symbol(&data->global_table, "length", &internal_error);
if (internal_error) return false;
id->defined = true;
id->type = TYPE_INT;
if (!sym_table_add_param(id, TYPE_STRING)) return false;
// SubStr(s As String, i As Integer, n As Integer) As String
id = sym_table_add_symbol(&data->global_table, "substr", &internal_error);
if (internal_error) return false;
id->defined = true;
id->type = TYPE_STRING;
if (!sym_table_add_param(id, TYPE_STRING)) return false;
if (!sym_table_add_param(id, TYPE_INT)) return false;
if (!sym_table_add_param(id, TYPE_INT)) return false;