-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
98 lines (82 loc) · 2.36 KB
/
ast.h
File metadata and controls
98 lines (82 loc) · 2.36 KB
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
#ifndef AST_H
#define AST_H
#include "types.h"
/*
* INT_VAL_NODE
* FLOAT_VAL_NODE
* STRING_VAL_NODE
* VOID_VAL_NODE
* BLOCK_NODE
* PROGRAM_NODE block_node;
* FUNC_DECL_NODE -> type = return_type; data = 0; list of var_use_node;
* block_node; VAR_DECL_NODE -> type = decl_type; data = idx; node_exp;
* WHILE_NODE -> type = VOID; data = 0; exp_node; block_node;
* IF_NODE -> type = VOID; data = 0; exp_node; block_node; block_node;
* ASSIGN_NODE -> type = VOID; data = 0; var_use/array_var_use; exp_node;
* FUNC_USE_NODE -> type = return_type; data = 0; var_list_node;
* RETURN_NODE -> type = exp_type; data = 0; exp_node;
* NOT_NODE -> type = exp_type; data = 0; exp_node;
* NEG_NODE -> type = exp_type; data = 0; exp_node;
* AND_NODE -> type = INT; data = 0; exp_node; exp_node;
* OR_NODE -> type = INT; data = 0; exp_node; exp_node;
* LT_NODE -> type = INT; data = 0; exp_node; exp_node;
* GT_NODE -> type = INT; data = 0; exp_node; exp_node;
* EQ_NODE -> type = INT; data = 0; exp_node; exp_node;
* TIMES_NODE -> type = exp_type; data = 0; exp_node; exp_node;
* OVER_NODE -> type = exp_type; data = 0; exp_node; exp_node;
* PLUS_NODE -> type = exp_type; data = 0; exp_node; exp_node;
* MINUS_NODE -> type = exp_type; data = 0; exp_node; exp_node;
*/
typedef enum {
PROGRAM_NODE,
BLOCK_NODE,
WHILE_NODE,
IF_NODE,
ASSIGN_NODE,
NOT_NODE,
EQ_NODE,
LT_NODE,
GT_NODE,
AND_NODE,
OR_NODE,
NEG_NODE,
PLUS_NODE,
MINUS_NODE,
TIMES_NODE,
OVER_NODE,
FUNC_DECL_NODE,
FUNC_USE_NODE,
RETURN_NODE,
VAR_DECL_NODE,
VAR_LIST_NODE,
VAR_USE_NODE,
ARRAY_USE_NODE,
INT_VAL_NODE,
FLOAT_VAL_NODE,
STRING_VAL_NODE,
VOID_VAL_NODE,
I2R_NODE,
R2I_NODE,
I2B_NODE,
B2I_NODE,
R2B_NODE,
B2R_NODE
} NodeKind;
struct node; // Opaque structure to ensure encapsulation.
typedef struct node AST;
const char *get_kind_text(NodeKind kind);
AST *new_node(NodeKind kind, int data, Type type);
void add_child(AST *parent, AST *child);
AST *get_child(AST *parent, int idx);
AST *new_subtree(NodeKind kind, Type type, int child_count, ...);
NodeKind get_kind(AST *node);
char *kind2str(NodeKind kind);
int get_data(AST *node);
void set_float_data(AST *node, float data);
float get_float_data(AST *node);
Type get_node_type(AST *node);
int get_child_count(AST *node);
void print_tree(AST *ast);
void print_dot(AST *ast);
void free_tree(AST *ast);
#endif