9 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
10 |
#include <string.h> |
#include <string.h> |
11 |
|
|
12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 2048 |
13 |
|
|
14 |
/* First, define some types. */ |
/* First, define some types. */ |
15 |
|
|
59 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
60 |
int err; /* Error flag */ |
int err; /* Error flag */ |
61 |
int non_eval_flag; |
int non_eval_flag; |
62 |
|
char *in_string; /* Input pending to be read */ |
63 |
|
char *free_string; /* Free this string when all input is |
64 |
|
read from in_string */ |
65 |
} environment; |
} environment; |
66 |
|
|
67 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
73 |
{ |
{ |
74 |
int i; |
int i; |
75 |
|
|
76 |
|
env->in_string= NULL; |
77 |
env->err= 0; |
env->err= 0; |
78 |
env->non_eval_flag= 0; |
env->non_eval_flag= 0; |
79 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
|
/* Generic push function. */ |
|
|
void push(stackitem** stack_head, stackitem* in_item) |
|
|
{ |
|
|
in_item->next= *stack_head; |
|
|
*stack_head= in_item; |
|
|
} |
|
|
|
|
160 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
161 |
void push_val(stackitem **stack_head, value *val) |
void push_val(environment *env, value *val) |
162 |
{ |
{ |
163 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
164 |
new_item->item= val; |
new_item->item= val; |
165 |
val->refcount++; |
val->refcount++; |
166 |
push(stack_head, new_item); |
new_item->next= env->head; |
167 |
|
env->head= new_item; |
168 |
} |
} |
169 |
|
|
170 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
171 |
void push_int(stackitem **stack_head, int in_val) |
void push_int(environment *env, int in_val) |
172 |
{ |
{ |
173 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item= new_value; |
|
174 |
|
|
175 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
176 |
new_value->type= integer; |
new_value->type= integer; |
177 |
new_value->refcount=1; |
new_value->refcount=1; |
178 |
|
|
179 |
push(stack_head, new_item); |
push_val(env, new_value); |
180 |
} |
} |
181 |
|
|
182 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
183 |
void push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
184 |
{ |
{ |
185 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
|
|
new_item->item=new_value; |
|
186 |
|
|
187 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
188 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
189 |
new_value->type= string; |
new_value->type= string; |
190 |
new_value->refcount=1; |
new_value->refcount=1; |
191 |
|
|
192 |
push(stack_head, new_item); |
push_val(env, new_value); |
193 |
} |
} |
194 |
|
|
195 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
238 |
new_value->type= string; |
new_value->type= string; |
239 |
new_value->refcount=1; |
new_value->refcount=1; |
240 |
|
|
241 |
push_val(&(env->head), new_value); |
push_val(env, new_value); |
242 |
} |
} |
243 |
|
|
244 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
245 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
246 |
{ |
{ |
|
stackitem *new_item; /* The new stack item */ |
|
|
/* ...which will contain... */ |
|
247 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
248 |
/* ...which might point to... */ |
/* ...which might point to... */ |
249 |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
256 |
const char *dlerr; /* Dynamic linker error */ |
const char *dlerr; /* Dynamic linker error */ |
257 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
258 |
|
|
|
/* Create a new stack item containing a new value */ |
|
|
new_item= malloc(sizeof(stackitem)); |
|
259 |
new_value= malloc(sizeof(value)); |
new_value= malloc(sizeof(value)); |
|
new_item->item=new_value; |
|
260 |
|
|
261 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
262 |
new_value->type= symb; |
new_value->type= symb; |
300 |
new_fvalue->refcount= 1; |
new_fvalue->refcount= 1; |
301 |
} |
} |
302 |
} |
} |
303 |
push(&(env->head), new_item); |
push_val(env, new_value); |
304 |
} |
} |
305 |
|
|
306 |
/* Print newline. */ |
/* Print newline. */ |
336 |
case list: |
case list: |
337 |
push_sym(env, "list"); |
push_sym(env, "list"); |
338 |
break; |
break; |
|
default: |
|
|
push_sym(env, "unknown"); |
|
|
break; |
|
339 |
} |
} |
340 |
} |
} |
341 |
|
|
342 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
343 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
344 |
{ |
{ |
345 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
346 |
case integer: |
case integer: |
347 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
348 |
break; |
break; |
349 |
case string: |
case string: |
350 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
351 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
352 |
|
else |
353 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
354 |
break; |
break; |
355 |
case symb: |
case symb: |
356 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
363 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
364 |
printf("[ "); |
printf("[ "); |
365 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
366 |
print_h(stack_head); |
print_h(stack_head, noquote); |
367 |
printf(" "); |
printf(" "); |
368 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
369 |
} |
} |
370 |
printf("]"); |
printf("]"); |
371 |
break; |
break; |
|
default: |
|
|
printf("#<unknown %p>", (stack_head->item->content.ptr)); |
|
|
break; |
|
372 |
} |
} |
373 |
} |
} |
374 |
|
|
378 |
env->err=1; |
env->err=1; |
379 |
return; |
return; |
380 |
} |
} |
381 |
print_h(env->head); |
print_h(env->head, 0); |
382 |
|
nl(); |
383 |
} |
} |
384 |
|
|
385 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
390 |
toss(env); |
toss(env); |
391 |
} |
} |
392 |
|
|
393 |
|
extern void princ_(environment *env) { |
394 |
|
if(env->head==NULL) { |
395 |
|
printerr("Too Few Arguments"); |
396 |
|
env->err=1; |
397 |
|
return; |
398 |
|
} |
399 |
|
print_h(env->head, 1); |
400 |
|
} |
401 |
|
|
402 |
|
/* Prints the top element of the stack and then discards it. */ |
403 |
|
extern void princ(environment *env) |
404 |
|
{ |
405 |
|
princ_(env); |
406 |
|
if(env->err) return; |
407 |
|
toss(env); |
408 |
|
} |
409 |
|
|
410 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
411 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
412 |
{ |
{ |
413 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
414 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
415 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
416 |
print_h(stack_head); |
print_h(stack_head, 0); |
417 |
nl(); |
nl(); |
418 |
} |
} |
419 |
|
|
421 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
422 |
{ |
{ |
423 |
if(env->head == NULL) { |
if(env->head == NULL) { |
424 |
|
printf("Stack Empty\n"); |
425 |
return; |
return; |
426 |
} |
} |
427 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
428 |
} |
} |
429 |
|
|
430 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
485 |
} |
} |
486 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
487 |
if(env->err) return; |
if(env->err) return; |
488 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(env, val); /* Return its bound value */ |
489 |
} |
} |
490 |
|
|
|
void stack_read(environment*, char*); |
|
|
|
|
491 |
/* 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 |
492 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
493 |
function. */ |
function. */ |
496 |
funcp in_func; |
funcp in_func; |
497 |
value* temp_val; |
value* temp_val; |
498 |
stackitem* iterator; |
stackitem* iterator; |
499 |
char* temp_string; |
|
500 |
|
eval_start: |
501 |
|
|
502 |
if(env->head==NULL) { |
if(env->head==NULL) { |
503 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
511 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
512 |
if(env->err) return; |
if(env->err) return; |
513 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
514 |
eval(env); /* evaluate the value */ |
goto eval_start; |
|
return; |
|
515 |
} |
} |
516 |
break; |
return; |
517 |
|
|
518 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
519 |
case func: |
case func: |
520 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
521 |
toss(env); |
toss(env); |
522 |
if(env->err) return; |
if(env->err) return; |
523 |
(*in_func)(env); |
return (*in_func)(env); |
|
break; |
|
524 |
|
|
525 |
/* If it's a list */ |
/* If it's a list */ |
526 |
case list: |
case list: |
529 |
toss(env); |
toss(env); |
530 |
if(env->err) return; |
if(env->err) return; |
531 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
532 |
while(iterator!=NULL && iterator->item!=NULL) { |
while(iterator!=NULL) { |
533 |
push_val(&(env->head), iterator->item); |
push_val(env, iterator->item); |
534 |
if(env->head->item->type==symb |
if(env->head->item->type==symb |
535 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
536 |
toss(env); |
toss(env); |
537 |
if(env->err) return; |
if(env->err) return; |
538 |
|
if(iterator->next == NULL){ |
539 |
|
free_val(temp_val); |
540 |
|
goto eval_start; |
541 |
|
} |
542 |
eval(env); |
eval(env); |
543 |
if(env->err) return; |
if(env->err) return; |
544 |
} |
} |
545 |
iterator= iterator->next; |
iterator= iterator->next; |
546 |
} |
} |
547 |
free_val(temp_val); |
free_val(temp_val); |
548 |
break; |
return; |
|
|
|
|
/* If it's a string */ |
|
|
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, "[ "); |
|
|
strcat(temp_string, (char*)temp_val->content.ptr); |
|
|
strcat(temp_string, " ]"); |
|
|
stack_read(env, temp_string); |
|
|
eval(env); |
|
|
if(env->err) return; |
|
|
free_val(temp_val); |
|
|
free(temp_string); |
|
|
break; |
|
549 |
|
|
550 |
case integer: |
default: |
551 |
break; |
return; |
552 |
} |
} |
553 |
} |
} |
554 |
|
|
582 |
/* Make a list. */ |
/* Make a list. */ |
583 |
extern void pack(environment *env) |
extern void pack(environment *env) |
584 |
{ |
{ |
|
void* delimiter; |
|
585 |
stackitem *iterator, *temp; |
stackitem *iterator, *temp; |
586 |
value *pack; |
value *pack; |
587 |
|
|
|
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
|
|
toss(env); |
|
|
|
|
588 |
iterator= env->head; |
iterator= env->head; |
589 |
|
|
590 |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
if(iterator==NULL |
591 |
|
|| (iterator->item->type==symb |
592 |
|
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
593 |
temp= NULL; |
temp= NULL; |
594 |
toss(env); |
toss(env); |
595 |
} else { |
} else { |
596 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
597 |
while(iterator->next!=NULL |
while(iterator->next!=NULL |
598 |
&& iterator->next->item->content.ptr!=delimiter) |
&& (iterator->next->item->type!=symb |
599 |
|
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
600 |
iterator= iterator->next; |
iterator= iterator->next; |
601 |
|
|
602 |
/* Extract list */ |
/* Extract list */ |
614 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
615 |
pack->refcount= 1; |
pack->refcount= 1; |
616 |
|
|
617 |
temp= malloc(sizeof(stackitem)); |
push_val(env, pack); |
|
temp->item= pack; |
|
|
|
|
|
push(&(env->head), temp); |
|
618 |
rev(env); |
rev(env); |
619 |
} |
} |
620 |
|
|
|
/* 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); |
|
|
} |
|
|
|
|
621 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
622 |
extern void expand(environment *env) |
extern void expand(environment *env) |
623 |
{ |
{ |
674 |
result= (left==right); |
result= (left==right); |
675 |
|
|
676 |
toss(env); toss(env); |
toss(env); toss(env); |
677 |
push_int(&(env->head), result); |
push_int(env, result); |
678 |
} |
} |
679 |
|
|
680 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
696 |
|
|
697 |
val= env->head->item->content.val; |
val= env->head->item->content.val; |
698 |
toss(env); |
toss(env); |
699 |
push_int(&(env->head), !val); |
push_int(env, !val); |
700 |
} |
} |
701 |
|
|
702 |
/* 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 |
739 |
toss(env); toss(env); |
toss(env); toss(env); |
740 |
} |
} |
741 |
|
|
742 |
|
extern void clear(environment *); |
743 |
|
void forget_sym(symbol **); |
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 |
|
if (env->err) return; |
752 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
753 |
|
while(env->symbols[i]!= NULL) { |
754 |
|
forget_sym(&(env->symbols[i])); |
755 |
|
} |
756 |
|
env->symbols[i]= NULL; |
757 |
|
} |
758 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
759 |
} |
} |
760 |
|
|
780 |
} |
} |
781 |
} |
} |
782 |
|
|
783 |
|
/* Internal forget function */ |
784 |
|
void forget_sym(symbol **hash_entry) { |
785 |
|
symbol *temp; |
786 |
|
|
787 |
|
temp= *hash_entry; |
788 |
|
*hash_entry= (*hash_entry)->next; |
789 |
|
|
790 |
|
if(temp->val!=NULL) { |
791 |
|
free_val(temp->val); |
792 |
|
} |
793 |
|
free(temp->id); |
794 |
|
free(temp); |
795 |
|
} |
796 |
|
|
797 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
798 |
extern void forget(environment *env) |
extern void forget(environment *env) |
799 |
{ |
{ |
800 |
char* sym_id; |
char* sym_id; |
801 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
802 |
|
|
803 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
804 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
815 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
816 |
toss(env); |
toss(env); |
817 |
|
|
818 |
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); |
|
819 |
} |
} |
820 |
|
|
821 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
822 |
extern void errn(environment *env){ |
extern void errn(environment *env){ |
823 |
push_int(&(env->head), env->err); |
push_int(env, env->err); |
824 |
} |
} |
825 |
|
|
826 |
|
extern void read(environment*); |
827 |
|
|
828 |
int main() |
int main() |
829 |
{ |
{ |
830 |
environment myenv; |
environment myenv; |
|
char in_string[100]; |
|
831 |
|
|
832 |
init_env(&myenv); |
init_env(&myenv); |
833 |
|
|
834 |
printf("okidok\n "); |
while(1) { |
835 |
|
if(myenv.in_string==NULL) { |
836 |
while(fgets(in_string, 100, stdin) != NULL) { |
nl(); |
837 |
stack_read(&myenv, in_string); |
printstack(&myenv); |
838 |
|
printf("> "); |
839 |
|
} |
840 |
|
read(&myenv); |
841 |
if(myenv.err) { |
if(myenv.err) { |
842 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
843 |
myenv.err=0; |
myenv.err=0; |
844 |
|
} else if(myenv.head!=NULL |
845 |
|
&& myenv.head->item->type==symb |
846 |
|
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
847 |
|
toss(&myenv); /* No error check in main */ |
848 |
|
eval(&myenv); |
849 |
} |
} |
|
printf("okidok\n "); |
|
850 |
} |
} |
851 |
quit(&myenv); |
quit(&myenv); |
852 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
878 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
879 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
880 |
free_val(a_val); free_val(b_val); |
free_val(a_val); free_val(b_val); |
881 |
push_cstring(&(env->head), new_string); |
push_cstring(env, new_string); |
882 |
free(new_string); |
free(new_string); |
883 |
return; |
return; |
884 |
} |
} |
892 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
893 |
toss(env); |
toss(env); |
894 |
if(env->err) return; |
if(env->err) return; |
895 |
b=env->head->item->content.val; |
if(env->head->item->refcount == 1) |
896 |
|
env->head->item->content.val += a; |
897 |
|
else { |
898 |
|
b=env->head->item->content.val; |
899 |
|
toss(env); |
900 |
|
if(env->err) return; |
901 |
|
push_int(env, a+b); |
902 |
|
} |
903 |
|
} |
904 |
|
|
905 |
|
/* - */ |
906 |
|
extern void sx_2d(environment *env) { |
907 |
|
int a, b; |
908 |
|
|
909 |
|
if((env->head)==NULL || env->head->next==NULL) { |
910 |
|
printerr("Too Few Arguments"); |
911 |
|
env->err=1; |
912 |
|
return; |
913 |
|
} |
914 |
|
|
915 |
|
if(env->head->item->type!=integer |
916 |
|
|| env->head->next->item->type!=integer) { |
917 |
|
printerr("Bad Argument Type"); |
918 |
|
env->err=2; |
919 |
|
return; |
920 |
|
} |
921 |
|
a=env->head->item->content.val; |
922 |
|
toss(env); |
923 |
|
if(env->err) return; |
924 |
|
if(env->head->item->refcount == 1) |
925 |
|
env->head->item->content.val -= a; |
926 |
|
else { |
927 |
|
b=env->head->item->content.val; |
928 |
|
toss(env); |
929 |
|
if(env->err) return; |
930 |
|
push_int(env, b-a); |
931 |
|
} |
932 |
|
} |
933 |
|
|
934 |
|
/* > */ |
935 |
|
extern void sx_3e(environment *env) { |
936 |
|
int a, b; |
937 |
|
|
938 |
|
if((env->head)==NULL || env->head->next==NULL) { |
939 |
|
printerr("Too Few Arguments"); |
940 |
|
env->err=1; |
941 |
|
return; |
942 |
|
} |
943 |
|
|
944 |
|
if(env->head->item->type!=integer |
945 |
|
|| env->head->next->item->type!=integer) { |
946 |
|
printerr("Bad Argument Type"); |
947 |
|
env->err=2; |
948 |
|
return; |
949 |
|
} |
950 |
|
a=env->head->item->content.val; |
951 |
toss(env); |
toss(env); |
952 |
if(env->err) return; |
if(env->err) return; |
953 |
push_int(&(env->head), a+b); |
if(env->head->item->refcount == 1) |
954 |
|
env->head->item->content.val = (env->head->item->content.val > a); |
955 |
|
else { |
956 |
|
b=env->head->item->content.val; |
957 |
|
toss(env); |
958 |
|
if(env->err) return; |
959 |
|
push_int(env, b>a); |
960 |
|
} |
961 |
} |
} |
962 |
|
|
963 |
/* Return copy of a value */ |
/* Return copy of a value */ |
1012 |
env->err=1; |
env->err=1; |
1013 |
return; |
return; |
1014 |
} |
} |
1015 |
push_val(&(env->head), copy_val(env->head->item)); |
push_val(env, copy_val(env->head->item)); |
1016 |
} |
} |
1017 |
|
|
1018 |
/* If-Then */ |
/* "if", If-Then */ |
1019 |
extern void sx_6966(environment *env) { |
extern void sx_6966(environment *env) { |
1020 |
|
|
1021 |
int truth; |
int truth; |
1086 |
extern void sx_7768696c65(environment *env) { |
extern void sx_7768696c65(environment *env) { |
1087 |
|
|
1088 |
int truth; |
int truth; |
1089 |
|
value *loop, *test; |
1090 |
|
|
1091 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
1092 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1094 |
return; |
return; |
1095 |
} |
} |
1096 |
|
|
1097 |
|
loop= env->head->item; |
1098 |
|
loop->refcount++; |
1099 |
|
toss(env); if(env->err) return; |
1100 |
|
|
1101 |
|
test= env->head->item; |
1102 |
|
test->refcount++; |
1103 |
|
toss(env); if(env->err) return; |
1104 |
|
|
1105 |
do { |
do { |
1106 |
swap(env); if(env->err) return; |
push_val(env, test); |
1107 |
dup(env); if(env->err) return; |
eval(env); |
|
eval(env); if(env->err) return; |
|
1108 |
|
|
1109 |
if(env->head->item->type != integer) { |
if(env->head->item->type != integer) { |
1110 |
printerr("Bad Argument Type"); |
printerr("Bad Argument Type"); |
1113 |
} |
} |
1114 |
|
|
1115 |
truth= env->head->item->content.val; |
truth= env->head->item->content.val; |
|
|
|
1116 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
|
swap(env); if(env->err) return; |
|
1117 |
|
|
1118 |
if(truth) { |
if(truth) { |
1119 |
dup(env); |
push_val(env, loop); |
1120 |
eval(env); |
eval(env); |
1121 |
} else { |
} else { |
1122 |
toss(env); |
toss(env); |
|
toss(env); |
|
1123 |
} |
} |
1124 |
|
|
1125 |
} while(truth); |
} while(truth); |
1126 |
|
|
1127 |
|
free_val(test); |
1128 |
|
free_val(loop); |
1129 |
|
} |
1130 |
|
|
1131 |
|
/* For-loop */ |
1132 |
|
extern void sx_666f72(environment *env) { |
1133 |
|
|
1134 |
|
value *loop, *foo; |
1135 |
|
stackitem *iterator; |
1136 |
|
|
1137 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1138 |
|
printerr("Too Few Arguments"); |
1139 |
|
env->err=1; |
1140 |
|
return; |
1141 |
|
} |
1142 |
|
|
1143 |
|
if(env->head->next->item->type != list) { |
1144 |
|
printerr("Bad Argument Type"); |
1145 |
|
env->err=2; |
1146 |
|
return; |
1147 |
|
} |
1148 |
|
|
1149 |
|
loop= env->head->item; |
1150 |
|
loop->refcount++; |
1151 |
|
toss(env); if(env->err) return; |
1152 |
|
|
1153 |
|
foo= env->head->item; |
1154 |
|
foo->refcount++; |
1155 |
|
toss(env); if(env->err) return; |
1156 |
|
|
1157 |
|
iterator= foo->content.ptr; |
1158 |
|
|
1159 |
|
while(iterator!=NULL) { |
1160 |
|
push_val(env, iterator->item); |
1161 |
|
push_val(env, loop); |
1162 |
|
eval(env); if(env->err) return; |
1163 |
|
iterator= iterator->next; |
1164 |
|
} |
1165 |
|
|
1166 |
|
free_val(loop); |
1167 |
|
free_val(foo); |
1168 |
|
} |
1169 |
|
|
1170 |
|
/* 'to' */ |
1171 |
|
extern void to(environment *env) { |
1172 |
|
int i, start, ending; |
1173 |
|
stackitem *temp_head; |
1174 |
|
value *temp_val; |
1175 |
|
|
1176 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1177 |
|
printerr("Too Few Arguments"); |
1178 |
|
env->err=1; |
1179 |
|
return; |
1180 |
|
} |
1181 |
|
|
1182 |
|
if(env->head->item->type!=integer |
1183 |
|
|| env->head->next->item->type!=integer) { |
1184 |
|
printerr("Bad Argument Type"); |
1185 |
|
env->err=2; |
1186 |
|
return; |
1187 |
|
} |
1188 |
|
|
1189 |
|
ending= env->head->item->content.val; |
1190 |
|
toss(env); if(env->err) return; |
1191 |
|
start= env->head->item->content.val; |
1192 |
|
toss(env); if(env->err) return; |
1193 |
|
|
1194 |
|
temp_head= env->head; |
1195 |
|
env->head= NULL; |
1196 |
|
|
1197 |
|
if(ending>=start) { |
1198 |
|
for(i= ending; i>=start; i--) |
1199 |
|
push_int(env, i); |
1200 |
|
} else { |
1201 |
|
for(i= ending; i<=start; i++) |
1202 |
|
push_int(env, i); |
1203 |
|
} |
1204 |
|
|
1205 |
|
temp_val= malloc(sizeof(value)); |
1206 |
|
temp_val->content.ptr= env->head; |
1207 |
|
temp_val->refcount= 1; |
1208 |
|
temp_val->type= list; |
1209 |
|
env->head= temp_head; |
1210 |
|
push_val(env, temp_val); |
1211 |
|
} |
1212 |
|
|
1213 |
|
/* Read a string */ |
1214 |
|
extern void readline(environment *env) { |
1215 |
|
char in_string[101]; |
1216 |
|
|
1217 |
|
fgets(in_string, 100, stdin); |
1218 |
|
push_cstring(env, in_string); |
1219 |
|
} |
1220 |
|
|
1221 |
|
/* Read a value and place on stack */ |
1222 |
|
extern void read(environment *env) { |
1223 |
|
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1224 |
|
const char strform[]= "\"%[^\"]\"%n"; |
1225 |
|
const char intform[]= "%i%n"; |
1226 |
|
const char blankform[]= "%*[ \t]%n"; |
1227 |
|
const char ebrackform[]= "%*1[]]%n"; |
1228 |
|
const char semicform[]= "%*1[;]%n"; |
1229 |
|
const char bbrackform[]= "%*1[[]%n"; |
1230 |
|
|
1231 |
|
int itemp, readlength= -1; |
1232 |
|
static int depth= 0; |
1233 |
|
char *rest, *match; |
1234 |
|
size_t inlength; |
1235 |
|
|
1236 |
|
if(env->in_string==NULL) { |
1237 |
|
if(depth > 0) { |
1238 |
|
printf("]> "); |
1239 |
|
} |
1240 |
|
readline(env); if(env->err) return; |
1241 |
|
|
1242 |
|
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1243 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
1244 |
|
strcpy(env->in_string, env->head->item->content.ptr); |
1245 |
|
toss(env); if(env->err) return; |
1246 |
|
} |
1247 |
|
|
1248 |
|
inlength= strlen(env->in_string)+1; |
1249 |
|
match= malloc(inlength); |
1250 |
|
rest= malloc(inlength); |
1251 |
|
|
1252 |
|
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1253 |
|
&& readlength != -1) { |
1254 |
|
; |
1255 |
|
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
1256 |
|
&& readlength != -1) { |
1257 |
|
push_int(env, itemp); |
1258 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1259 |
|
&& readlength != -1) { |
1260 |
|
push_cstring(env, match); |
1261 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
1262 |
|
&& readlength != -1) { |
1263 |
|
push_sym(env, match); |
1264 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
1265 |
|
&& readlength != -1) { |
1266 |
|
pack(env); if(env->err) return; |
1267 |
|
if(depth != 0) depth--; |
1268 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
1269 |
|
&& readlength != -1) { |
1270 |
|
push_sym(env, ";"); |
1271 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
1272 |
|
&& readlength != -1) { |
1273 |
|
push_sym(env, "["); |
1274 |
|
depth++; |
1275 |
|
} else { |
1276 |
|
free(env->free_string); |
1277 |
|
env->in_string = env->free_string = NULL; |
1278 |
|
free(match); |
1279 |
|
} |
1280 |
|
if ( env->in_string != NULL) { |
1281 |
|
env->in_string += readlength; |
1282 |
|
} |
1283 |
|
|
1284 |
|
if(depth) |
1285 |
|
return read(env); |
1286 |
} |
} |