-
Notifications
You must be signed in to change notification settings - Fork 209
/
parser.y
1099 lines (969 loc) · 26.2 KB
/
parser.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
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
/*
PROLOGUE
http://www.gnu.org/software/bison/manual/bison.html#Prologue
*/
%{
#include <stdio.h>
#include <stdlib.h>
#include "iks_grammar.h"
#include "iks_dict.h"
#include "iks_tree.h"
#include "iks_stack.h"
#include "iks_ast.h"
#include "iks_types.h"
#include "iks_gv.h"
#include "iks_iloc.h"
//#include "iks_hash.h"
iks_tree_t *ptr_function;
iks_tree_t *ptr_function_call;
iks_grammar_symbol_t *function_with_param;
iks_list_t *args;
unsigned int dimen_counter;
int coercion;
%}
/*
DECLARATIONS
*/
/* Declaração dos tokens da gramática da Linguagem IKS */
%token TK_PR_INT 256
%token TK_PR_FLOAT 257
%token TK_PR_BOOL 258
%token TK_PR_CHAR 259
%token TK_PR_STRING 260
%token TK_PR_IF 261
%token TK_PR_THEN 262
%token TK_PR_ELSE 263
%token TK_PR_WHILE 264
%token TK_PR_DO 265
%token TK_PR_INPUT 267
%token TK_PR_OUTPUT 268
%token TK_PR_RETURN 269
%token TK_OC_LE 270
%token TK_OC_GE 271
%token TK_OC_EQ 272
%token TK_OC_NE 273
%token TK_OC_AND 274
%token TK_OC_OR 275
%union {
int type;
iks_grammar_symbol_t *symbol;
iks_tree_t *nt;
iks_list_t *list;
struct reg_or_label *temp;
}
%token<symbol> TK_LIT_INT 280
%token<symbol> TK_LIT_FLOAT 281
%token<symbol> TK_LIT_FALSE 282
%token<symbol> TK_LIT_TRUE 283
%token<symbol> TK_LIT_CHAR 284
%token<symbol> TK_LIT_STRING 285
%token<symbol> TK_IDENTIFICADOR 286
%token TOKEN_ERRO 290
%start p
%type<symbol> decl
%type<list> array_decl_dimen
%type<nt> prog
%type<nt> func
%type<nt> command_block
%type<nt> command_block_f
%type<nt> command_seq
%type<nt> command
%type<nt> commands
%type<nt> ctrl_flow
%type<nt> output_list
%type<nt> expr
%type<nt> arim_expr
%type<nt> logic_expr
%type<nt> func_param_list
%type<nt> param_list
%type<nt> terminal_value
%type<nt> id
%type<nt> idv
%type<list> idv_dimen
%type<nt> func_call
%type<type> type
%right TK_PR_THEN TK_PR_ELSE
%right '='
%left '<'
%left '>'
%left TK_OC_LE TK_OC_GE TK_OC_EQ TK_OC_NE
%left TK_OC_OR
%left TK_OC_AND
%left '+' '-'
%left '*' '/'
%left '!'
%left INVERSAO
%%
/*
GRAMMAR RULES
http://www.gnu.org/software/bison/manual/bison.html#Rules
*/
/* 2 */
p:
{
reg_ctrl = 0; //migrar para funcao de inicializacao
label_ctrl = 0;
ptr_function=NULL;
ptr_function_call=NULL;
function_with_param=NULL;
iks_dict_t *symbol_table_global;
symbol_table_global = new_iks_dict();
//scope->st = iks_stack_push(scope->st,(void*)symbol_table_global);
scope_t *scope = new_scope();
scope->st = symbol_table_global;
scope->type=IKS_SCOPE_GLOBAL;
scopes = new_iks_stack();
scopes = iks_stack_push(scopes,(void*)scope);
// just the first function is child of ast
iks_ast_node_value_t *v;
v = new_iks_ast_node_value();
iks_ast_node_value_set(v,IKS_AST_PROGRAMA,NULL);
iks_tree_set_item(ast,(void*)v);
gv_declare(IKS_AST_PROGRAMA,ast,NULL);
dimen_counter = 0;
} prog
;
prog:
prog global_decl
{
//symbol_table_print((iks_dict_t*)iks_stack_top(scope->st));
}
| prog func
{
/* 3.A.1 */
// just the first function is child of ast
if (iks_list_is_empty(ast->children)) {
iks_ast_connect_nodes(ast,$func);
}
else {
iks_ast_connect_nodes($1,$func);
}
//symbol_table_print((iks_dict_t*)iks_stack_top(scope->st));
$$ = $func;
//iks_ast_node_value_t *program = $$->item;
//program_iloc = program->code;
//iloc_print(program_iloc);
}
| /* empty */ {}
;
/* 2.1 */
global_decl:
decl ';'
| array_decl ';'
;
array_decl:
decl {
dimen_counter = 0;
} array_decl_dimen {
update_vector_symbol($decl,dimen_counter,$array_decl_dimen); //add information about dimensions
}
;
array_decl_dimen:
array_decl_dimen '[' TK_LIT_INT ']'
{
//append to list of dimensions
iks_list_t *list = $1;
iks_grammar_symbol_t *lit = $TK_LIT_INT;
int *size = malloc(sizeof(int));
*size = atoi(lit->value);
iks_list_append(list, (void*)size);
dimen_counter++;
$$ = list;
}
| '[' TK_LIT_INT ']'
{
//create list of dimensions
iks_grammar_symbol_t *lit = $TK_LIT_INT;
int *size = malloc(sizeof(int));
*size = atoi(lit->value);
iks_list_t *dimen = new_iks_list();
iks_list_set_item(dimen,(void*)size);
dimen_counter++;
$$ = dimen;
}
;
decl:
type ':' TK_IDENTIFICADOR
{
//if (!decl_symbol($3,$1,IKS_DECL_VAR,iks_stack_top(scope->st),function_with_param)) {
//if (!decl_symbol($TK_IDENTIFICADOR,$type,IKS_DECL_VAR,scope,function_with_param)) {
if (!decl_symbol($3,$1,IKS_DECL_VAR,iks_stack_top(scopes),function_with_param)) {
return(IKS_ERROR_DECLARED); //was already declared before
}
$$ = $TK_IDENTIFICADOR;
}
;
type:
TK_PR_INT
{
$$ = IKS_INT;
}
| TK_PR_FLOAT
{
$$ = IKS_FLOAT;
}
| TK_PR_BOOL
{
$$ = IKS_BOOL;
}
| TK_PR_CHAR
{
$$ = IKS_CHAR;
}
| TK_PR_STRING
{
$$ = IKS_STRING;
}
;
/* 2.2 */
func:
type ':' TK_IDENTIFICADOR {
//if (!decl_symbol($3,$1,IKS_DECL_FUNCTION,iks_stack_top(scope->st),function_with_param)) {
//if (!decl_symbol($TK_IDENTIFICADOR,$type,IKS_DECL_FUNCTION,scope,function_with_param)) {
if (!decl_symbol($3,$1,IKS_DECL_FUNCTION,iks_stack_top(scopes),function_with_param)) {
return(IKS_ERROR_DECLARED);
}
iks_dict_t *symbol_table_local;
symbol_table_local = new_iks_dict();
/* 3.A.2 */
iks_tree_t *funcao = iks_ast_new_node(IKS_AST_FUNCAO,$3);
iks_ast_node_value_t *funcaon = funcao->item;
funcaon->iks_type = $type;
scope_t *scope = new_scope();
scope->st = symbol_table_local;
scope->type=IKS_SCOPE_LOCAL;
scopes = iks_stack_push(scopes,(void*)scope);
//scope->st = iks_stack_push(scope->st,(void*)symbol_table_local);
ptr_function=funcao;
function_with_param=$TK_IDENTIFICADOR; //begin params decl
} '(' func_param_decl_list ')' {
function_with_param=NULL; // end params decl
} decl_list {
//symbol_table_print((iks_dict_t*)iks_stack_top(scope->st));
} command_block_f {
if ($command_block_f) {
iks_ast_connect_nodes(ptr_function,$command_block_f);
}
//iks_dict_t *st = (iks_dict_t*) iks_stack_top(scope->st);
//iks_dict_delete(st);
//symbol_table_delete(scope->st->item);
//scope->st = iks_stack_pop(scope->st);
//symbol_table_delete(scope->st);
scopes = iks_stack_pop(scopes);
$$ = ptr_function;
ptr_function = NULL; //returns are unacceptable outside functions
}
;
func_param_decl_list:
param_decl_list
| /* empty */
;
param_decl_list:
decl ',' param_decl_list
| decl
;
decl_list: // pode ser vazia?
decl ';' decl_list
| /* empty */
;
/* 2.3 */
command_block_f://two command_block because cmd_blk from function isnt in ast
'{' command_seq '}'
{
$$ = $command_seq;
}
;
command_block:
'{' command_seq '}'
{
iks_tree_t *bloco = iks_ast_new_node(IKS_AST_BLOCO,NULL);
if ($command_seq) { //because command_seq <- command <- empty is a possibility
iks_ast_connect_nodes(bloco,$command_seq);
iks_ast_node_value_t *S = $command_seq->item;
iks_ast_node_value_t *bloco_n = bloco->item;
bloco_n->code = S->code;
}
$$ = bloco;
}
;
command_seq:
command ';' command_seq
{
/* 3.A.10 */
if ($3) { //because command_seq <- command <- empty is a possibility
iks_ast_connect_nodes($command,$3);
}
}
| command
;
/* 2.4 */
command:
commands
| /* empty */
{
$$ = NULL;
}
;
commands:
func_call
| command_block
| ctrl_flow
| id '=' expr
{
/* 3.A.8 */
iks_ast_node_value_t *idn = $id->item;
iks_grammar_symbol_t *ids = idn->symbol;
if(!symbol_is_decl_type(ids,IKS_DECL_VAR)) { //was symbol declared?
return iks_error(ids,IKS_ERROR_USE);
}
$$ = iks_ast_new_node(IKS_AST_ATRIBUICAO,NULL);
iks_ast_node_value_t *atrn = $$->item;
int type = infer_type($id, $expr);
if(type > 5) //erro de coerção
return type;
atrn->iks_type = type;
iks_ast_connect_nodes($$,$id);
iks_ast_connect_nodes($$,$expr);
}
| idv '=' expr
{
/* 3.A.8 */
iks_tree_t *atribuicao = iks_ast_new_node(IKS_AST_ATRIBUICAO,NULL);
iks_ast_node_value_t *atrn = atribuicao->item;
int type = infer_type($idv, $expr);
if(type > 5) //erro de coerção
return type;
atrn->iks_type = type;
iks_ast_connect_nodes(atribuicao,$idv);
iks_ast_connect_nodes(atribuicao,$expr);
$$ = atribuicao;
}
| TK_PR_OUTPUT output_list
{
/* 3.A.7 */
iks_tree_t *output = iks_ast_new_node(IKS_AST_OUTPUT,NULL);
iks_ast_connect_nodes(output,$output_list);
$$ = output;
}
| TK_PR_INPUT expr
{
/* 3.A.6 */
iks_ast_node_value_t *exprn;
exprn = $2->item;
iks_grammar_symbol_t *exprs;
exprs = exprn->symbol;
if(exprn->type != IKS_AST_IDENTIFICADOR) {
return iks_error(exprs,IKS_ERROR_WRONG_PAR_INPUT);
} else {
iks_tree_t *input = iks_ast_new_node(IKS_AST_INPUT,NULL);
iks_ast_connect_nodes(input,$expr);
$$ = input;
}
}
| TK_PR_RETURN expr
{
iks_ast_node_value_t *exprn;
exprn = $2->item;
iks_grammar_symbol_t *exprs;
exprs = exprn->symbol;
iks_ast_node_value_t *fn;
fn = ptr_function->item;
iks_grammar_symbol_t *fs;
fs = fn->symbol;
if(exprn->iks_type != fn->iks_type) {
coercion = verify_coercion(ptr_function, $expr);
if(coercion) {
fprintf(stderr, "return: tipo %d deveria ser %d\n", exprn->iks_type, fn->iks_type);
return iks_error(exprs,IKS_ERROR_WRONG_PAR_RETURN);
}
}
/* 3.A.9 */
iks_tree_t *ret = iks_ast_new_node(IKS_AST_RETURN,NULL);
iks_ast_connect_nodes(ret,$expr);
$$ = ret;
}
;
id:
TK_IDENTIFICADOR
{
iks_grammar_symbol_t *s;
//s = search_symbol_global($1,scope->st);
s = search_symbol_global($1,scopes);
if (s) {
$$ = iks_ast_new_node(IKS_AST_IDENTIFICADOR,s);
iks_ast_node_value_t *idn = $$->item;
idn->iks_type = s->iks_type;
} else {
fprintf(stderr,"identificador não declarado\n");
return(IKS_ERROR_UNDECLARED);
}
}
;
idv:
id idv_dimen
{
iks_ast_node_value_t *idn = $id->item;
iks_grammar_symbol_t *ids = idn->symbol;
if(symbol_is_decl_type(ids,IKS_DECL_VECTOR)) { //is id a vector?
iks_tree_t *vet = iks_ast_new_node(IKS_AST_VETOR_INDEXADO,NULL);
iks_ast_connect_nodes(vet,$id);
iks_list_t *list_of_dimens = $idv_dimen;
do { //all dimension nodes will be children of the vector node
iks_ast_connect_nodes(vet,(iks_tree_t *)list_of_dimens->item);
list_of_dimens = list_of_dimens->next;
} while(list_of_dimens != $idv_dimen);
iks_ast_node_value_t *vetn = vet->item;
vetn->iks_type = idn->iks_type; //type of this ast node is that of the id
$$ = vet;
} else { //using wrong identificador. id is not a vector
return iks_error(ids,IKS_ERROR_USE);
}
}
;
idv_dimen:
idv_dimen '[' expr ']'
{
iks_tree_t *int_tree = iks_ast_new_node(IKS_AST_INDEFINIDO,NULL); //temporary tree so infer_type can infer between IKS_INT and expr
iks_ast_node_value_t *int_treen = int_tree->item;
int_treen->iks_type = IKS_INT; //index of vector should be an integer
int type = infer_type(int_tree, $expr); //checks if the type of expr can be inferred to int
if(type > 5) //coercion error
return type;
iks_ast_node_value_delete(int_treen); //removing temporary tree
iks_tree_delete(int_tree);
iks_list_append($1,$expr);
$$ = $1;
}
| '[' expr ']'
{
iks_tree_t *int_tree = iks_ast_new_node(IKS_AST_INDEFINIDO,NULL); //temporary tree so infer_type can infer between IKS_INT and expr
iks_ast_node_value_t *int_treen = int_tree->item;
int_treen->iks_type = IKS_INT; //index of vector should be an integer
int type = infer_type(int_tree, $expr); //checks if the type of expr can be inferred to int
if(type > 5) //coercion error
return type;
iks_ast_node_value_delete(int_treen); //removing temporary tree
iks_tree_delete(int_tree);
iks_list_t *list_of_dimens = new_iks_list();
iks_list_append(list_of_dimens,$expr);
$$ = list_of_dimens;
}
;
output_list:
expr
{
iks_ast_node_value_t *exprn;
exprn = $expr->item;
iks_grammar_symbol_t *exprs;
exprs = exprn->symbol;
switch(exprn->type)
{
case IKS_AST_LITERAL:
if(exprs != NULL) {
if(exprs->iks_type != IKS_STRING) {
fprintf(stderr,"line %d: '%s' não é literal string\n",exprs->code_line_number, exprs->value);
return(IKS_ERROR_WRONG_PAR_OUTPUT);
}
} else fprintf(stderr,"literal em parametro de output sem simbolo\n");
break;
case IKS_AST_ARIM_SOMA:
case IKS_AST_ARIM_SUBTRACAO:
case IKS_AST_ARIM_MULTIPLICACAO:
case IKS_AST_ARIM_DIVISAO:
case IKS_AST_ARIM_INVERSAO:
break;
default:
if (exprs) {
fprintf(stderr,"line %d: '%s' não é expressao artimetica ou string\n",exprs->code_line_number, exprs->value);
fprintf(stderr,"tipo do no: %d\n", exprn->type);
} else {
fprintf(stderr,"parametro de output não é expressao artimetica ou string\n");
fprintf(stderr,"tipo do no: %d\n", exprn->type);
}
return(IKS_ERROR_WRONG_PAR_OUTPUT);
break;
}
}
| expr ',' output_list
{
iks_ast_node_value_t *exprn;
exprn = $expr->item;
iks_grammar_symbol_t *exprs;
exprs = exprn->symbol;
switch(exprn->type)
{
case IKS_AST_LITERAL:
if(exprs != NULL) {
if(exprs->iks_type != IKS_STRING) {
fprintf(stderr,"line %d: '%s' não é literal string\n",exprs->code_line_number, exprs->value);
return(IKS_ERROR_WRONG_PAR_OUTPUT);
}
} else fprintf(stderr,"literal em parametro de output sem simbolo\n");
break;
case IKS_AST_ARIM_SOMA:
case IKS_AST_ARIM_SUBTRACAO:
case IKS_AST_ARIM_MULTIPLICACAO:
case IKS_AST_ARIM_DIVISAO:
case IKS_AST_ARIM_INVERSAO:
break;
default:
if (exprs) {
fprintf(stderr,"line %d: '%s' não é expressao artimetica ou string\n",exprs->code_line_number, exprs->value);
fprintf(stderr,"tipo do no: %d\n", exprn->type);
} else {
fprintf(stderr,"parametro de output não é expressao artimetica ou string\n");
fprintf(stderr,"tipo do no: %d\n", exprn->type);
}
return(IKS_ERROR_WRONG_PAR_OUTPUT);
break;
}
iks_ast_connect_nodes($expr,$3);
}
;
/* 2.5 */
expr:
id
{
iks_ast_node_value_t *n;
n = $id->item;
iks_grammar_symbol_t *s;
s = n->symbol;
if(!symbol_is_decl_type(s,IKS_DECL_VAR)) {
return iks_error(s,IKS_ERROR_USE);
}
}
| idv
{
$$ = $idv;
}
| terminal_value
| '(' expr ')'
{
$$ = $2;
}
| func_call
| arim_expr
| logic_expr
;
arim_expr:
expr '+' expr
{
/* 3.A.12 */
$$ = iks_ast_new_node(IKS_AST_ARIM_SOMA,NULL);
iks_ast_node_value_t *oon = $$->item;
iks_ast_node_value_t *n1 = $1->item;
iks_ast_node_value_t *n2 = $3->item;
int type = infer_type($1, $3);
if(type > 5) //erro de coerção
return type;
oon->iks_type = type;
iks_ast_connect_nodes($$,$1);
iks_ast_connect_nodes($$,$3);
}
| expr '-' expr
{
/* 3.A.12 */
$$ = iks_ast_new_node(IKS_AST_ARIM_SUBTRACAO,NULL);
iks_ast_node_value_t *oon = $$->item;
iks_ast_node_value_t *n1 = $1->item;
iks_ast_node_value_t *n2 = $3->item;
int type = infer_type($1, $3);
if(type > 5) //erro de coerção
return type;
oon->iks_type = type;
iks_ast_connect_nodes($$,$1);
iks_ast_connect_nodes($$,$3);
}
| expr '*' expr
{
/* 3.A.12 */
$$ = iks_ast_new_node(IKS_AST_ARIM_MULTIPLICACAO,NULL);
iks_ast_node_value_t *oon = $$->item;
iks_ast_node_value_t *n1 = $1->item;
iks_ast_node_value_t *n2 = $3->item;
int type = infer_type($1, $3);
if(type > 5) //erro de coerção
return type;
oon->iks_type = type;
iks_ast_connect_nodes($$,$1);
iks_ast_connect_nodes($$,$3);
}
| expr '/' expr
{
/* 3.A.12 */
$$ = iks_ast_new_node(IKS_AST_ARIM_DIVISAO,NULL);
iks_ast_node_value_t *oon = $$->item;
iks_ast_node_value_t *n1 = $1->item;
iks_ast_node_value_t *n2 = $3->item;
int type = infer_type($1, $3);
if(type > 5) //erro de coerção
return type;
oon->iks_type = type;
iks_ast_connect_nodes($$,$1);
iks_ast_connect_nodes($$,$3);
}
| '-' expr %prec INVERSAO
{
/* 3.A.15 */
iks_tree_t *oo = iks_ast_new_node(IKS_AST_ARIM_INVERSAO,NULL);
iks_ast_node_value_t *oon = oo->item;
iks_ast_node_value_t *exprn = $expr->item;
oon->iks_type = exprn->iks_type;
iks_ast_connect_nodes(oo,$expr);
$$ = oo;
}
;
logic_expr:
expr '<' expr
{
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_L,NULL);
iks_ast_node_value_t *oon = $$->item;
iks_ast_node_value_t *n1 = $1->item;
iks_ast_node_value_t *n2 = $3->item;
int type = infer_type($1, $3);
if(type > 5) //erro de coerção
return type;
oon->iks_type = type;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| expr '>' expr
{
/* 3.A.14 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_G,NULL);
iks_ast_node_value_t *oon = $$->item;
iks_ast_node_value_t *n1 = $1->item;
iks_ast_node_value_t *n2 = $3->item;
int type = infer_type($1, $3);
if(type > 5) //erro de coerção
return type;
oon->iks_type = type;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| expr TK_OC_LE expr
{
/* 3.A.14 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_LE,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| expr TK_OC_GE expr
{
/* 3.A.14 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_GE,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| expr TK_OC_EQ expr
{
/* 3.A.14 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_IGUAL,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| expr TK_OC_NE expr
{
/* 3.A.14 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_DIF,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| '!' expr
{
/* 3.A.15 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_COMP_NEGACAO,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($2,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$2);
}
| expr TK_OC_AND expr
{
/* 3.A.14 */
$$ = iks_ast_new_node(IKS_AST_LOGICO_E,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
| expr TK_OC_OR expr
{
$$ = iks_ast_new_node(IKS_AST_LOGICO_OU,NULL);
iks_ast_node_value_t *oon = $$->item;
oon->iks_type = IKS_BOOL;
coercion=verify_coercion($1,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$1);
coercion=verify_coercion($3,$$);
if (coercion) { //if coercion is invalid
return coercion;
}
iks_ast_connect_nodes($$,$3);
}
;
func_call:
id {
iks_ast_node_value_t *n;
n = $id->item;
iks_grammar_symbol_t *s;
s = n->symbol;
/* 3.A.17 */
if(symbol_is_decl_type(s,IKS_DECL_FUNCTION)) {
iks_tree_t *x = iks_ast_new_node(IKS_AST_CHAMADA_DE_FUNCAO,s);
iks_ast_node_value_t *xn = x->item;
xn->iks_type = n->iks_type;
iks_ast_connect_nodes(x,$id);
ptr_function_call=x;
} else {
return iks_error(s,IKS_ERROR_USE);
}
args = new_iks_list();
function_with_param=s;
} '(' func_param_list ')' {
// not so good, better if analyze during parser?
int arg_analyze = verify_function_args(function_with_param,args);
function_with_param=NULL;
if (arg_analyze==0) {
if ($func_param_list) { //if no params, so NULL
iks_ast_connect_nodes(ptr_function_call,$func_param_list);
}
$$ = ptr_function_call;
} else {
return arg_analyze; //arg error
}
//iks_list_delete(args);
}
;
terminal_value:
/* 3.A.11 */
TK_LIT_INT
{
$TK_LIT_INT->iks_type=IKS_INT;
$$ = iks_ast_new_node(IKS_AST_LITERAL,$TK_LIT_INT);
iks_ast_node_value_t *litn = $$->item;
litn->iks_type = $TK_LIT_INT->iks_type;
}
| TK_LIT_FLOAT
{
$TK_LIT_FLOAT->iks_type=IKS_FLOAT;
$$ = iks_ast_new_node(IKS_AST_LITERAL,$TK_LIT_FLOAT);
iks_ast_node_value_t *litn = $$->item;
litn->iks_type = $TK_LIT_FLOAT->iks_type;
}
| TK_LIT_FALSE
{
$1->iks_type=IKS_BOOL;
iks_ast_node_value_t *v1;
v1 = new_iks_ast_node_value();
iks_ast_node_value_set(v1,IKS_AST_LITERAL,$TK_LIT_FALSE);
$$ = new_iks_tree();
iks_tree_set_item($$,(void*)v1);
iks_ast_node_value_t *litn = $$->item;
litn->iks_type = $TK_LIT_FALSE->iks_type;
gv_declare(IKS_AST_LITERAL,$$,"false");
}
| TK_LIT_TRUE
{
$TK_LIT_TRUE->iks_type=IKS_BOOL;
iks_ast_node_value_t *v1;
v1 = new_iks_ast_node_value();
iks_ast_node_value_set(v1,IKS_AST_LITERAL,$TK_LIT_TRUE);
$$ = new_iks_tree();
iks_tree_set_item($$,(void*)v1);
iks_ast_node_value_t *litn = $$->item;
litn->iks_type = $TK_LIT_TRUE->iks_type;
gv_declare(IKS_AST_LITERAL,$$,"true");
}
| TK_LIT_CHAR
{
$TK_LIT_CHAR->iks_type=IKS_CHAR;
$$ = iks_ast_new_node(IKS_AST_LITERAL,$TK_LIT_CHAR);
iks_ast_node_value_t *litn = $$->item;
litn->iks_type = $TK_LIT_CHAR->iks_type;
}
| TK_LIT_STRING
{
$TK_LIT_STRING->iks_type=IKS_STRING;
$$ = iks_ast_new_node(IKS_AST_LITERAL,$TK_LIT_STRING);
iks_ast_node_value_t *litn = $$->item;
litn->iks_type = $TK_LIT_STRING->iks_type;
}
;
func_param_list:
param_list
| { $$ = NULL; } /* empty */
;
param_list:
expr
{
iks_ast_node_value_t *n;
n = $expr->item;
iks_grammar_symbol_t *s;
s = n->symbol;
iks_list_t *l;
l = new_iks_list();
iks_list_set_item(l,(void*)s);