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

Diff of /stack/stack.c

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

revision 1.17 by masse, Thu Jan 31 23:09:07 2002 UTC revision 1.37 by teddy, Wed Feb 6 01:51:08 2002 UTC
# Line 6  Line 6 
6  #include <stddef.h>  #include <stddef.h>
7  /* dlopen, dlsym, dlerror */  /* 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    /* A value of some type */
17    typedef struct {
18    enum {    enum {
19      value,                      /* Integer */      integer,
20      string,      string,
     ref,            /* Reference (to an element in the hash table) */  
21      func,                       /* Function pointer */      func,                       /* Function pointer */
22      symbol,      symb,
23      list      list
24    } type;                       /* Tells what kind of stack element */    } type;                       /* Type of stack element */
25      
26    union {    union {
27      void* ptr;                  /* Pointer to the content */      void *ptr;                  /* Pointer to the content */
28      int val;                    /* ...or an integer */      int val;                    /* ...or an integer */
29    } content;                    /* Stores a pointer or an integer */    } content;                    /* Stores a pointer or an integer */
30    
31    char* id;                     /* Symbol name */    int refcount;                 /* Reference counter */
32    struct stack_item* next;      /* Next element */  
33  } stackitem;  } 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  typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */  /* A type for a hash table for symbols */
45  typedef void (*funcp)(stackitem**); /* Function pointer declaration */  typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */
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    /* An environment; gives access to the stack and a hash table of
55       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  /* Initiates a newly created hash table. */  /* Initialize a newly created environment */
67  void init_hashtbl(hashtbl out_hash)  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  /* Returns a pointer to an element in the hash table. */  /* Returns a pointer to a pointer to an element in the hash table. */
77  stackitem** hash(hashtbl in_hashtbl, const char* in_string)  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){                     /* Hash in_string */    while(1){                     /* Hash in_string */
85      key= in_string[i++];      key= in_string[i++];
# Line 61  stackitem** hash(hashtbl in_hashtbl, con Line 91  stackitem** hash(hashtbl in_hashtbl, con
91    out_hash= out_hash%HASHTBLSIZE;    out_hash= out_hash%HASHTBLSIZE;
92    position= &(in_hashtbl[out_hash]);    position= &(in_hashtbl[out_hash]);
93    
94    while(1){                     /* Return position if empty */    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) /* Return position if match */      if(strcmp(in_string, (*position)->id)==0) /* If match */
99        return position;        return position;
100    
101      position= &((*position)->next); /* Try next */      position= &((*position)->next); /* Try next */
# Line 73  stackitem** hash(hashtbl in_hashtbl, con Line 103  stackitem** hash(hashtbl in_hashtbl, con
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;
   return 1;  
110  }  }
111    
112  /* Push a value on the stack. */  /* Push a value onto the stack */
113  int push_val(stackitem** stack_head, int in_val)  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  /* Copy a string onto the stack. */  /* Push an integer onto the stack. */
122  int push_cstring(stackitem** stack_head, const char* in_string)  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  /* Create a new hash entry. */  /* Copy a string onto the stack. */
136  int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)  void push_cstring(stackitem **stack_head, const char *in_string)
 {  
   in_item->id= malloc(strlen(id)+1);  
   
   strcpy(in_item->id, id);  
   push(hash(in_hashtbl, id), in_item);  
   
   return 1;  
 }  
   
 /* Define a function a new function in the hash table. */  
 void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)  
