summaryrefslogtreecommitdiff
path: root/ast.c
diff options
context:
space:
mode:
Diffstat (limited to 'ast.c')
-rw-r--r--ast.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/ast.c b/ast.c
new file mode 100644
index 0000000..1890528
--- /dev/null
+++ b/ast.c
@@ -0,0 +1,70 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "ast.h"
+#include "parser.h"
+
+ast *new_pair(ast *l, ast *r) {
+ ast *ret = malloc(sizeof(*ret));
+ ret->type = PAIR;
+ ret->c.p.l = l;
+ ret->c.p.r = r;
+ return ret;
+}
+
+ast *new_int(char *s) {
+ ast *ret = malloc(sizeof(*ret));
+ ret->type = INT;
+ ret->c.i = strtol(s, NULL, 0);
+ return ret;
+}
+
+ast *new_float(char *s) {
+ ast *ret = malloc(sizeof(*ret));
+ ret->type = FLOAT;
+ ret->c.f = strtof(s, NULL);
+ return ret;
+}
+
+ast *new_id(char *s) {
+ ast *ret = malloc(sizeof(*ret));
+ ret->type = ID;
+ ret->c.s = strdup(s);
+ return ret;
+}
+
+void free_pair(ast *p) {
+ free_ast(p->c.p.l);
+ free_ast(p->c.p.r);
+ free(p);
+}
+
+void free_int(ast *p) {
+ free(p);
+}
+
+void free_float(ast *p) {
+ free(p);
+}
+
+void free_id(ast *p) {
+ free(p->c.s);
+ free(p);
+}
+
+void free_ast(ast *p) {
+ switch (p->type) {
+ case PAIR:
+ free_pair(p);
+ break;
+ case INT:
+ free_int(p);
+ break;
+ case FLOAT:
+ free_float(p);
+ break;
+ case ID:
+ free_id(p);
+ break;
+ }
+}