-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
89 lines (74 loc) · 1.73 KB
/
ast.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "st.h"
extern char *g_op_type_arr[8];
FfNode* ff_create_op_node(enum ff_op_type type)
{
FfNode *node = malloc(sizeof(FfNode));
node->type = node_op;
node->node_val_op = type;
node->is_empty = false;
node->next = NULL;
return node;
}
FfNode* ff_create_id_node(char *id_name)
{
assert(!!id_name);
FfNode *node = malloc(sizeof(FfNode));
node->type = node_id;
node->node_val_id = id_name;
node->is_empty = false;
node->next = NULL;
return node;
}
FfNode* ff_create_list_node(FfNode *list)
{
assert(!!list);
FfNode *node = malloc(sizeof(FfNode));
node->type = node_list;
node->node_val_list = list;
node->is_empty = false;
node->next = NULL;
return node;
}
FfNode* ff_create_empty_node()
{
FfNode *node = malloc(sizeof(FfNode));
node->is_empty = true;
node->next = NULL;
return node;
}
FfNode* ff_link_node(FfNode *left, FfNode *right)
{
assert(!!left && !!right);
FfNode *curr_tail = left;
while(curr_tail->next) {
curr_tail = curr_tail->next;
}
curr_tail->next = right;
return left;
}
FfNode* ff_copy_node(FfNode *src)
{
assert(!!src);
FfNode *dest = malloc(sizeof(FfNode));
dest->is_empty = src->is_empty;
dest->type = src->type;
if (src->type == node_op) {
dest->node_val_id = g_op_type_arr[src->node_val_op];
}
else if (src->type == node_id) {
dest->node_val_id = strdup(src->node_val_id);
}
return dest;
}
FfNode* ff_create_bool_node(bool is_true)
{
FfNode *node = ff_create_empty_node();
node->type = node_bool;
node->node_val_bool = is_true;
return node;
}