commit 0e5daaccd0f0a6e82b71ff2c85712ba0312526bc
parent 3c7144d5d343692883589bedf327c4e3244d9f29
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 29 Jun 2012 16:33:20 +0200
Added tree module
This module has all the functions for creating a evaluation tree, which will
be generated in each syntax rule fire. In this moment we put only iden, op1,
op2 and op3, but you can be sure more functions will be here in the future.
Diffstat:
M | Makefile | | | 2 | +- |
M | syntax.h | | | 14 | +++++++++++++- |
A | tree.c | | | 87 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 101 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
OBJS = types.o decl.o lex.o error.o symbol.o flow.o main.o expr.o keyword.o \
- code.o wrapper.o
+ code.o wrapper.o tree.o
LIBS =
all: kcc
diff --git a/syntax.h b/syntax.h
@@ -3,8 +3,20 @@
extern unsigned char nested_level;
+
+struct node;
+struct symbol;
+
+typedef void nodeop(struct node *np);
+
extern void compound(void);
-extern void expr(void);
+extern struct node *expr(void);
extern unsigned char decl(void);
extern void type_name(void);
+
+extern struct node *leaf(struct symbol *sym);
+extern struct node *op1(nodeop *op, struct node *np);
+extern struct node *op2(nodeop *op, struct node *np1, struct node *np2);
+extern struct node *op3(nodeop *op,
+ struct node *np1, struct node *np2, struct node *np3);
#endif
diff --git a/tree.c b/tree.c
@@ -0,0 +1,87 @@
+
+#include <stdarg.h>
+#include <stddef.h>
+
+#include "cc.h"
+#include "syntax.h"
+
+struct node {
+ nodeop *op;
+ struct node *child[];
+};
+
+#define issym(np) (!(np)->op)
+#define getsym(np) ((struct symbol *)(np)->child[0])
+
+
+static struct node *
+node_alloc(unsigned char n)
+{
+ return xmalloc(sizeof(struct node) + sizeof(struct node *) * n);
+}
+
+static struct node *
+node1(struct node *np1)
+{
+ register struct node *np = node_alloc(1);
+
+ np->child[0] = np1;
+ return np;
+}
+
+static struct node *
+node2(struct node *np1, struct node *np2)
+{
+ register struct node *np = node_alloc(2);
+
+ np->child[0] = np1;
+ np->child[1] = np2;
+ return np;
+}
+
+static struct node *
+node3(struct node *np1, struct node *np2, struct node *np3)
+{
+ register struct node *np = node_alloc(2);
+
+ np->child[0] = np1;
+ np->child[1] = np2;
+ np->child[2] = np3;
+ return np;
+}
+
+struct node *
+leaf(struct symbol *sym)
+{
+ register struct node *new = node1(NULL);
+
+ new->child[0] = (struct node *) sym;
+ return new;
+}
+
+struct node *
+op1(nodeop op, struct node *np)
+{
+ register struct node *new = node1(np);
+
+ new->op = op;
+ return new;
+}
+
+struct node *
+op2(nodeop op, struct node *np1, struct node *np2)
+{
+ register struct node *new = node2(np1, np2);
+
+ new->op = op;
+ return new;
+}
+
+struct node *
+op3(nodeop op, struct node *np1, struct node *np2, struct node *np3)
+{
+ register struct node *new = node3(np1, np2, np3);
+
+ new->op = op;
+ return new;
+}