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

Diff of /stack/stack.c

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

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

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.34

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26