-
Notifications
You must be signed in to change notification settings - Fork 1
/
oa.h
255 lines (238 loc) · 7.21 KB
/
oa.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#ifndef OA_H
#define OA_H
#ifdef __cplusplus
extern "C" {
#endif
//expression variable type
#define OA_INT 0
#define OA_DOUBLE 1
#define OA_CHAR 2
#define OA_STRING 3
#define OA_LEFT_VALUE 4
#define OA_ARRAY_VALUE 5
#define OA_FUNCTION_VALUE 6
//parse tree node type
#define VAR_DECLARE_NODE 0
#define VAR_DEFINE_NODE 1
#define VAR_ASSIGN_NODE 2
#define ARRAY_DECLARE_NODE 3
#define ARRAY_DEFINE_NODE 4
#define ARRAY_ASSIGN_NODE 5
#define IF_NODE 6
#define ELIF_NODE 7
#define ELSE_NODE 8
#define WHILE_NODE 9
#define FOREACH_NODE 10
#define CLASS_DEFINE_NODE 11
#define FUNCTION_DECLARE_NODE 12
#define FUNCTION_DEFINE_NODE 13
#define CLASS_METHOD_DEFINE_NODE 14
#define BREAK_NODE 15
#define CONTINUE_NODE 16
#define RETURN_NODE 17
//expression operator type
#define OA_EXP_NONE 0
#define OA_EXP_MULTIPLE 1
#define OA_EXP_DIVIDE 2
#define OA_EXP_PLUS 3
#define OA_EXP_MINUS 4
#define OA_EXP_EQ 5
#define OA_EXP_NE 6
#define OA_EXP_GT 7
#define OA_EXP_GE 8
#define OA_EXP_LT 9
#define OA_EXP_LE 10
#define OA_EXP_AND 11
#define OA_EXP_OR 12
#define OA_EXP_NOT 13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//------------------word and syntax part------------------
//---------------------parameter part---------------------
struct FormParam{
char *type;
char *name;
struct FormParam *next;
};
struct FactParam{
struct Expression *exp;
struct FactParam *next;
};
struct FormParam *createFormParam(char *type, char *name);
struct FactParam *createFactParam(struct Expression *exp);
//-------------------end parameter part--------------------
//---------------------expression part---------------------
struct LeftValue{
char *name;
struct LeftValue *next;
};
struct ArrayValue{
struct LeftValue *name;
struct Expression *index;
};
struct FunctionValue{
struct LeftValue *name;
struct FactParam *factParam;
};
struct Expression{
struct Expression *left;
struct Expression *right;
int op;
int leafType;
union{
int number_int;
double number_double;
char type_char;
char *type_string;
struct LeftValue *name;
struct ArrayValue *arrayValue;
struct FunctionValue * functionValue;
};
};
struct LeftValue *createLeftValue(char *name);
struct Expression *createExpression(struct Expression *left, struct Expression *right, int op);
struct Expression *createExpressionIntLeaf(int value, int changeSymbol);
struct Expression *createExpressionDoubleLeaf(double value, int changeSymbol);
struct Expression *createExpressionCharLeaf(char value);
struct Expression *createExpressionStringLeaf(char *value);
struct Expression *createExpressionLeftValueLeaf(struct LeftValue *name);
struct Expression *createExpressionArrayValue(struct LeftValue *name, struct Expression *index);
struct Expression *createExpressionFunctionValue(struct LeftValue *name, struct FactParam *factParam);
//-----------------end expression part-----------------
//------------------ statement part---------------------
struct VarDeclareNode{
char *type;
char *name;
};
struct VarDefineNode{
char *type;
char *name;
struct Expression *exp;
};
struct VarAssignNode{
struct LeftValue *name;
struct Expression *exp;
struct Expression *expOfVar;
};
struct ArrayDeclareNode{
char *type;
char *name;
};
struct ArrayDefineNode{
char *type;
char *name;
struct Expression *exp;
};
struct ArrayAssignNode{
struct LeftValue *name;
char *type;
struct Expression *exp;
};
struct IfNode{
struct Expression *exp;
struct TreeNode *stmts;
struct TreeNode *elifStmts;
struct TreeNode *elseStmts;
};
struct ElifNode{
struct Expression *exp;
struct TreeNode *stmts;
};
struct ElseNode{
struct TreeNode *stmts;
};
struct WhileNode{
struct Expression *exp;
struct TreeNode *stmts;
};
struct ForeachNode{
char *nameIn;
struct LeftValue *nameOut;
struct TreeNode *stmts;
};
struct ClassDefineNode{
char *type;
char *typeParent;
struct TreeNode *stmts;
};
struct FunctionDeclareNode{
char *type;
char *name;
struct FormParam *formParams;
};
struct FunctionDefineNode{
char *type;
char *name;
struct FormParam *formParams;
struct TreeNode *stmts;
};
struct ClassMethodDefineNode{
char *classType;
char *type;
char *name;
struct FormParam *formParams;
struct TreeNode *stmts;
};
struct ReturnNode{
struct Expression *exp;
};
struct TreeNode {
int type;
int lineno;
char *str;
struct TreeNode *next;
union{
//[WANING] break and continue statement has no node
struct VarDeclareNode *varDeclareNode;
struct VarDefineNode *varDefineNode;
struct VarAssignNode *varAssignNode;
struct ArrayDeclareNode *arrayDeclareNode;
struct ArrayDefineNode *arrayDefineNode;
struct ArrayAssignNode *arrayAssignNode;
struct IfNode *ifNode;
struct ElifNode *elifNode;
struct ElseNode *elseNode;
struct WhileNode *whileNode;
struct ForeachNode *foreachNode;
struct ClassDefineNode *classDefineNode;
struct FunctionDeclareNode *functionDeclareNode;
struct FunctionDefineNode *functionDefineNode;
struct ClassMethodDefineNode *classMethodDefineNode;
struct ReturnNode *returnNode;
};
};
struct ParseTree{
struct TreeNode *root;
};
char *createArrayType(char *type);
struct TreeNode *createVarDeclare(char *type, char *name);
struct TreeNode *createVarDefine(char *type, char *name, struct Expression *exp);
struct TreeNode *createVarAssign(struct LeftValue *name, struct Expression *exp, struct Expression *expOfVar);
struct TreeNode *createArrayDeclare(char *type, char *name);
struct TreeNode *createArrayDefine(char *type, char *name, char *type2, struct Expression *exp);
struct TreeNode *createArrayAssign(struct LeftValue *name, char *type, struct Expression *exp);
struct TreeNode *createIf(struct Expression *exp, struct TreeNode *stmts, struct TreeNode *elifStmts, struct TreeNode *elseStmts);
struct TreeNode *createElifPart(struct Expression *exp, struct TreeNode *stmts);
struct TreeNode *createElsePart(struct TreeNode *stmts);
struct TreeNode *createWhile(struct Expression *exp, struct TreeNode *stmts);
struct TreeNode *createForeach(char *nameIn, struct LeftValue *nameOut, struct TreeNode *stmts);
struct TreeNode *createClassDefine(char *type, char *typeParent, struct TreeNode *stmts);
struct TreeNode *createFunctionDeclare(char *type, char *name, struct FormParam *formParams);
struct TreeNode *createFunctionDefine(char *type, char *name, struct FormParam *formParams, struct TreeNode *stmts);
struct TreeNode *createClassMethodDefine(char *type, char *name, struct FormParam *formParams, struct TreeNode *stmts, char *classType);
struct TreeNode *createBreak();
struct TreeNode *createContinue();
struct TreeNode *createReturn(struct Expression *exp);
//-------------------end statement part-------------------
//----------------end word and syntax part----------------
//-------------helper variables and functions-------------
extern struct ParseTree *parseTree;
void printJason(struct TreeNode *node);
void printExpression(struct Expression *exp);
int executeParser(const char *filename);
void parsePrintFunction(struct FactParam *params);
#ifdef __cplusplus
}
#endif
#endif