-
Notifications
You must be signed in to change notification settings - Fork 2
/
SyntaxTree.h
166 lines (166 loc) · 3.88 KB
/
SyntaxTree.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef SYNTAX_TREE_H
#define SYNTAX_TREE_H
#include <iostream>
using namespace std;
/* Node type define*/
typedef enum {
/* top */
routine_kind,
/* const part */
const_kind,
/* type part */
type_kind,
/* var part */
var_kind,
/* routine part */
sub_kind,
/* routine body */
stmt_kind,
expr_kind
} NodeKind;
/* type_kind */
typedef enum {
decl_type,
integer_type,
real_type,
boolean_type,
char_type,
string_type,
enum_type,
array_type,
record_type,
//record_domain_type,
selfdefined_type
} TypeKind;
/* sub_kind*/
typedef enum {
func_kind,
proc_kind,
param_var_kind,
param_val_kind
} SubKind;
/* stmt_kind */
typedef enum {
assign_stmt,
proc_stmt,
if_stmt,
repeat_stmt,
while_stmt,
for_stmt,
case_stmt,
goto_stmt,
case_exp_stmt
} StmtKind;
/* expr_kind */
typedef enum {
op_kind, // operation
id_kind, // identifer
fn_kind, // function_kind
con_kind // const value kind
} ExprKind;
/* id_kind */
typedef enum { Basic, Array, Record } IdKind;
/* op_kind */
typedef enum {
plus_kind, // plus
minus_kind, // minus
or_kind, // or
mul_kind, // muliply
div_kind, // div
mod_kind, // mod
and_kind, // and
ge_kind, // greater equal
gt_kind, // greater than
le_kind, // less equal
lt_kind, // less than
eq_kind, // equal
ueq_kind, // unequal
not_kind, // not
neg_kind // negative
} OpKind;
/* attr */
typedef struct {
int intVal;
float realVal;
char strVal[256];
} Attr;
/* Tree Node*/
#define MaxChildren 4
typedef struct TreeNode{
struct TreeNode * children[MaxChildren], *sibling;
int lineNo;
/* universal */
NodeKind node;
/* sub_kind */
SubKind sub;
/* type_kind */
TypeKind type;
/* stmt_kind */
StmtKind stmt;
/* expr_kind */
ExprKind expr;
/* id_kind */
IdKind id;
/* op_kind */
OpKind op;
/* attr */
Attr attr;
/* name */
char name[256];
/* label */
int label;
/* system function/procedure */
int system;
struct TreeNode *dtype;
} TreeNode;
/* function define */
/* global */
TreeNode *CreateNode(NodeKind kind);
/* routine */
TreeNode *CreateRoutine(TreeNode* routine_head, TreeNode* routine_body);
/* const part */
TreeNode *CreateConst(char* ConstName, TreeNode* ConstValue);
/* type part */
TreeNode *CreateSimpleType(TypeKind type);
TreeNode *CreateEnumType(TreeNode *exp1, TreeNode *exp2);
TreeNode *CreateArrayType(TreeNode *enumT, TreeNode *baseT);
TreeNode *CreateRecordType(TreeNode *domain);
//TreeNode *CreateDomainType(TreeNode *namelist, TreeNode *type);
TreeNode *CreateSelfDefineType(char *name);
TreeNode *CreateTypeDef(char *TypeName, TreeNode *type);
/* var part */
TreeNode *CreateVar(TreeNode* VarNameList, TreeNode* TypeDecl);
/* expr part*/
TreeNode *CreateConstExp(TypeKind type);
TreeNode *CreateIdExp(char *name);
TreeNode *CreateIdArrayExp(char *name, TreeNode *exp);
TreeNode *CreateIdRecordExp(char *name, char *domain);
TreeNode *CreateOpExp(OpKind op);
TreeNode *CreateFuncExp(char *name, TreeNode *args);
/* stmt part */
TreeNode *CreateStmtNode(StmtKind stmt);
/* function/procedure part */
TreeNode *CreateFunction(TreeNode *func_head, TreeNode *sub_routine);
TreeNode *CreateFuncHead(char *name, TreeNode *param, TreeNode *type);
TreeNode *CreateProcedure(TreeNode *proc_head, TreeNode *sub_routine);
TreeNode *CreateProcHead(char *name, TreeNode *param);
TreeNode *CreateParam(TreeNode *list, TreeNode *type, SubKind k);
/*================================================*/
void strCatch(char *d, char *s);
TreeNode *makeLabel(TreeNode *p, int label);
TreeNode *ConnectNodes(TreeNode *p, TreeNode*q);
/*================================================*/
void print_type(TreeNode *p);
void print_expr_op(TreeNode *p);
void print_expr_id(TreeNode *p);
void print_expr_fn(TreeNode *p);
void print_expr_con(TreeNode *p);
void print_expr(TreeNode *p);
void print_stmt(TreeNode *p);
void print_sub(TreeNode *p);
void print_tree(TreeNode *p, int level);
extern int lineNo;
extern FILE *fout;
extern TreeNode *root;
//extern SymbolTable *SymTab;
#endif