-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.c
More file actions
415 lines (397 loc) · 9.9 KB
/
Copy pathenvironment.c
File metadata and controls
415 lines (397 loc) · 9.9 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "common.h"
#include "stdproc.h"
/*************************************************************
*This file provides procedures operating environment
*************************************************************/
////////////////////////////////////////////////////////
//frame
////////////////////////////////////////////////////////
//two args: vars & vals
static cellpoint make_frame(void)
{
reg = cons(args_ref(1), args_ref(2));
args_pop(2);
return reg;
}
//one arg: frame
static cellpoint frame_variables(void)
{
reg = car(args_ref(1));
args_pop(1);
return reg;
}
//one arg: frame
static cellpoint frame_values(void)
{
reg = cdr(args_ref(1));
args_pop(1);
return reg;
}
//three args: var, val & frame
static cellpoint add_binding_to_frame(void)
{
//add var
args_push(args_ref(3));
reg = frame_variables();
reg = cons(args_ref(1), reg);
car_set(args_ref(3), reg);
//add val
args_push(args_ref(3));
reg = frame_values();
reg = cons(args_ref(2), reg);
cdr_set(args_ref(3), reg);
args_pop(3);
return args_ref(3);
}
////////////////////////////////////////////////////////
//environment
////////////////////////////////////////////////////////
static cellpoint the_empty_env = NIL;
//one arg: env
static cellpoint first_frame(void)
{
reg = car(args_ref(1));
args_pop(1);
return reg;
}
//one arg: env
static cellpoint enclosing_env(void)
{
reg = cdr(args_ref(1));
args_pop(1);
return reg;
}
//there args: vars, vals & base_env
cellpoint extend_env(void)
{
int len1, len2;
//vars length
args_push(args_ref(1));
reg = list_len();
len1 = get_integer(reg);
//vals length
args_push(args_ref(2));
reg = list_len();
len2 = get_integer(reg);
if (len1 != len2){
printf("Error: the length of vars(%d) not equal the lengths of vals(%d). --EXTEND_ENV\n", len1, len2);
error_handler();
}else {
reg = args_ref(1);
args_push(args_ref(2));
args_push(reg);
reg = make_frame();
reg = cons(reg, args_ref(3));
}
args_pop(3);
return reg;
}
//two args: var & env
cellpoint lookup_var_val(void)
{
//push env
stack_push(&vars_stack, args_ref(2));
while (stack_top(&vars_stack) != the_empty_env){
args_push(stack_top(&vars_stack));
reg = first_frame();
stack_push(&vars_stack, reg);
//get vals
args_push(stack_top(&vars_stack));
reg = frame_values();
//get vars
args_push(stack_pop(&vars_stack));
stack_push(&vars_stack, reg);
reg = frame_variables();
while (reg != NIL){
stack_push(&vars_stack, reg);
args_push(args_ref(1));
args_push(car(stack_top(&vars_stack)));
reg = eq();
if (is_true(reg)){
stack_pop(&vars_stack);
//get the car of vals
reg = car(stack_pop(&vars_stack));
//pop env
stack_pop(&vars_stack);
args_pop(2);
return reg;
}
//renew the vars
reg = cdr(stack_pop(&vars_stack));
//renew the vals
stack_push(&vars_stack, cdr(stack_pop(&vars_stack)));
}
//pop vals
stack_pop(&vars_stack);
//renew env
args_push(stack_pop(&vars_stack));
reg = enclosing_env();
stack_push(&vars_stack, reg);
}
printf("Error: unknown variables ");
display(args_ref(1));
newline();
error_handler();
args_pop(2);
return NIL;
}
//three args: var, val & env
void define_var(void)
{
//get first frame
args_push(args_ref(3));
reg = first_frame();
stack_push(&vars_stack, reg);
//get vals
args_push(stack_top(&vars_stack));
reg = frame_values();
//get vars
args_push(stack_pop(&vars_stack));
stack_push(&vars_stack, reg);
reg = frame_variables();
while (reg != NIL){
//push vars
stack_push(&vars_stack, reg);
args_push(args_ref(1));
args_push(car(stack_top(&vars_stack)));
reg = eq();
if (is_true(reg)){
//pop vars
stack_pop(&vars_stack);
//set new val
reg = stack_pop(&vars_stack);
car_set(reg, args_ref(2));
args_pop(3);
return;
}
//renew vars
reg = cdr(stack_pop(&vars_stack));
//renew vals
stack_push(&vars_stack, cdr(stack_pop(&vars_stack)));
}
//pop vals
stack_pop(&vars_stack);
//add binding the the first frame
args_push(args_ref(3));
reg = first_frame();
stack_push(&vars_stack, args_ref(1));
stack_push(&vars_stack, args_ref(2));
args_push(reg);
args_push(stack_pop(&vars_stack));
args_push(stack_pop(&vars_stack));
add_binding_to_frame();
//if the val is a compound procedure, then sets its name
if (is_true(is_compound_proc(args_ref(2)))){
//checks whether the procedure has a name
args_push(args_ref(2));
reg = procedure_name();
if (is_true(is_null(reg))){
reg = args_ref(2);
args_push(args_ref(1));
args_push(reg);
procedure_name_set();
}
}
args_pop(3);
}
//three args: var, val & env
void set_var_val(void)
{
//push env
stack_push(&vars_stack, args_ref(3));
while (stack_top(&vars_stack) != the_empty_env){
//get first frame
args_push(stack_top(&vars_stack));
reg = first_frame();
stack_push(&vars_stack, reg);
//get vals
args_push(stack_top(&vars_stack));
reg = frame_values();
//get vars
args_push(stack_pop(&vars_stack));
stack_push(&vars_stack, reg);
reg = frame_variables();
while (reg != NIL){
//push vars
stack_push(&vars_stack, reg);
args_push(args_ref(1));
args_push(car(stack_top(&vars_stack)));
reg = eq();
if (is_true(reg)){
//pop vars
stack_pop(&vars_stack);
//set new val
reg = stack_pop(&vars_stack);
car_set(reg, args_ref(2));
//pop env
stack_pop(&vars_stack);
args_pop(3);
return;
}
//renew vars
reg = cdr(stack_pop(&vars_stack));
//renew vals
stack_push(&vars_stack, cdr(stack_pop(&vars_stack)));
}
//pop vals
stack_pop(&vars_stack);
//renew env
args_push(stack_pop(&vars_stack));
reg = enclosing_env();
stack_push(&vars_stack, reg);
}
printf("Error: unknown variable ");
display(args_ref(1));
newline();
error_handler();
//pop env
stack_pop(&vars_stack);
args_pop(3);
}
///////////////////////////////////////////////////////
//setup environment
///////////////////////////////////////////////////////
struct prim_proc_pair{
char *name;
prim_proc_t func;
};
static struct prim_proc_pair pparray[] = {
//pair procedure
{"pair?", pair_pred_proc},
{"cons", cons_proc},
{"car", car_proc},
{"cdr", cdr_proc},
{"car-set!", car_set_proc},
{"cdr-set!", cdr_set_proc},
//equivalence predicate procedure
{"eq?", eq_proc},
{"eqv?", eqv_proc},
{"equal?", equal_proc},
//list procedure
{"null?", null_proc},
{"list?", list_pred_proc},
{"list", list_proc},
{"length", length_proc},
{"append", append_proc},
{"reverse", reverse_proc},
{"list-tail", list_tail_proc},
{"list-ref", list_ref_proc},
{"memq", memq_proc},
{"memv", memv_proc},
{"member", member_proc},
{"assq", assq_proc},
{"assv", assv_proc},
{"assoc", assoc_proc},
//boolean procedure
{"boolean?", boolean_pred_proc},
{"not", boolean_not_proc},
//character procedure
{"char?", char_pred_proc},
{"char=?", char_eq_proc},
{"char<?", char_less_proc},
{"char>?", char_greater_proc},
{"char<=?", char_less_eq_proc},
{"char>=?", char_greater_eq_proc},
{"char-ci=?", char_eq_ci_proc},
{"char-ci<?", char_less_ci_proc},
{"char-ci>?", char_greater_ci_proc},
{"char-ci<=?", char_less_eq_ci_proc},
{"char-ci>=?", char_greater_eq_ci_proc},
{"char-upper-case?", char_upper_case_proc},
{"char-lower-case?", char_lower_case_proc},
{"char-alphabetic?", char_alphabetic_proc},
{"char-numeric?", char_numeric_proc},
{"char-whitespace?", char_whitespace_proc},
{"char->integer", char_2_integer_proc},
{"integer->char", integer_2_char_proc},
{"char-upcase", char_upcase_proc},
{"char-downcase", char_downcase_proc},
//symbol procedure
{"symbol?", symbol_pred_proc},
{"symbol->string", symbol_2_string_proc},
{"string->symbol", string_2_symbol_proc},
//string procedure
{"string?", string_pred_proc},
{"make-string", make_string_proc},
{"string", string_proc},
{"string-length", string_length_proc},
{"string-ref", string_ref_proc},
{"string-set!", string_set_proc},
{"string=?", string_eq_proc},
{"string<?", string_less_proc},
{"string>?", string_greater_proc},
{"string<=?", string_less_eq_proc},
{"string>=?", string_greater_eq_proc},
{"string-ci=?", string_eq_ci_proc},
{"string-ci<?", string_less_ci_proc},
{"string-ci>?", string_greater_ci_proc},
{"string-ci<=?", string_less_eq_ci_proc},
{"string-ci>=?", string_greater_eq_ci_proc},
{"substring", substring_proc},
{"string-append", string_append_proc},
{"string->list", string_2_list_proc},
{"list->string", list_2_string_proc},
{"string-copy", string_copy_proc},
{"string-fill!", string_fill_proc},
//number procedure
{"integer?", integer_pred_proc},
{"number?", number_pred_proc},
{"=", number_eq_proc},
{"<", number_less_proc},
{">", number_greater_proc},
{"<=", number_less_eq_proc},
{">=", number_greater_eq_proc},
{"+", add_proc},
{"-", sub_proc},
{"*", mul_proc},
{"/", div_proc},
{"remainder", remainder_proc},
//vector procedure
{"vector?", vector_pred_proc},
{"make-vector", make_vector_proc},
{"vector", vector_proc},
{"vector-length", vector_length_proc},
{"vector-ref", vector_ref_proc},
{"vector-set!", vector_set_proc},
{"vector->list", vector_2_list_proc},
{"list->vector", list_2_vector_proc},
{"vector-fill!", vector_fill_proc},
//output procedures
{"write", write_proc},
{"display", display_proc},
{"newline", newline_proc},
{"write-char", write_char_proc},
//control procedures
{"procedure?", proc_pred_proc},
{"apply", apply_proc}
};
static cellpoint prim_proc_names(void)
{
int len = sizeof(pparray) / sizeof(struct prim_proc_pair);
int i;
reg = NIL;
for (i=len-1; i >= 0; --i){
reg = cons(make_symbol(pparray[i].name), reg);
}
return reg;
}
static cellpoint prim_proc_objs(void)
{
int len = sizeof(pparray) / sizeof(struct prim_proc_pair);
int i;
reg = NIL;
for (i = len-1; i >= 0; --i){
stack_push(&vars_stack, reg);
reg = make_prim_proc(make_symbol(pparray[i].name), pparray[i].func);
reg = cons(reg, stack_pop(&vars_stack));
}
return reg;
}
cellpoint setup_environment(void)
{
args_push(the_empty_env);
args_push(prim_proc_objs());
args_push(prim_proc_names());
reg = extend_env();
return reg;
}