137  {  {
138    stackitem* temp= malloc(sizeof(stackitem));    value *new_value= malloc(sizeof(value));
139      stackitem *new_item= malloc(sizeof(stackitem));
140      new_item->item=new_value;
141    
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    temp->type= func;    push(stack_head, new_item);
   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);  
148  }  }
149    
150  /* Push a reference to an entry in the hash table onto the stack. */  /* Push a symbol onto the stack. */
151  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)  void push_sym(environment *env, const char *in_string)
152  {  {
153    static void* handle= NULL;    stackitem *new_item;          /* The new stack item */
154    void* symbol;    /* ...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    stackitem* new_item= malloc(sizeof(stackitem));      /* Intern the new symbol in the hash table */
188    new_item->content.ptr= *hash(in_hash, in_string);      new_value->content.ptr= *new_symbol;
   new_item->type= ref;  
189    
190    if(new_item->content.ptr==NULL) { /* If hash entry empty */      /* 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 */      if(handle==NULL)            /* If no handle */
193        handle= dlopen(NULL, RTLD_LAZY);            handle= dlopen(NULL, RTLD_LAZY);
194    
195      symbol= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
196      if(dlerror()==NULL)         /* If existing function pointer */      if(dlerror()==NULL) {       /* If a function was found */
197        def_func(in_hash, symbol, in_string); /* Store function pointer */        new_fvalue= malloc(sizeof(value)); /* Create a new value */
198      else        new_fvalue->type=func;    /* The new value is a function pointer */
199        def_sym(in_hash, in_string); /* Make symbol */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
200                (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
201      new_item->content.ptr= *hash(in_hash, in_string); /* XXX */                                           function value */
202      new_item->type= ref;        new_fvalue->refcount= 1;
203        }
204    }    }
205      push(&(env->head), new_item);
   push(stack_head, new_item);  
   return 1;  
206  }  }
207    
208  void printerr(const char* in_string) {  void printerr(const char* in_string) {
209    fprintf(stderr, "Err: %s\n", in_string);    fprintf(stderr, "Err: %s\n", in_string);
210  }  }
211    
212    /* Throw away a value */
213    void free_val(value *val){
214      stackitem *item, *temp;
215    
216      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  /* Discard the top element of the stack. */  /* Discard the top element of the stack. */
239  extern void toss(stackitem** stack_head)  extern void toss(environment *env)
240  {  {
241    stackitem* temp= *stack_head;    stackitem *temp= env->head;
242    
243    if((*stack_head)==NULL) {    if((env->head)==NULL) {
244      printerr("Stack empty");      printerr("Too Few Arguments");
245        env->err=1;
246      return;      return;
247    }    }
248        
249    if((*stack_head)->type==string)    free_val(env->head->item);    /* Free the value */
250      free((*stack_head)->content.ptr);    env->head= env->head->next;   /* Remove the top stack item */
251      free(temp);                   /* Free the old top stack item */
   *stack_head= (*stack_head)->next;  
   free(temp);  
252  }  }
253    
254  /* Print newline. */  /* Print newline. */
# Line 189  extern void nl() Line 257  extern void nl()
257    printf("\n");    printf("\n");
258  }  }
259    
260  /* Prints the top element of the stack. */  /* Gets the type of a value */
261  void print_(stackitem** stack_head)  extern void type(environment *env){
262  {    int typenum;
263    if((*stack_head)==NULL) {  
264      printerr("Stack empty");    if((env->head)==NULL) {
265        printerr("Too Few Arguments");
266        env->err=1;
267      return;      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    switch((*stack_head)->type) {  /* Prints the top element of the stack. */
294    case value:  void print_h(stackitem *stack_head)
295      printf("%d", (*stack_head)->content.val);  {
296      switch(stack_head->item->type) {
297      case integer:
298        printf("%d", stack_head->item->content.val);
299      break;      break;
300    case string:    case string:
301      printf("%s", (char*)(*stack_head)->content.ptr);      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;      break;
306    case ref:    case func:
307      printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);      printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
308        break;
309      case list:
310        printf("#<list %p>", (funcp)(stack_head->item->content.ptr));
311      break;      break;
   case symbol:  
312    default:    default:
313      printf("%p", (*stack_head)->content.ptr);      printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr));
314      break;      break;
315    }    }
316  }  }
317    
318    extern void print_(environment *env) {
319      if(env->head==NULL) {
320        printerr("Too Few Arguments");
321        env->err=1;
322        return;
323      }
324      print_h(env->head);
325    }
326    
327  /* Prints the top element of the stack and then discards it. */  /* Prints the top element of the stack and then discards it. */
328  extern void print(stackitem** stack_head)  extern void print(environment *env)
329  {  {
330    print_(stack_head);    print_(env);
331    toss(stack_head);    if(env->err) return;
332      toss(env);
333  }  }
334    
335  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
336  void print_st(stackitem* stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
337  {  {
338    if(stack_head->next != NULL)    if(stack_head->next != NULL)
339      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
   
340    printf("%ld: ", counter);    printf("%ld: ", counter);
341    print_(&stack_head);    print_h(stack_head);
342    nl();    nl();
343  }  }
344    
345    
346    
347  /* Prints the stack. */  /* Prints the stack. */
348  extern void printstack(stackitem** stack_head)  extern void printstack(environment *env)
349  {  {
350    if(*stack_head != NULL) {    if(env->head == NULL) {
351      print_st(*stack_head, 1);      return;
     printf("\n");  
   } else {  
     printerr("Stack empty");  
352    }    }
353      print_st(env->head, 1);
354      nl();
355  }  }
356    
357  /* If the top element is a reference, determine if it's a reference to a  /* Swap the two top elements on the stack. */
358     function, and if it is, toss the reference and execute the function. */  extern void swap(environment *env)
 extern void eval(stackitem** stack_head)  
359  {  {
360    funcp in_func;    stackitem *temp= env->head;
361      
362      if((env->head)==NULL) {
363        printerr("Too Few Arguments");
364        env->err=1;
365        return;
366      }
367    
368      if(env->head->next==NULL) {
369        printerr("Too Few Arguments");
370        env->err=1;
371        return;
372      }
373    
374      env->head= env->head->next;
375      temp->next= env->head->next;
376      env->head->next= temp;
377    }
378    
379    stackitem* copy(stackitem* in_item)
380    {
381      stackitem *out_item= malloc(sizeof(stackitem));
382    
383      memcpy(out_item, in_item, sizeof(stackitem));
384      out_item->next= NULL;
385    
386      return out_item;
387    }
388    
389    if((*stack_head)==NULL || (*stack_head)->type!=ref) {  /* Recall a value from a symbol, if bound */
390      printerr("Stack empty or not a reference");  extern void rcl(environment *env)
391    {
392      value *val;
393    
394      if(env->head == NULL) {
395        printerr("Too Few Arguments");
396        env->err=1;
397        return;
398      }
399    
400      if(env->head->item->type!=symb) {
401        printerr("Bad Argument Type");
402        env->err=2;
403      return;      return;
404    }    }
405    
406    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {    val=((symbol *)(env->head->item->content.ptr))->val;
407      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;    if(val == NULL){
408      toss(stack_head);      printerr("Unbound Variable");
409      (*in_func)(stack_head);      env->err=3;
410      return;      return;
411    } else    }
412      printerr("Not a function");    toss(env);            /* toss the symbol */
413      if(env->err) return;
414      push_val(&(env->head), val); /* Return its bound value */
415    }
416    
417    /* If the top element is a symbol, determine if it's bound to a
418       function value, and if it is, toss the symbol and execute the
419       function. */
420    extern void eval(environment *env)
421    {
422      funcp in_func;
423      if(env->head==NULL) {
424        printerr("Too Few Arguments");
425        env->err=1;
426        return;
427      }
428    
429      /* if it's a symbol */
430      if(env->head->item->type==symb) {
431    
432        rcl(env);                   /* get its contents */
433        if(env->err) return;
434        if(env->head->item->type!=symb){ /* don't recurse symbols */
435          eval(env);                        /* evaluate the value */
436          return;
437        }
438      }
439    
440      /* If it's a lone function value, run it */
441      if(env->head->item->type==func) {
442        in_func= (funcp)(env->head->item->content.ptr);
443        toss(env);
444        if(env->err) return;
445        (*in_func)(env);
446      }
447    }
448    
449    /* Make a list. */
450    extern void pack(environment *env)
451    {
452      void* delimiter;
453      stackitem *iterator, *temp;
454      value *pack;
455    
456      delimiter= env->head->item->content.ptr; /* Get delimiter */
457      toss(env);
458    
459      iterator= env->head;
460    
461      if(iterator==NULL || iterator->item->content.ptr==delimiter) {
462        temp= NULL;
463        toss(env);
464      } else {
465        /* Search for first delimiter */
466        while(iterator->next!=NULL
467              && iterator->next->item->content.ptr!=delimiter)
468          iterator= iterator->next;
469        
470        /* Extract list */
471        temp= env->head;
472        env->head= iterator->next;
473        iterator->next= NULL;
474        
475        if(env->head!=NULL)
476          toss(env);
477      }
478    
479      /* Push list */
480      pack= malloc(sizeof(value));
481      pack->type= list;
482      pack->content.ptr= temp;
483      pack->refcount= 1;
484    
485      temp= malloc(sizeof(stackitem));
486      temp->item= pack;
487    
488      push(&(env->head), temp);
489  }  }
490    
491  /* Parse input. */  /* Parse input. */
492  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)  void stack_read(environment *env, char *in_line)
493  {  {
494    char *temp, *rest;    char *temp, *rest;
495    int itemp;    int itemp;
496    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
497    int convert= 0;    int convert= 0;
498      static int non_eval_flag= 0;
499    
500    temp= malloc(inlength);    temp= malloc(inlength);
501    rest= malloc(inlength);    rest= malloc(inlength);
# Line 277  int stack_read(stackitem** stack_head, h Line 503  int stack_read(stackitem** stack_head, h
503    do {    do {
504      /* If string */      /* If string */
505      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
506        push_cstring(stack_head, temp);        push_cstring(&(env->head), temp);
507        break;        break;
508      }      }
509      /* If value */      /* If integer */
510      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
511        push_val(stack_head, itemp);        push_int(&(env->head), itemp);
512        break;        break;
513      }      }
514      /* Escape ';' with '\' */      /* Escape ';' with '\' */
515      if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
516        temp[1]= '\0';        temp[1]= '\0';
517        push_ref(stack_head, in_hash, temp);        push_sym(env, temp);
518        break;        break;
519      }      }
520      /* If symbol */      /* If symbol */
521      if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
522          push_ref(stack_head, in_hash, temp);          push_sym(env, temp);
523          break;          break;
524      }      }
525      /* If ';' */      /* If single char */
526      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
527        eval(stack_head);         /* Evaluate top element */        if(*temp==';') {
528        break;          if(!non_eval_flag) {
529              eval(env);            /* Evaluate top element */
530              break;
531            }
532            
533            push_sym(env, ";");
534            break;
535          }
536    
537          if(*temp==']') {
538            push_sym(env, "[");
539            pack(env);
540            if(non_eval_flag!=0)
541              non_eval_flag--;
542            break;
543          }
544    
545          if(*temp=='[') {
546            push_sym(env, "[");
547            non_eval_flag++;
548            break;
549          }
550      }      }
551    } while(0);    } while(0);
552    
   
