18 |
enum { |
enum { |
19 |
integer, |
integer, |
20 |
string, |
string, |
|
ref, /* Reference (to an element in the |
|
|
hash table) */ |
|
21 |
func, /* Function pointer */ |
func, /* Function pointer */ |
22 |
symb, |
symb, |
23 |
list |
list |
56 |
typedef struct { |
typedef struct { |
57 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
58 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
59 |
|
int err; /* Error flag */ |
60 |
} environment; |
} environment; |
61 |
|
|
62 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
68 |
{ |
{ |
69 |
long i; |
long i; |
70 |
|
|
71 |
|
env->err=0; |
72 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
73 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
74 |
} |
} |
103 |
} |
} |
104 |
|
|
105 |
/* Generic push function. */ |
/* Generic push function. */ |
106 |
int push(stackitem** stack_head, stackitem* in_item) |
void push(stackitem** stack_head, stackitem* in_item) |
107 |
{ |
{ |
108 |
in_item->next= *stack_head; |
in_item->next= *stack_head; |
109 |
*stack_head= in_item; |
*stack_head= in_item; |
110 |
return 1; |
} |
111 |
|
|
112 |
|
/* Push a value onto the stack */ |
113 |
|
void push_val(stackitem **stack_head, value *val) |
114 |
|
{ |
115 |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
116 |
|
new_item->item= val; |
117 |
|
val->refcount++; |
118 |
|
push(stack_head, new_item); |
119 |
} |
} |
120 |
|
|
121 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
122 |
int push_val(stackitem **stack_head, int in_val) |
void push_int(stackitem **stack_head, int in_val) |
123 |
{ |
{ |
124 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
125 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
130 |
new_value->refcount=1; |
new_value->refcount=1; |
131 |
|
|
132 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
133 |
} |
} |
134 |
|
|
135 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
136 |
int push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(stackitem **stack_head, const char *in_string) |
137 |
{ |
{ |
138 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
139 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
145 |
new_value->refcount=1; |
new_value->refcount=1; |
146 |
|
|
147 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
148 |
} |
} |
149 |
|
|
150 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
151 |
int push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
152 |
{ |
{ |
153 |
stackitem *new_item; /* The new stack item */ |
stackitem *new_item; /* The new stack item */ |
154 |
/* ...which will contain... */ |
/* ...which will contain... */ |
155 |
value *new_value; /* A new symbol value */ |
value *new_value; /* A new symbol value */ |
156 |
/* ...which might point to... */ |
/* ...which might point to... */ |
157 |
symbol *new_symbol; /* (if needed) A new actual symbol */ |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
158 |
/* ...which, if possible, will be bound to... */ |
/* ...which, if possible, will be bound to... */ |
159 |
value *new_fvalue; /* (if needed) A new function value */ |
value *new_fvalue; /* (if needed) A new function value */ |
160 |
/* ...which will point to... */ |
/* ...which will point to... */ |
172 |
new_value->refcount= 1; |
new_value->refcount= 1; |
173 |
|
|
174 |
/* Look up the symbol name in the hash table */ |
/* Look up the symbol name in the hash table */ |
175 |
new_value->content.ptr= *hash(env->symbols, in_string); |
new_symbol= hash(env->symbols, in_string); |
176 |
|
new_value->content.ptr= *new_symbol; |
177 |
|
|
178 |
if(new_value->content.ptr==NULL) { /* If symbol was undefined */ |
if(*new_symbol==NULL) { /* If symbol was undefined */ |
179 |
|
|
180 |
/* Create a new symbol */ |
/* Create a new symbol */ |
181 |
new_symbol= malloc(sizeof(symbol)); |
(*new_symbol)= malloc(sizeof(symbol)); |
182 |
new_symbol->val= NULL; /* undefined value */ |
(*new_symbol)->val= NULL; /* undefined value */ |
183 |
new_symbol->next= NULL; |
(*new_symbol)->next= NULL; |
184 |
new_symbol->id= malloc(strlen(in_string)+1); |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
185 |
strcpy(new_symbol->id, in_string); |
strcpy((*new_symbol)->id, in_string); |
186 |
|
|
187 |
/* Intern the new symbol in the hash table */ |
/* Intern the new symbol in the hash table */ |
188 |
new_value->content.ptr= new_symbol; |
new_value->content.ptr= *new_symbol; |
189 |
|
|
190 |
/* Try to load the symbol name as an external function, to see if |
/* Try to load the symbol name as an external function, to see if |
191 |
we should bind the symbol to a new function pointer value */ |
we should bind the symbol to a new function pointer value */ |
197 |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
198 |
new_fvalue->type=func; /* The new value is a function pointer */ |
new_fvalue->type=func; /* The new value is a function pointer */ |
199 |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
200 |
new_symbol->val= new_fvalue; /* Bind the symbol to the new |
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
201 |
function value */ |
function value */ |
202 |
new_fvalue->refcount= 1; |
new_fvalue->refcount= 1; |
203 |
} |
} |
204 |
} |
} |
205 |
push(&(env->head), new_item); |
push(&(env->head), new_item); |
|
return 1; |
|
206 |
} |
} |
207 |
|
|
208 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) { |
241 |
|
|
242 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
243 |
printerr("Stack empty"); |
printerr("Stack empty"); |
244 |
|
env->err=1; |
245 |
return; |
return; |
246 |
} |
} |
247 |
|
|
259 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
260 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head) |
261 |
{ |
{ |
|
|
|
|
if(stack_head==NULL) { |
|
|
printerr("Stack empty"); |
|
|
return; |
|
|
} |
|
|
|
|
262 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
263 |
case integer: |
case integer: |
264 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
269 |
case symb: |
case symb: |
270 |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
271 |
break; |
break; |
272 |
|
case func: |
273 |
|
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
274 |
|
break; |
275 |
|
case list: |
276 |
|
printf("#<list %p>", (funcp)(stack_head->item->content.ptr)); |
277 |
|
break; |
278 |
default: |
default: |
279 |
printf("%p", (funcp)(stack_head->item->content.ptr)); |
printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr)); |
280 |
break; |
break; |
281 |
} |
} |
282 |
} |
} |
283 |
|
|
284 |
extern void print_(environment *env) { |
extern void print_(environment *env) { |
285 |
|
if(env->head==NULL) { |
286 |
|
printerr("Stack empty"); |
287 |
|
env->err=1; |
288 |
|
return; |
289 |
|
} |
290 |
print_h(env->head); |
print_h(env->head); |
291 |
} |
} |
292 |
|
|
294 |
extern void print(environment *env) |
extern void print(environment *env) |
295 |
{ |
{ |
296 |
print_(env); |
print_(env); |
297 |
|
if(env->err) return; |
298 |
toss(env); |
toss(env); |
299 |
} |
} |
300 |
|
|
313 |
/* Prints the stack. */ |
/* Prints the stack. */ |
314 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
315 |
{ |
{ |
316 |
if(env->head != NULL) { |
if(env->head == NULL) { |
|
print_st(env->head, 1); |
|
|
nl(); |
|
|
} else { |
|
317 |
printerr("Stack empty"); |
printerr("Stack empty"); |
318 |
|
env->err=1; |
319 |
|
return; |
320 |
} |
} |
321 |
|
print_st(env->head, 1); |
322 |
|
nl(); |
323 |
} |
} |
324 |
|
|
325 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
329 |
|
|
330 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
331 |
printerr("Stack empty"); |
printerr("Stack empty"); |
332 |
|
env->err=1; |
333 |
return; |
return; |
334 |
} |
} |
335 |
|
|
336 |
if(env->head->next==NULL) { |
if(env->head->next==NULL) { |
337 |
printerr("Not enough arguments"); |
printerr("Not enough arguments"); |
338 |
|
env->err=1; |
339 |
return; |
return; |
340 |
} |
} |
341 |
|
|
354 |
return out_item; |
return out_item; |
355 |
} |
} |
356 |
|
|
357 |
|
/* Recall a value from a symbol, if bound */ |
358 |
|
extern void rcl(environment *env) |
359 |
|
{ |
360 |
|
value *val; |
361 |
|
|
362 |
|
if(env->head == NULL) { |
363 |
|
printerr("Stack empty"); |
364 |
|
env->err=1; |
365 |
|
return; |
366 |
|
} |
367 |
|
|
368 |
|
if(env->head->item->type!=symb) { |
369 |
|
printerr("Not a symbol"); |
370 |
|
env->err=1; |
371 |
|
return; |
372 |
|
} |
373 |
|
|
374 |
|
val=((symbol *)(env->head->item->content.ptr))->val; |
375 |
|
if(val == NULL){ |
376 |
|
printerr("Unbound variable"); |
377 |
|
env->err=1; |
378 |
|
return; |
379 |
|
} |
380 |
|
toss(env); /* toss the symbol */ |
381 |
|
if(env->err) return; |
382 |
|
push_val(&(env->head), val); /* Return its bound value */ |
383 |
|
} |
384 |
|
|
385 |
/* If the top element is a reference, determine if it's a reference to a |
/* If the top element is a symbol, determine if it's bound to a |
386 |
function, and if it is, toss the reference and execute the function. */ |
function value, and if it is, toss the symbol and execute the |
387 |
|
function. */ |
388 |
extern void eval(environment *env) |
extern void eval(environment *env) |
389 |
{ |
{ |
390 |
funcp in_func; |
funcp in_func; |
391 |
stackitem* temp= env->head; |
if(env->head==NULL) { |
|
|
|
|
if(temp==NULL) { |
|
392 |
printerr("Stack empty"); |
printerr("Stack empty"); |
393 |
|
env->err=1; |
394 |
return; |
return; |
395 |
} |
} |
396 |
|
|
397 |
if(temp->item->type==symb |
/* if it's a symbol */ |
398 |
&& ((symbol *)(temp->item->content.ptr))->val != NULL |
if(env->head->item->type==symb) { |
399 |
&& ((symbol *)(temp->item->content.ptr))->val->type == func) { |
|
400 |
in_func= (funcp)(((symbol *)(temp->item->content.ptr))->val->content.ptr); |
rcl(env); /* get its contents */ |
401 |
toss(env); |
if(env->err) return; |
402 |
(*in_func)(env); |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
403 |
return; |
eval(env); /* evaluate the value */ |
404 |
|
return; |
405 |
|
} |
406 |
} |
} |
|
|
|
407 |
|
|
408 |
if(temp->item->type==func) { |
/* If it's a lone function value, run it */ |
409 |
in_func= (funcp)(temp->item->content.ptr); |
if(env->head->item->type==func) { |
410 |
|
in_func= (funcp)(env->head->item->content.ptr); |
411 |
toss(env); |
toss(env); |
412 |
|
if(env->err) return; |
413 |
(*in_func)(env); |
(*in_func)(env); |
|
return; |
|
414 |
} |
} |
|
|
|
|
push(&(env->head), copy(temp)); |
|
|
swap(env); |
|
|
toss(env); |
|
415 |
} |
} |
416 |
|
|
417 |
/* Make a list. */ |
/* Make a list. */ |
476 |
} |
} |
477 |
/* If integer */ |
/* If integer */ |
478 |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
479 |
push_val(&(env->head), itemp); |
push_int(&(env->head), itemp); |
480 |
break; |
break; |
481 |
} |
} |
482 |
/* Escape ';' with '\' */ |
/* Escape ';' with '\' */ |
486 |
break; |
break; |
487 |
} |
} |
488 |
/* If symbol */ |
/* If symbol */ |
489 |
if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
490 |
push_sym(env, temp); |
push_sym(env, temp); |
491 |
break; |
break; |
492 |
} |
} |
540 |
/* Is top element a list? */ |
/* Is top element a list? */ |
541 |
if(env->head==NULL || env->head->item->type!=list) { |
if(env->head==NULL || env->head->item->type!=list) { |
542 |
printerr("Stack empty or not a list"); |
printerr("Stack empty or not a list"); |
543 |
|
env->err=1; |
544 |
return; |
return; |
545 |
} |
} |
546 |
|
|
568 |
|
|
569 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
570 |
printerr("Not enough elements to compare"); |
printerr("Not enough elements to compare"); |
571 |
|
env->err=1; |
572 |
return; |
return; |
573 |
} |
} |
574 |
|
|
578 |
result= (left==right); |
result= (left==right); |
579 |
|
|
580 |
toss(env); toss(env); |
toss(env); toss(env); |
581 |
push_val(&(env->head), result); |
push_int(&(env->head), result); |
582 |
} |
} |
583 |
|
|
584 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
588 |
|
|
589 |
if((env->head)==NULL || env->head->item->type!=integer) { |
if((env->head)==NULL || env->head->item->type!=integer) { |
590 |
printerr("Stack empty or element is not a integer"); |
printerr("Stack empty or element is not a integer"); |
591 |
|
env->err=1; |
592 |
return; |
return; |
593 |
} |
} |
594 |
|
|
595 |
val= env->head->item->content.val; |
val= env->head->item->content.val; |
596 |
toss(env); |
toss(env); |
597 |
push_val(&(env->head), !val); |
push_int(&(env->head), !val); |
598 |
} |
} |
599 |
|
|
600 |
/* 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 |
614 |
if(env->head==NULL || env->head->next==NULL |
if(env->head==NULL || env->head->next==NULL |
615 |
|| env->head->item->type!=symb) { |
|| env->head->item->type!=symb) { |
616 |
printerr("Define what?"); |
printerr("Define what?"); |
617 |
|
env->err=1; |
618 |
return; |
return; |
619 |
} |
} |
620 |
|
|
645 |
toss(env); |
toss(env); |
646 |
} |
} |
647 |
|
|
648 |
|
/* List all defined words */ |
649 |
|
extern void words(environment *env) |
650 |
|
{ |
651 |
|
symbol *temp; |
652 |
|
int i; |
653 |
|
|
654 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
655 |
|
temp= env->symbols[i]; |
656 |
|
while(temp!=NULL) { |
657 |
|
printf("%s\n", temp->id); |
658 |
|
temp= temp->next; |
659 |
|
} |
660 |
|
} |
661 |
|
} |
662 |
|
|
663 |
|
/* Forgets a symbol (remove it from the hash table) */ |
664 |
|
extern void forget(environment *env) |
665 |
|
{ |
666 |
|
char* sym_id; |
667 |
|
stackitem *stack_head= env->head; |
668 |
|
symbol **hash_entry, *temp; |
669 |
|
|
670 |
|
if(stack_head==NULL || stack_head->item->type!=symb) { |
671 |
|
printerr("Stack empty or not a symbol"); |
672 |
|
return; |
673 |
|
} |
674 |
|
|
675 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
676 |
|
toss(env); |
677 |
|
|
678 |
|
hash_entry= hash(env->symbols, sym_id); |
679 |
|
temp= *hash_entry; |
680 |
|
*hash_entry= (*hash_entry)->next; |
681 |
|
|
682 |
|
if(temp->val!=NULL) { |
683 |
|
free_val(temp->val); |
684 |
|
} |
685 |
|
free(temp->id); |
686 |
|
free(temp); |
687 |
|
} |
688 |
|
|
689 |
int main() |
int main() |
690 |
{ |
{ |
691 |
environment myenv; |
environment myenv; |
697 |
|
|
698 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(fgets(in_string, 100, stdin) != NULL) { |
699 |
stack_read(&myenv, in_string); |
stack_read(&myenv, in_string); |
700 |
|
if(myenv.err) { |
701 |
|
printf("(error %d) ", myenv.err); |
702 |
|
myenv.err=0; |
703 |
|
} |
704 |
printf("okidok\n "); |
printf("okidok\n "); |
705 |
} |
} |
706 |
|
|