8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
10 |
#include <string.h> |
#include <string.h> |
11 |
|
/* mtrace, muntrace */ |
12 |
|
#include <mcheck.h> |
13 |
|
|
14 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 2048 |
15 |
|
|
16 |
/* First, define some types. */ |
/* First, define some types. */ |
17 |
|
|
60 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
61 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
62 |
int err; /* Error flag */ |
int err; /* Error flag */ |
63 |
int non_eval_flag; |
char *in_string; /* Input pending to be read */ |
64 |
|
char *free_string; /* Free this string when all input is |
65 |
|
read from in_string */ |
66 |
} environment; |
} environment; |
67 |
|
|
68 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
74 |
{ |
{ |
75 |
int i; |
int i; |
76 |
|
|
77 |
|
env->in_string= NULL; |
78 |
env->err= 0; |
env->err= 0; |
|
env->non_eval_flag= 0; |
|
79 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
80 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
81 |
} |
} |
102 |
free(item); /* free the stackitem */ |
free(item); /* free the stackitem */ |
103 |
item=temp; /* go to next stackitem */ |
item=temp; /* go to next stackitem */ |
104 |
} |
} |
|
free(val); /* Free the actual list value */ |
|
105 |
break; |
break; |
106 |
case integer: |
case integer: |
107 |
case func: |
case func: |
108 |
|
break; |
109 |
case symb: |
case symb: |
110 |
|
free(((symbol*)(val->content.ptr))->id); |
111 |
|
if(((symbol*)(val->content.ptr))->val!=NULL) |
112 |
|
free_val(((symbol*)(val->content.ptr))->val); |
113 |
|
free(val->content.ptr); |
114 |
break; |
break; |
115 |
} |
} |
116 |
|
free(val); /* Free the actual value structure */ |
117 |
} |
} |
118 |
} |
} |
119 |
|
|
162 |
} |
} |
163 |
} |
} |
164 |
|
|
|
/* Generic push function. */ |
|
|
void push(stackitem** stack_head, stackitem* in_item) |
|
|
{ |
|
|
in_item->next= *stack_head; |
|
|
*stack_head= in_item; |
|
|
} |
|
|
|
|
165 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
166 |
void push_val(stackitem **stack_head, value *val) |
void push_val(environment *env, value *val) |
167 |
{ |
{ |
168 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
169 |
new_item->item= val; |
new_item->item= val; |
170 |
val->refcount++; |
val->refcount++; |
171 |
push(stack_head, new_item); |
new_item->next= env->head; |
172 |
|
env->head= new_item; |
173 |
} |
} |
174 |
|
|
175 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
176 |
void push_int(stackitem **stack_head, int in_val) |
void push_int(environment *env, int in_val) |
177 |
{ |
{ |
178 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item= new_value; |
|
179 |
|
|
180 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
181 |
new_value->type= integer; |
new_value->type= integer; |
182 |
new_value->refcount=1; |
new_value->refcount= 0; |
183 |
|
|
184 |
push(stack_head, new_item); |
push_val(env, new_value); |
185 |
} |
} |
186 |
|
|
187 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
188 |
void push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
189 |
{ |
{ |
190 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item=new_value; |
|
191 |
|
|
192 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
193 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
194 |
new_value->type= string; |
new_value->type= string; |
195 |
new_value->refcount=1; |
new_value->refcount= 0; |
196 |
|
|
197 |
push(stack_head, new_item); |
push_val(env, new_value); |
198 |
} |
} |
199 |
|
|
200 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
218 |
} |
} |
219 |
|
|
220 |
extern void mangle(environment *env){ |
extern void mangle(environment *env){ |
|
value *new_value; |
|
221 |
char *new_string; |
char *new_string; |
222 |
|
|
223 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
237 |
toss(env); |
toss(env); |
238 |
if(env->err) return; |
if(env->err) return; |
239 |
|
|
240 |
new_value= malloc(sizeof(value)); |
push_cstring(env, new_string); |
|
new_value->content.ptr= new_string; |
|
|
new_value->type= string; |
|
|
new_value->refcount=1; |
|
|
|
|
|
push_val(&(env->head), new_value); |
|
241 |
} |
} |
242 |
|
|
243 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
244 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
245 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
246 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
247 |
/* ...which might point to... */ |
/* ...which might point to... */ |
248 |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
255 |
const char *dlerr; /* Dynamic linker error */ |
const char *dlerr; /* Dynamic linker error */ |
256 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
257 |
|
|
|
/* Create a new stack item containing a new value */ |
|
|
new_item= malloc(sizeof(stackitem)); |
|
258 |
new_value= malloc(sizeof(value)); |
new_value= malloc(sizeof(value)); |
|
new_item->item=new_value; |
|
259 |
|
|
260 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
261 |
new_value->type= symb; |
new_value->type= symb; |
299 |
new_fvalue->refcount= 1; |
new_fvalue->refcount= 1; |
300 |
} |
} |
301 |
} |
} |
302 |
push(&(env->head), new_item); |
push_val(env, new_value); |
303 |
} |
} |
304 |
|
|
305 |
/* Print newline. */ |
/* Print newline. */ |
339 |
} |
} |
340 |
|
|
341 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
342 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
343 |
{ |
{ |
344 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
345 |
case integer: |
case integer: |
346 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
347 |
break; |
break; |
348 |
case string: |
case string: |
349 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
350 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
351 |
|
else |
352 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
353 |
break; |
break; |
354 |
case symb: |
case symb: |
355 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
362 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
363 |
printf("[ "); |
printf("[ "); |
364 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
365 |
print_h(stack_head); |
print_h(stack_head, noquote); |
366 |
printf(" "); |
printf(" "); |
367 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
368 |
} |
} |
377 |
env->err=1; |
env->err=1; |
378 |
return; |
return; |
379 |
} |
} |
380 |
print_h(env->head); |
print_h(env->head, 0); |
381 |
|
nl(); |
382 |
} |
} |
383 |
|
|
384 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
389 |
toss(env); |
toss(env); |
390 |
} |
} |
391 |
|
|
392 |
|
extern void princ_(environment *env) { |
393 |
|
if(env->head==NULL) { |
394 |
|
printerr("Too Few Arguments"); |
395 |
|
env->err=1; |
396 |
|
return; |
397 |
|
} |
398 |
|
print_h(env->head, 1); |
399 |
|
} |
400 |
|
|
401 |
|
/* Prints the top element of the stack and then discards it. */ |
402 |
|
extern void princ(environment *env) |
403 |
|
{ |
404 |
|
princ_(env); |
405 |
|
if(env->err) return; |
406 |
|
toss(env); |
407 |
|
} |
408 |
|
|
409 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
410 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
411 |
{ |
{ |
412 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
413 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
414 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
415 |
print_h(stack_head); |
print_h(stack_head, 0); |
416 |
nl(); |
nl(); |
417 |
} |
} |
418 |
|
|
420 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
421 |
{ |
{ |
422 |
if(env->head == NULL) { |
if(env->head == NULL) { |
423 |
|
printf("Stack Empty\n"); |
424 |
return; |
return; |
425 |
} |
} |
426 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
427 |
} |
} |
428 |
|
|
429 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
484 |
} |
} |
485 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
486 |
if(env->err) return; |
if(env->err) return; |
487 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(env, val); /* Return its bound value */ |
488 |
} |
} |
489 |
|
|
|
void stack_read(environment*, char*); |
|
|
|
|
490 |
/* 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 |
491 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
492 |
function. */ |
function. */ |
495 |
funcp in_func; |
funcp in_func; |
496 |
value* temp_val; |
value* temp_val; |
497 |
stackitem* iterator; |
stackitem* iterator; |
498 |
char* temp_string; |
|
499 |
|
eval_start: |
500 |
|
|
501 |
if(env->head==NULL) { |
if(env->head==NULL) { |
502 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
510 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
511 |
if(env->err) return; |
if(env->err) return; |
512 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
513 |
return eval(env); /* evaluate the value */ |
goto eval_start; |
514 |
} |
} |
515 |
return; |
return; |
516 |
|
|
529 |
if(env->err) return; |
if(env->err) return; |
530 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
531 |
while(iterator!=NULL) { |
while(iterator!=NULL) { |
532 |
push_val(&(env->head), iterator->item); |
push_val(env, iterator->item); |
533 |
if(env->head->item->type==symb |
if(env->head->item->type==symb |
534 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
535 |
toss(env); |
toss(env); |
536 |
if(env->err) return; |
if(env->err) return; |
537 |
if(iterator->next == NULL){ |
if(iterator->next == NULL){ |
538 |
free_val(temp_val); |
free_val(temp_val); |
539 |
return eval(env); |
goto eval_start; |
540 |
} |
} |
541 |
eval(env); |
eval(env); |
542 |
if(env->err) return; |
if(env->err) return; |
546 |
free_val(temp_val); |
free_val(temp_val); |
547 |
return; |
return; |
548 |
|
|
549 |
/* If it's a string */ |
default: |
|
case string: |
|
|
temp_val= env->head->item; |
|
|
env->head->item->refcount++; |
|
|
toss(env); |
|
|
if(env->err) return; |
|
|
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
|
|
strcpy(temp_string, "[ "); |
|
|
strcpy(temp_string+2, (char*)temp_val->content.ptr); |
|
|
free_val(temp_val); |
|
|
strcat(temp_string, " ]"); |
|
|
stack_read(env, temp_string); |
|
|
free(temp_string); |
|
|
return eval(env); |
|
|
|
|
|
case integer: |
|
550 |
return; |
return; |
551 |
} |
} |
552 |
} |
} |
581 |
/* Make a list. */ |
/* Make a list. */ |
582 |
extern void pack(environment *env) |
extern void pack(environment *env) |
583 |
{ |
{ |
|
void* delimiter; |
|
584 |
stackitem *iterator, *temp; |
stackitem *iterator, *temp; |
585 |
value *pack; |
value *pack; |
586 |
|
|
|
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
|
|
toss(env); |
|
|
|
|
587 |
iterator= env->head; |
iterator= env->head; |
588 |
|
|
589 |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
if(iterator==NULL |
590 |
|
|| (iterator->item->type==symb |
591 |
|
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
592 |
temp= NULL; |
temp= NULL; |
593 |
toss(env); |
toss(env); |
594 |
} else { |
} else { |
595 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
596 |
while(iterator->next!=NULL |
while(iterator->next!=NULL |
597 |
&& iterator->next->item->content.ptr!=delimiter) |
&& (iterator->next->item->type!=symb |
598 |
|
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
599 |
iterator= iterator->next; |
iterator= iterator->next; |
600 |
|
|
601 |
/* Extract list */ |
/* Extract list */ |
611 |
pack= malloc(sizeof(value)); |
pack= malloc(sizeof(value)); |
612 |
pack->type= list; |
pack->type= list; |
613 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
614 |
pack->refcount= 1; |
pack->refcount= 0; |
615 |
|
|
616 |
temp= malloc(sizeof(stackitem)); |
push_val(env, pack); |
|
temp->item= pack; |
|
|
|
|
|
push(&(env->head), temp); |
|
617 |
rev(env); |
rev(env); |
618 |
} |
} |
619 |
|
|
|
/* Parse input. */ |
|
|
void stack_read(environment *env, char *in_line) |
|
|
{ |
|
|
char *temp, *rest; |
|
|
int itemp; |
|
|
size_t inlength= strlen(in_line)+1; |
|
|
int convert= 0; |
|
|
|
|
|
temp= malloc(inlength); |
|
|
rest= malloc(inlength); |
|
|
|
|
|
do { |
|
|
/* If comment */ |
|
|
if((convert= sscanf(in_line, "#%[^\n\r]", rest))) { |
|
|
free(temp); free(rest); |
|
|
return; |
|
|
} |
|
|
|
|
|
/* If string */ |
|
|
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
|
|
push_cstring(&(env->head), temp); |
|
|
break; |
|
|
} |
|
|
/* If integer */ |
|
|
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
|
|
push_int(&(env->head), itemp); |
|
|
break; |
|
|
} |
|
|
/* Escape ';' with '\' */ |
|
|
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
|
|
temp[1]= '\0'; |
|
|
push_sym(env, temp); |
|
|
break; |
|
|
} |
|
|
/* If symbol */ |
|
|
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
|
|
push_sym(env, temp); |
|
|
break; |
|
|
} |
|
|
/* If single char */ |
|
|
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
|
|
if(*temp==';') { |
|
|
if(!env->non_eval_flag) { |
|
|
eval(env); /* Evaluate top element */ |
|
|
break; |
|
|
} |
|
|
|
|
|
push_sym(env, ";"); |
|
|
break; |
|
|
} |
|
|
|
|
|
if(*temp==']') { |
|
|
push_sym(env, "["); |
|
|
pack(env); |
|
|
if(env->non_eval_flag) |
|
|
env->non_eval_flag--; |
|
|
break; |
|
|
} |
|
|
|
|
|
if(*temp=='[') { |
|
|
push_sym(env, "["); |
|
|
env->non_eval_flag++; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} while(0); |
|
|
|
|
|
free(temp); |
|
|
|
|
|
if(convert<2) { |
|
|
free(rest); |
|
|
return; |
|
|
} |
|
|
|
|
|
stack_read(env, rest); |
|
|
|
|
|
free(rest); |
|
|
} |
|
|
|
|
620 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
621 |
extern void expand(environment *env) |
extern void expand(environment *env) |
622 |
{ |
{ |
673 |
result= (left==right); |
result= (left==right); |
674 |
|
|
675 |
toss(env); toss(env); |
toss(env); toss(env); |
676 |
push_int(&(env->head), result); |
push_int(env, result); |
677 |
} |
} |
678 |
|
|
679 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
695 |
|
|
696 |
val= env->head->item->content.val; |
val= env->head->item->content.val; |
697 |
toss(env); |
toss(env); |
698 |
push_int(&(env->head), !val); |
push_int(env, !val); |
699 |
} |
} |
700 |
|
|
701 |
/* Compares the two top elements on the stack and return 0 if they're the |
/* Compares the two top elements on the stack and return 0 if they're the |
738 |
toss(env); toss(env); |
toss(env); toss(env); |
739 |
} |
} |
740 |
|
|
741 |
|
extern void clear(environment *); |
742 |
|
void forget_sym(symbol **); |
743 |
|
extern void words(environment *); |
744 |
|
|
745 |
/* Quit stack. */ |
/* Quit stack. */ |
746 |
extern void quit(environment *env) |
extern void quit(environment *env) |
747 |
{ |
{ |
748 |
|
long i; |
749 |
|
|
750 |
|
clear(env); |
751 |
|
|
752 |
|
words(env); |
753 |
|
|
754 |
|
if (env->err) return; |
755 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
756 |
|
while(env->symbols[i]!= NULL) { |
757 |
|
forget_sym(&(env->symbols[i])); |
758 |
|
} |
759 |
|
env->symbols[i]= NULL; |
760 |
|
} |
761 |
|
|
762 |
|
words(env); |
763 |
|
|
764 |
|
if(env->free_string!=NULL) |
765 |
|
free(env->free_string); |
766 |
|
|
767 |
|
muntrace(); |
768 |
|
|
769 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
770 |
} |
} |
771 |
|
|
791 |
} |
} |
792 |
} |
} |
793 |
|
|
794 |
|
/* Internal forget function */ |
795 |
|
void forget_sym(symbol **hash_entry) { |
796 |
|
symbol *temp; |
797 |
|
|
798 |
|
temp= *hash_entry; |
799 |
|
*hash_entry= (*hash_entry)->next; |
800 |
|
|
801 |
|
if(temp->val!=NULL) { |
802 |
|
free_val(temp->val); |
803 |
|
} |
804 |
|
free(temp->id); |
805 |
|
free(temp); |
806 |
|
} |
807 |
|
|
808 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
809 |
extern void forget(environment *env) |
extern void forget(environment *env) |
810 |
{ |
{ |
811 |
char* sym_id; |
char* sym_id; |
812 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
813 |
|
|
814 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
815 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
826 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
827 |
toss(env); |
toss(env); |
828 |
|
|
829 |
hash_entry= hash(env->symbols, sym_id); |
return forget_sym(hash(env->symbols, sym_id)); |
|
temp= *hash_entry; |
|
|
*hash_entry= (*hash_entry)->next; |
|
|
|
|
|
if(temp->val!=NULL) { |
|
|
free_val(temp->val); |
|
|
} |
|
|
free(temp->id); |
|
|
free(temp); |
|
830 |
} |
} |
831 |
|
|
832 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
833 |
extern void errn(environment *env){ |
extern void errn(environment *env){ |
834 |
push_int(&(env->head), env->err); |
push_int(env, env->err); |
835 |
} |
} |
836 |
|
|
837 |
|
extern void read(environment*); |
838 |
|
|
839 |
int main() |
int main() |
840 |
{ |
{ |
841 |
environment myenv; |
environment myenv; |
|
char in_string[100]; |
|
842 |
|
|
843 |
init_env(&myenv); |
mtrace(); |
844 |
|
|
845 |
printf("okidok\n "); |
init_env(&myenv); |
846 |
|
|
847 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(1) { |
848 |
stack_read(&myenv, in_string); |
if(myenv.in_string==NULL) { |
849 |
|
nl(); |
850 |
|
printstack(&myenv); |
851 |
|
printf("> "); |
852 |
|
} |
853 |
|
read(&myenv); |
854 |
if(myenv.err) { |
if(myenv.err) { |
855 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
856 |
myenv.err=0; |
myenv.err=0; |
857 |
|
} else if(myenv.head!=NULL |
858 |
|
&& myenv.head->item->type==symb |
859 |
|
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
860 |
|
toss(&myenv); /* No error check in main */ |
861 |
|
eval(&myenv); |
862 |
} |
} |
|
printf("okidok\n "); |
|
863 |
} |
} |
864 |
quit(&myenv); |
quit(&myenv); |
865 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
891 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
892 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
893 |
free_val(a_val); free_val(b_val); |
free_val(a_val); free_val(b_val); |
894 |
push_cstring(&(env->head), new_string); |
push_cstring(env, new_string); |
895 |
free(new_string); |
free(new_string); |
896 |
return; |
return; |
897 |
} |
} |
905 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
906 |
toss(env); |
toss(env); |
907 |
if(env->err) return; |
if(env->err) return; |
908 |
b=env->head->item->content.val; |
if(env->head->item->refcount == 1) |
909 |
toss(env); |
env->head->item->content.val += a; |
910 |
if(env->err) return; |
else { |
911 |
push_int(&(env->head), a+b); |
b=env->head->item->content.val; |
912 |
|
toss(env); |
913 |
|
if(env->err) return; |
914 |
|
push_int(env, a+b); |
915 |
|
} |
916 |
} |
} |
917 |
|
|
918 |
/* - */ |
/* - */ |
919 |
extern void sx_2d(environment *env) { |
extern void sx_2d(environment *env) { |
920 |
int a; |
int a, b; |
921 |
|
|
922 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
923 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
934 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
935 |
toss(env); |
toss(env); |
936 |
if(env->err) return; |
if(env->err) return; |
937 |
env->head->item->content.val -= a; |
if(env->head->item->refcount == 1) |
938 |
|
env->head->item->content.val -= a; |
939 |
|
else { |
940 |
|
b=env->head->item->content.val; |
941 |
|
toss(env); |
942 |
|
if(env->err) return; |
943 |
|
push_int(env, b-a); |
944 |
|
} |
945 |
} |
} |
946 |
|
|
947 |
/* > */ |
/* > */ |
948 |
extern void sx_3e(environment *env) { |
extern void sx_3e(environment *env) { |
949 |
int a; |
int a, b; |
950 |
|
|
951 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
952 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
963 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
964 |
toss(env); |
toss(env); |
965 |
if(env->err) return; |
if(env->err) return; |
966 |
env->head->item->content.val = (env->head->item->content.val > a); |
if(env->head->item->refcount == 1) |
967 |
|
env->head->item->content.val = (env->head->item->content.val > a); |
968 |
|
else { |
969 |
|
b=env->head->item->content.val; |
970 |
|
toss(env); |
971 |
|
if(env->err) return; |
972 |
|
push_int(env, b>a); |
973 |
|
} |
974 |
} |
} |
975 |
|
|
976 |
/* Return copy of a value */ |
/* Return copy of a value */ |
1025 |
env->err=1; |
env->err=1; |
1026 |
return; |
return; |
1027 |
} |
} |
1028 |
push_val(&(env->head), copy_val(env->head->item)); |
push_val(env, copy_val(env->head->item)); |
1029 |
} |
} |
1030 |
|
|
1031 |
/* "if", If-Then */ |
/* "if", If-Then */ |
1099 |
extern void sx_7768696c65(environment *env) { |
extern void sx_7768696c65(environment *env) { |
1100 |
|
|
1101 |
int truth; |
int truth; |
1102 |
|
value *loop, *test; |
1103 |
|
|
1104 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
1105 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1107 |
return; |
return; |
1108 |
} |
} |
1109 |
|
|
1110 |
|
loop= env->head->item; |
1111 |
|
loop->refcount++; |
1112 |
|
toss(env); if(env->err) return; |
1113 |
|
|
1114 |
|
test= env->head->item; |
1115 |
|
test->refcount++; |
1116 |
|
toss(env); if(env->err) return; |
1117 |
|
|
1118 |
do { |
do { |
1119 |
swap(env); if(env->err) return; |
push_val(env, test); |
1120 |
dup(env); if(env->err) return; |
eval(env); |
|
eval(env); if(env->err) return; |
|
1121 |
|
|
1122 |
if(env->head->item->type != integer) { |
if(env->head->item->type != integer) { |
1123 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1126 |
} |
} |
1127 |
|
|
1128 |
truth= env->head->item->content.val; |
truth= env->head->item->content.val; |
|
|
|
1129 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
|
swap(env); if(env->err) return; |
|
1130 |
|
|
1131 |
if(truth) { |
if(truth) { |
1132 |
dup(env); |
push_val(env, loop); |
1133 |
eval(env); |
eval(env); |
1134 |
} else { |
} else { |
1135 |
toss(env); |
toss(env); |
|
toss(env); |
|
1136 |
} |
} |
1137 |
|
|
1138 |
} while(truth); |
} while(truth); |
1139 |
|
|
1140 |
|
free_val(test); |
1141 |
|
free_val(loop); |
1142 |
|
} |
1143 |
|
|
1144 |
|
/* For-loop */ |
1145 |
|
extern void sx_666f72(environment *env) { |
1146 |
|
|
1147 |
|
value *loop, *foo; |
1148 |
|
stackitem *iterator; |
1149 |
|
|
1150 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1151 |
|
printerr("Too Few Arguments"); |
1152 |
|
env->err=1; |
1153 |
|
return; |
1154 |
|
} |
1155 |
|
|
1156 |
|
if(env->head->next->item->type != list) { |
1157 |
|
printerr("Bad Argument Type"); |
1158 |
|
env->err=2; |
1159 |
|
return; |
1160 |
|
} |
1161 |
|
|
1162 |
|
loop= env->head->item; |
1163 |
|
loop->refcount++; |
1164 |
|
toss(env); if(env->err) return; |
1165 |
|
|
1166 |
|
foo= env->head->item; |
1167 |
|
foo->refcount++; |
1168 |
|
toss(env); if(env->err) return; |
1169 |
|
|
1170 |
|
iterator= foo->content.ptr; |
1171 |
|
|
1172 |
|
while(iterator!=NULL) { |
1173 |
|
push_val(env, iterator->item); |
1174 |
|
push_val(env, loop); |
1175 |
|
eval(env); if(env->err) return; |
1176 |
|
iterator= iterator->next; |
1177 |
|
} |
1178 |
|
|
1179 |
|
free_val(loop); |
1180 |
|
free_val(foo); |
1181 |
|
} |
1182 |
|
|
1183 |
|
/* 'to' */ |
1184 |
|
extern void to(environment *env) { |
1185 |
|
int i, start, ending; |
1186 |
|
stackitem *temp_head; |
1187 |
|
value *temp_val; |
1188 |
|
|
1189 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1190 |
|
printerr("Too Few Arguments"); |
1191 |
|
env->err=1; |
1192 |
|
return; |
1193 |
|
} |
1194 |
|
|
1195 |
|
if(env->head->item->type!=integer |
1196 |
|
|| env->head->next->item->type!=integer) { |
1197 |
|
printerr("Bad Argument Type"); |
1198 |
|
env->err=2; |
1199 |
|
return; |
1200 |
|
} |
1201 |
|
|
1202 |
|
ending= env->head->item->content.val; |
1203 |
|
toss(env); if(env->err) return; |
1204 |
|
start= env->head->item->content.val; |
1205 |
|
toss(env); if(env->err) return; |
1206 |
|
|
1207 |
|
temp_head= env->head; |
1208 |
|
env->head= NULL; |
1209 |
|
|
1210 |
|
if(ending>=start) { |
1211 |
|
for(i= ending; i>=start; i--) |
1212 |
|
push_int(env, i); |
1213 |
|
} else { |
1214 |
|
for(i= ending; i<=start; i++) |
1215 |
|
push_int(env, i); |
1216 |
|
} |
1217 |
|
|
1218 |
|
temp_val= malloc(sizeof(value)); |
1219 |
|
temp_val->content.ptr= env->head; |
1220 |
|
temp_val->refcount= 0; |
1221 |
|
temp_val->type= list; |
1222 |
|
env->head= temp_head; |
1223 |
|
push_val(env, temp_val); |
1224 |
|
} |
1225 |
|
|
1226 |
|
/* Read a string */ |
1227 |
|
extern void readline(environment *env) { |
1228 |
|
char in_string[101]; |
1229 |
|
|
1230 |
|
fgets(in_string, 100, stdin); |
1231 |
|
push_cstring(env, in_string); |
1232 |
|
} |
1233 |
|
|
1234 |
|
/* Read a value and place on stack */ |
1235 |
|
extern void read(environment *env) { |
1236 |
|
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1237 |
|
const char strform[]= "\"%[^\"]\"%n"; |
1238 |
|
const char intform[]= "%i%n"; |
1239 |
|
const char blankform[]= "%*[ \t]%n"; |
1240 |
|
const char ebrackform[]= "%*1[]]%n"; |
1241 |
|
const char semicform[]= "%*1[;]%n"; |
1242 |
|
const char bbrackform[]= "%*1[[]%n"; |
1243 |
|
|
1244 |
|
int itemp, readlength= -1; |
1245 |
|
static int depth= 0; |
1246 |
|
char *match; |
1247 |
|
size_t inlength; |
1248 |
|
|
1249 |
|
if(env->in_string==NULL) { |
1250 |
|
if(depth > 0) { |
1251 |
|
printf("]> "); |
1252 |
|
} |
1253 |
|
readline(env); if(env->err) return; |
1254 |
|
|
1255 |
|
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1256 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
1257 |
|
strcpy(env->in_string, env->head->item->content.ptr); |
1258 |
|
toss(env); if(env->err) return; |
1259 |
|
} |
1260 |
|
|
1261 |
|
inlength= strlen(env->in_string)+1; |
1262 |
|
match= malloc(inlength); |
1263 |
|
|
1264 |
|
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1265 |
|
&& readlength != -1) { |
1266 |
|
; |
1267 |
|
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
1268 |
|
&& readlength != -1) { |
1269 |
|
push_int(env, itemp); |
1270 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1271 |
|
&& readlength != -1) { |
1272 |
|
push_cstring(env, match); |
1273 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
1274 |
|
&& readlength != -1) { |
1275 |
|
push_sym(env, match); |
1276 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
1277 |
|
&& readlength != -1) { |
1278 |
|
pack(env); if(env->err) return; |
1279 |
|
if(depth != 0) depth--; |
1280 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
1281 |
|
&& readlength != -1) { |
1282 |
|
push_sym(env, ";"); |
1283 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
1284 |
|
&& readlength != -1) { |
1285 |
|
push_sym(env, "["); |
1286 |
|
depth++; |
1287 |
|
} else { |
1288 |
|
free(env->free_string); |
1289 |
|
env->in_string = env->free_string = NULL; |
1290 |
|
} |
1291 |
|
if ( env->in_string != NULL) { |
1292 |
|
env->in_string += readlength; |
1293 |
|
} |
1294 |
|
|
1295 |
|
free(match); |
1296 |
|
|
1297 |
|
if(depth) |
1298 |
|
return read(env); |
1299 |
} |
} |