/[cvs]/stack/stack.c
ViewVC logotype

Diff of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by masse, Mon Jan 7 23:09:01 2002 UTC revision 1.38 by teddy, Wed Feb 6 11:15:05 2002 UTC
# Line 4  Line 4 
4  #include <stdlib.h>  #include <stdlib.h>
5  /* NULL */  /* NULL */
6  #include <stddef.h>  #include <stddef.h>
7    /* dlopen, dlsym, dlerror */
8  #include <dlfcn.h>  #include <dlfcn.h>
9    /* assert */
10    #include <assert.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
13    
14  typedef struct stack_item  /* First, define some types. */
15  {  
16    enum {value, string, ref, func} type;  /* A value of some type */
17    typedef struct {
18      enum {
19        integer,
20        string,
21        func,                       /* Function pointer */
22        symb,
23        list
24      } type;                       /* Type of stack element */
25    
26    union {    union {
27      void* ptr;      void *ptr;                  /* Pointer to the content */
28      int val;      int val;                    /* ...or an integer */
29    } content;    } content;                    /* Stores a pointer or an integer */
30    
31      int refcount;                 /* Reference counter */
32    
33    } value;
34    
35    /* A symbol with a name and possible value */
36    /* (These do not need reference counters, they are kept unique by
37       hashing.) */
38    typedef struct symbol_struct {
39      char *id;                     /* Symbol name */
40      value *val;                   /* The value (if any) bound to it */
41      struct symbol_struct *next;   /* In case of hashing conflicts, a */
42    } symbol;                       /* symbol is a kind of stack item. */
43    
44    char* id;  /* A type for a hash table for symbols */
45    struct stack_item* next;  typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */
 } stackitem;  
