20 |
Teddy Hogeborn <teddy@fukt.bth.se> |
Teddy Hogeborn <teddy@fukt.bth.se> |
21 |
*/ |
*/ |
22 |
|
|
23 |
|
#define CAR(X) (X->content.c->car) |
24 |
|
#define CDR(X) (X->content.c->cdr) |
25 |
|
|
26 |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
27 |
#include <stdio.h> |
#include <stdio.h> |
28 |
/* exit, EXIT_SUCCESS, malloc, free */ |
/* exit, EXIT_SUCCESS, malloc, free */ |
37 |
#include <unistd.h> |
#include <unistd.h> |
38 |
/* EX_NOINPUT, EX_USAGE */ |
/* EX_NOINPUT, EX_USAGE */ |
39 |
#include <sysexits.h> |
#include <sysexits.h> |
40 |
|
/* assert */ |
41 |
|
#include <assert.h> |
42 |
|
|
43 |
|
#ifdef __linux__ |
44 |
/* mtrace, muntrace */ |
/* mtrace, muntrace */ |
45 |
#include <mcheck.h> |
#include <mcheck.h> |
46 |
/* ioctl */ |
/* ioctl */ |
47 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
48 |
/* KDMKTONE */ |
/* KDMKTONE */ |
49 |
#include <linux/kd.h> |
#include <linux/kd.h> |
50 |
|
#endif /* __linux__ */ |
51 |
|
|
52 |
#include "stack.h" |
#include "stack.h" |
53 |
|
|
56 |
{ |
{ |
57 |
int i; |
int i; |
58 |
|
|
59 |
env->gc_limit= 20; |
env->gc_limit= 400000; |
60 |
env->gc_count= 0; |
env->gc_count= 0; |
61 |
env->gc_ref= NULL; |
env->gc_ref= NULL; |
|
env->gc_protect= NULL; |
|
62 |
|
|
63 |
env->head= NULL; |
env->head= new_val(env); |
64 |
|
env->head->type= empty; |
65 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
66 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
67 |
env->err= 0; |
env->err= 0; |
71 |
env->interactive= 1; |
env->interactive= 1; |
72 |
} |
} |
73 |
|
|
74 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) |
75 |
|
{ |
76 |
fprintf(stderr, "Err: %s\n", in_string); |
fprintf(stderr, "Err: %s\n", in_string); |
77 |
} |
} |
78 |
|
|
79 |
/* Discard the top element of the stack. */ |
/* Discard the top element of the stack. */ |
80 |
extern void toss(environment *env) |
extern void toss(environment *env) |
81 |
{ |
{ |
82 |
stackitem *temp= env->head; |
if(env->head->type==empty) { |
|
|
|
|
if((env->head)==NULL) { |
|
83 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
84 |
env->err= 1; |
env->err= 1; |
85 |
return; |
return; |
86 |
} |
} |
87 |
|
|
88 |
env->head= env->head->next; /* Remove the top stack item */ |
env->head= CDR(env->head); /* Remove the top stack item */ |
|
free(temp); /* Free the old top stack item */ |
|
|
|
|
|
gc_init(env); |
|
89 |
} |
} |
90 |
|
|
91 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
117 |
} |
} |
118 |
} |
} |
119 |
|
|
120 |
value* new_val(environment *env) { |
/* Create new value */ |
121 |
|
value* new_val(environment *env) |
122 |
|
{ |
123 |
value *nval= malloc(sizeof(value)); |
value *nval= malloc(sizeof(value)); |
124 |
stackitem *nitem= malloc(sizeof(stackitem)); |
stackitem *nitem= malloc(sizeof(stackitem)); |
125 |
|
|
126 |
nval->content.ptr= NULL; |
nval->content.ptr= NULL; |
127 |
|
nval->type= integer; |
128 |
|
|
129 |
nitem->item= nval; |
nitem->item= nval; |
130 |
nitem->next= env->gc_ref; |
nitem->next= env->gc_ref; |
|
env->gc_ref= nitem; |
|
131 |
|
|
132 |
env->gc_count++; |
env->gc_ref= nitem; |
133 |
|
|
134 |
protect(env, nval); |
env->gc_count += sizeof(value); |
135 |
gc_init(env); |
nval->gc.flag.mark= 0; |
136 |
unprotect(env); |
nval->gc.flag.protect= 0; |
137 |
|
|
138 |
return nval; |
return nval; |
139 |
} |
} |
140 |
|
|
141 |
void gc_mark(value *val) { |
/* Mark values recursively. |
142 |
stackitem *iterator; |
Marked values are not collected by the GC. */ |
143 |
|
inline void gc_mark(value *val) |
144 |
if(val==NULL || val->gc_garb==0) |
{ |
145 |
|
if(val==NULL || val->gc.flag.mark) |
146 |
return; |
return; |
147 |
|
|
148 |
val->gc_garb= 0; |
val->gc.flag.mark= 1; |
149 |
|
|
150 |
if(val->type==list) { |
if(val->type==tcons) { |
151 |
iterator= val->content.ptr; |
gc_mark(CAR(val)); |
152 |
|
gc_mark(CDR(val)); |
|
while(iterator!=NULL) { |
|
|
gc_mark(iterator->item); |
|
|
iterator= iterator->next; |
|
|
} |
|
153 |
} |
} |
154 |
} |
} |
155 |
|
|
156 |
extern void gc_init(environment *env) { |
inline void gc_maybe(environment *env) |
157 |
stackitem *new_head= NULL, *titem, *iterator; |
{ |
158 |
|
if(env->gc_count < env->gc_limit) |
159 |
|
return; |
160 |
|
else |
161 |
|
return gc_init(env); |
162 |
|
} |
163 |
|
|
164 |
|
/* Start GC */ |
165 |
|
extern void gc_init(environment *env) |
166 |
|
{ |
167 |
|
stackitem *new_head= NULL, *titem; |
168 |
|
cons *iterator; |
169 |
symbol *tsymb; |
symbol *tsymb; |
170 |
int i; |
int i; |
171 |
|
|
172 |
if(env->gc_count < env->gc_limit) |
if(env->interactive) |
173 |
return; |
printf("Garbage collecting."); |
174 |
|
|
175 |
/* Garb by default */ |
/* Mark values on stack */ |
176 |
iterator= env->gc_ref; |
gc_mark(env->head); |
|
while(iterator!=NULL) { |
|
|
iterator->item->gc_garb= 1; |
|
|
iterator= iterator->next; |
|
|
} |
|
177 |
|
|
178 |
/* Mark protected values */ |
if(env->interactive) |
179 |
iterator= env->gc_protect; |
printf("."); |
|
while(iterator!=NULL) { |
|
|
gc_mark(iterator->item); |
|
|
iterator= iterator->next; |
|
|
} |
|
180 |
|
|
|
/* Mark values in stack */ |
|
|
iterator= env->head; |
|
|
while(iterator!=NULL) { |
|
|
gc_mark(iterator->item); |
|
|
iterator= iterator->next; |
|
|
} |
|
181 |
|
|
182 |
/* Mark values in hashtable */ |
/* Mark values in hashtable */ |
183 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) |
184 |
tsymb= env->symbols[i]; |
for(tsymb= env->symbols[i]; tsymb!=NULL; tsymb= tsymb->next) |
185 |
while(tsymb!=NULL) { |
if (tsymb->val != NULL) |
186 |
gc_mark(tsymb->val); |
gc_mark(tsymb->val); |
187 |
tsymb= tsymb->next; |
|
188 |
} |
|
189 |
} |
if(env->interactive) |
190 |
|
printf("."); |
191 |
|
|
192 |
env->gc_count= 0; |
env->gc_count= 0; |
193 |
|
|
194 |
/* Sweep */ |
while(env->gc_ref!=NULL) { /* Sweep unused values */ |
|
while(env->gc_ref!=NULL) { |
|
195 |
|
|
196 |
if(env->gc_ref->item->gc_garb) { |
if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ |
197 |
|
|
198 |
/* Remove content */ |
if(env->gc_ref->item->type==string) /* Remove content */ |
|
switch(env->gc_ref->item->type) { |
|
|
case string: |
|
199 |
free(env->gc_ref->item->content.ptr); |
free(env->gc_ref->item->content.ptr); |
200 |
break; |
|
|
case list: |
|
|
while(env->gc_ref->item->content.ptr!=NULL) { |
|
|
titem= env->gc_ref->item->content.ptr; |
|
|
env->gc_ref->item->content.ptr= titem->next; |
|
|
free(titem); |
|
|
} |
|
|
break; |
|
|
default: |
|
|
break; |
|
|
} |
|
201 |
free(env->gc_ref->item); /* Remove from gc_ref */ |
free(env->gc_ref->item); /* Remove from gc_ref */ |
202 |
titem= env->gc_ref->next; |
titem= env->gc_ref->next; |
203 |
free(env->gc_ref); /* Remove value */ |
free(env->gc_ref); /* Remove value */ |
204 |
env->gc_ref= titem; |
env->gc_ref= titem; |
205 |
} else { /* Save */ |
continue; |
206 |
titem= env->gc_ref->next; |
} |
207 |
env->gc_ref->next= new_head; |
#ifdef DEBUG |
208 |
new_head= env->gc_ref; |
printf("Kept value (%p)", env->gc_ref->item); |
209 |
env->gc_ref= titem; |
if(env->gc_ref->item->gc.flag.mark) |
210 |
env->gc_count++; |
printf(" (marked)"); |
211 |
|
if(env->gc_ref->item->gc.flag.protect) |
212 |
|
printf(" (protected)"); |
213 |
|
switch(env->gc_ref->item->type){ |
214 |
|
case integer: |
215 |
|
printf(" integer: %d", env->gc_ref->item->content.i); |
216 |
|
break; |
217 |
|
case func: |
218 |
|
printf(" func: %p", env->gc_ref->item->content.ptr); |
219 |
|
break; |
220 |
|
case symb: |
221 |
|
printf(" symb: %s", env->gc_ref->item->content.sym->id); |
222 |
|
break; |
223 |
|
case tcons: |
224 |
|
printf(" tcons: %p\t%p", env->gc_ref->item->content.c->car, |
225 |
|
env->gc_ref->item->content.c->cdr); |
226 |
|
break; |
227 |
|
default: |
228 |
|
printf(" <unknown %d>", (env->gc_ref->item->type)); |
229 |
} |
} |
230 |
|
printf("\n"); |
231 |
|
#endif /* DEBUG */ |
232 |
|
|
233 |
|
/* Keep values */ |
234 |
|
env->gc_count += sizeof(value); |
235 |
|
if(env->gc_ref->item->type==string) |
236 |
|
env->gc_count += strlen(env->gc_ref->item->content.ptr); |
237 |
|
|
238 |
|
titem= env->gc_ref->next; |
239 |
|
env->gc_ref->next= new_head; |
240 |
|
new_head= env->gc_ref; |
241 |
|
new_head->item->gc.flag.mark= 0; |
242 |
|
env->gc_ref= titem; |
243 |
} |
} |
244 |
|
|
245 |
env->gc_limit= env->gc_count*2; |
if (env->gc_limit < env->gc_count*2) |
246 |
|
env->gc_limit= env->gc_count*2; |
247 |
|
|
248 |
env->gc_ref= new_head; |
env->gc_ref= new_head; |
249 |
|
|
250 |
|
if(env->interactive) |
251 |
|
printf("done (%d bytes still allocated)\n", env->gc_count); |
252 |
|
|
253 |
} |
} |
254 |
|
|
255 |
void protect(environment *env, value *val) |
/* Protect values from GC */ |
256 |
|
void protect(value *val) |
257 |
{ |
{ |
258 |
stackitem *new_item= malloc(sizeof(stackitem)); |
if(val==NULL || val->gc.flag.protect) |
259 |
new_item->item= val; |
return; |
260 |
new_item->next= env->gc_protect; |
|
261 |
env->gc_protect= new_item; |
val->gc.flag.protect= 1; |
262 |
|
|
263 |
|
if(val->type==tcons) { |
264 |
|
protect(CAR(val)); |
265 |
|
protect(CDR(val)); |
266 |
|
} |
267 |
} |
} |
268 |
|
|
269 |
void unprotect(environment *env) |
/* Unprotect values from GC */ |
270 |
|
void unprotect(value *val) |
271 |
{ |
{ |
272 |
stackitem *temp= env->gc_protect; |
if(val==NULL || !(val->gc.flag.protect)) |
273 |
env->gc_protect= env->gc_protect->next; |
return; |
274 |
free(temp); |
|
275 |
|
val->gc.flag.protect= 0; |
276 |
|
|
277 |
|
if(val->type==tcons) { |
278 |
|
unprotect(CAR(val)); |
279 |
|
unprotect(CDR(val)); |
280 |
|
} |
281 |
} |
} |
282 |
|
|
283 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
284 |
void push_val(environment *env, value *val) |
void push_val(environment *env, value *val) |
285 |
{ |
{ |
286 |
stackitem *new_item= malloc(sizeof(stackitem)); |
value *new_value= new_val(env); |
287 |
new_item->item= val; |
|
288 |
new_item->next= env->head; |
new_value->content.c= malloc(sizeof(cons)); |
289 |
env->head= new_item; |
assert(new_value->content.c!=NULL); |
290 |
|
new_value->type= tcons; |
291 |
|
CAR(new_value)= val; |
292 |
|
CDR(new_value)= env->head; |
293 |
|
env->head= new_value; |
294 |
} |
} |
295 |
|
|
296 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack */ |
297 |
void push_int(environment *env, int in_val) |
void push_int(environment *env, int in_val) |
298 |
{ |
{ |
299 |
value *new_value= new_val(env); |
value *new_value= new_val(env); |
304 |
push_val(env, new_value); |
push_val(env, new_value); |
305 |
} |
} |
306 |
|
|
307 |
|
/* Push a floating point number onto the stack */ |
308 |
void push_float(environment *env, float in_val) |
void push_float(environment *env, float in_val) |
309 |
{ |
{ |
310 |
value *new_value= new_val(env); |
value *new_value= new_val(env); |
319 |
void push_cstring(environment *env, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
320 |
{ |
{ |
321 |
value *new_value= new_val(env); |
value *new_value= new_val(env); |
322 |
|
int length= strlen(in_string)+1; |
323 |
|
|
324 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(length); |
325 |
|
env->gc_count += length; |
326 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
327 |
new_value->type= string; |
new_value->type= string; |
328 |
|
|
330 |
} |
} |
331 |
|
|
332 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
333 |
char *mangle_str(const char *old_string){ |
char *mangle_str(const char *old_string) |
334 |
|
{ |
335 |
char validchars[]= "0123456789abcdef"; |
char validchars[]= "0123456789abcdef"; |
336 |
char *new_string, *current; |
char *new_string, *current; |
337 |
|
|
349 |
return new_string; /* The caller must free() it */ |
return new_string; /* The caller must free() it */ |
350 |
} |
} |
351 |
|
|
352 |
extern void mangle(environment *env){ |
extern void mangle(environment *env) |
353 |
|
{ |
354 |
char *new_string; |
char *new_string; |
355 |
|
|
356 |
if((env->head)==NULL) { |
if(env->head->type==empty) { |
357 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
358 |
env->err= 1; |
env->err= 1; |
359 |
return; |
return; |
360 |
} |
} |
361 |
|
|
362 |
if(env->head->item->type!=string) { |
if(CAR(env->head)->type!=string) { |
363 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
364 |
env->err= 2; |
env->err= 2; |
365 |
return; |
return; |
366 |
} |
} |
367 |
|
|
368 |
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
new_string= |
369 |
|
mangle_str((const char *)(CAR(env->head)->content.ptr)); |
370 |
|
|
371 |
toss(env); |
toss(env); |
372 |
if(env->err) return; |
if(env->err) return; |
390 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
391 |
|
|
392 |
new_value= new_val(env); |
new_value= new_val(env); |
393 |
protect(env, new_value); |
protect(new_value); |
394 |
new_fvalue= new_val(env); |
new_fvalue= new_val(env); |
395 |
protect(env, new_fvalue); |
protect(new_fvalue); |
396 |
|
|
397 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
398 |
new_value->type= symb; |
new_value->type= symb; |
420 |
|
|
421 |
mangled= mangle_str(in_string); /* mangle the name */ |
mangled= mangle_str(in_string); /* mangle the name */ |
422 |
funcptr= dlsym(handle, mangled); /* and try to find it */ |
funcptr= dlsym(handle, mangled); /* and try to find it */ |
423 |
free(mangled); |
|
424 |
dlerr= dlerror(); |
dlerr= dlerror(); |
425 |
if(dlerr != NULL) { /* If no function was found */ |
if(dlerr != NULL) { /* If no function was found */ |
426 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
427 |
dlerr= dlerror(); |
dlerr= dlerror(); |
428 |
} |
} |
429 |
|
|
430 |
if(dlerr==NULL) { /* If a function was found */ |
if(dlerr==NULL) { /* If a function was found */ |
431 |
new_fvalue->type= func; /* The new value is a function pointer */ |
new_fvalue->type= func; /* The new value is a function pointer */ |
432 |
new_fvalue->content.ptr= funcptr; /* Store function pointer */ |
new_fvalue->content.ptr= funcptr; /* Store function pointer */ |
433 |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
434 |
function value */ |
function value */ |
435 |
} |
} |
436 |
|
|
437 |
|
free(mangled); |
438 |
} |
} |
439 |
|
|
440 |
push_val(env, new_value); |
push_val(env, new_value); |
441 |
unprotect(env); unprotect(env); |
unprotect(new_value); unprotect(new_fvalue); |
442 |
} |
} |
443 |
|
|
444 |
/* Print newline. */ |
/* Print newline. */ |
448 |
} |
} |
449 |
|
|
450 |
/* Gets the type of a value */ |
/* Gets the type of a value */ |
451 |
extern void type(environment *env){ |
extern void type(environment *env) |
452 |
int typenum; |
{ |
453 |
|
if(env->head->type==empty) { |
|
if((env->head)==NULL) { |
|
454 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
455 |
env->err=1; |
env->err= 1; |
456 |
return; |
return; |
457 |
} |
} |
458 |
typenum=env->head->item->type; |
|
459 |
toss(env); |
switch(CAR(env->head)->type){ |
460 |
switch(typenum){ |
case empty: |
461 |
|
push_sym(env, "empty"); |
462 |
|
break; |
463 |
case integer: |
case integer: |
464 |
push_sym(env, "integer"); |
push_sym(env, "integer"); |
465 |
break; |
break; |
475 |
case func: |
case func: |
476 |
push_sym(env, "function"); |
push_sym(env, "function"); |
477 |
break; |
break; |
478 |
case list: |
case tcons: |
479 |
push_sym(env, "list"); |
push_sym(env, "pair"); |
480 |
break; |
break; |
481 |
} |
} |
482 |
|
swap(env); |
483 |
|
if (env->err) return; |
484 |
|
toss(env); |
485 |
} |
} |
486 |
|
|
487 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
488 |
void print_h(stackitem *stack_head, int noquote) |
void print_val(value *val, int noquote) |
489 |
{ |
{ |
490 |
switch(stack_head->item->type) { |
switch(val->type) { |
491 |
|
case empty: |
492 |
|
printf("[]"); |
493 |
|
break; |
494 |
case integer: |
case integer: |
495 |
printf("%d", stack_head->item->content.i); |
printf("%d", val->content.i); |
496 |
break; |
break; |
497 |
case tfloat: |
case tfloat: |
498 |
printf("%f", stack_head->item->content.f); |
printf("%f", val->content.f); |
499 |
break; |
break; |
500 |
case string: |
case string: |
501 |
if(noquote) |
if(noquote) |
502 |
printf("%s", (char*)stack_head->item->content.ptr); |
printf("%s", (char*)(val->content.ptr)); |
503 |
else |
else |
504 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
printf("\"%s\"", (char*)(val->content.ptr)); |
505 |
break; |
break; |
506 |
case symb: |
case symb: |
507 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", val->content.sym->id); |
508 |
break; |
break; |
509 |
case func: |
case func: |
510 |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
printf("#<function %p>", (funcp)(val->content.ptr)); |
511 |
break; |
break; |
512 |
case list: |
case tcons: |
|
/* A list is just a stack, so make stack_head point to it */ |
|
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
|
513 |
printf("[ "); |
printf("[ "); |
514 |
while(stack_head != NULL) { |
do { |
515 |
print_h(stack_head, noquote); |
print_val(CAR(val), noquote); |
516 |
printf(" "); |
switch(CDR(val)->type){ |
517 |
stack_head=stack_head->next; |
case empty: |
518 |
} |
break; |
519 |
printf("]"); |
case tcons: |
520 |
|
printf(" "); |
521 |
|
break; |
522 |
|
default: |
523 |
|
printf(" . "); /* Improper list */ |
524 |
|
print_val(CDR(val), noquote); |
525 |
|
} |
526 |
|
val= CDR(val); |
527 |
|
} while(val->type == tcons); |
528 |
|
printf(" ]"); |
529 |
break; |
break; |
530 |
} |
} |
531 |
} |
} |
532 |
|
|
533 |
extern void print_(environment *env) { |
extern void print_(environment *env) |
534 |
if(env->head==NULL) { |
{ |
535 |
|
if(env->head->type==empty) { |
536 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
537 |
env->err=1; |
env->err= 1; |
538 |
return; |
return; |
539 |
} |
} |
540 |
print_h(env->head, 0); |
print_val(CAR(env->head), 0); |
541 |
nl(); |
nl(); |
542 |
} |
} |
543 |
|
|
549 |
toss(env); |
toss(env); |
550 |
} |
} |
551 |
|
|
552 |
extern void princ_(environment *env) { |
extern void princ_(environment *env) |
553 |
if(env->head==NULL) { |
{ |
554 |
|
if(env->head->type==empty) { |
555 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
556 |
env->err=1; |
env->err= 1; |
557 |
return; |
return; |
558 |
} |
} |
559 |
print_h(env->head, 1); |
print_val(CAR(env->head), 1); |
560 |
} |
} |
561 |
|
|
562 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
568 |
} |
} |
569 |
|
|
570 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
571 |
void print_st(stackitem *stack_head, long counter) |
void print_st(value *stack_head, long counter) |
572 |
{ |
{ |
573 |
if(stack_head->next != NULL) |
if(CDR(stack_head)->type != empty) |
574 |
print_st(stack_head->next, counter+1); |
print_st(CDR(stack_head), counter+1); |
575 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
576 |
print_h(stack_head, 0); |
print_val(CAR(stack_head), 0); |
577 |
nl(); |
nl(); |
578 |
} |
} |
579 |
|
|
580 |
/* Prints the stack. */ |
/* Prints the stack. */ |
581 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
582 |
{ |
{ |
583 |
if(env->head == NULL) { |
if(env->head->type == empty) { |
584 |
printf("Stack Empty\n"); |
printf("Stack Empty\n"); |
585 |
return; |
return; |
586 |
} |
} |
587 |
|
|
588 |
print_st(env->head, 1); |
print_st(env->head, 1); |
589 |
} |
} |
590 |
|
|
591 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
592 |
extern void swap(environment *env) |
extern void swap(environment *env) |
593 |
{ |
{ |
594 |
stackitem *temp= env->head; |
value *temp= env->head; |
595 |
|
|
596 |
if(env->head==NULL || env->head->next==NULL) { |
if(env->head->type == empty || CDR(env->head)->type == empty) { |
597 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
598 |
env->err=1; |
env->err=1; |
599 |
return; |
return; |
600 |
} |
} |
601 |
|
|
602 |
env->head= env->head->next; |
env->head= CDR(env->head); |
603 |
temp->next= env->head->next; |
CDR(temp)= CDR(env->head); |
604 |
env->head->next= temp; |
CDR(env->head)= temp; |
605 |
} |
} |
606 |
|
|
607 |
/* Rotate the first three elements on the stack. */ |
/* Rotate the first three elements on the stack. */ |
608 |
extern void rot(environment *env) |
extern void rot(environment *env) |
609 |
{ |
{ |
610 |
stackitem *temp= env->head; |
value *temp= env->head; |
611 |
|
|
612 |
if(env->head==NULL || env->head->next==NULL |
if(env->head->type == empty || CDR(env->head)->type == empty |
613 |
|| env->head->next->next==NULL) { |
|| CDR(CDR(env->head))->type == empty) { |
614 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
615 |
env->err=1; |
env->err= 1; |
616 |
return; |
return; |
617 |
} |
} |
618 |
|
|
619 |
env->head= env->head->next->next; |
env->head= CDR(CDR(env->head)); |
620 |
temp->next->next= env->head->next; |
CDR(CDR(temp))= CDR(env->head); |
621 |
env->head->next= temp; |
CDR(env->head)= temp; |
622 |
} |
} |
623 |
|
|
624 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
626 |
{ |
{ |
627 |
value *val; |
value *val; |
628 |
|
|
629 |
if(env->head == NULL) { |
if(env->head->type==empty) { |
630 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
631 |
env->err=1; |
env->err= 1; |
632 |
return; |
return; |
633 |
} |
} |
634 |
|
|
635 |
if(env->head->item->type!=symb) { |
if(CAR(env->head)->type!=symb) { |
636 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
637 |
env->err=2; |
env->err= 2; |
638 |
return; |
return; |
639 |
} |
} |
640 |
|
|
641 |
val=((symbol *)(env->head->item->content.ptr))->val; |
val= CAR(env->head)->content.sym->val; |
642 |
if(val == NULL){ |
if(val == NULL){ |
643 |
printerr("Unbound Variable"); |
printerr("Unbound Variable"); |
644 |
env->err=3; |
env->err= 3; |
645 |
return; |
return; |
646 |
} |
} |
647 |
protect(env, val); |
push_val(env, val); /* Return the symbol's bound value */ |
648 |
toss(env); /* toss the symbol */ |
swap(env); |
649 |
|
if(env->err) return; |
650 |
|
toss(env); /* toss the symbol */ |
651 |
if(env->err) return; |
if(env->err) return; |
|
push_val(env, val); /* Return its bound value */ |
|
|
unprotect(env); |
|
652 |
} |
} |
653 |
|
|
654 |
/* If the top element is a symbol, determine if it's bound to a |
/* If the top element is a symbol, determine if it's bound to a |
658 |
{ |
{ |
659 |
funcp in_func; |
funcp in_func; |
660 |
value* temp_val; |
value* temp_val; |
661 |
stackitem* iterator; |
value* iterator; |
662 |
|
|
663 |
eval_start: |
eval_start: |
664 |
|
|
665 |
if(env->head==NULL) { |
gc_maybe(env); |
666 |
|
|
667 |
|
if(env->head->type==empty) { |
668 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
669 |
env->err=1; |
env->err= 1; |
670 |
return; |
return; |
671 |
} |
} |
672 |
|
|
673 |
switch(env->head->item->type) { |
switch(CAR(env->head)->type) { |
674 |
/* if it's a symbol */ |
/* if it's a symbol */ |
675 |
case symb: |
case symb: |
676 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
677 |
if(env->err) return; |
if(env->err) return; |
678 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(CAR(env->head)->type!=symb){ /* don't recurse symbols */ |
679 |
goto eval_start; |
goto eval_start; |
680 |
} |
} |
681 |
return; |
return; |
682 |
|
|
683 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
684 |
case func: |
case func: |
685 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(CAR(env->head)->content.ptr); |
686 |
toss(env); |
toss(env); |
687 |
if(env->err) return; |
if(env->err) return; |
688 |
return in_func(env); |
return in_func(env); |
689 |
|
|
690 |
/* If it's a list */ |
/* If it's a list */ |
691 |
case list: |
case tcons: |
692 |
temp_val= env->head->item; |
temp_val= CAR(env->head); |
693 |
protect(env, temp_val); |
protect(temp_val); |
694 |
|
|
695 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
696 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= temp_val; |
697 |
|
|
698 |
while(iterator!=NULL) { |
while(iterator->type != empty) { |
699 |
push_val(env, iterator->item); |
push_val(env, CAR(iterator)); |
700 |
|
|
701 |
if(env->head->item->type==symb |
if(CAR(env->head)->type==symb |
702 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& CAR(env->head)->content.sym->id[0]==';') { |
703 |
toss(env); |
toss(env); |
704 |
if(env->err) return; |
if(env->err) return; |
705 |
|
|
706 |
if(iterator->next == NULL){ |
if(CDR(iterator)->type == empty){ |
707 |
goto eval_start; |
goto eval_start; |
708 |
} |
} |
709 |
eval(env); |
eval(env); |
710 |
if(env->err) return; |
if(env->err) return; |
711 |
} |
} |
712 |
iterator= iterator->next; |
if (CDR(iterator)->type == empty || CDR(iterator)->type == tcons) |
713 |
|
iterator= CDR(iterator); |
714 |
|
else { |
715 |
|
printerr("Bad Argument Type"); /* Improper list */ |
716 |
|
env->err= 2; |
717 |
|
return; |
718 |
|
} |
719 |
} |
} |
720 |
unprotect(env); |
unprotect(temp_val); |
721 |
return; |
return; |
722 |
|
|
723 |
default: |
default: |
726 |
} |
} |
727 |
|
|
728 |
/* Reverse (flip) a list */ |
/* Reverse (flip) a list */ |
729 |
extern void rev(environment *env){ |
extern void rev(environment *env) |
730 |
stackitem *old_head, *new_head, *item; |
{ |
731 |
|
value *old_head, *new_head, *item; |
732 |
|
|
733 |
if((env->head)==NULL) { |
if(env->head->type==empty) { |
734 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
735 |
env->err= 1; |
env->err= 1; |
736 |
return; |
return; |
737 |
} |
} |
738 |
|
|
739 |
if(env->head->item->type!=list) { |
if(CAR(env->head)->type==empty) |
740 |
|
return; /* Don't reverse an empty list */ |
741 |
|
|
742 |
|
if(CAR(env->head)->type!=tcons) { |
743 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
744 |
env->err= 2; |
env->err= 2; |
745 |
return; |
return; |
746 |
} |
} |
747 |
|
|
748 |
old_head= (stackitem *)(env->head->item->content.ptr); |
old_head= CAR(env->head); |
749 |
new_head= NULL; |
new_head= new_val(env); |
750 |
while(old_head != NULL){ |
new_head->type= empty; |
751 |
|
while(old_head->type != empty) { |
752 |
item= old_head; |
item= old_head; |
753 |
old_head= old_head->next; |
old_head= CDR(old_head); |
754 |
item->next= new_head; |
CDR(item)= new_head; |
755 |
new_head= item; |
new_head= item; |
756 |
} |
} |
757 |
env->head->item->content.ptr= new_head; |
CAR(env->head)= new_head; |
758 |
} |
} |
759 |
|
|
760 |
/* Make a list. */ |
/* Make a list. */ |
761 |
extern void pack(environment *env) |
extern void pack(environment *env) |
762 |
{ |
{ |
763 |
stackitem *iterator, *temp; |
value *iterator, *temp, *ending; |
|
value *pack; |
|
764 |
|
|
765 |
iterator= env->head; |
ending=new_val(env); |
766 |
pack= new_val(env); |
ending->type=empty; |
|
protect(env, pack); |
|
767 |
|
|
768 |
if(iterator==NULL |
iterator= env->head; |
769 |
|| (iterator->item->type==symb |
if(iterator->type == empty |
770 |
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
|| (CAR(iterator)->type==symb |
771 |
temp= NULL; |
&& CAR(iterator)->content.sym->id[0]=='[')) { |
772 |
|
temp= ending; |
773 |
toss(env); |
toss(env); |
774 |
} else { |
} else { |
775 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
776 |
while(iterator->next!=NULL |
while(CDR(iterator)->type != empty |
777 |
&& (iterator->next->item->type!=symb |
&& (CAR(CDR(iterator))->type!=symb |
778 |
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
|| CAR(CDR(iterator))->content.sym->id[0]!='[')) |
779 |
iterator= iterator->next; |
iterator= CDR(iterator); |
780 |
|
|
781 |
/* Extract list */ |
/* Extract list */ |
782 |
temp= env->head; |
temp= env->head; |
783 |
env->head= iterator->next; |
env->head= CDR(iterator); |
784 |
iterator->next= NULL; |
CDR(iterator)= ending; |
785 |
|
|
786 |
pack->type= list; |
if(env->head->type != empty) |
|
pack->content.ptr= temp; |
|
|
|
|
|
if(env->head!=NULL) |
|
787 |
toss(env); |
toss(env); |
788 |
} |
} |
789 |
|
|
790 |
/* Push list */ |
/* Push list */ |
791 |
|
|
792 |
push_val(env, pack); |
push_val(env, temp); |
793 |
rev(env); |
rev(env); |
|
|
|
|
unprotect(env); |
|
794 |
} |
} |
795 |
|
|
796 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
797 |
extern void expand(environment *env) |
extern void expand(environment *env) |
798 |
{ |
{ |
799 |
stackitem *temp, *new_head; |
value *temp, *new_head; |
800 |
|
|
801 |
/* Is top element a list? */ |
/* Is top element a list? */ |
802 |
if(env->head==NULL) { |
if(env->head->type==empty) { |
803 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
804 |
env->err= 1; |
env->err= 1; |
805 |
return; |
return; |
806 |
} |
} |
807 |
if(env->head->item->type!=list) { |
|
808 |
|
if(CAR(env->head)->type!=tcons) { |
809 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
810 |
env->err= 2; |
env->err= 2; |
811 |
return; |
return; |
817 |
return; |
return; |
818 |
|
|
819 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
820 |
new_head= temp= env->head->item->content.ptr; |
new_head= temp= CAR(env->head); |
821 |
|
|
822 |
toss(env); |
toss(env); |
823 |
|
|
824 |
/* Find the end of the list */ |
/* Find the end of the list */ |
825 |
while(temp->next!=NULL) |
while(CDR(temp)->type != empty) { |
826 |
temp= temp->next; |
if (CDR(temp)->type == tcons) |
827 |
|
temp= CDR(temp); |
828 |
|
else { |
829 |
|
printerr("Bad Argument Type"); /* Improper list */ |
830 |
|
env->err= 2; |
831 |
|
return; |
832 |
|
} |
833 |
|
} |
834 |
|
|
835 |
/* Connect the tail of the list with the old stack head */ |
/* Connect the tail of the list with the old stack head */ |
836 |
temp->next= env->head; |
CDR(temp)= env->head; |
837 |
env->head= new_head; /* ...and voila! */ |
env->head= new_head; /* ...and voila! */ |
838 |
|
|
839 |
} |
} |
842 |
extern void eq(environment *env) |
extern void eq(environment *env) |
843 |
{ |
{ |
844 |
void *left, *right; |
void *left, *right; |
|
int result; |
|
845 |
|
|
846 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
847 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
848 |
env->err= 1; |
env->err= 1; |
849 |
return; |
return; |
850 |
} |
} |
851 |
|
|
852 |
left= env->head->item->content.ptr; |
left= CAR(env->head)->content.ptr; |
853 |
swap(env); |
right= CAR(CDR(env->head))->content.ptr; |
|
right= env->head->item->content.ptr; |
|
|
result= (left==right); |
|
|
|
|
854 |
toss(env); toss(env); |
toss(env); toss(env); |
855 |
push_int(env, result); |
|
856 |
|
push_int(env, left==right); |
857 |
} |
} |
858 |
|
|
859 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
861 |
{ |
{ |
862 |
int val; |
int val; |
863 |
|
|
864 |
if((env->head)==NULL) { |
if(env->head->type==empty) { |
865 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
866 |
env->err= 1; |
env->err= 1; |
867 |
return; |
return; |
868 |
} |
} |
869 |
|
|
870 |
if(env->head->item->type!=integer) { |
if(CAR(env->head)->type!=integer) { |
871 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
872 |
env->err= 2; |
env->err= 2; |
873 |
return; |
return; |
874 |
} |
} |
875 |
|
|
876 |
val= env->head->item->content.i; |
val= CAR(env->head)->content.i; |
877 |
toss(env); |
toss(env); |
878 |
push_int(env, !val); |
push_int(env, !val); |
879 |
} |
} |
892 |
symbol *sym; |
symbol *sym; |
893 |
|
|
894 |
/* Needs two values on the stack, the top one must be a symbol */ |
/* Needs two values on the stack, the top one must be a symbol */ |
895 |
if(env->head==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
896 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
897 |
env->err= 1; |
env->err= 1; |
898 |
return; |
return; |
899 |
} |
} |
900 |
|
|
901 |
if(env->head->item->type!=symb) { |
if(CAR(env->head)->type!=symb) { |
902 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
903 |
env->err= 2; |
env->err= 2; |
904 |
return; |
return; |
905 |
} |
} |
906 |
|
|
907 |
/* long names are a pain */ |
/* long names are a pain */ |
908 |
sym= env->head->item->content.ptr; |
sym= CAR(env->head)->content.ptr; |
909 |
|
|
910 |
/* Bind the symbol to the value */ |
/* Bind the symbol to the value */ |
911 |
sym->val= env->head->next->item; |
sym->val= CAR(CDR(env->head)); |
912 |
|
|
913 |
toss(env); toss(env); |
toss(env); toss(env); |
914 |
} |
} |
916 |
/* Quit stack. */ |
/* Quit stack. */ |
917 |
extern void quit(environment *env) |
extern void quit(environment *env) |
918 |
{ |
{ |
919 |
long i; |
int i; |
920 |
|
|
921 |
clear(env); |
clear(env); |
922 |
|
|
929 |
} |
} |
930 |
|
|
931 |
env->gc_limit= 0; |
env->gc_limit= 0; |
932 |
gc_init(env); |
gc_maybe(env); |
933 |
|
|
934 |
|
words(env); |
935 |
|
|
936 |
if(env->free_string!=NULL) |
if(env->free_string!=NULL) |
937 |
free(env->free_string); |
free(env->free_string); |
938 |
|
|
939 |
|
#ifdef __linux__ |
940 |
muntrace(); |
muntrace(); |
941 |
|
#endif |
942 |
|
|
943 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
944 |
} |
} |
946 |
/* Clear stack */ |
/* Clear stack */ |
947 |
extern void clear(environment *env) |
extern void clear(environment *env) |
948 |
{ |
{ |
949 |
while(env->head!=NULL) |
while(env->head->type != empty) |
950 |
toss(env); |
toss(env); |
951 |
} |
} |
952 |
|
|
959 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
960 |
temp= env->symbols[i]; |
temp= env->symbols[i]; |
961 |
while(temp!=NULL) { |
while(temp!=NULL) { |
962 |
|
#ifdef DEBUG |
963 |
|
if (temp->val != NULL && temp->val->gc.flag.protect) |
964 |
|
printf("(protected) "); |
965 |
|
#endif /* DEBUG */ |
966 |
printf("%s\n", temp->id); |
printf("%s\n", temp->id); |
967 |
temp= temp->next; |
temp= temp->next; |
968 |
} |
} |
970 |
} |
} |
971 |
|
|
972 |
/* Internal forget function */ |
/* Internal forget function */ |
973 |
void forget_sym(symbol **hash_entry) { |
void forget_sym(symbol **hash_entry) |
974 |
|
{ |
975 |
symbol *temp; |
symbol *temp; |
976 |
|
|
977 |
temp= *hash_entry; |
temp= *hash_entry; |
985 |
extern void forget(environment *env) |
extern void forget(environment *env) |
986 |
{ |
{ |
987 |
char* sym_id; |
char* sym_id; |
|
stackitem *stack_head= env->head; |
|
988 |
|
|
989 |
if(stack_head==NULL) { |
if(env->head->type==empty) { |
990 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
991 |
env->err=1; |
env->err= 1; |
992 |
return; |
return; |
993 |
} |
} |
994 |
|
|
995 |
if(stack_head->item->type!=symb) { |
if(CAR(env->head)->type!=symb) { |
996 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
997 |
env->err=2; |
env->err= 2; |
998 |
return; |
return; |
999 |
} |
} |
1000 |
|
|
1001 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= CAR(env->head)->content.sym->id; |
1002 |
toss(env); |
toss(env); |
1003 |
|
|
1004 |
return forget_sym(hash(env->symbols, sym_id)); |
return forget_sym(hash(env->symbols, sym_id)); |
1005 |
} |
} |
1006 |
|
|
1007 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
1008 |
extern void errn(environment *env){ |
extern void errn(environment *env) |
1009 |
|
{ |
1010 |
push_int(env, env->err); |
push_int(env, env->err); |
1011 |
} |
} |
1012 |
|
|
1016 |
|
|
1017 |
int c; /* getopt option character */ |
int c; /* getopt option character */ |
1018 |
|
|
1019 |
|
#ifdef __linux__ |
1020 |
mtrace(); |
mtrace(); |
1021 |
|
#endif |
1022 |
|
|
1023 |
init_env(&myenv); |
init_env(&myenv); |
1024 |
|
|
1032 |
break; |
break; |
1033 |
case '?': |
case '?': |
1034 |
fprintf (stderr, |
fprintf (stderr, |
1035 |
"Unknown option character `\\x%x'.\n", |
"Unknown option character '\\x%x'.\n", |
1036 |
optopt); |
optopt); |
1037 |
return EX_USAGE; |
return EX_USAGE; |
1038 |
default: |
default: |
1051 |
if(myenv.interactive) { |
if(myenv.interactive) { |
1052 |
printf("Stack version $Revision$\n\ |
printf("Stack version $Revision$\n\ |
1053 |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ |
1054 |
Stack comes with ABSOLUTELY NO WARRANTY; for details type `warranty;'.\n\ |
Stack comes with ABSOLUTELY NO WARRANTY; for details type 'warranty;'.\n\ |
1055 |
This is free software, and you are welcome to redistribute it\n\ |
This is free software, and you are welcome to redistribute it\n\ |
1056 |
under certain conditions; type `copying;' for details.\n"); |
under certain conditions; type 'copying;' for details.\n"); |
1057 |
} |
} |
1058 |
|
|
1059 |
while(1) { |
while(1) { |
1068 |
} |
} |
1069 |
myenv.err=0; |
myenv.err=0; |
1070 |
} |
} |
1071 |
sx_72656164(&myenv); |
sx_72656164(&myenv); /* "read" */ |
1072 |
if (myenv.err==4) { |
if (myenv.err==4) { /* EOF */ |
1073 |
return EXIT_SUCCESS; /* EOF */ |
myenv.err=0; |
1074 |
} else if(myenv.head!=NULL |
quit(&myenv); |
1075 |
&& myenv.head->item->type==symb |
} else if(myenv.head->type!=empty |
1076 |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
&& CAR(myenv.head)->type==symb |
1077 |
|
&& CAR(myenv.head)->content.sym->id[0] |
1078 |
|
==';') { |
1079 |
toss(&myenv); /* No error check in main */ |
toss(&myenv); /* No error check in main */ |
1080 |
eval(&myenv); |
eval(&myenv); |
1081 |
} |
} |
1082 |
gc_init(&myenv); |
gc_maybe(&myenv); |
1083 |
} |
} |
1084 |
quit(&myenv); |
quit(&myenv); |
1085 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
1086 |
} |
} |
1087 |
|
|
1088 |
/* "+" */ |
/* "+" */ |
1089 |
extern void sx_2b(environment *env) { |
extern void sx_2b(environment *env) |
1090 |
|
{ |
1091 |
int a, b; |
int a, b; |
1092 |
float fa, fb; |
float fa, fb; |
1093 |
size_t len; |
size_t len; |
1094 |
char* new_string; |
char* new_string; |
1095 |
value *a_val, *b_val; |
value *a_val, *b_val; |
1096 |
|
|
1097 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1098 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1099 |
env->err= 1; |
env->err= 1; |
1100 |
return; |
return; |
1101 |
} |
} |
1102 |
|
|
1103 |
if(env->head->item->type==string |
if(CAR(env->head)->type==string |
1104 |
&& env->head->next->item->type==string) { |
&& CAR(CDR(env->head))->type==string) { |
1105 |
a_val= env->head->item; |
a_val= CAR(env->head); |
1106 |
b_val= env->head->next->item; |
b_val= CAR(CDR(env->head)); |
1107 |
protect(env, a_val); protect(env, b_val); |
protect(a_val); protect(b_val); |
1108 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1109 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1110 |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
1112 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
1113 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
1114 |
push_cstring(env, new_string); |
push_cstring(env, new_string); |
1115 |
unprotect(env); unprotect(env); |
unprotect(a_val); unprotect(b_val); |
1116 |
free(new_string); |
free(new_string); |
1117 |
|
|
1118 |
return; |
return; |
1119 |
} |
} |
1120 |
|
|
1121 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
1122 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
1123 |
a=env->head->item->content.i; |
a= CAR(env->head)->content.i; |
1124 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1125 |
b=env->head->item->content.i; |
b= CAR(env->head)->content.i; |
1126 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1127 |
push_int(env, b+a); |
push_int(env, b+a); |
1128 |
|
|
1129 |
return; |
return; |
1130 |
} |
} |
1131 |
|
|
1132 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
1133 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
1134 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
1135 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1136 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
1137 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1138 |
push_float(env, fb+fa); |
push_float(env, fb+fa); |
1139 |
|
|
1140 |
return; |
return; |
1141 |
} |
} |
1142 |
|
|
1143 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
1144 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
1145 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
1146 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1147 |
b= env->head->item->content.i; |
b= CAR(env->head)->content.i; |
1148 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1149 |
push_float(env, b+fa); |
push_float(env, b+fa); |
1150 |
|
|
1151 |
return; |
return; |
1152 |
} |
} |
1153 |
|
|
1154 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
1155 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
1156 |
a= env->head->item->content.i; |
a= CAR(env->head)->content.i; |
1157 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1158 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
1159 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1160 |
push_float(env, fb+a); |
push_float(env, fb+a); |
1161 |
|
|
1167 |
} |
} |
1168 |
|
|
1169 |
/* "-" */ |
/* "-" */ |
1170 |
extern void sx_2d(environment *env) { |
extern void sx_2d(environment *env) |
1171 |
|
{ |
1172 |
int a, b; |
int a, b; |
1173 |
float fa, fb; |
float fa, fb; |
1174 |
|
|
1175 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1176 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1177 |
env->err=1; |
env->err=1; |
1178 |
return; |
return; |
1179 |
} |
} |
1180 |
|
|
1181 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
1182 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
1183 |
a=env->head->item->content.i; |
a= CAR(env->head)->content.i; |
1184 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1185 |
b=env->head->item->content.i; |
b= CAR(env->head)->content.i; |
1186 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1187 |
push_int(env, b-a); |
push_int(env, b-a); |
1188 |
|
|
1189 |
return; |
return; |
1190 |
} |
} |
1191 |
|
|
1192 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
1193 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
1194 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
1195 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1196 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
1197 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1198 |
push_float(env, fb-fa); |
push_float(env, fb-fa); |
1199 |
|
|
1200 |
return; |
return; |
1201 |
} |
} |
1202 |
|
|
1203 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
1204 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
1205 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
1206 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1207 |
b= env->head->item->content.i; |
b= CAR(env->head)->content.i; |
1208 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1209 |
push_float(env, b-fa); |
push_float(env, b-fa); |
1210 |
|
|
1211 |
return; |
return; |
1212 |
} |
} |
1213 |
|
|
1214 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
1215 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
1216 |
a= env->head->item->content.i; |
a= CAR(env->head)->content.i; |
1217 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1218 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
1219 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1220 |
push_float(env, fb-a); |
push_float(env, fb-a); |
1221 |
|
|
1227 |
} |
} |
1228 |
|
|
1229 |
/* ">" */ |
/* ">" */ |
1230 |
extern void sx_3e(environment *env) { |
extern void sx_3e(environment *env) |
1231 |
|
{ |
1232 |
int a, b; |
int a, b; |
1233 |
float fa, fb; |
float fa, fb; |
1234 |
|
|
1235 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1236 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1237 |
env->err=1; |
env->err= 1; |
1238 |
return; |
return; |
1239 |
} |
} |
1240 |
|
|
1241 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
1242 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
1243 |
a=env->head->item->content.i; |
a= CAR(env->head)->content.i; |
1244 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1245 |
b=env->head->item->content.i; |
b= CAR(env->head)->content.i; |
1246 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1247 |
push_int(env, b>a); |
push_int(env, b>a); |
1248 |
|
|
1249 |
return; |
return; |
1250 |
} |
} |
1251 |
|
|
1252 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
1253 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
1254 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
1255 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1256 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
1257 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1258 |
push_int(env, fb>fa); |
push_int(env, fb>fa); |
1259 |
|
|
1260 |
return; |
return; |
1261 |
} |
} |
1262 |
|
|
1263 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
1264 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
1265 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
1266 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1267 |
b= env->head->item->content.i; |
b= CAR(env->head)->content.i; |
1268 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1269 |
push_int(env, b>fa); |
push_int(env, b>fa); |
1270 |
|
|
1271 |
return; |
return; |
1272 |
} |
} |
1273 |
|
|
1274 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
1275 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
1276 |
a= env->head->item->content.i; |
a= CAR(env->head)->content.i; |
1277 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1278 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
1279 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1280 |
push_int(env, fb>a); |
push_int(env, fb>a); |
1281 |
|
|
1283 |
} |
} |
1284 |
|
|
1285 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1286 |
env->err=2; |
env->err= 2; |
1287 |
} |
} |
1288 |
|
|
1289 |
/* "<" */ |
/* "<" */ |
1290 |
extern void sx_3c(environment *env) { |
extern void sx_3c(environment *env) |
1291 |
|
{ |
1292 |
swap(env); if(env->err) return; |
swap(env); if(env->err) return; |
1293 |
sx_3e(env); |
sx_3e(env); |
1294 |
} |
} |
1295 |
|
|
1296 |
/* "<=" */ |
/* "<=" */ |
1297 |
extern void sx_3c3d(environment *env) { |
extern void sx_3c3d(environment *env) |
1298 |
|
{ |
1299 |
sx_3e(env); if(env->err) return; |
sx_3e(env); if(env->err) return; |
1300 |
not(env); |
not(env); |
1301 |
} |
} |
1302 |
|
|
1303 |
/* ">=" */ |
/* ">=" */ |
1304 |
extern void sx_3e3d(environment *env) { |
extern void sx_3e3d(environment *env) |
1305 |
|
{ |
1306 |
sx_3c(env); if(env->err) return; |
sx_3c(env); if(env->err) return; |
1307 |
not(env); |
not(env); |
1308 |
} |
} |
1309 |
|
|
1310 |
/* Return copy of a value */ |
/* Return copy of a value */ |
1311 |
value *copy_val(environment *env, value *old_value){ |
value *copy_val(environment *env, value *old_value) |
1312 |
stackitem *old_item, *new_item, *prev_item; |
{ |
1313 |
value *new_value; |
value *new_value; |
1314 |
|
|
1315 |
protect(env, old_value); |
if(old_value==NULL) |
1316 |
|
return NULL; |
1317 |
|
|
1318 |
|
protect(old_value); |
1319 |
new_value= new_val(env); |
new_value= new_val(env); |
|
protect(env, new_value); |
|
1320 |
new_value->type= old_value->type; |
new_value->type= old_value->type; |
1321 |
|
|
1322 |
switch(old_value->type){ |
switch(old_value->type){ |
1330 |
(char *)(new_value->content.ptr)= |
(char *)(new_value->content.ptr)= |
1331 |
strdup((char *)(old_value->content.ptr)); |
strdup((char *)(old_value->content.ptr)); |
1332 |
break; |
break; |
1333 |
case list: |
case tcons: |
|
new_value->content.ptr= NULL; |
|
1334 |
|
|
1335 |
prev_item= NULL; |
new_value->content.c= malloc(sizeof(cons)); |
1336 |
old_item= (stackitem*)(old_value->content.ptr); |
assert(new_value->content.c!=NULL); |
1337 |
|
|
1338 |
while(old_item != NULL) { /* While list is not empty */ |
CAR(new_value)= copy_val(env, CAR(old_value)); /* recurse */ |
1339 |
new_item= malloc(sizeof(stackitem)); |
CDR(new_value)= copy_val(env, CDR(old_value)); /* recurse */ |
|
new_item->item= copy_val(env, old_item->item); /* recurse */ |
|
|
new_item->next= NULL; |
|
|
if(prev_item != NULL) /* If this wasn't the first item */ |
|
|
prev_item->next= new_item; /* point the previous item to the |
|
|
new item */ |
|
|
else |
|
|
new_value->content.ptr= new_item; |
|
|
old_item= old_item->next; |
|
|
prev_item= new_item; |
|
|
} |
|
1340 |
break; |
break; |
1341 |
} |
} |
1342 |
|
|
1343 |
unprotect(env); unprotect(env); |
unprotect(old_value); |
1344 |
|
|
1345 |
return new_value; |
return new_value; |
1346 |
} |
} |
1347 |
|
|
1348 |
/* "dup"; duplicates an item on the stack */ |
/* "dup"; duplicates an item on the stack */ |
1349 |
extern void sx_647570(environment *env) { |
extern void sx_647570(environment *env) |
1350 |
if((env->head)==NULL) { |
{ |
1351 |
|
if(env->head->type==empty) { |
1352 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1353 |
env->err= 1; |
env->err= 1; |
1354 |
return; |
return; |
1355 |
} |
} |
1356 |
push_val(env, copy_val(env, env->head->item)); |
push_val(env, copy_val(env, CAR(env->head))); |
1357 |
} |
} |
1358 |
|
|
1359 |
/* "if", If-Then */ |
/* "if", If-Then */ |
1360 |
extern void sx_6966(environment *env) { |
extern void sx_6966(environment *env) |
1361 |
|
{ |
1362 |
int truth; |
int truth; |
1363 |
|
|
1364 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1365 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1366 |
env->err= 1; |
env->err= 1; |
1367 |
return; |
return; |
1368 |
} |
} |
1369 |
|
|
1370 |
if(env->head->next->item->type != integer) { |
if(CAR(CDR(env->head))->type != integer) { |
1371 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1372 |
env->err=2; |
env->err= 2; |
1373 |
return; |
return; |
1374 |
} |
} |
1375 |
|
|
1376 |
swap(env); |
swap(env); |
1377 |
if(env->err) return; |
if(env->err) return; |
1378 |
|
|
1379 |
truth=env->head->item->content.i; |
truth= CAR(env->head)->content.i; |
1380 |
|
|
1381 |
toss(env); |
toss(env); |
1382 |
if(env->err) return; |
if(env->err) return; |
1388 |
} |
} |
1389 |
|
|
1390 |
/* If-Then-Else */ |
/* If-Then-Else */ |
1391 |
extern void ifelse(environment *env) { |
extern void ifelse(environment *env) |
1392 |
|
{ |
1393 |
int truth; |
int truth; |
1394 |
|
|
1395 |
if((env->head)==NULL || env->head->next==NULL |
if(env->head->type==empty || CDR(env->head)->type==empty |
1396 |
|| env->head->next->next==NULL) { |
|| CDR(CDR(env->head))->type==empty) { |
1397 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1398 |
env->err=1; |
env->err= 1; |
1399 |
return; |
return; |
1400 |
} |
} |
1401 |
|
|
1402 |
if(env->head->next->next->item->type != integer) { |
if(CAR(CDR(CDR(env->head)))->type!=integer) { |
1403 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1404 |
env->err=2; |
env->err= 2; |
1405 |
return; |
return; |
1406 |
} |
} |
1407 |
|
|
1408 |
rot(env); |
rot(env); |
1409 |
if(env->err) return; |
if(env->err) return; |
1410 |
|
|
1411 |
truth=env->head->item->content.i; |
truth= CAR(env->head)->content.i; |
1412 |
|
|
1413 |
toss(env); |
toss(env); |
1414 |
if(env->err) return; |
if(env->err) return; |
1423 |
eval(env); |
eval(env); |
1424 |
} |
} |
1425 |
|
|
1426 |
/* "while" */ |
extern void sx_656c7365(environment *env) |
1427 |
extern void sx_7768696c65(environment *env) { |
{ |
1428 |
|
if(env->head->type==empty || CDR(env->head)->type==empty |
1429 |
|
|| CDR(CDR(env->head))->type==empty || CDR(CDR(CDR(env->head)))->type==empty |
1430 |
|
|| CDR(CDR(CDR(CDR(env->head))))->type==empty) { |
1431 |
|
printerr("Too Few Arguments"); |
1432 |
|
env->err= 1; |
1433 |
|
return; |
1434 |
|
} |
1435 |
|
|
1436 |
|
if(CAR(CDR(env->head))->type!=symb |
1437 |
|
|| strcmp(CAR(CDR(env->head))->content.sym->id, "then")!=0 |
1438 |
|
|| CAR(CDR(CDR(CDR(env->head))))->type!=symb |
1439 |
|
|| strcmp(CAR(CDR(CDR(CDR(env->head))))->content.sym->id, "if")!=0) { |
1440 |
|
printerr("Bad Argument Type"); |
1441 |
|
env->err= 2; |
1442 |
|
return; |
1443 |
|
} |
1444 |
|
|
1445 |
|
swap(env); toss(env); rot(env); toss(env); |
1446 |
|
ifelse(env); |
1447 |
|
} |
1448 |
|
|
1449 |
|
extern void then(environment *env) |
1450 |
|
{ |
1451 |
|
if(env->head->type==empty || CDR(env->head)->type==empty |
1452 |
|
|| CDR(CDR(env->head))->type==empty) { |
1453 |
|
printerr("Too Few Arguments"); |
1454 |
|
env->err= 1; |
1455 |
|
return; |
1456 |
|
} |
1457 |
|
|
1458 |
|
if(CAR(CDR(env->head))->type!=symb |
1459 |
|
|| strcmp(CAR(CDR(env->head))->content.sym->id, "if")!=0) { |
1460 |
|
printerr("Bad Argument Type"); |
1461 |
|
env->err= 2; |
1462 |
|
return; |
1463 |
|
} |
1464 |
|
|
1465 |
|
swap(env); toss(env); |
1466 |
|
sx_6966(env); |
1467 |
|
} |
1468 |
|
|
1469 |
|
/* "while" */ |
1470 |
|
extern void sx_7768696c65(environment *env) |
1471 |
|
{ |
1472 |
int truth; |
int truth; |
1473 |
value *loop, *test; |
value *loop, *test; |
1474 |
|
|
1475 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1476 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1477 |
env->err=1; |
env->err= 1; |
1478 |
return; |
return; |
1479 |
} |
} |
1480 |
|
|
1481 |
loop= env->head->item; |
loop= CAR(env->head); |
1482 |
protect(env, loop); |
protect(loop); |
1483 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1484 |
|
|
1485 |
test= env->head->item; |
test= CAR(env->head); |
1486 |
protect(env, test); |
protect(test); |
1487 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1488 |
|
|
1489 |
do { |
do { |
1490 |
push_val(env, test); |
push_val(env, test); |
1491 |
eval(env); |
eval(env); |
1492 |
|
|
1493 |
if(env->head->item->type != integer) { |
if(CAR(env->head)->type != integer) { |
1494 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1495 |
env->err= 2; |
env->err= 2; |
1496 |
return; |
return; |
1497 |
} |
} |
1498 |
|
|
1499 |
truth= env->head->item->content.i; |
truth= CAR(env->head)->content.i; |
1500 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1501 |
|
|
1502 |
if(truth) { |
if(truth) { |
1508 |
|
|
1509 |
} while(truth); |
} while(truth); |
1510 |
|
|
1511 |
unprotect(env); unprotect(env); |
unprotect(loop); unprotect(test); |
1512 |
} |
} |
1513 |
|
|
1514 |
|
|
1515 |
/* "for"; for-loop */ |
/* "for"; for-loop */ |
1516 |
extern void sx_666f72(environment *env) { |
extern void sx_666f72(environment *env) |
1517 |
|
{ |
1518 |
value *loop; |
value *loop; |
1519 |
int foo1, foo2; |
int foo1, foo2; |
1520 |
|
|
1521 |
if(env->head==NULL || env->head->next==NULL |
if(env->head->type==empty || CDR(env->head)->type==empty |
1522 |
|| env->head->next->next==NULL) { |
|| CDR(CDR(env->head))->type==empty) { |
1523 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1524 |
env->err= 1; |
env->err= 1; |
1525 |
return; |
return; |
1526 |
} |
} |
1527 |
|
|
1528 |
if(env->head->next->item->type!=integer |
if(CAR(CDR(env->head))->type!=integer |
1529 |
|| env->head->next->next->item->type!=integer) { |
|| CAR(CDR(CDR(env->head)))->type!=integer) { |
1530 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1531 |
env->err= 2; |
env->err= 2; |
1532 |
return; |
return; |
1533 |
} |
} |
1534 |
|
|
1535 |
loop= env->head->item; |
loop= CAR(env->head); |
1536 |
protect(env, loop); |
protect(loop); |
1537 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1538 |
|
|
1539 |
foo2= env->head->item->content.i; |
foo2= CAR(env->head)->content.i; |
1540 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1541 |
|
|
1542 |
foo1= env->head->item->content.i; |
foo1= CAR(env->head)->content.i; |
1543 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1544 |
|
|
1545 |
if(foo1<=foo2) { |
if(foo1<=foo2) { |
1557 |
foo1--; |
foo1--; |
1558 |
} |
} |
1559 |
} |
} |
1560 |
unprotect(env); |
unprotect(loop); |
1561 |
} |
} |
1562 |
|
|
1563 |
/* Variant of for-loop */ |
/* Variant of for-loop */ |
1564 |
extern void foreach(environment *env) { |
extern void foreach(environment *env) |
1565 |
|
{ |
1566 |
value *loop, *foo; |
value *loop, *foo; |
1567 |
stackitem *iterator; |
value *iterator; |
1568 |
|
|
1569 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1570 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1571 |
env->err= 1; |
env->err= 1; |
1572 |
return; |
return; |
1573 |
} |
} |
1574 |
|
|
1575 |
if(env->head->next->item->type != list) { |
if(CAR(CDR(env->head))->type!=tcons) { |
1576 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1577 |
env->err= 2; |
env->err= 2; |
1578 |
return; |
return; |
1579 |
} |
} |
1580 |
|
|
1581 |
loop= env->head->item; |
loop= CAR(env->head); |
1582 |
protect(env, loop); |
protect(loop); |
1583 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1584 |
|
|
1585 |
foo= env->head->item; |
foo= CAR(env->head); |
1586 |
protect(env, foo); |
protect(foo); |
1587 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1588 |
|
|
1589 |
iterator= foo->content.ptr; |
iterator= foo; |
1590 |
|
|
1591 |
while(iterator!=NULL) { |
while(iterator!=NULL) { |
1592 |
push_val(env, iterator->item); |
push_val(env, CAR(iterator)); |
1593 |
push_val(env, loop); |
push_val(env, loop); |
1594 |
eval(env); if(env->err) return; |
eval(env); if(env->err) return; |
1595 |
iterator= iterator->next; |
if (iterator->type == tcons){ |
1596 |
|
iterator= CDR(iterator); |
1597 |
|
} else { |
1598 |
|
printerr("Bad Argument Type"); /* Improper list */ |
1599 |
|
env->err= 2; |
1600 |
|
break; |
1601 |
|
} |
1602 |
} |
} |
1603 |
unprotect(env); unprotect(env); |
unprotect(loop); unprotect(foo); |
1604 |
} |
} |
1605 |
|
|
1606 |
/* "to" */ |
/* "to" */ |
1607 |
extern void to(environment *env) { |
extern void to(environment *env) |
1608 |
int i, start, ending; |
{ |
1609 |
stackitem *temp_head; |
int ending, start, i; |
1610 |
value *temp_val; |
value *iterator, *temp; |
1611 |
|
|
1612 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1613 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1614 |
env->err=1; |
env->err= 1; |
1615 |
return; |
return; |
1616 |
} |
} |
1617 |
|
|
1618 |
if(env->head->item->type!=integer |
if(CAR(env->head)->type!=integer |
1619 |
|| env->head->next->item->type!=integer) { |
|| CAR(CDR(env->head))->type!=integer) { |
1620 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1621 |
env->err=2; |
env->err= 2; |
1622 |
return; |
return; |
1623 |
} |
} |
1624 |
|
|
1625 |
ending= env->head->item->content.i; |
ending= CAR(env->head)->content.i; |
1626 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1627 |
start= env->head->item->content.i; |
start= CAR(env->head)->content.i; |
1628 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1629 |
|
|
1630 |
temp_head= env->head; |
push_sym(env, "["); |
|
env->head= NULL; |
|
1631 |
|
|
1632 |
if(ending>=start) { |
if(ending>=start) { |
1633 |
for(i= ending; i>=start; i--) |
for(i= ending; i>=start; i--) |
1637 |
push_int(env, i); |
push_int(env, i); |
1638 |
} |
} |
1639 |
|
|
1640 |
temp_val= new_val(env); |
iterator= env->head; |
|
protect(env, temp_val); |
|
1641 |
|
|
1642 |
temp_val->content.ptr= env->head; |
if(iterator->type==empty |
1643 |
temp_val->type= list; |
|| (CAR(iterator)->type==symb |
1644 |
env->head= temp_head; |
&& CAR(iterator)->content.sym->id[0]=='[')) { |
1645 |
push_val(env, temp_val); |
temp= NULL; |
1646 |
|
toss(env); |
1647 |
|
} else { |
1648 |
|
/* Search for first delimiter */ |
1649 |
|
while(CDR(iterator)!=NULL |
1650 |
|
&& (CAR(CDR(iterator))->type!=symb |
1651 |
|
|| CAR(CDR(iterator))->content.sym->id[0]!='[')) |
1652 |
|
iterator= CDR(iterator); |
1653 |
|
|
1654 |
|
/* Extract list */ |
1655 |
|
temp= env->head; |
1656 |
|
env->head= CDR(iterator); |
1657 |
|
CDR(iterator)= NULL; |
1658 |
|
|
1659 |
unprotect(env); |
if(env->head!=NULL) |
1660 |
|
toss(env); |
1661 |
|
} |
1662 |
|
|
1663 |
|
/* Push list */ |
1664 |
|
push_val(env, temp); |
1665 |
} |
} |
1666 |
|
|
1667 |
/* Read a string */ |
/* Read a string */ |
1668 |
extern void readline(environment *env) { |
extern void readline(environment *env) |
1669 |
|
{ |
1670 |
char in_string[101]; |
char in_string[101]; |
1671 |
|
|
1672 |
if(fgets(in_string, 100, env->inputstream)==NULL) |
if(fgets(in_string, 100, env->inputstream)==NULL) |
1676 |
} |
} |
1677 |
|
|
1678 |
/* "read"; Read a value and place on stack */ |
/* "read"; Read a value and place on stack */ |
1679 |
extern void sx_72656164(environment *env) { |
extern void sx_72656164(environment *env) |
1680 |
|
{ |
1681 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1682 |
const char strform[]= "\"%[^\"]\"%n"; |
const char strform[]= "\"%[^\"]\"%n"; |
1683 |
const char intform[]= "%i%n"; |
const char intform[]= "%i%n"; |
1700 |
} |
} |
1701 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
1702 |
|
|
1703 |
if(((char *)(env->head->item->content.ptr))[0]=='\0'){ |
if(((char *)(CAR(env->head)->content.ptr))[0]=='\0'){ |
1704 |
env->err= 4; /* "" means EOF */ |
env->err= 4; /* "" means EOF */ |
1705 |
return; |
return; |
1706 |
} |
} |
1707 |
|
|
1708 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(CAR(env->head)->content.ptr)+1); |
1709 |
env->free_string= env->in_string; /* Save the original pointer */ |
env->free_string= env->in_string; /* Save the original pointer */ |
1710 |
strcpy(env->in_string, env->head->item->content.ptr); |
strcpy(env->in_string, CAR(env->head)->content.ptr); |
1711 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1712 |
} |
} |
1713 |
|
|
1756 |
return sx_72656164(env); |
return sx_72656164(env); |
1757 |
} |
} |
1758 |
|
|
1759 |
extern void beep(environment *env) { |
#ifdef __linux__ |
1760 |
|
extern void beep(environment *env) |
1761 |
|
{ |
1762 |
int freq, dur, period, ticks; |
int freq, dur, period, ticks; |
1763 |
|
|
1764 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
1765 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1766 |
env->err=1; |
env->err= 1; |
1767 |
return; |
return; |
1768 |
} |
} |
1769 |
|
|
1770 |
if(env->head->item->type!=integer |
if(CAR(env->head)->type!=integer |
1771 |
|| env->head->next->item->type!=integer) { |
|| CAR(CDR(env->head))->type!=integer) { |
1772 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1773 |
env->err=2; |
env->err= 2; |
1774 |
return; |
return; |
1775 |
} |
} |
1776 |
|
|
1777 |
dur=env->head->item->content.i; |
dur= CAR(env->head)->content.i; |
1778 |
toss(env); |
toss(env); |
1779 |
freq=env->head->item->content.i; |
freq= CAR(env->head)->content.i; |
1780 |
toss(env); |
toss(env); |
1781 |
|
|
1782 |
period=1193180/freq; /* convert freq from Hz to period |
period= 1193180/freq; /* convert freq from Hz to period |
1783 |
length */ |
length */ |
1784 |
ticks=dur*.001193180; /* convert duration from µseconds to |
ticks= dur*.001193180; /* convert duration from µseconds to |
1785 |
timer ticks */ |
timer ticks */ |
1786 |
|
|
1787 |
/* ticks=dur/1000; */ |
/* ticks=dur/1000; */ |
1788 |
|
|
1789 |
/* if (ioctl(STDOUT_FILENO, KDMKTONE, (125<<16) + 0x637)==0) */ |
/* if (ioctl(STDOUT_FILENO, KDMKTONE, (125<<16) + 0x637)==0) */ |
1790 |
switch (ioctl(STDOUT_FILENO, KDMKTONE, (ticks<<16) | period)){ |
switch (ioctl(STDOUT_FILENO, KDMKTONE, (ticks<<16) | period)){ |
1791 |
case 0: |
case 0: |
1792 |
usleep(dur); |
usleep(dur); |
1793 |
return; |
return; |
1794 |
case -1: |
case -1: |
1795 |
perror("beep"); |
perror("beep"); |
1796 |
env->err=5; |
env->err= 5; |
1797 |
return; |
return; |
1798 |
default: |
default: |
1799 |
abort(); |
abort(); |
1800 |
} |
} |
1801 |
}; |
} |
1802 |
|
#endif /* __linux__ */ |
1803 |
|
|
1804 |
/* "wait" */ |
/* "wait" */ |
1805 |
extern void sx_77616974(environment *env) { |
extern void sx_77616974(environment *env) |
1806 |
|
{ |
1807 |
int dur; |
int dur; |
1808 |
|
|
1809 |
if((env->head)==NULL) { |
if(env->head->type==empty) { |
1810 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1811 |
env->err=1; |
env->err= 1; |
1812 |
return; |
return; |
1813 |
} |
} |
1814 |
|
|
1815 |
if(env->head->item->type!=integer) { |
if(CAR(env->head)->type!=integer) { |
1816 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1817 |
env->err=2; |
env->err= 2; |
1818 |
return; |
return; |
1819 |
} |
} |
1820 |
|
|
1821 |
dur=env->head->item->content.i; |
dur= CAR(env->head)->content.i; |
1822 |
toss(env); |
toss(env); |
1823 |
|
|
1824 |
usleep(dur); |
usleep(dur); |
1825 |
}; |
} |
1826 |
|
|
1827 |
extern void copying(environment *env){ |
extern void copying(environment *env) |
1828 |
printf("GNU GENERAL PUBLIC LICENSE\n\ |
{ |
1829 |
|
printf(" GNU GENERAL PUBLIC LICENSE\n\ |
1830 |
Version 2, June 1991\n\ |
Version 2, June 1991\n\ |
1831 |
\n\ |
\n\ |
1832 |
Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n\ |
Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n\ |
2084 |
of promoting the sharing and reuse of software generally.\n"); |
of promoting the sharing and reuse of software generally.\n"); |
2085 |
} |
} |
2086 |
|
|
2087 |
extern void warranty(environment *env){ |
extern void warranty(environment *env) |
2088 |
|
{ |
2089 |
printf(" NO WARRANTY\n\ |
printf(" NO WARRANTY\n\ |
2090 |
\n\ |
\n\ |
2091 |
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n\ |
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n\ |
2115 |
int a, b; |
int a, b; |
2116 |
float fa, fb; |
float fa, fb; |
2117 |
|
|
2118 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2119 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
2120 |
env->err=1; |
env->err= 1; |
2121 |
return; |
return; |
2122 |
} |
} |
2123 |
|
|
2124 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
2125 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
2126 |
a=env->head->item->content.i; |
a= CAR(env->head)->content.i; |
2127 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2128 |
b=env->head->item->content.i; |
b= CAR(env->head)->content.i; |
2129 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2130 |
push_int(env, b*a); |
push_int(env, b*a); |
2131 |
|
|
2132 |
return; |
return; |
2133 |
} |
} |
2134 |
|
|
2135 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
2136 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
2137 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
2138 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2139 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
2140 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2141 |
push_float(env, fb*fa); |
push_float(env, fb*fa); |
2142 |
|
|
2143 |
return; |
return; |
2144 |
} |
} |
2145 |
|
|
2146 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
2147 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
2148 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
2149 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2150 |
b= env->head->item->content.i; |
b= CAR(env->head)->content.i; |
2151 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2152 |
push_float(env, b*fa); |
push_float(env, b*fa); |
2153 |
|
|
2154 |
return; |
return; |
2155 |
} |
} |
2156 |
|
|
2157 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
2158 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
2159 |
a= env->head->item->content.i; |
a= CAR(env->head)->content.i; |
2160 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2161 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
2162 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2163 |
push_float(env, fb*a); |
push_float(env, fb*a); |
2164 |
|
|
2166 |
} |
} |
2167 |
|
|
2168 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
2169 |
env->err=2; |
env->err= 2; |
2170 |
} |
} |
2171 |
|
|
2172 |
/* "/" */ |
/* "/" */ |
2175 |
int a, b; |
int a, b; |
2176 |
float fa, fb; |
float fa, fb; |
2177 |
|
|
2178 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2179 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
2180 |
env->err=1; |
env->err= 1; |
2181 |
return; |
return; |
2182 |
} |
} |
2183 |
|
|
2184 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
2185 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
2186 |
a=env->head->item->content.i; |
a= CAR(env->head)->content.i; |
2187 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2188 |
b=env->head->item->content.i; |
b= CAR(env->head)->content.i; |
2189 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2190 |
push_float(env, b/a); |
push_float(env, b/a); |
2191 |
|
|
2192 |
return; |
return; |
2193 |
} |
} |
2194 |
|
|
2195 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
2196 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
2197 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
2198 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2199 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
2200 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2201 |
push_float(env, fb/fa); |
push_float(env, fb/fa); |
2202 |
|
|
2203 |
return; |
return; |
2204 |
} |
} |
2205 |
|
|
2206 |
if(env->head->item->type==tfloat |
if(CAR(env->head)->type==tfloat |
2207 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
2208 |
fa= env->head->item->content.f; |
fa= CAR(env->head)->content.f; |
2209 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2210 |
b= env->head->item->content.i; |
b= CAR(env->head)->content.i; |
2211 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2212 |
push_float(env, b/fa); |
push_float(env, b/fa); |
2213 |
|
|
2214 |
return; |
return; |
2215 |
} |
} |
2216 |
|
|
2217 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
2218 |
&& env->head->next->item->type==tfloat) { |
&& CAR(CDR(env->head))->type==tfloat) { |
2219 |
a= env->head->item->content.i; |
a= CAR(env->head)->content.i; |
2220 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2221 |
fb= env->head->item->content.f; |
fb= CAR(env->head)->content.f; |
2222 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2223 |
push_float(env, fb/a); |
push_float(env, fb/a); |
2224 |
|
|
2226 |
} |
} |
2227 |
|
|
2228 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
2229 |
env->err=2; |
env->err= 2; |
2230 |
} |
} |
2231 |
|
|
2232 |
/* "mod" */ |
/* "mod" */ |
2234 |
{ |
{ |
2235 |
int a, b; |
int a, b; |
2236 |
|
|
2237 |
if((env->head)==NULL || env->head->next==NULL) { |
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2238 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
2239 |
env->err=1; |
env->err= 1; |
2240 |
return; |
return; |
2241 |
} |
} |
2242 |
|
|
2243 |
if(env->head->item->type==integer |
if(CAR(env->head)->type==integer |
2244 |
&& env->head->next->item->type==integer) { |
&& CAR(CDR(env->head))->type==integer) { |
2245 |
a= env->head->item->content.i; |
a= CAR(env->head)->content.i; |
2246 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2247 |
b= env->head->item->content.i; |
b= CAR(env->head)->content.i; |
2248 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
2249 |
push_int(env, b%a); |
push_int(env, b%a); |
2250 |
|
|
2252 |
} |
} |
2253 |
|
|
2254 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
2255 |
env->err=2; |
env->err= 2; |
2256 |
|
} |
2257 |
|
|
2258 |
|
/* "div" */ |
2259 |
|
extern void sx_646976(environment *env) |
2260 |
|
{ |
2261 |
|
int a, b; |
2262 |
|
|
2263 |
|
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2264 |
|
printerr("Too Few Arguments"); |
2265 |
|
env->err= 1; |
2266 |
|
return; |
2267 |
|
} |
2268 |
|
|
2269 |
|
if(CAR(env->head)->type==integer |
2270 |
|
&& CAR(CDR(env->head))->type==integer) { |
2271 |
|
a= CAR(env->head)->content.i; |
2272 |
|
toss(env); if(env->err) return; |
2273 |
|
b= CAR(env->head)->content.i; |
2274 |
|
toss(env); if(env->err) return; |
2275 |
|
push_int(env, (int)b/a); |
2276 |
|
|
2277 |
|
return; |
2278 |
|
} |
2279 |
|
|
2280 |
|
printerr("Bad Argument Type"); |
2281 |
|
env->err= 2; |
2282 |
|
} |
2283 |
|
|
2284 |
|
extern void setcar(environment *env) |
2285 |
|
{ |
2286 |
|
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2287 |
|
printerr("Too Few Arguments"); |
2288 |
|
env->err= 1; |
2289 |
|
return; |
2290 |
|
} |
2291 |
|
|
2292 |
|
if(CDR(env->head)->type!=tcons) { |
2293 |
|
printerr("Bad Argument Type"); |
2294 |
|
env->err= 2; |
2295 |
|
return; |
2296 |
|
} |
2297 |
|
|
2298 |
|
CAR(CAR(CDR(env->head)))=CAR(env->head); |
2299 |
|
toss(env); |
2300 |
|
} |
2301 |
|
|
2302 |
|
extern void setcdr(environment *env) |
2303 |
|
{ |
2304 |
|
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2305 |
|
printerr("Too Few Arguments"); |
2306 |
|
env->err= 1; |
2307 |
|
return; |
2308 |
|
} |
2309 |
|
|
2310 |
|
if(CDR(env->head)->type!=tcons) { |
2311 |
|
printerr("Bad Argument Type"); |
2312 |
|
env->err= 2; |
2313 |
|
return; |
2314 |
|
} |
2315 |
|
|
2316 |
|
CDR(CAR(CDR(env->head)))=CAR(env->head); |
2317 |
|
toss(env); |
2318 |
|
} |
2319 |
|
|
2320 |
|
extern void car(environment *env) |
2321 |
|
{ |
2322 |
|
if(env->head->type==empty) { |
2323 |
|
printerr("Too Few Arguments"); |
2324 |
|
env->err= 1; |
2325 |
|
return; |
2326 |
|
} |
2327 |
|
|
2328 |
|
if(CAR(env->head)->type!=tcons) { |
2329 |
|
printerr("Bad Argument Type"); |
2330 |
|
env->err= 2; |
2331 |
|
return; |
2332 |
|
} |
2333 |
|
|
2334 |
|
CAR(env->head)=CAR(CAR(env->head)); |
2335 |
|
} |
2336 |
|
|
2337 |
|
extern void cdr(environment *env) |
2338 |
|
{ |
2339 |
|
if(env->head->type==empty) { |
2340 |
|
printerr("Too Few Arguments"); |
2341 |
|
env->err= 1; |
2342 |
|
return; |
2343 |
|
} |
2344 |
|
|
2345 |
|
if(CAR(env->head)->type!=tcons) { |
2346 |
|
printerr("Bad Argument Type"); |
2347 |
|
env->err= 2; |
2348 |
|
return; |
2349 |
|
} |
2350 |
|
|
2351 |
|
CAR(env->head)=CDR(CAR(env->head)); |
2352 |
} |
} |