forked from yewentao256/Sicpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.c
215 lines (189 loc) · 6.49 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
#include "MEM.h"
#include "DBG.h"
#include "sicpy.h"
/* 定义函数 */
void scp_define_function(char *identifier, ParameterList *parameter_list, Block *block)
{
/* 如果已有该函数定义,则报错 */
if (scp_search_function(identifier)) {
scp_compile_error(FUNCTION_MULTIPLE_DEFINE_ERR,
STRING_MESSAGE_ARGUMENT, "name", identifier, MESSAGE_ARGUMENT_END);
return;
}
SCP_Interpreter *inter = scp_get_interpreter();
FunctionDefinition *f = scp_malloc(sizeof(FunctionDefinition));
f->name = identifier;
f->type = SICPY_FUNCTION_DEFINITION;
f->u.sicpy_f.parameter = parameter_list;
f->u.sicpy_f.block = block;
/* 头插法将函数加入函数链表 */
f->next = inter->function_list;
inter->function_list = f;
}
/* 传入标识符,创建单个参数链表 */
ParameterList * scp_create_one_parameter_list(char *identifier)
{
ParameterList *p =scp_malloc(sizeof(ParameterList));
p->name = identifier;
p->next = NULL;
return p;
}
/* 传入参数链表和标识符,连接并返回新链表 */
ParameterList * scp_chain_parameter_list(ParameterList *list, char *identifier)
{
ParameterList *pos;
for (pos = list; pos->next; pos = pos->next);
pos->next = scp_create_one_parameter_list(identifier);
return list;
}
/* 创建单个实参链表 */
ArgumentList * scp_create_one_argument_list(Expression *expression)
{
ArgumentList *al= scp_malloc(sizeof(ArgumentList));
al->expression = expression;
al->next = NULL;
return al;
}
/* 创建单个实参链表并连接现有实参链表 */
ArgumentList * scp_chain_argument_list(ArgumentList *list, Expression *expr)
{
ArgumentList *pos;
for (pos = list; pos->next; pos = pos->next);
pos->next = scp_create_one_argument_list(expr);
return list;
}
/* 传入语句,创建单语句链表 */
StatementList * scp_create_one_statement_list(Statement *statement)
{
StatementList *sl = scp_malloc(sizeof(StatementList));
sl->statement = statement;
sl->next = NULL;
return sl;
}
/* 连接语句链表 */
StatementList * scp_chain_statement_list(StatementList *list, Statement *statement)
{
StatementList *pos;
/* 当前语句链表为空则创建新链表 */
if (list == NULL)
return scp_create_one_statement_list(statement);
/* pos遍历至链表尾 */
for (pos = list; pos->next; pos = pos->next);
pos->next = scp_create_one_statement_list(statement);
return list;
}
/* 传入表达式类型,创建表达式 */
Expression * scp_alloc_expression(ExpressionType type)
{
Expression *exp = scp_malloc(sizeof(Expression));
exp->type = type;
exp->line_number = scp_get_interpreter()->current_line_number;
return exp;
}
/* 创建赋值表达式,传入变量(identifier)和操作数(等号右边表达式),返回新表达式 */
Expression * scp_create_assign_expression(char *variable, Expression *operand)
{
Expression *exp = scp_alloc_expression(ASSIGN_EXPRESSION);
/* 对应变量和操作数赋值 */
exp->u.assign_expression.variable = variable;
exp->u.assign_expression.operand = operand;
return exp;
}
/* 给表达式赋值 */
static Expression assign_value_to_expression(SCP_Value *v)
{
Expression expr;
/* 如果是int值 */
if (v->type == SCP_INT_VALUE) {
expr.type = INT_EXPRESSION;
expr.u.int_value = v->u.int_value;
}
/* 如果是double值 */
else if (v->type == SCP_DOUBLE_VALUE) {
expr.type = DOUBLE_EXPRESSION;
expr.u.double_value = v->u.double_value;
}
/* 如果是double值 */
else {
DBG_assert(v->type == SCP_BOOLEAN_VALUE,("v->type..%d\n", v->type));
expr.type = BOOLEAN_EXPRESSION;
expr.u.boolean_value = v->u.boolean_value;
}
return expr;
}
/* 创建二元表达式 */
Expression * scp_create_binary_expression(ExpressionType operator, Expression *left, Expression *right)
{
/* 如果是数值类型 */
if ((left->type == INT_EXPRESSION || left->type == DOUBLE_EXPRESSION)
&& (right->type == INT_EXPRESSION || right->type == DOUBLE_EXPRESSION)) {
SCP_Value v = scp_eval_binary_expression(scp_get_interpreter(), NULL, operator, left, right);
/* 将值赋给左式. */
*left = assign_value_to_expression(&v);
return left;
}
else {
Expression *exp = scp_alloc_expression(operator);
exp->u.binary_expression.left = left;
exp->u.binary_expression.right = right;
return exp;
}
}
/* 创建一元表达式 */
Expression * scp_create_minus_expression(Expression *exp)
{
/* 如果传入表达式为为int或double */
if (exp->type == INT_EXPRESSION || exp->type == DOUBLE_EXPRESSION) {
SCP_Value v = scp_eval_minus_expression(scp_get_interpreter(), NULL, exp);
*exp = assign_value_to_expression(&v); /* 注意,这里会覆盖原来的exp */
return exp;
}
else {
Expression *new_exp = scp_alloc_expression(MINUS_EXPRESSION);
new_exp->u.minus_expression = exp;
return new_exp;
}
}
/* 创建函数调用表达式 */
Expression * scp_create_function_call_expression(char *func_name, ArgumentList *argument)
{
Expression *exp = scp_alloc_expression(FUNCTION_CALL_EXPRESSION);
exp->u.function_call_expression.identifier = func_name;
exp->u.function_call_expression.argument = argument;
return exp;
}
/* 创建语句 */
Statement * alloc_statement(StatementType type)
{
Statement *st = scp_malloc(sizeof(Statement));
st->type = type;
st->line_number = scp_get_interpreter()->current_line_number;
return st;
}
/* 创建单个标识符链表 */
IdentifierList * scp_create_global_identifier(char *identifier)
{
IdentifierList *i_list = scp_malloc(sizeof(IdentifierList));
i_list->name = identifier;
i_list->next = NULL;
return i_list;
}
/* 连接标识符链表 */
IdentifierList * scp_chain_identifier(IdentifierList *list, char *identifier)
{
IdentifierList *pos;
for (pos = list; pos->next; pos = pos->next);
pos->next = scp_create_global_identifier(identifier);
return list;
}
/* 创建if语句 */
Statement * scp_create_if_statement(Expression *condition,
Block *then_block, Elif *elif_list, Block *else_block)
{
Statement *st = alloc_statement(IF_STATEMENT);
st->u.if_block.condition = condition;
st->u.if_block.then_block = then_block;
st->u.if_block.elif_list = elif_list;
st->u.if_block.else_block = else_block;
return st;
}