forked from yewentao256/Sicpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
277 lines (251 loc) · 9.73 KB
/
execute.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
#include <math.h>
#include <string.h>
#include "MEM.h"
#include "DBG.h"
#include "sicpy.h"
static StatementResult execute_statement(SCP_Interpreter *inter,
LocalEnvironment *env, Statement *statement);
/* 执行表达式语句 */
static StatementResult execute_expression_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
result.type = NORMAL_STATEMENT_RESULT;
/* 计算表达式值 */
SCP_Value v = scp_eval_expression(inter, env, statement->u.expression_s);
/* 如果是字符串类型,进行释放 */
if (v.type == SCP_STRING_VALUE) {
scp_release_string(v.u.string_value);
}
return result;
}
/* 执行global语句 */
static StatementResult execute_global_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
IdentifierList *pos;
StatementResult result;
result.type = NORMAL_STATEMENT_RESULT;
/* 如果没有局部变量环境,报错 */
if (env == NULL) {
scp_runtime_error(statement->line_number,
GLOBAL_STATEMENT_IN_TOPLEVEL_ERR, MESSAGE_ARGUMENT_END);
}
/* 遍历全局变量标识符链表 */
for (pos = statement->u.global_identifier_list; pos; pos = pos->next) {
GlobalVariableRef *global_v_ref;
/* 遍历局部环境的全局变量链表,如果发现已有全局变量标识符,则到NEXT_IDENTIFIER */
for (global_v_ref = env->global_variable; global_v_ref; global_v_ref = global_v_ref->next) {
if (!strcmp(global_v_ref->variable->name, pos->name))
goto NEXT_IDENTIFIER;
}
/* 搜索当前标识符的变量 */
Variable *variable = scp_search_global_variable(inter, pos->name);
if (variable == NULL) {
scp_runtime_error(statement->line_number, GLOBAL_VARIABLE_NOT_FOUND_ERR,
STRING_MESSAGE_ARGUMENT, "name", pos->name, MESSAGE_ARGUMENT_END);
}
/* 创建新引用,将该变量头插加入局部环境全局变量链表 */
GlobalVariableRef *new_ref = MEM_malloc(sizeof(GlobalVariableRef));
new_ref->variable = variable;
new_ref->next = env->global_variable;
env->global_variable = new_ref;
NEXT_IDENTIFIER:;
}
return result;
}
/* 执行elif语句 */
static StatementResult execute_elif(SCP_Interpreter *inter, LocalEnvironment *env,
Elif *elif_list, SCP_Boolean *executed)
{
StatementResult result;
SCP_Value cond;
Elif *pos;
*executed = SCP_FALSE;
result.type = NORMAL_STATEMENT_RESULT;
/* 对于elif链表的每个元素,进行elif运算 */
for (pos = elif_list; pos; pos = pos->next) {
cond = scp_eval_expression(inter, env, pos->condition);
if (cond.type != SCP_BOOLEAN_VALUE) {
scp_runtime_error(pos->condition->line_number,
NOT_BOOLEAN_TYPE_ERR, MESSAGE_ARGUMENT_END);
}
if (cond.u.boolean_value) {
result = scp_execute_statement_list(inter, env, pos->block->statement_list);
*executed = SCP_TRUE;
if (result.type != NORMAL_STATEMENT_RESULT)
break;
}
}
return result;
}
/* 执行if语句 */
static StatementResult execute_if_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
SCP_Value cond = scp_eval_expression(inter, env, statement->u.if_block.condition);
result.type = NORMAL_STATEMENT_RESULT;
/* 条件计算后不是布尔值报错 */
if (cond.type != SCP_BOOLEAN_VALUE) {
scp_runtime_error(statement->u.if_block.condition->line_number,
NOT_BOOLEAN_TYPE_ERR, MESSAGE_ARGUMENT_END);
}
DBG_assert(cond.type == SCP_BOOLEAN_VALUE, ("cond.type..%d", cond.type));
/* 条件值为真,执行if语句链表 */
if (cond.u.boolean_value) {
result = scp_execute_statement_list(inter, env,
statement->u.if_block.then_block ->statement_list);
}
else {
SCP_Boolean elif_executed;
result = execute_elif(inter, env, statement->u.if_block.elif_list, &elif_executed);
if (result.type != NORMAL_STATEMENT_RESULT)
goto FUNC_END;
/* elif语句没有执行且if语句块存在else块,执行else块 */
if (!elif_executed && statement->u.if_block.else_block) {
result = scp_execute_statement_list(inter, env,
statement->u.if_block.else_block ->statement_list);
}
}
FUNC_END:
return result;
}
/* 执行while语句 */
static StatementResult execute_while_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
SCP_Value cond;
result.type = NORMAL_STATEMENT_RESULT;
for (;;) {
cond = scp_eval_expression(inter, env, statement->u.while_block.condition);
if (cond.type != SCP_BOOLEAN_VALUE) {
scp_runtime_error(statement->u.while_block.condition->line_number,
NOT_BOOLEAN_TYPE_ERR, MESSAGE_ARGUMENT_END);
}
DBG_assert(cond.type == SCP_BOOLEAN_VALUE, ("cond.type..%d", cond.type));
/* 条件非真值退出 */
if (!cond.u.boolean_value)
break;
result = scp_execute_statement_list(inter, env,
statement->u.while_block.block ->statement_list);
/* 执行return则退出 */
if (result.type == RETURN_STATEMENT_RESULT) {
break;
}
/* 执行break则退出 */
else if (result.type == BREAK_STATEMENT_RESULT) {
result.type = NORMAL_STATEMENT_RESULT;
break;
}
}
return result;
}
/* 执行for语句 */
static StatementResult execute_for_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
SCP_Value cond;
result.type = NORMAL_STATEMENT_RESULT;
/* 如果init中有值或表达式,进行计算 */
if (statement->u.for_block.init) {
scp_eval_expression(inter, env, statement->u.for_block.init);
}
for (;;) {
if (statement->u.for_block.condition) {
cond = scp_eval_expression(inter, env, statement->u.for_block.condition);
if (cond.type != SCP_BOOLEAN_VALUE) {
scp_runtime_error(statement->u.for_block.condition->line_number,
NOT_BOOLEAN_TYPE_ERR, MESSAGE_ARGUMENT_END);
}
DBG_assert(cond.type == SCP_BOOLEAN_VALUE, ("cond.type..%d", cond.type));
if (!cond.u.boolean_value)
break;
}
result = scp_execute_statement_list(inter, env,
statement->u.for_block.block ->statement_list);
/* 执行return或break则退出 */
if (result.type == RETURN_STATEMENT_RESULT) {
break;
}
else if (result.type == BREAK_STATEMENT_RESULT) {
result.type = NORMAL_STATEMENT_RESULT;
break;
}
/* 如果post(for循环括号中第三部分)中有语句,执行 */
if (statement->u.for_block.post) {
scp_eval_expression(inter, env, statement->u.for_block.post);
}
}
return result;
}
/* 执行返回语句 */
static StatementResult execute_return_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
result.type = RETURN_STATEMENT_RESULT;
/* 有表达式则执行,否则返回空 */
if (statement->u.return_expression) {
result.return_value = scp_eval_expression(inter, env, statement->u.return_expression);
}
else {
result.return_value.type = SCP_NULL_VALUE;
}
return result;
}
/* 执行语句 */
static StatementResult execute_statement(SCP_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
result.type = NORMAL_STATEMENT_RESULT;
/* 根据语句类型执行语句 */
switch (statement->type) {
case EXPRESSION_STATEMENT:
result = execute_expression_statement(inter, env, statement);
break;
case GLOBAL_STATEMENT:
result = execute_global_statement(inter, env, statement);
break;
case IF_STATEMENT:
result = execute_if_statement(inter, env, statement);
break;
case WHILE_STATEMENT:
result = execute_while_statement(inter, env, statement);
break;
case FOR_STATEMENT:
result = execute_for_statement(inter, env, statement);
break;
case RETURN_STATEMENT:
result = execute_return_statement(inter, env, statement);
break;
case BREAK_STATEMENT:
result.type = BREAK_STATEMENT_RESULT;
break;
case CONTINUE_STATEMENT:
result.type = CONTINUE_STATEMENT_RESULT;
break;
case STATEMENT_TYPE_COUNT_PLUS_1: /* FALLTHRU */
default:
DBG_panic(("bad case...%d", statement->type));
}
return result;
}
/* 执行语句列表 */
StatementResult scp_execute_statement_list(SCP_Interpreter *inter, LocalEnvironment *env,
StatementList *list)
{
StatementList *pos;
StatementResult result;
result.type = NORMAL_STATEMENT_RESULT;
for (pos = list; pos; pos = pos->next) {
result = execute_statement(inter, env, pos->statement);
/* 如果不是正常的返回结果,则退出执行列表 */
if (result.type != NORMAL_STATEMENT_RESULT)
break;
}
return result;
}