Browse Source

Begin to define whole program AST

Forest Belton 6 months ago
parent
commit
cf023e3899
9 changed files with 66 additions and 7 deletions
  1. 1
    1
      Makefile
  2. 9
    5
      grammar.peg
  3. 11
    0
      include/ast/ast.h
  4. 11
    1
      include/ast/stmt.h
  5. 5
    0
      include/token.h
  6. 2
    0
      main.c
  7. 12
    0
      src/ast/stmt.c
  8. 9
    0
      src/token.c
  9. 6
    0
      test/004-parser.c

+ 1
- 1
Makefile View File

@@ -28,7 +28,7 @@ clean:
28 28
 	rm -f $(APP) grammar.c
29 29
 
30 30
 test/%: test/%.c
31
-	$(CC) $(CFLAGS) $(OFILES) $< -o $@
31
+	$(CC) -I. $(CFLAGS) $(OFILES) $< -o $@
32 32
 	./$@
33 33
 
34 34
 test: $(APP) $(TESTS)

+ 9
- 5
grammar.peg View File

@@ -1,4 +1,5 @@
1 1
 %{
2
+#include "ast/ast.h"
2 3
 #include "ast/expr.h"
3 4
 #include "ast/stmt.h"
4 5
 #include "list.h"
@@ -8,7 +9,8 @@
8 9
 #define YYSTYPE token
9 10
 #define YY_CTX_LOCAL
10 11
 #define YY_CTX_MEMBERS \
11
-    FILE *fp;
12
+    FILE *fp; \
13
+    prgm *p;
12 14
 
13 15
 #define YY_INPUT(ctx, buf, result, max_size) { \
14 16
     int yyc = fgetc(ctx->fp); \
@@ -18,7 +20,7 @@
18 20
 
19 21
 # Top-level definitions
20 22
 Program
21
-    = prog:TranslationUnit !. { $$ = prog }
23
+    = prog:TranslationUnit !. { yy->p = prog.data.prgm }
22 24
 
23 25
 TranslationUnit
24 26
     = ExternalDeclaration TranslationUnit?
@@ -244,8 +246,10 @@ IterationStatement
244 246
     } | DO body:Statement WHILE LPAREN predicate:Expression RPAREN SEMI {
245 247
         stmt *stmt = stmt_do_while(predicate.data.expr, body.data.stmt);
246 248
         $$ = token_stmt(stmt);
247
-    } | FOR LPAREN Expression? SEMI Expression? SEMI Expression? RPAREN Statement
248
-    | FOR Declaration Expression? SEMI Expression? LPAREN Statement
249
+    } | FOR LPAREN init_expr:Expression? SEMI predicate:Expression? SEMI incr_expr:Expression? RPAREN Statement {
250
+        stmt *stmt = stmt_for(init_expr.data.expr, predicate.data.expr, incr_expr.data.expr, body.data.stmt);
251
+        $$ = token_stmt(stmt);
252
+    } | FOR Declaration Expression? SEMI Expression? LPAREN Statement
249 253
 
250 254
 JumpStatement
251 255
     = GOTO label:Identifier SEMI {
@@ -432,7 +436,7 @@ PostfixExpression
432 436
     )* { $$ = lhs; }
433 437
 
434 438
 ArgumentExpressionList
435
-    = AssignmentExpression (COMMA ArgumentExpressionList)?
439
+    = h:AssignmentExpression (COMMA t:ArgumentExpressionList)?
436 440
 
437 441
 PrimaryExpression
438 442
     = Identifier

+ 11
- 0
include/ast/ast.h View File

@@ -0,0 +1,11 @@
1
+#ifndef CCC_AST_AST_H_
2
+#define CCC_AST_AST_H_
3
+
4
+#include "list.h"
5
+
6
+typedef struct {
7
+    list *decls;
8
+} prgm;
9
+
10
+#endif
11
+

+ 11
- 1
include/ast/stmt.h View File

@@ -14,7 +14,8 @@ typedef enum {
14 14
     STMT_IF_ELSE,
15 15
     STMT_RETURN,
16 16
     STMT_SWITCH,
17
-    STMT_WHILE
17
+    STMT_WHILE,
18
+    STMT_FOR
18 19
 } stmt_tag;