46    
47    /* An item (value) on a stack */
48    typedef struct stackitem_struct
49    {
50      value *item;                  /* The value on the stack */
51      struct stackitem_struct *next; /* Next item */
52    } stackitem;
53    
54  typedef stackitem* hashtbl[HASHTBLSIZE];  /* An environment; gives access to the stack and a hash table of
55  typedef void (*funcp)(stackitem**);     defined symbols */
56    typedef struct {
57      stackitem *head;              /* Head of the stack */
58      hashtbl symbols;              /* Hash table of all variable bindings */
59      int err;                      /* Error flag */
60    } environment;
61    
62    /* A type for pointers to external functions */
63    typedef void (*funcp)(environment *); /* funcp is a pointer to a void
64                                             function (environment *) */
65    
66  void init_hashtbl(hashtbl out_hash)  /* Initialize a newly created environment */
67    void init_env(environment *env)
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      out_hash[i]= NULL;      env->symbols[i]= NULL;
74  }  }
75    
76  stackitem** hash(hashtbl in_hashtbl, const char* in_string)  /* Returns a pointer to a pointer to an element in the hash table. */
77    symbol **hash(hashtbl in_hashtbl, const char *in_string)
78  {  {
79    long i= 0;    long i= 0;
80    unsigned long out_hash= 0;    unsigned long out_hash= 0;
81    char key= 0;    char key= '\0';
82    stackitem** position;    symbol **position;
83        
84    while(1){    while(1){                     /* Hash in_string */
85      key= in_string[i++];      key= in_string[i++];
86      if(key=='\0')      if(key=='\0')
87        break;        break;
# Line 50  stackitem** hash(hashtbl in_hashtbl, con Line 92  stackitem** hash(hashtbl in_hashtbl, con
92    position= &(in_hashtbl[out_hash]);    position= &(in_hashtbl[out_hash]);
93    
94    while(1){    while(1){
95      if(*position==NULL)      if(*position==NULL)         /* If empty */
96        return position;        return position;
97            
98      if(strcmp(in_string, (*position)->id)==0)      if(strcmp(in_string, (*position)->id)==0) /* If match */
99        return position;        return position;
100    
101      position= &((*position)->next);      position= &((*position)->next); /* Try next */
102    }    }
103  }  }
104    
105    /* 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;
   return 1;  
110  }  }
111    
112  int push_val(stackitem** stack_head, int in_val)  /* Push a value onto the stack */
113    void push_val(stackitem **stack_head, value *val)
114  {  {
115    stackitem* new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
116    new_item->content.val= in_val;    new_item->item= val;
117    new_item->type= value;    val->refcount++;
   
118    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
119  }  }
120    
121  int push_cstring(stackitem** stack_head, const char* in_string)  /* Push an integer onto the stack. */
122    void push_int(stackitem **stack_head, int in_val)
123  {  {
124    stackitem* new_item= malloc(sizeof(stackitem));    value *new_value= malloc(sizeof(value));
125    new_item->content.ptr= malloc(strlen(in_string)+1);    stackitem *new_item= malloc(sizeof(stackitem));
126    strcpy(new_item->content.ptr, in_string);    new_item->item= new_value;
127    new_item->type= string;    
128      new_value->content.val= in_val;
129      new_value->type= integer;
130      new_value->refcount=1;
131    
132    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
133  }  }
134    
135  int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)  /* Copy a string onto the stack. */
136    void push_cstring(stackitem **stack_head, const char *in_string)
137  {  {
138    in_item->id= malloc(strlen(id)+1);    value *new_value= malloc(sizeof(value));
139      stackitem *new_item= malloc(sizeof(stackitem));
140    strcpy(in_item->id, id);    new_item->item=new_value;
141    push(hash(in_hashtbl, id), in_item);  
142      new_value->content.ptr= malloc(strlen(in_string)+1);
143      strcpy(new_value->content.ptr, in_string);
144      new_value->type= string;
145      new_value->refcount=1;
146    
147    return 1;    push(stack_head, new_item);
148  }  }
149    
150  void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)  /* Push a symbol onto the stack. */
151    void push_sym(environment *env, const char *in_string)
152  {  {
153    stackitem* temp= malloc(sizeof(stackitem));    stackitem *new_item;          /* The new stack item */
154      /* ...which will contain... */
155      value *new_value;             /* A new symbol value */
156      /* ...which might point to... */
157      symbol **new_symbol;          /* (if needed) A new actual symbol */
158      /* ...which, if possible, will be bound to... */
159      value *new_fvalue;            /* (if needed) A new function value */
160      /* ...which will point to... */
161      void *funcptr;                /* A function pointer */
162    
163      static void *handle= NULL;    /* Dynamic linker handle */
164    
165      /* Create a new stack item containing a new value */
166      new_item= malloc(sizeof(stackitem));
167      new_value= malloc(sizeof(value));
168      new_item->item=new_value;
169    
170      /* The new value is a symbol */
171      new_value->type= symb;
172      new_value->refcount= 1;
173    
174      /* Look up the symbol name in the hash table */
175      new_symbol= hash(env->symbols, in_string);
176      new_value->content.ptr= *new_symbol;
177    
178      if(*new_symbol==NULL) { /* If symbol was undefined */
179    
180        /* Create a new symbol */
181        (*new_symbol)= malloc(sizeof(symbol));
182        (*new_symbol)->val= NULL;   /* undefined value */
183        (*new_symbol)->next= NULL;
184        (*new_symbol)->id= malloc(strlen(in_string)+1);
185        strcpy((*new_symbol)->id, in_string);
186    
187        /* Intern the new symbol in the hash table */
188        new_value->content.ptr= *new_symbol;
189    
190        /* 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 */
192        if(handle==NULL)            /* If no handle */
193          handle= dlopen(NULL, RTLD_LAZY);
194    
195        funcptr= dlsym(handle, in_string); /* Get function pointer */
196        if(dlerror()==NULL) {       /* If a function was found */
197          new_fvalue= malloc(sizeof(value)); /* Create a new value */
198          new_fvalue->type=func;    /* The new value is a function pointer */
199          new_fvalue->content.ptr=funcptr; /* Store function pointer */
200          (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
201                                             function value */
202          new_fvalue->refcount= 1;
203        }
204      }
205      push(&(env->head), new_item);
206    }
207    
208    void printerr(const char* in_string) {
209      fprintf(stderr, "Err: %s\n", in_string);
210    }
211    
212    temp->type= func;  /* Throw away a value */
213    temp->content.ptr= in_func;  void free_val(value *val){
214      stackitem *item, *temp;
215    
216    mk_hashentry(in_hashtbl, temp, id);    val->refcount--;              /* Decrease the reference count */
217      if(val->refcount == 0){
218        switch (val->type){         /* and free the contents if necessary */
219        case string:
220          free(val->content.ptr);
221          break;
222        case list:                  /* lists needs to be freed recursively */
223          item=val->content.ptr;
224          while(item != NULL) {     /* for all stack items */
225            free_val(item->item);   /* free the value */
226            temp=item->next;        /* save next ptr */
227            free(item);             /* free the stackitem */
228            item=temp;              /* go to next stackitem */
229          }
230          free(val);                /* Free the actual list value */
231          break;
232        default:
233          break;
234        }
235      }
236  }  }
237    
238  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)  /* Discard the top element of the stack. */
239    extern void toss(environment *env)
240  {  {
241    void* handle;    stackitem *temp= env->head;
   void* symbol;  
     
   stackitem* new_item= malloc(sizeof(stackitem));  
   new_item->content.ptr= *hash(in_hash, in_string);  
   new_item->type= ref;  
242    
243    if(new_item->content.ptr==NULL) {    if((env->head)==NULL) {
244      handle= dlopen(NULL, RTLD_LAZY);      printerr("Too Few Arguments");
245      symbol= dlsym(handle, in_string);      env->err=1;
246      if(dlerror()==NULL) {      return;
       def_func(in_hash, symbol, in_string);  
       new_item->content.ptr= *hash(in_hash, in_string);  
       new_item->type= ref;  
     }  
247    }    }
248      
249      free_val(env->head->item);    /* Free the value */
250      env->head= env->head->next;   /* Remove the top stack item */
251      free(temp);                   /* Free the old top stack item */
252    }
253    
254    push(stack_head, new_item);  /* Print newline. */
255    return 1;  extern void nl()
256    {
257      printf("\n");
258  }  }
259    
260  extern void toss(stackitem** stack_head)  /* Gets the type of a value */
261    extern void type(environment *env){
262      int typenum;
263    
264      if((env->head)==NULL) {
265        printerr("Too Few Arguments");
266        env->err=1;
267        return;
268      }
269      typenum=env->head->item->type;
270      toss(env);
271      switch(typenum){
272      case integer:
273        push_sym(env, "integer");
274        break;
275      case string:
276        push_sym(env, "string");
277        break;
278      case symb:
279        push_sym(env, "symbol");
280        break;
281      case func:
282        push_sym(env, "function");
283        break;
284      case list:
285        push_sym(env, "list");
286        break;
287      default:
288        push_sym(env, "unknown");
289        break;
290      }
291    }    
292    
293    /* Prints the top element of the stack. */
294    void print_h(stackitem *stack_head)
295  {  {
296    stackitem* temp= *stack_head;    switch(stack_head->item->type) {
297      case integer:
298        printf("%d", stack_head->item->content.val);
299        break;
300      case string:
301        printf("\"%s\"", (char*)stack_head->item->content.ptr);
302        break;
303      case symb:
304        printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id);
305        break;
306      case func:
307        printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
308        break;
309      case list:
310        /* A list is just a stack, so make stack_head point to it */
311        stack_head=(stackitem *)(stack_head->item->content.ptr);
312        printf("[ ");
313        while(stack_head != NULL) {
314          print_h(stack_head);
315          printf(" ");
316          stack_head=stack_head->next;
317        }
318        printf("] ");
319        break;
320      default:
321        printf("#<unknown %p>", (stack_head->item->content.ptr));
322        break;
323      }
324    }
325    
326    *stack_head= (*stack_head)->next;  extern void print_(environment *env) {
327    free(temp);    if(env->head==NULL) {
328        printerr("Too Few Arguments");
329        env->err=1;
330        return;
331      }
332      print_h(env->head);
333  }  }
334    
335    /* Prints the top element of the stack and then discards it. */
336    extern void print(environment *env)
337    {
338      print_(env);
339      if(env->err) return;
340      toss(env);
341    }
342    
343  /* print_stack(stack); */  /* Only to be called by function printstack. */
344  void print_st(stackitem* stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
345  {  {
346    if(stack_head->next != NULL)    if(stack_head->next != NULL)
347      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
348      printf("%ld: ", counter);
349    if(stack_head->type==value)    print_h(stack_head);
350      printf("%ld: %d\n", counter, (int)stack_head->content.val);    nl();
   else if(stack_head->type==string)  
     printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);  
   else  
     printf("%ld: %p\n", counter, stack_head->content.ptr);  
351  }  }
352    
353  extern void printstack(stackitem** stack_head)  
354    
355    /* Prints the stack. */
356    extern void printstack(environment *env)
357  {  {
358    if(*stack_head != NULL) {    if(env->head == NULL) {
359      print_st(*stack_head, 1);      return;
     printf("\n");  
360    }    }
361      print_st(env->head, 1);
362      nl();
363  }  }
364    
365    /* Swap the two top elements on the stack. */
366    extern void swap(environment *env)
367    {
368      stackitem *temp= env->head;
369      
370      if((env->head)==NULL) {
371        printerr("Too Few Arguments");
372        env->err=1;
373        return;
374      }
375    
376      if(env->head->next==NULL) {
377        printerr("Too Few Arguments");
378        env->err=1;
379        return;
380      }
381    
382  extern void eval(stackitem** stack_head)    env->head= env->head->next;
383      temp->next= env->head->next;
384      env->head->next= temp;
385    }
386    
387    stackitem* copy(stackitem* in_item)
388  {  {
389    funcp in_func;    stackitem *out_item= malloc(sizeof(stackitem));
390    
391      memcpy(out_item, in_item, sizeof(stackitem));
392      out_item->next= NULL;
393    
394      return out_item;
395    }
396    
397    /* Recall a value from a symbol, if bound */
398    extern void rcl(environment *env)
399    {
400      value *val;
401    
402    if((*stack_head)==NULL || (*stack_head)->type!=ref)    if(env->head == NULL) {
403        printerr("Too Few Arguments");
404        env->err=1;
405      return;      return;
406      }
407    
408      if(env->head->item->type!=symb) {
409        printerr("Bad Argument Type");
410        env->err=2;
411        return;
412      }
413    
414      val=((symbol *)(env->head->item->content.ptr))->val;
415      if(val == NULL){
416        printerr("Unbound Variable");
417        env->err=3;
418        return;
419      }
420      toss(env);            /* toss the symbol */
421      if(env->err) return;
422      push_val(&(env->head), val); /* Return its bound value */
423    }
424    
425    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {  /* If the top element is a symbol, determine if it's bound to a
426      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;     function value, and if it is, toss the symbol and execute the
427      toss(stack_head);     function. */
428      (*in_func)(stack_head);  extern void eval(environment *env)
429    {
430      funcp in_func;
431      if(env->head==NULL) {
432        printerr("Too Few Arguments");
433        env->err=1;
434      return;      return;
435    }    }
436    
437      /* if it's a symbol */
438      if(env->head->item->type==symb) {
439    
440        rcl(env);                   /* get its contents */
441        if(env->err) return;
442        if(env->head->item->type!=symb){ /* don't recurse symbols */
443          eval(env);                        /* evaluate the value */
444          return;
445        }
446      }
447    
448      /* If it's a lone function value, run it */
449      if(env->head->item->type==func) {
450        in_func= (funcp)(env->head->item->content.ptr);
451        toss(env);
452        if(env->err) return;
453        (*in_func)(env);
454      }
455    }
456    
457    /* Make a list. */
458    extern void pack(environment *env)
459    {
460      void* delimiter;
461      stackitem *iterator, *temp;
462      value *pack;
463    
464      delimiter= env->head->item->content.ptr; /* Get delimiter */
465      toss(env);
466    
467      iterator= env->head;
468    
469      if(iterator==NULL || iterator->item->content.ptr==delimiter) {
470        temp= NULL;
471        toss(env);
472      } else {
473        /* Search for first delimiter */
474        while(iterator->next!=NULL
475              && iterator->next->item->content.ptr!=delimiter)
476          iterator= iterator->next;
477        
478        /* Extract list */
479        temp= env->head;
480        env->head= iterator->next;
481        iterator->next= NULL;
482        
483        if(env->head!=NULL)
484          toss(env);
485      }
486    
487      /* Push list */
488      pack= malloc(sizeof(value));
489      pack->type= list;
490      pack->content.ptr= temp;
491      pack->refcount= 1;
492    
493      temp= malloc(sizeof(stackitem));
494      temp->item= pack;
495    
496      push(&(env->head), temp);
497  }  }
498    
499  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)  /* Parse input. */
500    void stack_read(environment *env, char *in_line)
501  {  {
502    char *temp, *rest;    char *temp, *rest;
503    int itemp;    int itemp;
504    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
505    int convert= 0;    int convert= 0;
506      static int non_eval_flag= 0;
507    
508    temp= malloc(inlength);    temp= malloc(inlength);
509    rest= malloc(inlength);    rest= malloc(inlength);
510    
511    if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1)    do {
512      push_cstring(stack_head, temp);      /* If string */
513    else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
514      push_val(stack_head, itemp);        push_cstring(&(env->head), temp);
515    else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)        break;
516      push_ref(stack_head, in_hash, temp);      }
517    else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)      /* If integer */
518      if(*temp==';')      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
519        eval(stack_head);        push_int(&(env->head), itemp);
520          break;
521        }
522        /* Escape ';' with '\' */
523        if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
524          temp[1]= '\0';
525          push_sym(env, temp);
526          break;
527        }
528        /* If symbol */
529        if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
530            push_sym(env, temp);
531            break;
532        }
533        /* If single char */
534        if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
535          if(*temp==';') {
536            if(!non_eval_flag) {
537              eval(env);            /* Evaluate top element */
538              break;
539            }
540            
541            push_sym(env, ";");
542            break;
543          }
544    
545          if(*temp==']') {
546            push_sym(env, "[");
547            pack(env);
548            if(non_eval_flag!=0)
549              non_eval_flag--;
550            break;
551          }
552    
553          if(*temp=='[') {
554            push_sym(env, "[");
555            non_eval_flag++;
556            break;
557          }
558        }
559      } while(0);
560    
561    free(temp);    free(temp);
562    
563    if(convert<2) {    if(convert<2) {
564      free(rest);      free(rest);
565      return 0;      return;
566    }    }
567        
568    stack_read(stack_head, in_hash, rest);    stack_read(env, rest);
569        
570    free(rest);    free(rest);
   return 1;  
