-
Notifications
You must be signed in to change notification settings - Fork 25
/
crowbar.h
337 lines (290 loc) · 6.99 KB
/
crowbar.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#ifndef PRIVATE_CROWBAR_H_INCLUDED
#define PRIVATE_CROWBAR_H_INCLUDED
#include <stdio.h>
#include "MEM.h"
#include "CRB.h"
#include "CRB_dev.h"
#define LINE_BUF_SIZE (1024)
/**
* 编译错误
*/
typedef enum {
/* 解析出错 */
PARSE_ERR = 1,
/* 错误的字符 */
CHARACTER_INVALID_ERR,
/* 一个函数定义了多次错误 */
FUNCTION_MULTIPLE_DEFINE_ERR,
COMPILE_ERROR_COUNT_PLUS_1
} CompileError;
/**
* 消息的参数类型
*/
typedef enum {
INT_MESSAGE_ARGUMENT = 1,
DOUBLE_MESSAGE_ARGUMENT,
STRING_MESSAGE_ARGUMENT,
CHARACTER_MESSAGE_ARGUMENT,
POINTER_MESSAGE_ARGUMENT,
MESSAGE_ARGUMENT_END
} MessageArgumentType;
typedef struct Variable_tag
{
/* 变量的名字 */
char *name;
/* 变量的值 */
CRB_Value value;
/* 下一个变量,组成一个链表 */
struct Variable_tag *next;
} Variable;
typedef enum {
/* crowbar 里面的函数 */
CROWBAR_FUNCTION_DEFINITION = 1,
/* native 函数 -- 其实就是 C 函数 */
NATIVE_FUNCTION_DEFINITION
} FunctionDefinitionType;
/**
* 参数列表
*/
typedef struct ParameterList_tag {
/* 参数的名字 */
char *name;
/* 下一个参数 */
struct ParameterList_tag *next;
} ParameterList;
typedef struct Statement_tag Statement;
typedef struct FunctionDefinition_tag {
/* 函数的名字 */
char *name;
/* 函数的类型 */
FunctionDefinitionType type;
union {
/* crowbar 函数体 */
struct {
/* 参数列表 */
ParameterList *parameter;
/* 函数体 */
Block *block;
} crowbar_f;
struct {
CRB_NativeFunctionProc *proc;
} native_f;
} u;
/* 下一个函数定义 */
struct FunctionDefinition_tag *next;
} FunctionDefinition;
struct CRB_Interpreter_tag {
/* 编译使用用的内存 */
MEM_Storage interpreter_storage;
/* 执行时候用的内存 */
MEM_Storage execute_storage;
/* 全局变量列表 */
Variable *variable;
/* 函数列表 */
FunctionDefinition *function_list;
/* 顶层语句列表 */
StatementList *statement_list;
/* 当前行号 */
int current_line_number;
};
typedef struct Expression_tag Expression;
typedef enum {
BOOLEAN_EXPRESSION = 1,
INT_EXPRESSION,
DOUBLE_EXPRESSION,
STRING_EXPRESSION,
IDENTIFIER_EXPRESSION,
ASSIGN_EXPRESSION,
ADD_EXPRESSION,
SUB_EXPRESSION,
MUL_EXPRESSION,
DIV_EXPRESSION,
MOD_EXPRESSION,
EQ_EXPRESSION,
NE_EXPRESSION,
GT_EXPRESSION,
GE_EXPRESSION,
LT_EXPRESSION,
LE_EXPRESSION,
LOGICAL_AND_EXPRESSION,
LOGICAL_OR_EXPRESSION,
MINUS_EXPRESSION,
FUNCTION_CALL_EXPRESSION,
NULL_EXPRESSION,
EXPRESSION_TYPE_COUNT_PLUS_1
} ExpressionType;
typedef struct ArgumentList_tag {
Expression *expression;
struct ArgumentList_tag *next;
} ArgumentList;
typedef struct {
char *variable;
Expression *operand;
} AssignExpression;
typedef struct {
Expression *left;
Expression *right;
} BinaryExpression;
typedef struct {
char *identifier;
ArgumentList *argument;
} FunctionCallExpression;
struct Expression_tag {
ExpressionType type;
int line_number;
union {
CRB_Boolean boolean_value;
int int_value;
double double_value;
char *string_value;
char *identifier;
AssignExpression assign_expression;
BinaryExpression binary_expression;
Expression *minus_expression;
FunctionCallExpression function_call_expression;
} u;
};
typedef struct StatementList_tag {
Statement *statement;
struct StatementList_tag *next;
} StatementList;
typedef struct {
StatementList *statemen_list;
} Block;
typedef struct IdentifierList_tag {
char *name;
struct IdentifierList_tag *next;
} IdentifierList;
typedef struct {
IdentifierList *identifier_list;
} GlobalStatement;
typedef struct Elsif_tag {
Expression *condition;
Block *block;
struct Elsif_tag *next;
} Elsif;
typedef struct {
Expression *condition;
Block *then_block;
Elsif *elsif_list;
Block *else_block;
} IfStatement;
typedef struct {
Expression *condition;
Block *block;
} WhileStatement;
typedef struct {
Expression *init;
Expression *condition;
Expression *post;
Block *block;
} ForStatement;
typedef struct {
Expression *return_value;
} ReturnStatement;
typedef enum {
EXPRESSION_STATEMENT = 1,
GLOBAL_STATEMENT,
IF_STATEMENT,
WHILE_STATEMENT,
FOR_STATEMENT,
RETURN_STATEMENT,
BREAK_STATEMENT,
CONTINUE_STATEMENT,
STATEMENT_TYPE_COUNT_PLUS_1
} StatementType;
struct Statement_tag {
StatementType type;
int line_number;
union {
Expression *expression_s;
GlobalStatement global_s;
IfStatement if_s;
WhileStatement while_s;
ForStatement for_s;
ReturnStatement return_s;
} u;
};
typedef struct ParameterList_tag {
char *name;
struct ParameterList_tag *next;
} ParameterList;
typedef enum {
CROWBAR_FUNCTION_DEFINITION = 1,
NATIVE_FUNCTION_DEFINITION
} FunctionDefinitionType;
typedef struct FunctionDefinition_tag {
char *name;
FunctionDefinitionType type;
union {
struct {
ParameterList *parameter;
Block *block;
} crowbar_f;
struct {
CRB_NativeFunctionProc *proc;
} native_f;
} u;
struct FunctionDefinition_tag *next;
} FunctionDefinition;
typedef struct Variable_tag {
char *name;
CRB_Value value;
struct Variable_tag *next;
} Variable;
typedef enum {
NORMAL_STATEMENT_RESULT = 1,
RETURN_STATEMENT_RESULT,
BREAK_STATEMENT_RESULT,
CONTINUE_STATEMENT_RESULT,
STATEMENT_RESULT_TYPE_COUNT_PLUS_1
} StatementResultType;
typedef struct {
StatementResultType type;
union {
CRB_Value return_value;
} u;
} StatementResult;
typedef struct GlobalVariableRef_tag {
Variable *variable;
struct GlobalVariableRef_tag *next;
} GlobalVariableRef;
typedef struct {
Variable *variable;
GlobalVariableRef *next;
} LocalEnvironment;
struct CRB_String_tag {
int ref_count;
char *string;
CRB_Boolean is_literal;
};
typedef struct {
CRB_String *strings;
} StringPool;
/* create.c */
void crb_function_define(char *identifier, ParameterList *parameter_list,
Block *block);
ParameterList *crb_create_parameter(char *identifier);
ParameterList *crb_chain_parameter(ParameterList *list,
char *identifier);
ArgumentList *crb_create_argument_list(Expression *expression);
ArgumentList *crb_chain_argument_list(ArgumentList *list, Expression *expr);
StatementList *crb_create_statement_list(Statement *statement);
StatementList *crb_chain_statement_list(StatementList *list,
Statement *statement);
Expression *crb_create_assign_expression(char *variable,
Expression *operand);
Expression *crb_create_binary_expression(ExpressionType operator,
Expression *left,
Expression *right);
/* eval.c */
CRB_Value crb_eval_binary_expression(CRB_Interpreter *inter,
LocalEnvironment *env,
ExpressionType operator,
Expression *left,
Expression *right);
/* string.c */
void crb_open_string_literal(void);
/* error.c */
void crb_compile_error(CompileError id, ...);
#endif /* PRIVATE_CROWBAR_H_INCLUDED */