| 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 */ |
| 157 |
} |
} |
| 158 |
} |
} |
| 159 |
|
|
|
/* Generic push function. */ |
|
|
void push(environment *env, stackitem* in_item) |
|
|
{ |
|
|
in_item->next= env->head; |
|
|
env->head= in_item; |
|
|
} |
|
|
|
|
| 160 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
| 161 |
void push_val(environment *env, 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(env, 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(environment *env, 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(env, 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(environment *env, 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(env, 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 */ |
| 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, new_item); |
push_val(env, new_value); |
| 304 |
} |
} |
| 305 |
|
|
| 306 |
/* Print newline. */ |
/* Print newline. */ |
| 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 |
|
if (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); |