G

G Programming Language
git clone http://git.omkov.net/G
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2022-03-19 08:40:37
Commit7d78e11e3b891e2e490936e53484ffc884b2c089
Parentedaa8c5f624a9ffe0feb37db7b2521e6fca75ac8

parse: Track parent AST nodes

Diffstat

M src/main.c | 6 ++----
M src/parse.c | 12 ++++++++----
M src/parse.h | 2 +-

3 files changed, 11 insertions, 9 deletions

diff --git a/src/main.c b/src/main.c
index 3b89a42..6d7184c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,7 +11,6 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdio.h>
-#include <stdio.h>
 #include <stdlib.h>
 
 static struct lop lops[] = {
@@ -29,7 +28,7 @@ static char *aw[] = {
 static void hlp(void);
 static void ver(void);
 
-int main(int ac, char *av[]) { (void)(ac); A0 = av[0];
+int main(int ac, char *av[]) { A0 = av[0];
 	if (ac == 1) { ac = 3; av = aw; } /* DEBUG */
 
 	struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops;
@@ -38,7 +37,7 @@ int main(int ac, char *av[]) { (void)(ac); A0 = av[0];
 	case 257: { ver(); } return 0;
 	case 258: { lflag = true; } break;
 	case 259: { pflag = true; } break;
-	default:  { return 1; }
+	default: {} return 1;
 	}
 
 	if (opt.ind == ac) { error(1, "Missing operand"); }
diff --git a/src/parse.c b/src/parse.c
index 0344bcd..4f6442a 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -62,13 +62,15 @@ ast *parse(lex *l) {
 
 	for (ast *c; T.k != LK_EOF;) {
 		/* Parse and append all child nodes */
-		if ((c = parse_decl(l, &a->st)) != NULL) { ast_push(a, c); }
+		if ((c = parse_decl(l, &a->st)) != NULL) { c->p = a; ast_push(a, c); }
 		else { /* TODO */ error(1, "NULL AST (parse_decl)"); }
 	}
 
 	return a;
 }
 
+#define SAC (sm.a->c.a[0]) /* First child of the symbol's AST node */
+
 /* Parse a declaration. */
 static ast *parse_decl(lex *l, syt *st) {
 	/* Store the declaration's line, column, hash, and identifier */
@@ -112,9 +114,8 @@ static ast *parse_decl(lex *l, syt *st) {
 		l->n, T.ln + 1, T.cl + 1
 	); }
 
-	switch (sm.a->c.a[0]->k) {
-	case AK_PROC: { sm.k = SK_PROC; } break;
-	// if (sm.a->c.a[0]->k == AK_PROC) { sm.a->c.a[0]->s = sm.a->s; }
+	switch (SAC->k) {
+	case AK_PROC: { sm.k = SK_PROC; SAC->s = sm.a->s; SAC->p = sm.a; } break;
 	default: { sm.k = SK_NASS; } break; /* TODO */
 	// default: { error( /* ERROR */
 	// 	1, "%s:%zu:%zu: error: unhandled AST kind \"%s\"",
@@ -140,6 +141,8 @@ static ast *parse_decl(lex *l, syt *st) {
 	syt_insert_h(st, sm.h, sm.s, sm); return sm.a;
 }
 
+#undef SAC
+
 /* Parse a statement. */
 static ast *parse_stmt(lex *l, syt *st) {
 	if (T.k == LK_LBRACE) { return parse_stmt_compound(l, st); }
diff --git a/src/parse.h b/src/parse.h
index 2c77d63..7fe9994 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -21,7 +21,7 @@ typedef enum {
 
 typedef struct ast_s {
 	ast_k k; UINT ln, cl; u64 h; char *s;
-	type *t; val v; syt st;
+	type *t; val v; syt st; struct ast_s *p;
 	struct { struct ast_s **a; UINT al, ac; } c;
 } ast;