571  }  }
572    
573  extern void print(stackitem** stack_head)  /* Relocate elements of the list on the stack. */
574    extern void expand(environment *env)
575  {  {
576    if((*stack_head)==NULL)    stackitem *temp, *new_head;
577    
578      /* Is top element a list? */
579      if(env->head==NULL) {
580        printerr("Too Few Arguments");
581        env->err=1;
582        return;
583      }
584      if(env->head->item->type!=list) {
585        printerr("Bad Argument Type");
586        env->err=2;
587      return;      return;
588      }
589    
590      /* The first list element is the new stack head */
591      new_head= temp= env->head->item->content.ptr;
592    
593      env->head->item->refcount++;
594      toss(env);
595    
596    if((*stack_head)->type==value)    /* Find the end of the list */
597      printf("%d", (*stack_head)->content.val);    while(temp->next!=NULL)
598    else if((*stack_head)->type==string)      temp= temp->next;
599      printf("%s", (char*)(*stack_head)->content.ptr);  
600    else    /* Connect the tail of the list with the old stack head */
601      printf("%p", (*stack_head)->content.ptr);    temp->next= env->head;
602      env->head= new_head;          /* ...and voila! */
603    
   toss(stack_head);  
604  }  }
605    
606  extern void nl()  /* Compares two elements by reference. */
607    extern void eq(environment *env)
608  {  {
609    printf("\n");    void *left, *right;
610      int result;
611    
612      if((env->head)==NULL || env->head->next==NULL) {
613        printerr("Too Few Arguments");
614        env->err=1;
615        return;
616      }
617    
618      left= env->head->item->content.ptr;
619      swap(env);
620      right= env->head->item->content.ptr;
621      result= (left==right);
622      
623      toss(env); toss(env);
624      push_int(&(env->head), result);
625    }
626    
627    /* Negates the top element on the stack. */
628    extern void not(environment *env)
629    {
630      int val;
631    
632      if((env->head)==NULL) {
633        printerr("Too Few Arguments");
634        env->err=1;
635        return;
636      }
637    
638      if(env->head->item->type!=integer) {
639        printerr("Bad Argument Type");
640        env->err=2;
641        return;
642      }
643    
644      val= env->head->item->content.val;
645      toss(env);
646      push_int(&(env->head), !val);
647    }
648    
649    /* Compares the two top elements on the stack and return 0 if they're the
650       same. */
651    extern void neq(environment *env)
652    {
653      eq(env);
654      not(env);
655    }
656    
657    /* Give a symbol some content. */
658    extern void def(environment *env)
659    {
660      symbol *sym;
661    
662      /* Needs two values on the stack, the top one must be a symbol */
663      if(env->head==NULL || env->head->next==NULL) {
664        printerr("Too Few Arguments");
665        env->err=1;
666        return;
667      }
668    
669      if(env->head->item->type!=symb) {
670        printerr("Bad Argument Type");
671        env->err=2;
672        return;
673      }
674    
675      /* long names are a pain */
676      sym=env->head->item->content.ptr;
677    
678      /* if the symbol was bound to something else, throw it away */
679      if(sym->val != NULL)
680        free_val(sym->val);
681    
682      /* Bind the symbol to the value */
683      sym->val= env->head->next->item;
684      sym->val->refcount++;         /* Increase the reference counter */
685    
686      toss(env); toss(env);
687    }
688    
689    /* Quit stack. */
690    extern void quit(environment *env)
691    {
692      exit(EXIT_SUCCESS);
693    }
694    
695    /* Clear stack */
696    extern void clear(environment *env)
697    {
698      while(env->head!=NULL)
699        toss(env);
700    }
701    
702    /* List all defined words */
703    extern void words(environment *env)
704    {
705      symbol *temp;
706      int i;
707      
708      for(i= 0; i<HASHTBLSIZE; i++) {
709        temp= env->symbols[i];
710        while(temp!=NULL) {
711          printf("%s\n", temp->id);
712          temp= temp->next;
713        }
714      }
715    }
716    
717    /* Forgets a symbol (remove it from the hash table) */
718    extern void forget(environment *env)
719    {
720      char* sym_id;
721      stackitem *stack_head= env->head;
722      symbol **hash_entry, *temp;
723    
724      if(stack_head==NULL) {
725        printerr("Too Few Arguments");
726        env->err=1;
727        return;
728      }
729      
730      if(stack_head->item->type!=symb) {
731        printerr("Bad Argument Type");
732        env->err=2;
733        return;
734      }
735    
736      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
737      toss(env);
738    
739      hash_entry= hash(env->symbols, sym_id);
740      temp= *hash_entry;
741      *hash_entry= (*hash_entry)->next;
742      
743      if(temp->val!=NULL) {
744        free_val(temp->val);
745      }
746      free(temp->id);
747      free(temp);
748    }
749    
750    /* Returns the current error number to the stack */
751    extern void errn(environment *env){
752      push_int(&(env->head), env->err);
753  }  }
754    
755  int main()  int main()
756  {  {
757    stackitem* s= NULL;    environment myenv;
   hashtbl myhash;  
758    char in_string[100];    char in_string[100];
759    
760    init_hashtbl(myhash);    init_env(&myenv);
761    
762    printf("okidok\n ");    printf("okidok\n ");
763    
764    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
765      stack_read(&s, myhash, in_string);      stack_read(&myenv, in_string);
766        if(myenv.err) {
767          printf("(error %d) ", myenv.err);
768          myenv.err=0;
769        }
770      printf("okidok\n ");      printf("okidok\n ");
771    }    }
772    
773      exit(EXIT_SUCCESS);
   return EXIT_SUCCESS;  
774  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.38

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26