Browse Source

Add whole-program type

Forest Belton 6 months ago
parent
commit
0034fc638d
3 changed files with 29 additions and 2 deletions
  1. 6
    2
      grammar.peg
  2. 4
    0
      include/ast/ast.h
  3. 19
    0
      src/ast/ast.c

+ 6
- 2
grammar.peg View File

@@ -20,10 +20,14 @@
20 20
 
21 21
 # Top-level definitions
22 22
 Program
23
-    = prog:TranslationUnit !. { yy->p = prog.data.prgm }
23
+    = prog:TranslationUnit !. { yy->p = prog.data.prgm; }
24 24
 
25
+# TODO: Fix append of decl once decl type declared
25 26
 TranslationUnit
26
-    = ExternalDeclaration TranslationUnit?
27
+    = decl:ExternalDeclaration prog:TranslationUnit? {
28
+        prgm *p = prgm_add_decl(prog.data.prgm, &decl);
29
+        $$ = token_prgm(p);
30
+    }
27 31
 
28 32
 ExternalDeclaration
29 33
     = FunctionDefinition

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

@@ -7,5 +7,9 @@ typedef struct {
7 7
     list *decls;
8 8
 } prgm;
9 9
 
10
+prgm *prgm_add_decl(prgm *prgm, void *decl);
11
+
12
+prgm *prgm_new(void);
13
+
10 14
 #endif
11 15
 

+ 19
- 0
src/ast/ast.c View File

@@ -0,0 +1,19 @@
1
+#include "ast/ast.h"
2
+#include "list.h"
3
+#include "util.h"
4
+
5
+#include <stdlib.h>
6
+
7
+prgm *prgm_add_decl(prgm *prgm, void *decl) {
8
+    prgm->decls = list_cons(decl, prgm->decls);
9
+    return prgm;
10
+}
11
+
12
+prgm *prgm_new(void) {
13
+    prgm *p = safe_malloc(sizeof *p);
14
+
15
+    p->decls = NULL;
16
+
17
+    return p;
18
+}
19
+

Loading…
Cancel
Save