-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_node.c
117 lines (113 loc) · 3.25 KB
/
ast_node.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "ast_node.h"
#include "cimple.h"
const char *node_tag_str[] = {
[NODE_TAG_UNARY] = "NODE_TAG_UNARY",
[NODE_TAG_BINARY] = "NODE_TAG_BINARY",
[NODE_TAG_MEMBER] = "NODE_TAG_MEMBER",
[NODE_TAG_IF] = "NODE_TAG_IF",
[NODE_TAG_FOR] = "NODE_TAG_FOR",
[NODE_TAG_BLOCK] = "NODE_TAG_BLOCK",
[NODE_TAG_FUNCALL] = "NODE_TAG_FUNCALL",
[NODE_TAG_VAR] = "NODE_TAG_VAR",
[NODE_TAG_NUM] = "NODE_TAG_NUM",
};
const char *node_tag_to_str(enum NodeTag tag)
{
return node_tag_str[tag];
}
Node *new_unary_node(UnaryKind kind, Node *expr, const Token *tok)
{
struct UnaryNode *node = calloc(1, sizeof(struct UnaryNode));
node->node.tag = NODE_TAG_UNARY;
node->node.tok = tok;
node->kind = kind;
node->expr = expr;
return (Node *)node;
}
Node *new_cast_node(Node *expr, struct Type *type)
{
add_type(expr);
Node *node = new_unary_node(NODE_CAST, expr, expr->tok);
node->type = type;
return node;
}
Node *new_binary_node(BinaryKind kind, Node *lhs, Node *rhs,
const Token *tok)
{
struct BinaryNode *node = calloc(1, sizeof(struct BinaryNode));
node->node.tag = NODE_TAG_BINARY;
node->node.tok = tok;
node->kind = kind;
node->lhs = lhs;
node->rhs = rhs;
return (Node *)node;
}
Node *new_member_node(Node *lhs, Member *member,
const Token *tok)
{
MemberNode *node = calloc(1, sizeof(struct MemberNode));
node->node.tag = NODE_TAG_MEMBER;
node->node.tok = tok;
node->lhs = lhs;
node->member = member;
return (Node *)node;
}
Node *new_if_node(Node *cond_expr, Node *then_stmt, Node *else_stmt,
const Token *tok)
{
struct IfNode *node = calloc(1, sizeof(struct IfNode));
node->node.tag = NODE_TAG_IF;
node->node.tok = tok;
node->cond_expr = cond_expr;
node->then_stmt = then_stmt;
node->else_stmt = else_stmt;
return (Node *)node;
}
Node *new_for_node(Node *init_expr, Node *cond_expr, Node *inc_expr,
Node *body_stmt, const Token *tok)
{
struct ForNode *node = calloc(1, sizeof(struct ForNode));
node->node.tag = NODE_TAG_FOR;
node->node.tok = tok;
node->init_expr = init_expr;
node->cond_expr = cond_expr;
node->inc_expr = inc_expr;
node->body_stmt = body_stmt;
return (Node *)node;
}
BlockNode *new_block_node(Node *body, const Token *tok)
{
struct BlockNode *node = calloc(1, sizeof(struct BlockNode));
node->node.tag = NODE_TAG_BLOCK;
node->node.tok = tok;
node->body = body;
return node;
}
Node *new_fun_call_node(const char *funcname, int funcname_len,
Node *args, const Token *tok, Type *func_type)
{
struct FunCallNode *node = calloc(1, sizeof(struct FunCallNode));
node->node.tag = NODE_TAG_FUNCALL;
node->node.tok = tok;
node->func_type = func_type;
node->funcname = funcname;
node->funcname_length = funcname_len;
node->args = args;
return (Node *)node;
}
Node *new_var_node(const Obj *var, const Token *tok)
{
struct VarNode *node = calloc(1, sizeof(struct VarNode));
node->node.tag = NODE_TAG_VAR;
node->node.tok = tok;
node->var = var;
return (Node *)node;
}
Node *new_num_node(int64_t val, const Token *tok)
{
struct NumNode *node = calloc(1, sizeof(struct NumNode));
node->node.tag = NODE_TAG_NUM;
node->node.tok = tok;
node->val = val;
return (Node *)node;
}