8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
/* assert */ |
/* assert */ |
10 |
#include <assert.h> |
#include <assert.h> |
11 |
|
/* strcat */ |
12 |
|
#include <string.h> |
13 |
|
|
14 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
15 |
|
|
20 |
enum { |
enum { |
21 |
integer, |
integer, |
22 |
string, |
string, |
|
ref, /* Reference (to an element in the |
|
|
hash table) */ |
|
23 |
func, /* Function pointer */ |
func, /* Function pointer */ |
24 |
symb, |
symb, |
25 |
list |
list |
59 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
60 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
61 |
int err; /* Error flag */ |
int err; /* Error flag */ |
62 |
|
int non_eval_flag; |
63 |
} environment; |
} environment; |
64 |
|
|
65 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
69 |
/* Initialize a newly created environment */ |
/* Initialize a newly created environment */ |
70 |
void init_env(environment *env) |
void init_env(environment *env) |
71 |
{ |
{ |
72 |
long i; |
int i; |
73 |
|
|
74 |
|
env->err= 0; |
75 |
|
env->non_eval_flag= 0; |
76 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
77 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
78 |
} |
} |
80 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
81 |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
82 |
{ |
{ |
83 |
long i= 0; |
int i= 0; |
84 |
unsigned long out_hash= 0; |
unsigned int out_hash= 0; |
85 |
char key= '\0'; |
char key= '\0'; |
86 |
symbol **position; |
symbol **position; |
87 |
|
|
107 |
} |
} |
108 |
|
|
109 |
/* Generic push function. */ |
/* Generic push function. */ |
110 |
int push(stackitem** stack_head, stackitem* in_item) |
void push(stackitem** stack_head, stackitem* in_item) |
111 |
{ |
{ |
112 |
in_item->next= *stack_head; |
in_item->next= *stack_head; |
113 |
*stack_head= in_item; |
*stack_head= in_item; |
|
return 1; |
|
114 |
} |
} |
115 |
|
|
116 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
123 |
} |
} |
124 |
|
|
125 |
/* Push an integer onto the stack. */ |
/* Push an integer onto the stack. */ |
126 |
int push_int(stackitem **stack_head, int in_val) |
void push_int(stackitem **stack_head, int in_val) |
127 |
{ |
{ |
128 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
129 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
134 |
new_value->refcount=1; |
new_value->refcount=1; |
135 |
|
|
136 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
137 |
} |
} |
138 |
|
|
139 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
140 |
int push_cstring(stackitem **stack_head, const char *in_string) |
void push_cstring(stackitem **stack_head, const char *in_string) |
141 |
{ |
{ |
142 |
value *new_value= malloc(sizeof(value)); |
value *new_value= malloc(sizeof(value)); |
143 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
149 |
new_value->refcount=1; |
new_value->refcount=1; |
150 |
|
|
151 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
152 |
} |
} |
153 |
|
|
154 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
155 |
int push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
156 |
{ |
{ |
157 |
stackitem *new_item; /* The new stack item */ |
stackitem *new_item; /* The new stack item */ |
158 |
/* ...which will contain... */ |
/* ...which will contain... */ |
207 |
} |
} |
208 |
} |
} |
209 |
push(&(env->head), new_item); |
push(&(env->head), new_item); |
|
return 1; |
|
210 |
} |
} |
211 |
|
|
212 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) { |
222 |
switch (val->type){ /* and free the contents if necessary */ |
switch (val->type){ /* and free the contents if necessary */ |
223 |
case string: |
case string: |
224 |
free(val->content.ptr); |
free(val->content.ptr); |
225 |
|
break; |
226 |
case list: /* lists needs to be freed recursively */ |
case list: /* lists needs to be freed recursively */ |
227 |
item=val->content.ptr; |
item=val->content.ptr; |
228 |
while(item != NULL) { /* for all stack items */ |
while(item != NULL) { /* for all stack items */ |
245 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
246 |
|
|
247 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
248 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
249 |
|
env->err=1; |
250 |
return; |
return; |
251 |
} |
} |
252 |
|
|
256 |
} |
} |
257 |
|
|
258 |
/* Print newline. */ |
/* Print newline. */ |
259 |
extern void nl(environment *env) |
extern void nl() |
260 |
{ |
{ |
261 |
printf("\n"); |
printf("\n"); |
262 |
} |
} |
263 |
|
|
264 |
/* Prints the top element of the stack. */ |
/* Gets the type of a value */ |
265 |
void print_h(stackitem *stack_head) |
extern void type(environment *env){ |
266 |
{ |
int typenum; |
267 |
|
|
268 |
if(stack_head==NULL) { |
if((env->head)==NULL) { |
269 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
270 |
|
env->err=1; |
271 |
return; |
return; |
272 |
} |
} |
273 |
|
typenum=env->head->item->type; |
274 |
|
toss(env); |
275 |
|
switch(typenum){ |
276 |
|
case integer: |
277 |
|
push_sym(env, "integer"); |
278 |
|
break; |
279 |
|
case string: |
280 |
|
push_sym(env, "string"); |
281 |
|
break; |
282 |
|
case symb: |
283 |
|
push_sym(env, "symbol"); |
284 |
|
break; |
285 |
|
case func: |
286 |
|
push_sym(env, "function"); |
287 |
|
break; |
288 |
|
case list: |
289 |
|
push_sym(env, "list"); |
290 |
|
break; |
291 |
|
default: |
292 |
|
push_sym(env, "unknown"); |
293 |
|
break; |
294 |
|
} |
295 |
|
} |
296 |
|
|
297 |
|
/* Prints the top element of the stack. */ |
298 |
|
void print_h(stackitem *stack_head) |
299 |
|
{ |
300 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
301 |
case integer: |
case integer: |
302 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
305 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
306 |
break; |
break; |
307 |
case symb: |
case symb: |
308 |
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
309 |
|
break; |
310 |
|
case func: |
311 |
|
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
312 |
|
break; |
313 |
|
case list: |
314 |
|
/* A list is just a stack, so make stack_head point to it */ |
315 |
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
316 |
|
printf("[ "); |
317 |
|
while(stack_head != NULL) { |
318 |
|
print_h(stack_head); |
319 |
|
printf(" "); |
320 |
|
stack_head=stack_head->next; |
321 |
|
} |
322 |
|
printf("]"); |
323 |
break; |
break; |
324 |
default: |
default: |
325 |
printf("%p", (funcp)(stack_head->item->content.ptr)); |
printf("#<unknown %p>", (stack_head->item->content.ptr)); |
326 |
break; |
break; |
327 |
} |
} |
328 |
} |
} |
329 |
|
|
330 |
extern void print_(environment *env) { |
extern void print_(environment *env) { |
331 |
|
if(env->head==NULL) { |
332 |
|
printerr("Too Few Arguments"); |
333 |
|
env->err=1; |
334 |
|
return; |
335 |
|
} |
336 |
print_h(env->head); |
print_h(env->head); |
337 |
} |
} |
338 |
|
|
340 |
extern void print(environment *env) |
extern void print(environment *env) |
341 |
{ |
{ |
342 |
print_(env); |
print_(env); |
343 |
|
if(env->err) return; |
344 |
toss(env); |
toss(env); |
345 |
} |
} |
346 |
|
|
354 |
nl(); |
nl(); |
355 |
} |
} |
356 |
|
|
|
|
|
|
|
|
357 |
/* Prints the stack. */ |
/* Prints the stack. */ |
358 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
359 |
{ |
{ |
360 |
if(env->head != NULL) { |
if(env->head == NULL) { |
361 |
print_st(env->head, 1); |
return; |
|
nl(); |
|
|
} else { |
|
|
printerr("Stack empty"); |
|
362 |
} |
} |
363 |
|
print_st(env->head, 1); |
364 |
|
nl(); |
365 |
} |
} |
366 |
|
|
367 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
369 |
{ |
{ |
370 |
stackitem *temp= env->head; |
stackitem *temp= env->head; |
371 |
|
|
372 |
if((env->head)==NULL) { |
if(env->head==NULL || env->head->next==NULL) { |
373 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
374 |
return; |
env->err=1; |
|
} |
|
|
|
|
|
if(env->head->next==NULL) { |
|
|
printerr("Not enough arguments"); |
|
375 |
return; |
return; |
376 |
} |
} |
377 |
|
|
380 |
env->head->next= temp; |
env->head->next= temp; |
381 |
} |
} |
382 |
|
|
|
stackitem* copy(stackitem* in_item) |
|
|
{ |
|
|
stackitem *out_item= malloc(sizeof(stackitem)); |
|
|
|
|
|
memcpy(out_item, in_item, sizeof(stackitem)); |
|
|
out_item->next= NULL; |
|
|
|
|
|
return out_item; |
|
|
} |
|
|
|
|
383 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
384 |
extern void rcl(environment *env) |
extern void rcl(environment *env) |
385 |
{ |
{ |
386 |
value *val; |
value *val; |
387 |
|
|
388 |
if(env->head == NULL) { |
if(env->head == NULL) { |
389 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
390 |
|
env->err=1; |
391 |
return; |
return; |
392 |
} |
} |
393 |
|
|
394 |
if(env->head->item->type!=symb) { |
if(env->head->item->type!=symb) { |
395 |
printerr("Not a symbol"); |
printerr("Bad Argument Type"); |
396 |
|
env->err=2; |
397 |
return; |
return; |
398 |
} |
} |
399 |
|
|
400 |
val=((symbol *)(env->head->item->content.ptr))->val; |
val=((symbol *)(env->head->item->content.ptr))->val; |
401 |
if(val == NULL){ |
if(val == NULL){ |
402 |
printerr("Unbound variable"); |
printerr("Unbound Variable"); |
403 |
|
env->err=3; |
404 |
return; |
return; |
405 |
} |
} |
406 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
407 |
|
if(env->err) return; |
408 |
push_val(&(env->head), val); /* Return its bound value */ |
push_val(&(env->head), val); /* Return its bound value */ |
409 |
} |
} |
410 |
|
|
411 |
|
void stack_read(environment*, char*); |
412 |
|
|
413 |
/* 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 |
414 |
function value, and if it is, toss the symbol and execute the |
function value, and if it is, toss the symbol and execute the |
415 |
function. */ |
function. */ |
416 |
extern void eval(environment *env) |
extern void eval(environment *env) |
417 |
{ |
{ |
418 |
funcp in_func; |
funcp in_func; |
419 |
value* val; |
value* temp_val; |
420 |
|
stackitem* iterator; |
421 |
|
char* temp_string; |
422 |
|
|
423 |
if(env->head==NULL) { |
if(env->head==NULL) { |
424 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
425 |
|
env->err=1; |
426 |
return; |
return; |
427 |
} |
} |
428 |
|
|
429 |
/* if it's a symbol */ |
switch(env->head->item->type) { |
430 |
if(env->head->item->type==symb) { |
/* if it's a symbol */ |
431 |
|
case symb: |
432 |
/* If it's not bound to anything */ |
rcl(env); /* get its contents */ |
433 |
if (((symbol *)(env->head->item->content.ptr))->val == NULL) { |
if(env->err) return; |
434 |
printerr("Unbound variable"); |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
435 |
return; |
eval(env); /* evaluate the value */ |
|
} |
|
|
|
|
|
/* If it contains a function */ |
|
|
if (((symbol *)(env->head->item->content.ptr))->val->type == func) { |
|
|
in_func= |
|
|
(funcp)(((symbol *)(env->head->item->content.ptr))->val->content.ptr); |
|
|
toss(env); |
|
|
(*in_func)(env); /* Run the function */ |
|
436 |
return; |
return; |
|
} else { /* If it's not a function */ |
|
|
val=((symbol *)(env->head->item->content.ptr))->val; |
|
|
toss(env); /* toss the symbol */ |
|
|
push_val(&(env->head), val); /* Return its bound value */ |
|
437 |
} |
} |
438 |
} |
break; |
439 |
|
|
440 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
441 |
if(env->head->item->type==func) { |
case func: |
442 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
443 |
toss(env); |
toss(env); |
444 |
|
if(env->err) return; |
445 |
(*in_func)(env); |
(*in_func)(env); |
446 |
|
break; |
447 |
|
|
448 |
|
/* If it's a list */ |
449 |
|
case list: |
450 |
|
temp_val= env->head->item; |
451 |
|
env->head->item->refcount++; |
452 |
|
toss(env); |
453 |
|
if(env->err) return; |
454 |
|
iterator= (stackitem*)temp_val->content.ptr; |
455 |
|
while(iterator!=NULL && iterator->item!=NULL) { |
456 |
|
push_val(&(env->head), iterator->item); |
457 |
|
if(env->head->item->type==symb |
458 |
|
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
459 |
|
toss(env); |
460 |
|
if(env->err) return; |
461 |
|
eval(env); |
462 |
|
if(env->err) return; |
463 |
|
} |
464 |
|
iterator= iterator->next; |
465 |
|
} |
466 |
|
free_val(temp_val); |
467 |
|
break; |
468 |
|
|
469 |
|
/* If it's a string */ |
470 |
|
case string: |
471 |
|
temp_val= env->head->item; |
472 |
|
env->head->item->refcount++; |
473 |
|
toss(env); |
474 |
|
if(env->err) return; |
475 |
|
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
476 |
|
strcat(temp_string, "[ "); |
477 |
|
strcat(temp_string, (char*)temp_val->content.ptr); |
478 |
|
strcat(temp_string, " ]"); |
479 |
|
stack_read(env, temp_string); |
480 |
|
eval(env); |
481 |
|
if(env->err) return; |
482 |
|
free_val(temp_val); |
483 |
|
free(temp_string); |
484 |
|
break; |
485 |
|
|
486 |
|
default: |
487 |
|
} |
488 |
|
} |
489 |
|
|
490 |
|
/* Reverse (flip) a list */ |
491 |
|
extern void rev(environment *env){ |
492 |
|
stackitem *old_head, *new_head, *item; |
493 |
|
|
494 |
|
if((env->head)==NULL) { |
495 |
|
printerr("Too Few Arguments"); |
496 |
|
env->err=1; |
497 |
return; |
return; |
498 |
} |
} |
499 |
|
|
500 |
|
if(env->head->item->type!=list) { |
501 |
|
printerr("Bad Argument Type"); |
502 |
|
env->err=2; |
503 |
|
return; |
504 |
|
} |
505 |
|
|
506 |
|
old_head=(stackitem *)(env->head->item->content.ptr); |
507 |
|
new_head=NULL; |
508 |
|
while(old_head != NULL){ |
509 |
|
item=old_head; |
510 |
|
old_head=old_head->next; |
511 |
|
item->next=new_head; |
512 |
|
new_head=item; |
513 |
|
} |
514 |
|
env->head->item->content.ptr=new_head; |
515 |
} |
} |
516 |
|
|
517 |
/* Make a list. */ |
/* Make a list. */ |
554 |
temp->item= pack; |
temp->item= pack; |
555 |
|
|
556 |
push(&(env->head), temp); |
push(&(env->head), temp); |
557 |
|
rev(env); |
558 |
} |
} |
559 |
|
|
560 |
/* Parse input. */ |
/* Parse input. */ |
561 |
int stack_read(environment *env, char *in_line) |
void stack_read(environment *env, char *in_line) |
562 |
{ |
{ |
563 |
char *temp, *rest; |
char *temp, *rest; |
564 |
int itemp; |
int itemp; |
565 |
size_t inlength= strlen(in_line)+1; |
size_t inlength= strlen(in_line)+1; |
566 |
int convert= 0; |
int convert= 0; |
|
static int non_eval_flag= 0; |
|
567 |
|
|
568 |
temp= malloc(inlength); |
temp= malloc(inlength); |
569 |
rest= malloc(inlength); |
rest= malloc(inlength); |
570 |
|
|
571 |
do { |
do { |
572 |
|
/* If comment */ |
573 |
|
if((convert= sscanf(in_line, "#%[^\n\r]", rest))) { |
574 |
|
free(temp); free(rest); |
575 |
|
return; |
576 |
|
} |
577 |
|
|
578 |
/* If string */ |
/* If string */ |
579 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
580 |
push_cstring(&(env->head), temp); |
push_cstring(&(env->head), temp); |
592 |
break; |
break; |
593 |
} |
} |
594 |
/* If symbol */ |
/* If symbol */ |
595 |
if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
596 |
push_sym(env, temp); |
push_sym(env, temp); |
597 |
break; |
break; |
598 |
} |
} |
599 |
/* If single char */ |
/* If single char */ |
600 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
601 |
if(*temp==';') { |
if(*temp==';') { |
602 |
if(!non_eval_flag) { |
if(!env->non_eval_flag) { |
603 |
eval(env); /* Evaluate top element */ |
eval(env); /* Evaluate top element */ |
604 |
break; |
break; |
605 |
} |
} |
611 |
if(*temp==']') { |
if(*temp==']') { |
612 |
push_sym(env, "["); |
push_sym(env, "["); |
613 |
pack(env); |
pack(env); |
614 |
if(non_eval_flag!=0) |
if(env->non_eval_flag) |
615 |
non_eval_flag--; |
env->non_eval_flag--; |
616 |
break; |
break; |
617 |
} |
} |
618 |
|
|
619 |
if(*temp=='[') { |
if(*temp=='[') { |
620 |
push_sym(env, "["); |
push_sym(env, "["); |
621 |
non_eval_flag++; |
env->non_eval_flag++; |
622 |
break; |
break; |
623 |
} |
} |
624 |
} |
} |
625 |
} while(0); |
} while(0); |
626 |
|
|
|
|
|
627 |
free(temp); |
free(temp); |
628 |
|
|
629 |
if(convert<2) { |
if(convert<2) { |
630 |
free(rest); |
free(rest); |
631 |
return 0; |
return; |
632 |
} |
} |
633 |
|
|
634 |
stack_read(env, rest); |
stack_read(env, rest); |
635 |
|
|
636 |
free(rest); |
free(rest); |
|
return 1; |
|
637 |
} |
} |
638 |
|
|
639 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
642 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
643 |
|
|
644 |
/* Is top element a list? */ |
/* Is top element a list? */ |
645 |
if(env->head==NULL || env->head->item->type!=list) { |
if(env->head==NULL) { |
646 |
printerr("Stack empty or not a list"); |
printerr("Too Few Arguments"); |
647 |
|
env->err=1; |
648 |
|
return; |
649 |
|
} |
650 |
|
if(env->head->item->type!=list) { |
651 |
|
printerr("Bad Argument Type"); |
652 |
|
env->err=2; |
653 |
return; |
return; |
654 |
} |
} |
655 |
|
|
656 |
|
rev(env); |
657 |
|
|
658 |
|
if(env->err) |
659 |
|
return; |
660 |
|
|
661 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
662 |
new_head= temp= env->head->item->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
663 |
|
|
681 |
int result; |
int result; |
682 |
|
|
683 |
if((env->head)==NULL || env->head->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
684 |
printerr("Not enough elements to compare"); |
printerr("Too Few Arguments"); |
685 |
|
env->err=1; |
686 |
return; |
return; |
687 |
} |
} |
688 |
|
|
700 |
{ |
{ |
701 |
int val; |
int val; |
702 |
|
|
703 |
if((env->head)==NULL || env->head->item->type!=integer) { |
if((env->head)==NULL) { |
704 |
printerr("Stack empty or element is not a integer"); |
printerr("Too Few Arguments"); |
705 |
|
env->err=1; |
706 |
|
return; |
707 |
|
} |
708 |
|
|
709 |
|
if(env->head->item->type!=integer) { |
710 |
|
printerr("Bad Argument Type"); |
711 |
|
env->err=2; |
712 |
return; |
return; |
713 |
} |
} |
714 |
|
|
731 |
symbol *sym; |
symbol *sym; |
732 |
|
|
733 |
/* Needs two values on the stack, the top one must be a symbol */ |
/* Needs two values on the stack, the top one must be a symbol */ |
734 |
if(env->head==NULL || env->head->next==NULL |
if(env->head==NULL || env->head->next==NULL) { |
735 |
|| env->head->item->type!=symb) { |
printerr("Too Few Arguments"); |
736 |
printerr("Define what?"); |
env->err=1; |
737 |
|
return; |
738 |
|
} |
739 |
|
|
740 |
|
if(env->head->item->type!=symb) { |
741 |
|
printerr("Bad Argument Type"); |
742 |
|
env->err=2; |
743 |
return; |
return; |
744 |
} |
} |
745 |
|
|
785 |
} |
} |
786 |
} |
} |
787 |
|
|
788 |
|
/* Forgets a symbol (remove it from the hash table) */ |
789 |
|
extern void forget(environment *env) |
790 |
|
{ |
791 |
|
char* sym_id; |
792 |
|
stackitem *stack_head= env->head; |
793 |
|
symbol **hash_entry, *temp; |
794 |
|
|
795 |
|
if(stack_head==NULL) { |
796 |
|
printerr("Too Few Arguments"); |
797 |
|
env->err=1; |
798 |
|
return; |
799 |
|
} |
800 |
|
|
801 |
|
if(stack_head->item->type!=symb) { |
802 |
|
printerr("Bad Argument Type"); |
803 |
|
env->err=2; |
804 |
|
return; |
805 |
|
} |
806 |
|
|
807 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
808 |
|
toss(env); |
809 |
|
|
810 |
|
hash_entry= hash(env->symbols, sym_id); |
811 |
|
temp= *hash_entry; |
812 |
|
*hash_entry= (*hash_entry)->next; |
813 |
|
|
814 |
|
if(temp->val!=NULL) { |
815 |
|
free_val(temp->val); |
816 |
|
} |
817 |
|
free(temp->id); |
818 |
|
free(temp); |
819 |
|
} |
820 |
|
|
821 |
|
/* Returns the current error number to the stack */ |
822 |
|
extern void errn(environment *env){ |
823 |
|
push_int(&(env->head), env->err); |
824 |
|
} |
825 |
|
|
826 |
int main() |
int main() |
827 |
{ |
{ |
828 |
environment myenv; |
environment myenv; |
834 |
|
|
835 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(fgets(in_string, 100, stdin) != NULL) { |
836 |
stack_read(&myenv, in_string); |
stack_read(&myenv, in_string); |
837 |
|
if(myenv.err) { |
838 |
|
printf("(error %d) ", myenv.err); |
839 |
|
myenv.err=0; |
840 |
|
} |
841 |
printf("okidok\n "); |
printf("okidok\n "); |
842 |
} |
} |
843 |
|
quit(&myenv); |
844 |
exit(EXIT_SUCCESS); |
return EXIT_FAILURE; |
845 |
} |
} |