19 20
 
20 21
 typedef struct stmt {
@@ -61,6 +62,13 @@ typedef struct stmt {
61 62
             expr *predicate;
62 63
             struct stmt *body;
63 64
         } while_; /* while (predicate) body */
65
+
66
+        struct {
67
+            expr *init_expr;
68
+            expr *predicate;
69
+            expr *incr_expr;
70
+            struct stmt *body;
71
+        } for_; /* for (init_expr; predicate; incr_expr) body */
64 72
     } d;
65 73
 } stmt;
66 74
 
@@ -86,5 +94,7 @@ stmt *stmt_while(expr *predicate, stmt *body);
86 94
 
87 95
 stmt *stmt_switch(expr *discriminator, stmt *body);
88 96
 
97
+stmt *stmt_for(expr *init_expr, expr *predicate, expr *incr_expr, stmt *body);
98
+
89 99
 #endif
90 100
 

+ 5
- 0
include/token.h View File

@@ -1,6 +1,7 @@
1 1
 #ifndef CCC_GRAMMAR_H_
2 2
 #define CCC_GRAMMAR_H_
3 3
 
4
+#include "ast/ast.h"
4 5
 #include "ast/expr.h"
5 6
 #include "ast/stmt.h"
6 7
 #include "list.h"
@@ -9,6 +10,7 @@ typedef enum {
9 10
     TOKEN_EXPR,
10 11
     TOKEN_INT,
11 12
     TOKEN_LIST,
13
+    TOKEN_PRGM,
12 14
     TOKEN_STMT,
13 15
     TOKEN_STRING
14 16
 } token_tag;
@@ -20,6 +22,7 @@ typedef struct {
20 22
         expr *expr;
21 23
         int intval;
22 24
         list *list;
25
+        prgm *prgm;
23 26
         stmt *stmt;
24 27
         char *string;
25 28
     } data;
@@ -33,6 +36,8 @@ extern token token_int(int intval);
33 36
 
34 37
 extern token token_list(list *list);
35 38
 
39
+extern token token_prgm(prgm *prgm);
40
+
36 41
 extern token token_stmt(stmt *stmt);
37 42
 
38 43
 extern token token_string(char *str);

+ 2
- 0
main.c View File

@@ -1,3 +1,4 @@
1
+#include <stdlib.h>
1 2
 #include <string.h>
2 3
 
3 4
 #include "grammar.c"
@@ -9,6 +10,7 @@ int main() {
9 10
     ctx.fp = stdin;
10 11
 
11 12
     while (yyparse(&ctx));
13
+    printf("%p\n", ctx.p);
12 14
 
13 15
     return 0;
14 16
 }

+ 12
- 0
src/ast/stmt.c View File

@@ -102,3 +102,15 @@ stmt *stmt_while(expr *predicate, stmt *body) {
102 102
     return stmt;
103 103
 }
104 104
 
105
+stmt *stmt_for(expr *init_expr, expr *predicate, expr *incr_expr, stmt *body) {
106
+    stmt *stmt = safe_malloc(sizeof *stmt);
107
+
108
+    stmt->tag = STMT_FOR;
109
+    stmt->d.for_.init_expr = init_expr;
110
+    stmt->d.for_.predicate = predicate;
111
+    stmt->d.for_.incr_expr = incr_expr;
112
+    stmt->d.for_.body = body;
113
+
114
+    return stmt;
115
+}
116
+

+ 9
- 0
src/token.c View File

@@ -36,6 +36,15 @@ token token_list(list *list) {
36 36
     return token;
37 37
 }
38 38
 
39
+token token_prgm(prgm *prgm) {
40
+    token token;
41
+
42
+    token.tag = TOKEN_PRGM;
43
+    token.data.prgm = prgm;
44
+
45
+    return token;
46
+}
47
+
39 48
 token token_stmt(stmt *stmt) {
40 49
     token token;
41 50
 

+ 6
- 0
test/004-parser.c View File

@@ -0,0 +1,6 @@
1
+#include <stdio.h>
2
+#include "grammar.c"
3
+
4
+int main() {
5
+}
6
+

Loading…
Cancel
Save