-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
273 lines (233 loc) · 8.53 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
/*************************************************************************
> File Name: execute.c
> Author: weapon
> Mail: [email protected]
> Created Time: 2016年07月10日 星期日 15时02分15秒
************************************************************************/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "MEM.h"
#include "DBG.h"
#include "origin.h"
/* 执行模块 */
static StatementResult execute_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement);
//执行表达式语句
static StatementResult execute_expression_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
StatementResult result;
ORG_Value value;
result.type = NORMAL_STATEMENT_RESULT;
value = org_eval_expression(inter, env, statement->u.expression_s);
if (value.type == ORG_STRING_VALUE) {
org_release_string(value.u.string_value);
}
return result;
}
//执行全局语句
static StatementResult execute_global_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
IdentifierList *pos;
StatementResult result;
result.type = NORMAL_STATEMENT_RESULT;
if (env == NULL) {
//runtime error
printf("runtime error");
exit(1);
}
for (pos = statement->u.global_s.identifier_list; pos; pos = pos->next) {
GlobalVariableRef *ref_pos;
GlobalVariableRef *new_ref;
Variable *variable;
for (ref_pos = env->global_variable; ref_pos; ref_pos = ref_pos->next) {
if (!strcmp(ref_pos->variable->name, pos->name)) {
goto NEXT_IDENTIFIER;
}
}
variable = org_search_global_variable(inter, pos->name);
if (variable == NULL) {
//runtime error
printf("runtime error");
exit(1);
}
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;
}
//执行elseif语句
static StatementResult execute_elseif(ORG_Interpreter *inter, LocalEnvironment *env, Elseif *elseif_list, ORG_Boolean *executed) {
StatementResult result;
ORG_Value cond;
Elseif *pos;
*executed = ORG_FALSE;
result.type = NORMAL_STATEMENT_RESULT;
for (pos = elseif_list; pos; pos = pos->next) {
cond = org_eval_expression(inter, env, pos->condition);
if (cond.type != ORG_BOOLEAN_VALUE) {
//runtime error
printf("runtime error");
exit(1);
}
if (cond.u.boolean_value) {
result = org_execute_statement_list(inter, env, pos->block->statement_list);
*executed = ORG_TRUE;
if (result.type != NORMAL_STATEMENT_RESULT)
goto FUNC_END;
}
}
FUNC_END:
return result;
}
//执行if语句
static StatementResult execute_if_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
StatementResult result;
ORG_Value cond;
result.type = NORMAL_STATEMENT_RESULT;
cond = org_eval_expression(inter, env, statement->u.if_s.condition);
if (cond.type != ORG_BOOLEAN_VALUE) {
//runtime error
printf("runtime error");
exit(1);
}
if (cond.u.boolean_value) {
result = org_execute_statement_list(inter, env, statement->u.if_s.then_block->statement_list);
} else {
ORG_Boolean elseif_executed;
result = execute_elseif(inter, env, statement->u.if_s.elseif_list, &elseif_executed);
if (result.type != NORMAL_STATEMENT_RESULT) {
return result;
}
if (!elseif_executed && statement->u.if_s.else_block) {
result = org_execute_statement_list(inter, env, statement->u.if_s.else_block->statement_list);
}
}
return result;
}
//执行while语句
static StatementResult execute_while_statement(ORG_Interpreter *inter, LocalEnvironment *env,
Statement *statement)
{
StatementResult result;
ORG_Value cond;
result.type = NORMAL_STATEMENT_RESULT;
for (;;) {
cond = org_eval_expression(inter, env, statement->u.while_s.condition);
if (cond.type != ORG_BOOLEAN_VALUE) {
//crb_runtime_error(statement->u.while_s.condition->line_number,NOT_BOOLEAN_TYPE_ERR, MESSAGE_ARGUMENT_END);
exit(1);
}
if (!cond.u.boolean_value)
break;
result = org_execute_statement_list(inter, env, statement->u.while_s.block->statement_list);
if (result.type == RETURN_STATEMENT_RESULT) {
break;
} else if (result.type == BREAK_STATEMENT_RESULT) {
result.type = NORMAL_STATEMENT_RESULT;
break;
}
}
return result;
}
static StatementResult execute_for_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
StatementResult result;
ORG_Value cond;
result.type = NORMAL_STATEMENT_RESULT;
if (statement->u.for_s.init) {
org_eval_expression(inter, env, statement->u.for_s.init);
}
for (;;) {
//for的判断循环条件
if (statement->u.for_s.condition) {
cond = org_eval_expression(inter, env, statement->u.for_s.condition);
if (cond.type != ORG_BOOLEAN_VALUE) {
//runtime error
exit(1);
}
if (!cond.u.boolean_value) {
break;
}
}
result = org_execute_statement_list(inter, env, statement->u.for_s.block->statement_list);
if (result.type == RETURN_STATEMENT_RESULT) {
break;
} else if (result.type == BREAK_STATEMENT_RESULT) {
result.type = NORMAL_STATEMENT_RESULT;
break;
}
if (statement->u.for_s.post) {
org_eval_expression(inter, env, statement->u.for_s.post);
}
}
return result;
}
static StatementResult execute_return_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
StatementResult result;
result.type = RETURN_STATEMENT_RESULT;
if(statement->u.return_s.return_value) {
result.u.return_value = org_eval_expression(inter, env, statement->u.return_s.return_value);
} else {
result.u.return_value.type = ORG_NULL_VALUE;
}
return result;
}
static StatementResult execute_break_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
StatementResult result;
result.type = BREAK_STATEMENT_RESULT;
return result;
}
static StatementResult execute_continue_statement(ORG_Interpreter *inter, LocalEnvironment *env, Statement *statement) {
StatementResult result;
result.type = CONTINUE_STATEMENT_RESULT;
return result;
}
static StatementResult execute_statement(ORG_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 = execute_break_statement(inter, env, statement);
break;
case CONTINUE_STATEMENT:
result = execute_continue_statement(inter, env, statement);
break;
case STATEMENT_RESULT_TYPE_COUNT_PLUS_1:
break;
default:
printf("bad case");
}
return result;
}
//开放,所以以org开头
StatementResult org_execute_statement_list(ORG_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) {
return result;
}
}
return result;
}