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 */ |
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 */ |
533 |
|
|
534 |
/* Reverse (flip) a list */ |
/* Reverse (flip) a list */ |
535 |
extern void rev(environment *env){ |
extern void rev(environment *env){ |
536 |
stackitem *item, *temp, *prev= NULL; |
stackitem *old_head, *new_head, *item; |
537 |
|
|
538 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
539 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
547 |
return; |
return; |
548 |
} |
} |
549 |
|
|
550 |
item= (stackitem *)(env->head->item->content.ptr); |
old_head=(stackitem *)(env->head->item->content.ptr); |
551 |
while(item->next!=NULL){ |
new_head=NULL; |
552 |
temp= item->next; |
while(old_head != NULL){ |
553 |
item->next= prev; |
item=old_head; |
554 |
prev= item; |
old_head=old_head->next; |
555 |
item= temp; |
item->next=new_head; |
556 |
|
new_head=item; |
557 |
} |
} |
558 |
item->next= prev; |
env->head->item->content.ptr=new_head; |
|
|
|
|
env->head->item->content.ptr=item; |
|
559 |
} |
} |
560 |
|
|
561 |
/* Make a list. */ |
/* Make a list. */ |
718 |
toss(env); toss(env); |
toss(env); toss(env); |
719 |
} |
} |
720 |
|
|
721 |
|
extern void clear(environment *); |
722 |
|
void forget_sym(symbol **); |
723 |
|
|
724 |
/* Quit stack. */ |
/* Quit stack. */ |
725 |
extern void quit(environment *env) |
extern void quit(environment *env) |
726 |
{ |
{ |
727 |
|
long i; |
728 |
|
|
729 |
|
clear(env); |
730 |
|
if (env->err) return; |
731 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
732 |
|
while(env->symbols[i]!= NULL) { |
733 |
|
forget_sym(&(env->symbols[i])); |
734 |
|
env->symbols[i]= NULL; |
735 |
|
} |
736 |
|
} |
737 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
738 |
} |
} |
739 |
|
|
759 |
} |
} |
760 |
} |
} |
761 |
|
|
762 |
|
/* Internal forget function */ |
763 |
|
void forget_sym(symbol **hash_entry) { |
764 |
|
symbol *temp; |
765 |
|
|
766 |
|
temp= *hash_entry; |
767 |
|
*hash_entry= (*hash_entry)->next; |
768 |
|
|
769 |
|
if(temp->val!=NULL) { |
770 |
|
free_val(temp->val); |
771 |
|
} |
772 |
|
free(temp->id); |
773 |
|
free(temp); |
774 |
|
} |
775 |
|
|
776 |
/* Forgets a symbol (remove it from the hash table) */ |
/* Forgets a symbol (remove it from the hash table) */ |
777 |
extern void forget(environment *env) |
extern void forget(environment *env) |
778 |
{ |
{ |
779 |
char* sym_id; |
char* sym_id; |
780 |
stackitem *stack_head= env->head; |
stackitem *stack_head= env->head; |
|
symbol **hash_entry, *temp; |
|
781 |
|
|
782 |
if(stack_head==NULL) { |
if(stack_head==NULL) { |
783 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
794 |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
795 |
toss(env); |
toss(env); |
796 |
|
|
797 |
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); |
|
798 |
} |
} |
799 |
|
|
800 |
/* Returns the current error number to the stack */ |
/* Returns the current error number to the stack */ |
1196 |
|
|
1197 |
/* Read a value and place on stack */ |
/* Read a value and place on stack */ |
1198 |
extern void read(environment *env) { |
extern void read(environment *env) { |
1199 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%[\001-\377]"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1200 |
const char strform[]= "\"%[^\"]\"%[\001-\377]"; |
const char strform[]= "\"%[^\"]\"%n"; |
1201 |
const char intform[]= "%i%[\001-\377]"; |
const char intform[]= "%i%n"; |
1202 |
const char blankform[]= "%*[ \t]%[\001-\377]"; |
const char blankform[]= "%*[ \t]%n"; |
1203 |
const char ebrackform[]= "%*1[]]%[\001-\377]"; |
const char ebrackform[]= "%*1[]]%n"; |
1204 |
const char semicform[]= "%*1[;]%[\001-\377]"; |
const char semicform[]= "%*1[;]%n"; |
1205 |
const char bbrackform[]= "%*1[[]%[\001-\377]"; |
const char bbrackform[]= "%*1[[]%n"; |
1206 |
|
|
1207 |
int itemp; |
int itemp, readlength= -1; |
1208 |
static int depth= 0; |
static int depth= 0; |
1209 |
char *rest, *match; |
char *rest, *match; |
1210 |
size_t inlength; |
size_t inlength; |
1213 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
1214 |
|
|
1215 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1216 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
1217 |
strcpy(env->in_string, env->head->item->content.ptr); |
strcpy(env->in_string, env->head->item->content.ptr); |
1218 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1219 |
} |
} |
1222 |
match= malloc(inlength); |
match= malloc(inlength); |
1223 |
rest= malloc(inlength); |
rest= malloc(inlength); |
1224 |
|
|
1225 |
if(sscanf(env->in_string, blankform, rest)) { |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1226 |
|
&& readlength != -1) { |
1227 |
; |
; |
1228 |
} else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { |
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
1229 |
|
&& readlength != -1) { |
1230 |
push_int(env, itemp); |
push_int(env, itemp); |
1231 |
} else if(sscanf(env->in_string, strform, match, rest) > 0) { |
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1232 |
|
&& readlength != -1) { |
1233 |
push_cstring(env, match); |
push_cstring(env, match); |
1234 |
} else if(sscanf(env->in_string, symbform, match, rest) > 0) { |
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
1235 |
|
&& readlength != -1) { |
1236 |
push_sym(env, match); |
push_sym(env, match); |
1237 |
} else if(sscanf(env->in_string, ebrackform, rest) > 0) { |
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
1238 |
|
&& readlength != -1) { |
1239 |
pack(env); if(env->err) return; |
pack(env); if(env->err) return; |
1240 |
if(depth!=0) depth--; |
if(depth != 0) depth--; |
1241 |
} else if(sscanf(env->in_string, semicform, rest) > 0) { |
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
1242 |
|
&& readlength != -1) { |
1243 |
push_sym(env, ";"); |
push_sym(env, ";"); |
1244 |
} else if(sscanf(env->in_string, bbrackform, rest) > 0) { |
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
1245 |
|
&& readlength != -1) { |
1246 |
push_sym(env, "["); |
push_sym(env, "["); |
1247 |
depth++; |
depth++; |
1248 |
} else { |
} else { |
1249 |
free(rest); |
free(env->free_string); |
1250 |
rest= NULL; |
env->in_string = env->free_string = NULL; |
1251 |
|
free(match); |
1252 |
|
} |
1253 |
|
if ( env->in_string != NULL) { |
1254 |
|
env->in_string += readlength; |
1255 |
} |
} |
|
|
|
|
free(env->in_string); |
|
|
free(match); |
|
|
|
|
|
env->in_string= rest; |
|
1256 |
|
|
1257 |
if(depth) |
if(depth) |
1258 |
return read(env); |
return read(env); |