-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.c
350 lines (290 loc) · 9.38 KB
/
create.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*************************************************************************
> File Name: create.c
> Author: weapon
> Mail: [email protected]
> Created Time: 2016年07月09日 星期六 23时39分01秒
************************************************************************/
#include "origin.h"
#include "bignum.h"
//创建函数
void org_function_define(char *identifier, ParameterList *parameter_list, Block *block) {
FunctionDefine *fun;
ORG_Interpreter *inter;
if (org_search_function(identifier)) {
//如果函数名不存在
//org_compile_error(FUNCTION);
return;
}
inter = org_get_current_interpreter();
fun = org_malloc(sizeof(FunctionDefine));
fun->name = identifier;
fun->type = ORIGIN_FUNCTION_DEF;
fun->u.origin_f.parameter = parameter_list;
fun->u.origin_f.block = block;
fun->next = inter->function_list;
inter->function_list = fun;
}
//创建参数
ParameterList *org_create_parameter(char *identifier) {
ParameterList *p;
p = org_malloc(sizeof(ParameterList));
p->name = identifier;
p->next = NULL;
return p;
}
ParameterList *org_chain_parameter(ParameterList *list, char *identifier) {
ParameterList *pos;
for (pos = list; pos->next != NULL; pos = pos->next);
pos->next = org_create_parameter(identifier);
return list;
}
//创建参数列表
ArgumentList *org_create_argument_list(Expression *expression) {
ArgumentList *al;
al = org_malloc(sizeof(ArgumentList));
al->expression = expression;
al->next = NULL;
return al;
}
ArgumentList *org_chain_argument_list(ArgumentList *list, Expression *expr) {
ArgumentList *pos;
for (pos = list; pos->next != NULL; pos = pos->next);
pos->next = org_create_argument_list(expr);
return list;
}
//创建语句 数组
StatementList *org_create_statement_list(Statement *statement) {
StatementList *sl;
sl = org_malloc(sizeof(StatementList));
sl->statement = statement;
sl->next = NULL;
return sl;
}
StatementList *org_chain_statement_list(StatementList *list, Statement *statement) {
StatementList *pos;
if (list == NULL) {
return org_create_statement_list(statement);
}
for (pos = list; pos->next != NULL; pos = pos->next);
pos->next = org_create_statement_list(statement);
return list;
}
Expression *org_create_bignum(const char *text) {
Expression *exp = org_alloc_expression(BIG_DATA_EXPRESSION);
mpi_init(&exp->u.big_num);
mpi_read_string( &exp->u.big_num, 10, text);
return exp;
}
//申请存放表达式的地址空间
Expression *org_alloc_expression(ExpressionType type) {
Expression *exp;
exp = org_malloc(sizeof(Expression));
exp->type = type;
exp->line_number = org_get_current_interpreter()->current_line_number;
return exp;
}
//创建赋值表达式
Expression *org_create_assign_expression(char *variable, Expression *operand) {
Expression *exp;
exp = org_alloc_expression(ASSIGN_EXPRESSION);
exp->u.assign_expression.variable = variable;
exp->u.assign_expression.operand = operand;
return exp;
}
//表达式换算 把任意类型换成表达式形式
static Expression convert_value_to_expression(ORG_Value *v) {
Expression expr;
if (v->type == ORG_INT_VALUE) {
expr.type = INT_EXPRESSION;
expr.u.int_value = v->u.int_value;
}else if (v->type == ORG_DOUBLE_VALUE) {
expr.type = DOUBLE_EXPRESSION;
expr.u.double_value = v->u.double_value;
} else {
//error
//DBG_assert(v->type = ORG_BOOLEAN_VALUE)
expr.type = BOOLEAN_EXPRESSION;
expr.u.boolean_value = v->u.boolean_value;
}
return expr;
}
//创建二元表达式
Expression *org_create_binary_expression(ExpressionType op, Expression *left, Expression *right) {
if ((left->type == INT_EXPRESSION || left->type == DOUBLE_EXPRESSION)
&& (right->type == INT_EXPRESSION || right == DOUBLE_EXPRESSION)) {
ORG_Value value;
/* Overwriting left hand expression. */
value = org_eval_binary_expression(org_get_current_interpreter(), NULL, op, left, right);
*left = convert_value_to_expression(&value);
return left;
} else {
Expression *exp;
exp = org_alloc_expression(op);
exp->u.binary_expression.left = left;
exp->u.binary_expression.right = right;
return exp;
}
}
//创建 单元取反 表达式
Expression *org_create_minus_expression(Expression *operand) {
if (operand->type == INT_EXPRESSION || operand->type == DOUBLE_EXPRESSION) {
ORG_Value v;
v = org_eval_minus_expression(org_get_current_interpreter(), NULL, operand);
/* Notice! Overwriting operand expression. */
*operand = convert_value_to_expression(&v);
return operand;
} else {
Expression *exp;
exp = org_alloc_expression(MINUS_EXPRESSION);
exp->u.minus_expression = operand;
return exp;
}
}
//变量表达式
Expression *org_create_identifier_expression(char *identifier) {
Expression *exp;
exp = org_alloc_expression(IDENTIFIER_EXPRESSION);
exp->u.identifier = identifier;
return exp;
}
//函数表达式
Expression *org_create_function_call_expression(char *fun_name, ArgumentList *argument) {
Expression *exp;
exp = org_alloc_expression(FUNCTION_CALL_EXPRESSION);
exp->u.function_call_expression.identifier = fun_name;
exp->u.function_call_expression.argument = argument;
return exp;
}
// 布尔类型表达式
Expression *org_create_boolean_expression(ORG_Boolean value) {
Expression *exp;
exp = org_alloc_expression(BOOLEAN_EXPRESSION);
exp->u.boolean_value = value;
return exp;
}
// null 表达式
Expression *org_create_null_expression(void) {
Expression *exp;
exp = org_alloc_expression(NULL_EXPRESSION);
return exp;
}
/* statement */
//申请语句内存空间
static Statement *org_alloc_statement(StatementType type) {
Statement *st;
st = org_malloc(sizeof(Statement));
st->type = type;
st->line_number = org_get_current_interpreter()->current_line_number;
return st;
}
//全局语句
Statement *org_create_global_statement(IdentifierList *identifier_list) {
Statement *st;
st = org_alloc_statement(GLOBAL_STATEMENT);
st->u.global_s.identifier_list = identifier_list;
return st;
}
//创建全局变量
IdentifierList *org_create_global_identifier(char *identifier) {
IdentifierList *i_list;
i_list = org_malloc(sizeof(IdentifierList));
i_list->name = identifier;
i_list->next = NULL;
return i_list;
}
IdentifierList *org_chain_identifier(IdentifierList *list, char *identifier) {
IdentifierList *pos;
for (pos = list; pos->next != NULL; pos = pos->next) ;
pos->next = org_create_global_identifier(identifier);
}
//if语句
Statement *org_create_if_statement(Expression *condition, Block *then_block,
Elseif *elseif_list, Block *else_block) {
Statement *st;
st = org_alloc_statement(IF_STATEMENT);
st->u.if_s.condition = condition;
st->u.if_s.then_block = then_block;
st->u.if_s.elseif_list = elseif_list;
st->u.if_s.else_block = else_block;
return st;
}
//elseif 语句
Elseif *org_create_elseif(Expression *condition, Block *block) {
Elseif *ei;
ei = org_malloc(sizeof(Elseif));
ei->condition = condition;
ei->block = block;
ei->next = NULL;
return ei;
}
Elseif *org_chain_elseif_list(Elseif *list, Elseif *add) {
Elseif *pos;
for (pos = list; pos->next != NULL; pos = pos->next) ;
pos->next = add;
return list;
}
//while 语句
Statement *org_create_while_statement(Expression *condition, Block *block) {
Statement *st;
st = org_alloc_statement(WHILE_STATEMENT);
st->u.while_s.condition = condition;
st->u.while_s.block = block;
return st;
}
// for 语句
Statement *org_create_for_statement(Expression *init, Expression *condition, Expression *post, Block *block) {
Statement *st;
st = org_alloc_statement(FOR_STATEMENT);
st->u.for_s.init = init;
st->u.for_s.condition = condition;
st->u.for_s.post = post;
st->u.for_s.block = block;
return st;
}
// 创建块段
Block *org_create_block(StatementList *statement_list) {
Block *block;
block = org_malloc(sizeof(Block));
block->statement_list = statement_list;
return block;
}
// 创建表达式语句
Statement *org_create_expression_statement(Expression *expression) {
Statement *st;
st = org_alloc_statement(EXPRESSION_STATEMENT);
st->u.expression_s = expression;
return st;
}
// return 语句
Statement *org_create_return_statement(Expression *expression) {
Statement *st;
st = org_alloc_statement(RETURN_STATEMENT);
st->u.return_s.return_value = expression;
return st;
}
// break 语句
Statement *org_create_break_statement(void) {
Statement *st;
st = org_alloc_statement(BREAK_STATEMENT);
return st;
}
// continue 语句
Statement *org_create_continue_statement(void) {
Statement *st;
st = org_alloc_statement(CONTINUE_STATEMENT);
return st;
}
// 把ORG_Value 转化为大数类型
mpi org_create_origin_bignum(ORG_Value v) {
if (v.type == ORG_BIGNUM_VALUE) {
return v.u.big_num;
}
char buf[LINE_BUF_SIZE];
mpi ret;
mpi_init(&ret);
if(v.type == ORG_INT_VALUE) {
sprintf(buf, "%d", v.u.int_value);
mpi_read_string( &ret, 10, buf);
}
return ret;
}