4
4
#include <stdlib.h>
5
5
#include <stdlib.h>
6
6
#include <stdio.h>
7
- #include "limits.h"
7
+ #include "limits.h"
8
8
9
9
10
10
@@ -31,7 +31,7 @@ struct AST* parse_op(int* tokens, int* index) {
31
31
int len = 0 ;
32
32
while (!t_isop (tokens [* index + len ])) {
33
33
34
- // skip any
34
+ // skip any
35
35
36
36
len ++ ;
37
37
}
@@ -74,7 +74,7 @@ struct AST* parse_op(int* tokens, int* index) {
74
74
break ;
75
75
case T_OR :
76
76
ast -> op -> tag = AST_OR ;
77
- break ;
77
+ break ;
78
78
case T_NOT :
79
79
ast -> op -> tag = AST_NOT ;
80
80
break ;
@@ -117,13 +117,13 @@ struct AST* parse_arg(int t) {
117
117
}
118
118
119
119
struct AST * parse_specifier (int t ) {
120
-
120
+
121
121
dbg (2 , "Parsing specifier %d" , t );
122
-
122
+
123
123
struct AST * ast = malloc (sizeof (struct AST ));
124
124
ast -> type = AST_SPEC ;
125
125
ast -> spec = malloc (sizeof (struct ASTspec ));
126
-
126
+
127
127
switch (t ) {
128
128
case T_T8 :
129
129
ast -> spec -> tag = AST_8 ;
@@ -139,14 +139,14 @@ struct AST* parse_specifier(int t) {
139
139
exit (1 );
140
140
break ;
141
141
}
142
-
142
+
143
143
return ast ;
144
144
}
145
145
146
146
struct AST * parse_loop (int * tokens , int * index ) {
147
147
148
148
dbg (2 , "Parsing loop from %d" , tokens [* index ]);
149
-
149
+
150
150
// Syntaxic definition
151
151
/*
152
152
loop -> while ( condition ) block
@@ -459,7 +459,7 @@ struct AST* parse_declare(int* tokens, int* index) {
459
459
capacity *= 2 ;
460
460
ast -> stmt -> args = realloc (ast -> stmt -> args , capacity * sizeof (struct AST * ));
461
461
}
462
- // first we should have a type indicator
462
+ // first we should have a type indicator
463
463
if (!t_istype (tokens [* index ])) {
464
464
msg (ERROR ,"Expected type in declare argument\n" );
465
465
exit (1 );
@@ -473,8 +473,8 @@ struct AST* parse_declare(int* tokens, int* index) {
473
473
474
474
if (tokens [* index ] == T_COMMA ) {
475
475
(* index )++ ;
476
- }
477
-
476
+ }
477
+
478
478
}
479
479
480
480
(* index )++ ;
@@ -494,21 +494,21 @@ struct AST* parse_block(int* tokens, int* index) {
494
494
stmts -> stmt stmts | stmt
495
495
- <stmt> <stmts> | <stmt>
496
496
*/
497
-
497
+
498
498
struct AST * ast = malloc (sizeof (struct AST ));
499
499
ast -> type = AST_BLOCK ;
500
500
ast -> block = malloc (sizeof (struct ASTblock ));
501
501
ast -> block -> size = 0 ;
502
502
int capacity = 10 ;
503
503
ast -> block -> childs = malloc (capacity * sizeof (struct AST ));
504
-
504
+
505
505
if (tokens [* index ] != T_LBRACE ) {
506
506
printf ("Expected '{' in block\n" );
507
507
exit (1 );
508
508
}
509
-
509
+
510
510
(* index )++ ;
511
-
511
+
512
512
while (tokens [* index ] != T_RBRACE ) {
513
513
if (ast -> block -> size >= capacity ) {
514
514
capacity *= 2 ;
@@ -517,9 +517,9 @@ struct AST* parse_block(int* tokens, int* index) {
517
517
ast -> block -> childs [ast -> block -> size ] = parse (tokens , index ,0 );
518
518
ast -> block -> size ++ ;
519
519
}
520
-
520
+
521
521
(* index )++ ;
522
-
522
+
523
523
return ast ;
524
524
}
525
525
@@ -558,10 +558,12 @@ struct AST* parse_opblock(int* tokens, int* index) {
558
558
559
559
struct AST * parse (int * tokens , int * index ,int len ) {
560
560
561
+ msg (INFO ,"Parsing %d tokens..." , len );
562
+
561
563
static int rec = 0 ;
562
564
rec ++ ;
563
565
if (rec > 10 ) {
564
- msg (ERROR , "Recursion limit reached\n " );
566
+ msg (ERROR , "Recursion limit reached" );
565
567
exit (1 );
566
568
}
567
569
@@ -570,19 +572,20 @@ struct AST* parse(int* tokens, int* index,int len) {
570
572
}
571
573
572
574
// The parser will be a recursive descent parser
573
-
575
+
574
576
// The parser need to identify structures in the tokenized code
575
577
// the parser will return an AST (Abstract Syntax Tree) element.
576
578
577
579
// in order to identify structure we will use a pattern table
578
-
580
+
579
581
// create a parser variable (function pointer) to the parse function
580
582
// the parser will return an AST element
581
583
582
584
// Assuming match_pattern returns a pair where value is a function pointer
583
585
pattern * pattern = match_pattern (tokens + * index , len );
584
586
if (pattern == NULL ) {
585
587
// Handle error: no matching pattern found
588
+ msg (ERROR ,"No matching pattern found\n" );
586
589
return NULL ;
587
590
}
588
591
@@ -678,4 +681,4 @@ void visualize_ast(struct AST ast) {
678
681
printf ("Unknown AST type\n" );
679
682
break ;
680
683
}
681
- }
684
+ }
0 commit comments