aboutsummaryrefslogtreecommitdiff
path: root/src/ast.h
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-02 02:14:10 -0500
committerMustafa Quraish <[email protected]>2022-02-02 02:14:10 -0500
commita5185aebb89eaa8ea318d7e3bf881f03349b789a (patch)
treebc97f376a3820889ab99d5c70e934f12dd54eb0a /src/ast.h
parentDefer: Pop elements off the defer-stack when returning (diff)
downloadcup-a5185aebb89eaa8ea318d7e3bf881f03349b789a.tar.xz
cup-a5185aebb89eaa8ea318d7e3bf881f03349b789a.zip
Modify how types are stored.
We now dynamically allocate the type structure, and recursively store a reference to the original type if it's a pointer. For now it's a little bit of a waste but it helps us future-proof stuff for more complex things to come
Diffstat (limited to 'src/ast.h')
-rw-r--r--src/ast.h23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/ast.h b/src/ast.h
index 09d15d1..74a8ef8 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -1,6 +1,7 @@
#pragma once
#include "common.h"
+#include "tokens.h"
#define ENUM_AST_TYPES(F) \
F(OP_NEG, "neg") \
@@ -8,6 +9,7 @@
F(OP_BWINV, "~") \
F(OP_PLUS, "+") \
F(OP_MINUS, "-") \
+ F(OP_ADDROF, "&") \
F(OP_MUL, "*") \
F(OP_DIV, "/") \
F(OP_MOD, "%") \
@@ -45,28 +47,34 @@ typedef enum {
#undef DEFINE_ENUM
} NodeType;
+NodeType binary_token_to_op(TokenType type);
+
char *node_type_to_str(NodeType type);
bool is_binary_op(NodeType type);
bool is_unary_op(NodeType type);
bool is_expression(NodeType type);
+bool is_lvalue(NodeType type);
+
typedef enum {
TYPE_NONE,
TYPE_INT,
+ TYPE_PTR,
} DataType;
char *data_type_to_str(DataType type);
-typedef struct {
+typedef struct data_type_node {
DataType type;
- // 0 = value, 1 = pointer, 2 = double pointer, ...
- int indirection;
+ struct data_type_node *ptr;
} Type;
+Type *type_new(DataType type);
+
typedef struct {
char *name;
- Type type;
+ Type *type;
i64 offset;
} Variable;
@@ -87,7 +95,7 @@ typedef struct ast_node {
// Function definition
struct {
char *name;
- Type return_type;
+ Type *return_type;
Node *body;
// TODO: Should we just dynamically allocate space on the
@@ -110,7 +118,7 @@ typedef struct ast_node {
} block;
struct {
- Type type;
+ Type *type;
union {
int as_int;
};
@@ -150,4 +158,7 @@ typedef struct ast_node {
};
} Node;
+void Node_add_child(Node *parent, Node *child);
+Node *Node_new(NodeType type);
+
void print_ast(Node *node); \ No newline at end of file