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 |
|
/* A list is just a stack, so make stack_head point to it */ |
311 |
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
312 |
|
printf("[ "); |
313 |
|
while(stack_head != NULL) { |
314 |
|
print_h(stack_head); |
315 |
|
printf(" "); |
316 |
|
stack_head=stack_head->next; |
317 |
|
} |
318 |
|
printf("]"); |
319 |
|
break; |
320 |
default: |
default: |
321 |
printf("%p", (funcp)(stack_head->item->content.ptr)); |
printf("#<unknown %p>", (stack_head->item->content.ptr)); |
322 |
break; |
break; |
323 |
} |
} |
324 |
} |
} |
325 |
|
|
326 |
extern void print_(environment *env) { |
extern void print_(environment *env) { |
327 |
|
if(env->head==NULL) { |
328 |
|
printerr("Too Few Arguments"); |
329 |
|
env->err=1; |
330 |
|
return; |
331 |
|
} |
332 |
print_h(env->head); |
print_h(env->head); |
333 |
} |
} |
334 |
|
|
336 |
extern void print(environment *env) |
extern void print(environment *env) |
337 |
{ |
{ |
338 |
print_(env); |
print_(env); |
339 |
|
if(env->err) return; |
340 |
toss(env); |
toss(env); |
341 |
} |
} |
342 |
|
|
355 |
/* Prints the stack. */ |
/* Prints the stack. */ |
356 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
357 |
{ |
{ |
358 |
if(env->head != NULL) { |
if(env->head == NULL) { |
359 |
print_st(env->head, 1); |
return; |
|
nl(); |
|
|
} else { |
|
|
printerr("Stack empty"); |
|
360 |
} |
} |
361 |
|
print_st(env->head, 1); |
362 |
|
nl(); |
363 |
} |
} |
364 |
|
|
365 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
368 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
369 |
|
|
370 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
371 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
372 |
|
env->err=1; |
373 |
return; |
return; |
374 |
} |
} |
375 |
|
|
376 |
if(env->head->next==NULL) { |
if(env->head->next==NULL) { |
377 |
printerr("Not enough arguments"); |
printerr("Too Few Arguments"); |
378 |
|
env->err=1; |
379 |
return; |
return; |
380 |
} |
} |
381 |
|
|
400 |
value *val; |
value *val; |
401 |
|
|
402 |
if(env->head == NULL) { |
if(env->head == NULL) { |
403 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
404 |
|
env->err=1; |
405 |
return; |
return; |
406 |
} |
} |
407 |
|
|
408 |
if(env->head->item->type!=symb) { |
if(env->head->item->type!=symb) { |
409 |
printerr("Not a symbol"); |
printerr("Bad Argument Type"); |
410 |
|
env->err=2; |
411 |
return; |
return; |
412 |
} |
} |
413 |
|
|
414 |
val=((symbol *)(env->head->item->content.ptr))->val; |
val=((symbol *)(env->head->item->content.ptr))->val; |
415 |
if(val == NULL){ |
if(val == NULL){ |
416 |
printerr("Unbound variable"); |
printerr("Unbound Variable"); |
417 |
|
env->err=3; |
418 |
return; |
return; |
419 |
} |
} |
420 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
421 |
|
if(env->err) return; |
422 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(&(env->head), val); /* Return its bound value */ |
423 |
} |
} |
424 |
|
|
428 |
extern void eval(environment *env) |
extern void eval(environment *env) |
429 |
{ |
{ |
430 |
funcp in_func; |
funcp in_func; |
431 |
value* val; |
value* temp_val; |
432 |
|
stackitem* iterator; |
433 |
|
|
434 |
if(env->head==NULL) { |
if(env->head==NULL) { |
435 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
436 |
|
env->err=1; |
437 |
return; |
return; |
438 |
} |
} |
439 |
|
|
440 |
/* if it's a symbol */ |
/* if it's a symbol */ |
441 |
if(env->head->item->type==symb) { |
if(env->head->item->type==symb) { |
442 |
|
|
443 |
/* If it's not bound to anything */ |
rcl(env); /* get its contents */ |
444 |
if (((symbol *)(env->head->item->content.ptr))->val == NULL) { |
if(env->err) return; |
445 |
printerr("Unbound variable"); |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
446 |
|
eval(env); /* evaluate the value */ |
447 |
return; |
return; |
448 |
} |
} |
|
|
|
|
/* 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 */ |
|
|
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 */ |
|
|
} |
|
449 |
} |
} |
450 |
|
|
451 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
452 |
if(env->head->item->type==func) { |
if(env->head->item->type==func) { |
453 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
454 |
toss(env); |
toss(env); |
455 |
|
if(env->err) return; |
456 |
(*in_func)(env); |
(*in_func)(env); |
457 |
return; |
return; |
458 |
} |
} |
459 |
|
|
460 |
|
/* If it's a list */ |
461 |
|
if(env->head->item->type==list) { |
462 |
|
temp_val= env->head->item; |
463 |
|
env->head->item->refcount++; |
464 |
|
toss(env); |
465 |
|
if(env->err) return; |
466 |
|
iterator= (stackitem*)temp_val->content.ptr; |
467 |
|
while(iterator!=NULL && iterator->item!=NULL) { |
468 |
|
push_val(&(env->head), iterator->item); |
469 |
|
if(env->head->item->type==symb |
470 |
|
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
471 |
|
toss(env); |
472 |
|
if(env->err) return; |
473 |
|
eval(env); |
474 |
|
} |
475 |
|
iterator= iterator->next; |
476 |
|
} |
477 |
|
free_val(temp_val); |
478 |
|
} |
479 |
|
} |
480 |
|
|
481 |
|
/* Reverse (flip) a list */ |
482 |
|
extern void rev(environment *env){ |
483 |
|
stackitem *old_head, *new_head, *item; |
484 |
|
|
485 |
|
if((env->head)==NULL) { |
486 |
|
printerr("Too Few Arguments"); |
487 |
|
env->err=1; |
488 |
|
return; |
489 |
|
} |
490 |
|
|
491 |
|
if(env->head->item->type!=list) { |
492 |
|
printerr("Bad Argument Type"); |
493 |
|
env->err=2; |
494 |
|
return; |
495 |
|
} |
496 |
|
|
497 |
|
old_head=(stackitem *)(env->head->item->content.ptr); |
498 |
|
new_head=NULL; |
499 |
|
while(old_head != NULL){ |
500 |
|
item=old_head; |
501 |
|
old_head=old_head->next; |
502 |
|
item->next=new_head; |
503 |
|
new_head=item; |
504 |
|
} |
505 |
|
env->head->item->content.ptr=new_head; |
506 |
} |
} |
507 |
|
|
508 |
/* Make a list. */ |
/* Make a list. */ |
545 |
temp->item= pack; |
temp->item= pack; |
546 |
|
|
547 |
push(&(env->head), temp); |
push(&(env->head), temp); |
548 |
|
rev(env); |
549 |
} |
} |
550 |
|
|
551 |
/* Parse input. */ |
/* Parse input. */ |
552 |
int stack_read(environment *env, char *in_line) |
void stack_read(environment *env, char *in_line) |
553 |
{ |
{ |
554 |
char *temp, *rest; |
char *temp, *rest; |
555 |
int itemp; |
int itemp; |
561 |
rest= malloc(inlength); |
rest= malloc(inlength); |
562 |
|
|
563 |
do { |
do { |
564 |
|
/* If comment */ |
565 |
|
if((convert= sscanf(in_line, "#%[^\n\r]", rest))) { |
566 |
|
free(temp); free(rest); |
567 |
|
return; |
568 |
|
} |
569 |
|
|
570 |
/* If string */ |
/* If string */ |
571 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
572 |
push_cstring(&(env->head), temp); |
push_cstring(&(env->head), temp); |
584 |
break; |
break; |
585 |
} |
} |
586 |
/* If symbol */ |
/* If symbol */ |
587 |
if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
588 |
push_sym(env, temp); |
push_sym(env, temp); |
589 |
break; |
break; |
590 |
} |
} |
616 |
} |
} |
617 |
} while(0); |
} while(0); |
618 |
|
|
|
|
|
619 |
free(temp); |
free(temp); |
620 |
|
|
621 |
if(convert<2) { |
if(convert<2) { |
622 |
free(rest); |
free(rest); |
623 |
return 0; |
return; |
624 |
} |
} |
625 |
|
|
626 |
stack_read(env, rest); |
stack_read(env, rest); |
627 |
|
|
628 |
free(rest); |
free(rest); |
|
return 1; |
|
629 |
} |
} |
630 |
|
|
631 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
634 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
635 |
|
|
636 |
/* Is top element a list? */ |
/* Is top element a list? */ |
637 |
if(env->head==NULL || env->head->item->type!=list) { |
if(env->head==NULL) { |
638 |
printerr("Stack empty or not a list"); |
printerr("Too Few Arguments"); |
639 |
|
env->err=1; |
640 |
return; |
return; |
641 |
} |
} |
642 |
|
if(env->head->item->type!=list) { |
643 |
|
printerr("Bad Argument Type"); |
644 |
|
env->err=2; |
645 |
|
return; |
646 |
|
} |
647 |
|
|
648 |
|
rev(env); |
649 |
|
|
650 |
|
if(env->err) |
651 |
|
return; |
652 |
|
|
653 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
654 |
new_head= temp= env->head->item->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
673 |
int result; |
int result; |
674 |
|
|
675 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
676 |
printerr("Not enough elements to compare"); |
printerr("Too Few Arguments"); |
677 |
|
env->err=1; |
678 |
return; |
return; |
679 |
} |
} |
680 |
|
|
692 |
{ |
{ |
693 |
int val; |
int val; |
694 |
|
|
695 |
if((env->head)==NULL || env->head->item->type!=integer) { |
if((env->head)==NULL) { |
696 |
printerr("Stack empty or element is not a integer"); |
printerr("Too Few Arguments"); |
697 |
|
env->err=1; |
698 |
|
return; |
699 |
|
} |
700 |
|
|
701 |
|
if(env->head->item->type!=integer) { |
702 |
|
printerr("Bad Argument Type"); |
703 |
|
env->err=2; |
704 |
return; |
return; |
705 |
} |
} |
706 |
|
|
723 |
symbol *sym; |
symbol *sym; |
724 |
|
|
725 |
/* 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 */ |
726 |
if(env->head==NULL || env->head->next==NULL |
if(env->head==NULL || env->head->next==NULL) { |
727 |
|| env->head->item->type!=symb) { |
printerr("Too Few Arguments"); |
728 |
printerr("Define what?"); |
env->err=1; |
729 |
|
return; |
730 |
|
} |
731 |
|
|
732 |
|
if(env->head->item->type!=symb) { |
733 |
|
printerr("Bad Argument Type"); |
734 |
|
env->err=2; |
735 |
return; |
return; |
736 |
} |
} |
737 |
|
|
777 |
} |
} |
778 |
} |
} |
779 |
|
|
780 |
|
/* Forgets a symbol (remove it from the hash table) */ |
781 |
|
extern void forget(environment *env) |
782 |
|
{ |
783 |
|
char* sym_id; |
784 |
|
stackitem *stack_head= env->head; |
785 |
|
symbol **hash_entry, *temp; |
786 |
|
|
787 |
|
if(stack_head==NULL) { |
788 |
|
printerr("Too Few Arguments"); |
789 |
|
env->err=1; |
790 |
|
return; |
791 |
|
} |
792 |
|
|
793 |
|
if(stack_head->item->type!=symb) { |
794 |
|
printerr("Bad Argument Type"); |
795 |
|
env->err=2; |
796 |
|
return; |
797 |
|
} |
798 |
|
|
799 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
800 |
|
toss(env); |
801 |
|
|
802 |
|
hash_entry= hash(env->symbols, sym_id); |
803 |
|
temp= *hash_entry; |
804 |
|
*hash_entry= (*hash_entry)->next; |
805 |
|
|
806 |
|
if(temp->val!=NULL) { |
807 |
|
free_val(temp->val); |
808 |
|
} |
809 |
|
free(temp->id); |
810 |
|
free(temp); |
811 |
|
} |
812 |
|
|
813 |
|
/* Returns the current error number to the stack */ |
814 |
|
extern void errn(environment *env){ |
815 |
|
push_int(&(env->head), env->err); |
816 |
|
} |
817 |
|
|
818 |
int main() |
int main() |
819 |
{ |
{ |
820 |
environment myenv; |
environment myenv; |
826 |
|
|
827 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(fgets(in_string, 100, stdin) != NULL) { |
828 |
stack_read(&myenv, in_string); |
stack_read(&myenv, in_string); |
829 |
|
if(myenv.err) { |
830 |
|
printf("(error %d) ", myenv.err); |
831 |
|
myenv.err=0; |
832 |
|
} |
833 |
printf("okidok\n "); |
printf("okidok\n "); |
834 |
} |
} |
835 |
|
quit(&myenv); |
836 |
exit(EXIT_SUCCESS); |
return EXIT_FAILURE; |
837 |
} |
} |