18 |
enum { |
enum { |
19 |
integer, |
integer, |
20 |
string, |
string, |
|
ref, /* Reference (to an element in the |
|
|
hash table) */ |
|
21 |
func, /* Function pointer */ |
func, /* Function pointer */ |
22 |
symb, |
symb, |
23 |
list |
list |
68 |
{ |
{ |
69 |
long i; |
long i; |
70 |
|
|
71 |
|
env->err=0; |
72 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
73 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
74 |
} |
} |
103 |
} |
} |
104 |
|
|
105 |
/* Generic push function. */ |
/* Generic push function. */ |
106 |
int push(stackitem** stack_head, stackitem* in_item) |
void push(stackitem** stack_head, stackitem* in_item) |
107 |
{ |
{ |
108 |
in_item->next= *stack_head; |
in_item->next= *stack_head; |
109 |
*stack_head= in_item; |
*stack_head= in_item; |
|
return 1; |
|
110 |
} |
} |
111 |
|
|
112 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
119 |
} |
} |
120 |
|
|
121 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
122 |
int push_int(stackitem **stack_head, int in_val) |
void push_int(stackitem **stack_head, int in_val) |
123 |
{ |
{ |
124 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
125 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
130 |
new_value->refcount=1; |
new_value->refcount=1; |
131 |
|
|
132 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
133 |
} |
} |
134 |
|
|
135 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
136 |
int push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(stackitem **stack_head, const char *in_string) |
137 |
{ |
{ |
138 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
139 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
145 |
new_value->refcount=1; |
new_value->refcount=1; |
146 |
|
|
147 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
148 |
} |
} |
149 |
|
|
150 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
151 |
int push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
152 |
{ |
{ |
153 |
stackitem *new_item; /* The new stack item */ |
stackitem *new_item; /* The new stack item */ |
154 |
/* ...which will contain... */ |
/* ...which will contain... */ |
203 |
} |
} |
204 |
} |
} |
205 |
push(&(env->head), new_item); |
push(&(env->head), new_item); |
|
return 1; |
|
206 |
} |
} |
207 |
|
|
208 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) { |
218 |
switch (val->type){ /* and free the contents if necessary */ |
switch (val->type){ /* and free the contents if necessary */ |
219 |
case string: |
case string: |
220 |
free(val->content.ptr); |
free(val->content.ptr); |
221 |
|
break; |
222 |
case list: /* lists needs to be freed recursively */ |
case list: /* lists needs to be freed recursively */ |
223 |
item=val->content.ptr; |
item=val->content.ptr; |
224 |
while(item != NULL) { /* for all stack items */ |
while(item != NULL) { /* for all stack items */ |
241 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
242 |
|
|
243 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
244 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
245 |
|
env->err=1; |
246 |
return; |
return; |
247 |
} |
} |
248 |
|
|
252 |
} |
} |
253 |
|
|
254 |
/* Print newline. */ |
/* Print newline. */ |
255 |
extern void nl(environment *env) |
extern void nl() |
256 |
{ |
{ |
257 |
printf("\n"); |
printf("\n"); |
258 |
} |
} |
259 |
|
|
260 |
/* Prints the top element of the stack. */ |
/* Gets the type of a value */ |
261 |
void print_h(stackitem *stack_head) |
extern void type(environment *env){ |
262 |
{ |
int typenum; |
263 |
|
|
264 |
if(stack_head==NULL) { |
if((env->head)==NULL) { |
265 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
266 |
|
env->err=1; |
267 |
return; |
return; |
268 |
} |
} |
269 |
|
typenum=env->head->item->type; |
270 |
|
toss(env); |
271 |
|
switch(typenum){ |
272 |
|
case integer: |
273 |
|
push_sym(env, "integer"); |
274 |
|
break; |
275 |
|
case string: |
276 |
|
push_sym(env, "string"); |
277 |
|
break; |
278 |
|
case symb: |
279 |
|
push_sym(env, "symbol"); |
280 |
|
break; |
281 |
|
case func: |
282 |
|
push_sym(env, "function"); |
283 |
|
break; |
284 |
|
case list: |
285 |
|
push_sym(env, "list"); |
286 |
|
break; |
287 |
|
default: |
288 |
|
push_sym(env, "unknown"); |
289 |
|
break; |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
/* Prints the top element of the stack. */ |
294 |
|
void print_h(stackitem *stack_head) |
295 |
|
{ |
296 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
297 |
case integer: |
case integer: |
298 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
303 |
case symb: |
case symb: |
304 |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
305 |
break; |
break; |
306 |
|
case func: |
307 |
|
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
308 |
|
break; |
309 |
|
case list: |
310 |
|
printf("#<list %p>", (funcp)(stack_head->item->content.ptr)); |
311 |
|
break; |
312 |
default: |
default: |
313 |
printf("%p", (funcp)(stack_head->item->content.ptr)); |
printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr)); |
314 |
break; |
break; |
315 |
} |
} |
316 |
} |
} |
317 |
|
|
318 |
extern void print_(environment *env) { |
extern void print_(environment *env) { |
319 |
|
if(env->head==NULL) { |
320 |
|
printerr("Too Few Arguments"); |
321 |
|
env->err=1; |
322 |
|
return; |
323 |
|
} |
324 |
print_h(env->head); |
print_h(env->head); |
325 |
} |
} |
326 |
|
|
328 |
extern void print(environment *env) |
extern void print(environment *env) |
329 |
{ |
{ |
330 |
print_(env); |
print_(env); |
331 |
|
if(env->err) return; |
332 |
toss(env); |
toss(env); |
333 |
} |
} |
334 |
|
|
347 |
/* Prints the stack. */ |
/* Prints the stack. */ |
348 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
349 |
{ |
{ |
350 |
if(env->head != NULL) { |
if(env->head == NULL) { |
351 |
print_st(env->head, 1); |
return; |
|
nl(); |
|
|
} else { |
|
|
printerr("Stack empty"); |
|
352 |
} |
} |
353 |
|
print_st(env->head, 1); |
354 |
|
nl(); |
355 |
} |
} |
356 |
|
|
357 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
360 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
361 |
|
|
362 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
363 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
364 |
|
env->err=1; |
365 |
return; |
return; |
366 |
} |
} |
367 |
|
|
368 |
if(env->head->next==NULL) { |
if(env->head->next==NULL) { |
369 |
printerr("Not enough arguments"); |
printerr("Too Few Arguments"); |
370 |
|
env->err=1; |
371 |
return; |
return; |
372 |
} |
} |
373 |
|
|
392 |
value *val; |
value *val; |
393 |
|
|
394 |
if(env->head == NULL) { |
if(env->head == NULL) { |
395 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
396 |
|
env->err=1; |
397 |
return; |
return; |
398 |
} |
} |
399 |
|
|
400 |
if(env->head->item->type!=symb) { |
if(env->head->item->type!=symb) { |
401 |
printerr("Not a symbol"); |
printerr("Bad Argument Type"); |
402 |
|
env->err=2; |
403 |
return; |
return; |
404 |
} |
} |
405 |
|
|
406 |
val=((symbol *)(env->head->item->content.ptr))->val; |
val=((symbol *)(env->head->item->content.ptr))->val; |
407 |
if(val == NULL){ |
if(val == NULL){ |
408 |
printerr("Unbound variable"); |
printerr("Unbound Variable"); |
409 |
|
env->err=3; |
410 |
return; |
return; |
411 |
} |
} |
412 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
413 |
|
if(env->err) return; |
414 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(&(env->head), val); /* Return its bound value */ |
415 |
} |
} |
416 |
|
|
420 |
extern void eval(environment *env) |
extern void eval(environment *env) |
421 |
{ |
{ |
422 |
funcp in_func; |
funcp in_func; |
|
value* val; |
|
423 |
if(env->head==NULL) { |
if(env->head==NULL) { |
424 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
425 |
|
env->err=1; |
426 |
return; |
return; |
427 |
} |
} |
428 |
|
|
429 |
/* if it's a symbol */ |
/* if it's a symbol */ |
430 |
if(env->head->item->type==symb) { |
if(env->head->item->type==symb) { |
431 |
|
|
432 |
/* If it's not bound to anything */ |
rcl(env); /* get its contents */ |
433 |
if (((symbol *)(env->head->item->content.ptr))->val == NULL) { |
if(env->err) return; |
434 |
printerr("Unbound variable"); |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
435 |
return; |
eval(env); /* evaluate the value */ |
|
} |
|
|
|
|
|
/* If it contains a function */ |
|
|
if (((symbol *)(env->head->item->content.ptr))->val->type == func) { |
|
|
in_func= |
|
|
(funcp)(((symbol *)(env->head->item->content.ptr))->val->content.ptr); |
|
|
toss(env); |
|
|
(*in_func)(env); /* Run the function */ |
|
436 |
return; |
return; |
|
} else { /* If it's not a function */ |
|
|
val=((symbol *)(env->head->item->content.ptr))->val; |
|
|
toss(env); /* toss the symbol */ |
|
|
push_val(&(env->head), val); /* Return its bound value */ |
|
437 |
} |
} |
438 |
} |
} |
439 |
|
|
441 |
if(env->head->item->type==func) { |
if(env->head->item->type==func) { |
442 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
443 |
toss(env); |
toss(env); |
444 |
|
if(env->err) return; |
445 |
(*in_func)(env); |
(*in_func)(env); |
|
return; |
|
446 |
} |
} |
|
|
|
447 |
} |
} |
448 |
|
|
449 |
/* Make a list. */ |
/* Make a list. */ |
489 |
} |
} |
490 |
|
|
491 |
/* Parse input. */ |
/* Parse input. */ |
492 |
int stack_read(environment *env, char *in_line) |
void stack_read(environment *env, char *in_line) |
493 |
{ |
{ |
494 |
char *temp, *rest; |
char *temp, *rest; |
495 |
int itemp; |
int itemp; |
518 |
break; |
break; |
519 |
} |
} |
520 |
/* If symbol */ |
/* If symbol */ |
521 |
if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
522 |
push_sym(env, temp); |
push_sym(env, temp); |
523 |
break; |
break; |
524 |
} |
} |
550 |
} |
} |
551 |
} while(0); |
} while(0); |
552 |
|
|
|
|
|
553 |
free(temp); |
free(temp); |
554 |
|
|
555 |
if(convert<2) { |
if(convert<2) { |
556 |
free(rest); |
free(rest); |
557 |
return 0; |
return; |
558 |
} |
} |
559 |
|
|
560 |
stack_read(env, rest); |
stack_read(env, rest); |
561 |
|
|
562 |
free(rest); |
free(rest); |
|
return 1; |
|
563 |
} |
} |
564 |
|
|
565 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
568 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
569 |
|
|
570 |
/* Is top element a list? */ |
/* Is top element a list? */ |
571 |
if(env->head==NULL || env->head->item->type!=list) { |
if(env->head==NULL) { |
572 |
printerr("Stack empty or not a list"); |
printerr("Too Few Arguments"); |
573 |
|
env->err=1; |
574 |
|
return; |
575 |
|
} |
576 |
|
if(env->head->item->type!=list) { |
577 |
|
printerr("Bad Argument Type"); |
578 |
|
env->err=2; |
579 |
return; |
return; |
580 |
} |
} |
581 |
|
|
602 |
int result; |
int result; |
603 |
|
|
604 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
605 |
printerr("Not enough elements to compare"); |
printerr("Too Few Arguments"); |
606 |
|
env->err=1; |
607 |
return; |
return; |
608 |
} |
} |
609 |
|
|
621 |
{ |
{ |
622 |
int val; |
int val; |
623 |
|
|
624 |
if((env->head)==NULL || env->head->item->type!=integer) { |
if((env->head)==NULL) { |
625 |
printerr("Stack empty or element is not a integer"); |
printerr("Too Few Arguments"); |
626 |
|
env->err=1; |
627 |
|
return; |
628 |
|
} |
629 |
|
|
630 |
|
if(env->head->item->type!=integer) { |
631 |
|
printerr("Bad Argument Type"); |
632 |
|
env->err=2; |
633 |
return; |
return; |
634 |
} |
} |
635 |
|
|
652 |
symbol *sym; |
symbol *sym; |
653 |
|
|
654 |
/* 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 */ |
655 |
if(env->head==NULL || env->head->next==NULL |
if(env->head==NULL || env->head->next==NULL) { |
656 |
|| env->head->item->type!=symb) { |
printerr("Too Few Arguments"); |
657 |
printerr("Define what?"); |
env->err=1; |
658 |
|
return; |
659 |
|
} |
660 |
|
|
661 |
|
if(env->head->item->type!=symb) { |
662 |
|
printerr("Bad Argument Type"); |
663 |
|
env->err=2; |
664 |
return; |
return; |
665 |
} |
} |
666 |
|
|
706 |
} |
} |
707 |
} |
} |
708 |
|
|
709 |
|
/* Forgets a symbol (remove it from the hash table) */ |
710 |
|
extern void forget(environment *env) |
711 |
|
{ |
712 |
|
char* sym_id; |
713 |
|
stackitem *stack_head= env->head; |
714 |
|
symbol **hash_entry, *temp; |
715 |
|
|
716 |
|
if(stack_head==NULL) { |
717 |
|
printerr("Too Few Arguments"); |
718 |
|
env->err=1; |
719 |
|
return; |
720 |
|
} |
721 |
|
|
722 |
|
if(stack_head->item->type!=symb) { |
723 |
|
printerr("Bad Argument Type"); |
724 |
|
env->err=2; |
725 |
|
return; |
726 |
|
} |
727 |
|
|
728 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
729 |
|
toss(env); |
730 |
|
|
731 |
|
hash_entry= hash(env->symbols, sym_id); |
732 |
|
temp= *hash_entry; |
733 |
|
*hash_entry= (*hash_entry)->next; |
734 |
|
|
735 |
|
if(temp->val!=NULL) { |
736 |
|
free_val(temp->val); |
737 |
|
} |
738 |
|
free(temp->id); |
739 |
|
free(temp); |
740 |
|
} |
741 |
|
|
742 |
|
/* Returns the current error number to the stack */ |
743 |
|
extern void errn(environment *env){ |
744 |
|
push_int(&(env->head), env->err); |
745 |
|
} |
746 |
|
|
747 |
int main() |
int main() |
748 |
{ |
{ |
749 |
environment myenv; |
environment myenv; |
755 |
|
|
756 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(fgets(in_string, 100, stdin) != NULL) { |
757 |
stack_read(&myenv, in_string); |
stack_read(&myenv, in_string); |
758 |
|
if(myenv.err) { |
759 |
|
printf("(error %d) ", myenv.err); |
760 |
|
myenv.err=0; |
761 |
|
} |
762 |
printf("okidok\n "); |
printf("okidok\n "); |
763 |
} |
} |
764 |
|
|