-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.h
156 lines (140 loc) · 2.53 KB
/
AST.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
typedef enum
{eInt = 0,eFloat} Typee;
typedef enum
{eNegative} Unop;
typedef enum
{eAssign,eCall,eRet,eWhile,eDoWhile,eFor,eIf,eCompound,eSemi} Stmt;
typedef enum
{eUnop,eBinop,eCallExpr,eIntnum,eFloatnum,eId,eExpr} Expre;
typedef enum
{ePlus,eMinus,eMult,eDiv,eLT,eGT,eLTE,eGTE,eEQ,eNEQ} Binop;
struct PROGRAM
{
struct DECLARATION *DeclList;
struct FUNCTION * FuncList;
};
struct PROGRAM *root;
struct DECLARATION
{
Typee t;
struct IDENTIFIER *ilist;
struct DECLARATION *prev;
};
struct IDENTIFIER
{
char *ID;
int intnum; //zero ,if scalar
struct IDENTIFIER *prev;
};
struct FUNCTION // *prev type id (parameter) {}
{
Typee t;
char *ID;
struct PARAMETER *ParamList;
struct COMPOUNDSTMT *CStmt;
struct FUNCTION *prev;
};
struct PARAMETER
{
Typee t;
struct IDENTIFIER *id;
struct PARAMETER *prev;
};
struct COMPOUNDSTMT // {}
{
struct DECLARATION *DeclList;
struct STMT *StmtList;
};
struct STMT
{
Stmt e_stmt;
union {
struct ASSIGN *assign_s; // id=expr;
struct CALL *call_s; //id(arg)
struct EXPR *return_s; //return expr
struct WHILEs *while_s; //while()stmt
struct DOWHILEs *dowhile_s;//do stmt
struct FORs *for_s; //for()stmt
struct IFs *if_s; //if()stmt
struct COMPOUNDSTMT *compound_s; // {}
} stmt;
struct STMT *prev;
};
///// id[index]=expr;
struct ASSIGN
{
char *ID;
struct EXPR *index; //Null, if LHS is scalar variable
struct EXPR *expr; // RHS
};
//// id(arglist?);
struct CALL
{
char *ID;
struct ARGLIST *arg;
};
//// (expr,expr*)
struct ARGLIST
{
struct EXPR *expr;
struct ARGLIST *prev;
};
///// while(condition)stmt;
struct WHILEs
{
struct EXPR *condition;
struct STMT *stmt;
};
////do stmt; while(condition);
struct DOWHILEs
{
struct EXPR *condition;
struct STMT *stmt;
};
/// for(init;condition;next)stmt;
struct FORs
{
struct ASSIGN *init;
struct EXPR *condition;
struct ASSIGN *next;
struct STMT *stmt;
};
//// if(condition)if_s else else_s
struct IFs
{
struct EXPR *condition;
struct STMT *if_s;
struct STMT *else_s; //NUll, if 'else' not exist
};
struct EXPR
{
Expre e_expr; // EXPR type (enumeration type)
union
{
int intnum; //int
float floatnum; // float
struct UNOP *unop_expr; //-expr
struct BINOP *binop_expr; // expr A expr
struct CALL *call_expr; //call
struct EXPR *bracket; //(expr)
struct IDs *ID_expr; //id[expr]
} expression;
};
struct UNOP
{
Unop u;
struct EXPR *expr;
};
/// expr1+ expr2
struct BINOP
{
Binop bi;
struct EXPR *expr1;
struct EXPR *expr2;
};
/// id[expr]
struct IDs
{
char *ID;
struct EXPR *expr; //NULL , if scalar variable
};