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 |
} environment; |
} environment; |
64 |
|
|
65 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
71 |
{ |
{ |
72 |
int i; |
int i; |
73 |
|
|
74 |
|
env->in_string= NULL; |
75 |
env->err= 0; |
env->err= 0; |
76 |
env->non_eval_flag= 0; |
env->non_eval_flag= 0; |
77 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
480 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(&(env->head), val); /* Return its bound value */ |
481 |
} |
} |
482 |
|
|
|
void stack_read(environment*, char*); |
|
|
|
|
483 |
/* 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 |
484 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
485 |
function. */ |
function. */ |
488 |
funcp in_func; |
funcp in_func; |
489 |
value* temp_val; |
value* temp_val; |
490 |
stackitem* iterator; |
stackitem* iterator; |
|
char* temp_string; |
|
491 |
|
|
492 |
if(env->head==NULL) { |
if(env->head==NULL) { |
493 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
539 |
free_val(temp_val); |
free_val(temp_val); |
540 |
return; |
return; |
541 |
|
|
542 |
/* 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); |
|
|
goto eval_start; |
|
|
|
|
|
case integer: |
|
543 |
return; |
return; |
544 |
} |
} |
545 |
} |
} |
614 |
rev(env); |
rev(env); |
615 |
} |
} |
616 |
|
|
|
/* 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); |
|
|
} |
|
|
|
|
617 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
618 |
extern void expand(environment *env) |
extern void expand(environment *env) |
619 |
{ |
{ |
810 |
init_env(&myenv); |
init_env(&myenv); |
811 |
|
|
812 |
while(1) { |
while(1) { |
813 |
fprintf(stderr, "okidok\n "); |
if(myenv.in_string==NULL) |
814 |
|
printstack(&myenv); |
815 |
read(&myenv); |
read(&myenv); |
816 |
if(myenv.err) { |
if(myenv.err) { |
817 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
818 |
myenv.err=0; |
myenv.err=0; |
819 |
} else if(myenv.head->item->type==symb |
} else if(myenv.head!=NULL |
820 |
|
&& myenv.head->item->type==symb |
821 |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
822 |
toss(&myenv); /* No error check in main */ |
toss(&myenv); /* No error check in main */ |
823 |
eval(&myenv); |
eval(&myenv); |
1188 |
|
|
1189 |
/* Read a value and place on stack */ |
/* Read a value and place on stack */ |
1190 |
extern void read(environment *env) { |
extern void read(environment *env) { |
1191 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%100c"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%[\001-\377]"; |
1192 |
const char strform[]= "\"%[^\"]\"%100c"; |
const char strform[]= "\"%[^\"]\"%[\001-\377]"; |
1193 |
const char intform[]= "%i%100c"; |
const char intform[]= "%i%[\001-\377]"; |
1194 |
const char blankform[]= "%*[ \t]%100c"; |
const char blankform[]= "%*[ \t]%[\001-\377]"; |
1195 |
const char ebrackform[]= "%*1[]]%100c"; |
const char ebrackform[]= "%*1[]]%[\001-\377]"; |
1196 |
const char semicform[]= "%*1[;]%100c"; |
const char semicform[]= "%*1[;]%[\001-\377]"; |
1197 |
const char bbrackform[]= "%*1[[]%100c"; |
const char bbrackform[]= "%*1[[]%[\001-\377]"; |
1198 |
|
|
1199 |
int itemp, rerun= 0; |
int itemp; |
1200 |
static int depth= 0; |
static int depth= 0; |
1201 |
char *rest, *match; |
char *rest, *match; |
|
static char *in_string= NULL; |
|
1202 |
size_t inlength; |
size_t inlength; |
1203 |
|
|
1204 |
if(in_string==NULL) { |
if(env->in_string==NULL) { |
1205 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
1206 |
|
|
1207 |
in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1208 |
strcpy(in_string, env->head->item->content.ptr); |
strcpy(env->in_string, env->head->item->content.ptr); |
1209 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1210 |
} |
} |
1211 |
|
|
1212 |
inlength= strlen(in_string)+1; |
inlength= strlen(env->in_string)+1; |
1213 |
match= malloc(inlength); |
match= malloc(inlength); |
1214 |
rest= malloc(inlength); |
rest= malloc(inlength); |
1215 |
|
|
1216 |
if(sscanf(in_string, blankform, rest)) { |
if(sscanf(env->in_string, blankform, rest)) { |
1217 |
rerun= 1; |
; |
1218 |
} else if(sscanf(in_string, intform, &itemp, rest) > 0) { |
} else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { |
1219 |
push_int(&(env->head), itemp); |
push_int(&(env->head), itemp); |
1220 |
} else if(sscanf(in_string, strform, match, rest) > 0) { |
} else if(sscanf(env->in_string, strform, match, rest) > 0) { |
1221 |
push_cstring(&(env->head), match); |
push_cstring(&(env->head), match); |
1222 |
} else if(sscanf(in_string, symbform, match, rest) > 0) { |
} else if(sscanf(env->in_string, symbform, match, rest) > 0) { |
1223 |
push_sym(env, match); |
push_sym(env, match); |
1224 |
} else if(sscanf(in_string, ebrackform, rest) > 0) { |
} else if(sscanf(env->in_string, ebrackform, rest) > 0) { |
1225 |
push_sym(env, "["); |
push_sym(env, "["); |
1226 |
pack(env); if(env->err) return; |
pack(env); if(env->err) return; |
1227 |
if(depth!=0) depth--; |
if(depth!=0) depth--; |
1228 |
} else if(sscanf(in_string, semicform, rest) > 0) { |
} else if(sscanf(env->in_string, semicform, rest) > 0) { |
1229 |
push_sym(env, ";"); |
push_sym(env, ";"); |
1230 |
} else if(sscanf(in_string, bbrackform, rest) > 0) { |
} else if(sscanf(env->in_string, bbrackform, rest) > 0) { |
1231 |
push_sym(env, "["); |
push_sym(env, "["); |
1232 |
depth++; |
depth++; |
1233 |
} else { |
} else { |
1234 |
free(rest); |
free(rest); |
1235 |
rest= NULL; |
rest= NULL; |
|
rerun= 1; |
|
1236 |
} |
} |
1237 |
|
|
1238 |
free(in_string); |
free(env->in_string); |
1239 |
free(match); |
free(match); |
1240 |
|
|
1241 |
in_string= rest; |
env->in_string= rest; |
1242 |
|
|
1243 |
if(rerun || depth) |
if(depth) |
1244 |
return read(env); |
return read(env); |
1245 |
} |
} |