aboutsummaryrefslogtreecommitdiff
path: root/include/lyn/ast.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/lyn/ast.h')
-rw-r--r--include/lyn/ast.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/include/lyn/ast.h b/include/lyn/ast.h
new file mode 100644
index 0000000..fd3b98b
--- /dev/null
+++ b/include/lyn/ast.h
@@ -0,0 +1,104 @@
+#ifndef LYN_AST_H
+#define LYN_AST_H
+
+#include <stdlib.h>
+#include <string.h>
+
+enum kind {
+ LYN_ID, LYN_STR, LYN_INT, LYN_FLOAT, LYN_CMD, LYN_LIST, LYN_APPLY
+};
+
+struct ast {
+ enum kind kind;
+ union {
+ long long i;
+ double d;
+ const char *s;
+ struct ast *args;
+ };
+ struct ast *next;
+};
+
+static inline struct ast *gen_id(const char *s)
+{
+ struct ast *id = calloc(1, sizeof(struct ast));
+ if (!id)
+ return NULL;
+
+ id->kind = LYN_ID;
+ id->s = strdup(s);
+ return id;
+}
+
+static inline struct ast *gen_str(const char *s)
+{
+ struct ast *str = calloc(1, sizeof(struct ast));
+ if (!str)
+ return NULL;
+
+ str->kind = LYN_STR;
+ str->s = strdup(s);
+ return str;
+}
+
+static inline struct ast *gen_int(long long i)
+{
+ struct ast *integer = calloc(1, sizeof(struct ast));
+ if (!integer)
+ return NULL;
+
+ integer->kind = LYN_INT;
+ integer->i = i;
+ return integer;
+}
+
+static inline struct ast *gen_float(double d)
+{
+ struct ast *floating = calloc(1, sizeof(struct ast));
+ if (!floating)
+ return NULL;
+
+ floating->kind = LYN_FLOAT;
+ floating->d = d;
+ return floating;
+}
+
+static inline struct ast *gen_apply(struct ast *cmds)
+{
+ struct ast *apply = calloc(1, sizeof(struct ast));
+ if (!apply)
+ return NULL;
+
+ apply->kind = LYN_APPLY;
+ apply->args = cmds;
+ return apply;
+}
+
+static inline struct ast *gen_list(struct ast *cmds)
+{
+ struct ast *list = calloc(1, sizeof(struct ast));
+ if (!list)
+ return NULL;
+
+ list->kind = LYN_LIST;
+ list->args = cmds;
+ return list;
+}
+
+static inline struct ast *gen_cmd(struct ast *args)
+{
+ struct ast *cmd = calloc(1, sizeof(struct ast));
+ if (!cmd)
+ return NULL;
+
+ cmd->kind = LYN_CMD;
+ cmd->args = args;
+ return cmd;
+}
+
+void ast_dump(int depth, struct ast *ast);
+void ast_dump_list(int depth, struct ast *ast);
+
+struct ast *reverse_ast_list(struct ast *ast);
+
+#endif /* LYN_AST_H */