-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.c
40 lines (32 loc) · 789 Bytes
/
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
#include "9cc.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Node *new_node(int ty, Node *lhs, Node *rhs) {
Node *node = malloc(sizeof(Node));
node->ty = ty;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
Node *new_node_num(int val) {
Node *node = malloc(sizeof(Node));
node->ty = ND_NUM;
node->c_ty = malloc(sizeof(Type));
node->c_ty->ty = INT;
node->val = val;
return node;
}
Node *new_node_id(char *name) {
Node *node = malloc(sizeof(Node));
node->ty = ND_IDENT;
node->name = malloc(sizeof(char) * strlen(name));
strcpy(node->name, name);
// TODO: test here
SYM_REC *rec = query_var(name);
if (rec == NULL)
error("error: use of undeclared identifier %s", name);
node->c_ty = rec->ty;
return node;
}