553    free(temp);    free(temp);
554    
555    if(convert<2) {    if(convert<2) {
556      free(rest);      free(rest);
557      return 0;      return;
558    }    }
559        
560    stack_read(stack_head, in_hash, rest);    stack_read(env, rest);
561        
562    free(rest);    free(rest);
   return 1;  
 }  
   
 /* Make a list. */  
 extern void pack(stackitem** stack_head)  
 {  
   void* delimiter;  
   stackitem *iterator, *temp, *pack;  
   
   if((*stack_head)==NULL) {  
     printerr("Stack empty");  
     return;  
   }  
   
   delimiter= (*stack_head)->content.ptr; /* Get delimiter */  
   toss(stack_head);  
   
   iterator= *stack_head;  
   
   /* Search for first delimiter */  
   while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)  
     iterator= iterator->next;  
   
   /* Extract list */  
   temp= *stack_head;  
   *stack_head= iterator->next;  
   iterator->next= NULL;  
     
   if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)  
     toss(stack_head);  
   
   /* Push list */  
   pack= malloc(sizeof(stackitem));  
   pack->type= list;  
   pack->content.ptr= temp;  
   
   push(stack_head, pack);  
563  }  }
564    
565  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
566  extern void expand(stackitem** stack_head)  extern void expand(environment *env)
567  {  {
568    stackitem *temp, *new_head;    stackitem *temp, *new_head;
569    
570    /* Is top element a list? */    /* Is top element a list? */
571    if((*stack_head)==NULL || (*stack_head)->type!=list) {    if(env->head==NULL) {
572      printerr("Stack empty or not a list");      printerr("Too Few Arguments");
573        env->err=1;
574        return;
575      }
576      if(env->head->item->type!=list) {
577        printerr("Bad Argument Type");
578        env->err=2;
579      return;      return;
580    }    }
581    
582    /* The first list element is the new stack head */    /* The first list element is the new stack head */
583    new_head= temp= (*stack_head)->content.ptr;    new_head= temp= env->head->item->content.ptr;
   toss(stack_head);  
584    
585    /* Search the end of the list */    env->head->item->refcount++;
586      toss(env);
587    
588      /* Find the end of the list */
589    while(temp->next!=NULL)    while(temp->next!=NULL)
590      temp= temp->next;      temp= temp->next;
591    
592    /* Connect the the tail of the list with the old stack head */    /* Connect the tail of the list with the old stack head */
593    temp->next= *stack_head;    temp->next= env->head;
594    *stack_head= new_head;        /* ...and voila! */    env->head= new_head;          /* ...and voila! */
 }  
   
 /* 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;  
595    
596    *stack_head= (*stack_head)->next;  }
   temp->next= (*stack_head)->next;  
   (*stack_head)->next= temp;  
 }  
597    
598  /* Compares two elements by reference. */  /* Compares two elements by reference. */
599  extern void eq(stackitem** stack_head)  extern void eq(environment *env)
600  {  {
601    void *left, *right;    void *left, *right;
602    int result;    int result;
603    
604    if((*stack_head)==NULL || (*stack_head)->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
605      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
606        env->err=1;
607      return;      return;
608    }    }
609    
610    left= (*stack_head)->content.ptr;    left= env->head->item->content.ptr;
611    swap(stack_head);    swap(env);
612    right= (*stack_head)->content.ptr;    right= env->head->item->content.ptr;
613    result= (left==right);    result= (left==right);
614        
615    toss(stack_head); toss(stack_head);    toss(env); toss(env);
616    push_val(stack_head, (left==right));    push_int(&(env->head), result);
617  }  }
618    
619  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
620  extern void not(stackitem** stack_head)  extern void not(environment *env)
621  {  {
622    int value;    int val;
623    
624    if((*stack_head)==NULL || (*stack_head)->type!=value) {    if((env->head)==NULL) {
625      printerr("Stack empty or element is not a value");      printerr("Too Few Arguments");
626        env->err=1;
627      return;      return;
628    }    }
629    
630    value= (*stack_head)->content.val;    if(env->head->item->type!=integer) {
631    toss(stack_head);      printerr("Bad Argument Type");
632    push_val(stack_head, !value);      env->err=2;
633        return;
634      }
635    
636      val= env->head->item->content.val;
637      toss(env);
638      push_int(&(env->head), !val);
639  }  }
640    
641  /* 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
642     same. */     same. */
643  extern void neq(stackitem** stack_head)  extern void neq(environment *env)
644  {  {
645    eq(stack_head);    eq(env);
646    not(stack_head);    not(env);
647  }  }
648    
649  /* Give a symbol some content. */  /* Give a symbol some content. */
650  extern void def(stackitem** stack_head)  extern void def(environment *env)
651  {  {
652    stackitem *temp, *value;    symbol *sym;
653    
654      /* Needs two values on the stack, the top one must be a symbol */
655      if(env->head==NULL || env->head->next==NULL) {
656        printerr("Too Few Arguments");
657        env->err=1;
658        return;
659      }
660    
661    if(*stack_head==NULL || (*stack_head)->next==NULL    if(env->head->item->type!=symb) {
662       || (*stack_head)->type!=ref) {      printerr("Bad Argument Type");
663      printerr("Define what?");      env->err=2;
664      return;      return;
665    }    }
666    
667    temp= (*stack_head)->content.ptr;    /* long names are a pain */
668    value= (*stack_head)->next;    sym=env->head->item->content.ptr;
   temp->content= value->content;  
   value->content.ptr=NULL;  
   temp->type= value->type;  
669    
670    toss(stack_head); toss(stack_head);    /* if the symbol was bound to something else, throw it away */
671      if(sym->val != NULL)
672        free_val(sym->val);
673    
674      /* Bind the symbol to the value */
675      sym->val= env->head->next->item;
676      sym->val->refcount++;         /* Increase the reference counter */
677    
678      toss(env); toss(env);
679  }  }
680    
681  /* Quit stack. */  /* Quit stack. */
682  extern void quit()  extern void quit(environment *env)
683  {  {
684    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
685  }  }
686    
687    /* Clear stack */
688    extern void clear(environment *env)
689    {
690      while(env->head!=NULL)
691        toss(env);
692    }
693    
694    /* List all defined words */
695    extern void words(environment *env)
696    {
697      symbol *temp;
698      int i;
699      
700      for(i= 0; i<HASHTBLSIZE; i++) {
701        temp= env->symbols[i];
702        while(temp!=NULL) {
703          printf("%s\n", temp->id);
704          temp= temp->next;
705        }
706      }
707    }
708    
709    /* Forgets a symbol (remove it from the hash table) */
710    extern void forget(environment *env)
711    {
712      char* sym_id;
713      stackitem *stack_head= env->head;
714      symbol **hash_entry, *temp;
715    
716      if(stack_head==NULL) {
717        printerr("Too Few Arguments");
718        env->err=1;
719        return;
720      }
721      
722      if(stack_head->item->type!=symb) {
723        printerr("Bad Argument Type");
724        env->err=2;
725        return;
726      }
727    
728      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
729      toss(env);
730    
731      hash_entry= hash(env->symbols, sym_id);
732      temp= *hash_entry;
733      *hash_entry= (*hash_entry)->next;
734      
735      if(temp->val!=NULL) {
736        free_val(temp->val);
737      }
738      free(temp->id);
739      free(temp);
740    }
741    
742    /* Returns the current error number to the stack */
743    extern void errn(environment *env){
744      push_int(&(env->head), env->err);
745    }
746    
747  int main()  int main()
748  {  {
749    stackitem* s= NULL;    environment myenv;
   hashtbl myhash;  
750    char in_string[100];    char in_string[100];
751    
752    init_hashtbl(myhash);    init_env(&myenv);
753    
754    printf("okidok\n ");    printf("okidok\n ");
755    
756    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
757      stack_read(&s, myhash, in_string);      stack_read(&myenv, in_string);
758        if(myenv.err) {
759          printf("(error %d) ", myenv.err);
760          myenv.err=0;
761        }
762      printf("okidok\n ");      printf("okidok\n ");
763    }    }
764    
765    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
766  }  }
   
 /* Local Variables: */  
 /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */  
 /* End: */  

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.37

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26