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

Diff of /stack/stack.c

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

revision 1.15 by masse, Thu Jan 31 21:47:20 2002 UTC revision 1.59 by teddy, Fri Feb 8 04:58:23 2002 UTC
# Line 1  Line 1 
1  /* printf */  /* printf, sscanf, fgets, fprintf */
2  #include <stdio.h>  #include <stdio.h>
3  /* EXIT_SUCCESS */  /* exit, EXIT_SUCCESS, malloc, free */
4  #include <stdlib.h>  #include <stdlib.h>
5  /* NULL */  /* NULL */
6  #include <stddef.h>  #include <stddef.h>
7  /* dlopen, dlsym, dlerror */  /* dlopen, dlsym, dlerror */
8  #include <dlfcn.h>  #include <dlfcn.h>
9    /* strcmp, strcpy, strlen, strcat, strdup */
10    #include <string.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
13    
14  typedef struct stack_item  /* First, define some types. */
 {  
   enum {value, string, ref, func, symbol, list} type;  
   union {  
     void* ptr;  
     int val;  
   } content;  
15    
16    char* id;  /* A value of some type */
17    struct stack_item* next;  typedef struct {
18  } stackitem;    enum {
19        integer,
20        string,
21        func,                       /* Function pointer */
22        symb,
23        list
24      } type;                       /* Type of stack element */
25    
26      union {
27        void *ptr;                  /* Pointer to the content */
28        int val;                    /* ...or an integer */
29      } 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    /* A type for a hash table for symbols */
45    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                                    /* (This is never NULL) */
52      struct stackitem_struct *next; /* Next item */
53    } stackitem;
54    
55  typedef stackitem* hashtbl[HASHTBLSIZE];  /* An environment; gives access to the stack and a hash table of
56  typedef void (*funcp)(stackitem**);     defined symbols */
57    typedef struct {
58      stackitem *head;              /* Head of the stack */
59      hashtbl symbols;              /* Hash table of all variable bindings */
60      int err;                      /* Error flag */
61      int non_eval_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  /* Initiates 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;    int i;
72    
73      env->err= 0;
74      env->non_eval_flag= 0;
75    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
76      out_hash[i]= NULL;      env->symbols[i]= NULL;
77    }
78    
79    void printerr(const char* in_string) {
80      fprintf(stderr, "Err: %s\n", in_string);
81    }
82    
83    /* Throw away a value */
84    void free_val(value *val){
85      stackitem *item, *temp;
86    
87      val->refcount--;              /* Decrease the reference count */
88      if(val->refcount == 0){
89        switch (val->type){         /* and free the contents if necessary */
90        case string:
91          free(val->content.ptr);
92          break;
93        case list:                  /* lists needs to be freed recursively */
94          item=val->content.ptr;
95          while(item != NULL) {     /* for all stack items */
96            free_val(item->item);   /* free the value */
97            temp=item->next;        /* save next ptr */
98            free(item);             /* free the stackitem */
99            item=temp;              /* go to next stackitem */
100          }
101          free(val);                /* Free the actual list value */
102          break;
103        case integer:
104        case func:
105        case symb:
106          break;
107        }
108      }
109  }  }
110    
111  /* Returns a pointer to an element in the hash table. */  /* Discard the top element of the stack. */
112  stackitem** hash(hashtbl in_hashtbl, const char* in_string)  extern void toss(environment *env)
113  {  {
114    long i= 0;    stackitem *temp= env->head;
115    unsigned long out_hash= 0;  
116    char key= 0;    if((env->head)==NULL) {
117    stackitem** position;      printerr("Too Few Arguments");
118        env->err=1;
119        return;
120      }
121        
122    while(1){    free_val(env->head->item);    /* Free the value */
123      env->head= env->head->next;   /* Remove the top stack item */
124      free(temp);                   /* Free the old top stack item */
125    }
126    
127    /* Returns a pointer to a pointer to an element in the hash table. */
128    symbol **hash(hashtbl in_hashtbl, const char *in_string)
129    {
130      int i= 0;
131      unsigned int out_hash= 0;
132      char key= '\0';
133      symbol **position;
134      
135      while(1){                     /* Hash in_string */
136      key= in_string[i++];      key= in_string[i++];
137      if(key=='\0')      if(key=='\0')
138        break;        break;
# Line 54  stackitem** hash(hashtbl in_hashtbl, con Line 143  stackitem** hash(hashtbl in_hashtbl, con
143    position= &(in_hashtbl[out_hash]);    position= &(in_hashtbl[out_hash]);
144    
145    while(1){    while(1){
146      if(*position==NULL)      if(*position==NULL)         /* If empty */
147        return position;        return position;
148            
149      if(strcmp(in_string, (*position)->id)==0)      if(strcmp(in_string, (*position)->id)==0) /* If match */
150        return position;        return position;
151    
152      position= &((*position)->next);      position= &((*position)->next); /* Try next */
153    }    }
154  }  }
155    
156  /* Generic push function. */  /* Generic push function. */
157  int push(stackitem** stack_head, stackitem* in_item)  void push(stackitem** stack_head, stackitem* in_item)
158  {  {
159    in_item->next= *stack_head;    in_item->next= *stack_head;
160    *stack_head= in_item;    *stack_head= in_item;
   return 1;  
161  }  }
162    
163  /* Push a value on the stack. */  /* Push a value onto the stack */
164  int push_val(stackitem** stack_head, int in_val)  void push_val(stackitem **stack_head, value *val)
165  {  {
166    stackitem* new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
167    new_item->content.val= in_val;    new_item->item= val;
168    new_item->type= value;    val->refcount++;
   
169    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
170  }  }
171    
172  /* Copy a string onto the stack. */  /* Push an integer onto the stack. */
173  int push_cstring(stackitem** stack_head, const char* in_string)  void push_int(stackitem **stack_head, int in_val)
174  {  {
175    stackitem* new_item= malloc(sizeof(stackitem));    value *new_value= malloc(sizeof(value));
176    new_item->content.ptr= malloc(strlen(in_string)+1);    stackitem *new_item= malloc(sizeof(stackitem));
177    strcpy(new_item->content.ptr, in_string);    new_item->item= new_value;
178    new_item->type= string;    
179      new_value->content.val= in_val;
180      new_value->type= integer;
181      new_value->refcount=1;
182    
183    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
184  }  }
185    
186  /* Create a new hash entry. */  /* Copy a string onto the stack. */
187  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)  
188  {  {
189    stackitem* temp= malloc(sizeof(stackitem));    value *new_value= malloc(sizeof(value));
190      stackitem *new_item= malloc(sizeof(stackitem));
191      new_item->item=new_value;
192    
193      new_value->content.ptr= malloc(strlen(in_string)+1);
194      strcpy(new_value->content.ptr, in_string);
195      new_value->type= string;
196      new_value->refcount=1;
197    
198    temp->type= func;    push(stack_head, new_item);
   temp->content.ptr= in_func;  
   
   mk_hashentry(in_hashtbl, temp, id);  
199  }  }
200    
201  /* Define a new symbol in the hash table. */  /* Mangle a symbol name to a valid C identifier name */
202  void def_sym(hashtbl in_hashtbl, const char* id)  char *mangle_str(const char *old_string){
203  {    char validchars[]
204    stackitem* temp= malloc(sizeof(stackitem));      ="0123456789abcdef";
205        char *new_string, *current;
206    temp->type= symbol;  
207      new_string=malloc((strlen(old_string)*2)+4);
208      strcpy(new_string, "sx_");    /* Stack eXternal */
209      current=new_string+3;
210      while(old_string[0] != '\0'){
211        current[0]=validchars[(unsigned char)(old_string[0])/16];
212        current[1]=validchars[(unsigned char)(old_string[0])%16];
213        current+=2;
214        old_string++;
215      }
216      current[0]='\0';
217    
218    mk_hashentry(in_hashtbl, temp, id);    return new_string;            /* The caller must free() it */
219  }  }
220    
221  /* Push a reference to an entry in the hash table onto the stack. */  extern void mangle(environment *env){
222  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)    value *new_value;
223  {    char *new_string;
   static void* handle= NULL;  
   void* symbol;  
   
   stackitem* new_item= malloc(sizeof(stackitem));  
   new_item->content.ptr= *hash(in_hash, in_string);  
   new_item->type= ref;  
224    
225    if(new_item->content.ptr==NULL) {    if((env->head)==NULL) {
226      if(handle==NULL)      printerr("Too Few Arguments");
227        handle= dlopen(NULL, RTLD_LAZY);          env->err=1;
228        return;
     symbol= dlsym(handle, in_string);  
     if(dlerror()==NULL)  
       def_func(in_hash, symbol, in_string);  
     else  
       def_sym(in_hash, in_string);  
         
     new_item->content.ptr= *hash(in_hash, in_string);  
     new_item->type= ref;  
229    }    }
230    
231    push(stack_head, new_item);    if(env->head->item->type!=string) {
232    return 1;      printerr("Bad Argument Type");
233  }      env->err=2;
234        return;
235      }
236    
237  /* Discard the top element of the stack. */    new_string= mangle_str((const char *)(env->head->item->content.ptr));
 extern void toss(stackitem** stack_head)  
 {  
   stackitem* temp= *stack_head;  
238    
239    if((*stack_head)==NULL)    toss(env);
240      return;    if(env->err) return;
     
   if((*stack_head)->type==string)  
     free((*stack_head)->content.ptr);  
241    
242    *stack_head= (*stack_head)->next;    new_value= malloc(sizeof(value));
243    free(temp);    new_value->content.ptr= new_string;
244      new_value->type= string;
245      new_value->refcount=1;
246    
247      push_val(&(env->head), new_value);
248    }
249    
250    /* Push a symbol onto the stack. */
251    void push_sym(environment *env, const char *in_string)
252    {
253      stackitem *new_item;          /* The new stack item */
254      /* ...which will contain... */
255      value *new_value;             /* A new symbol value */
256      /* ...which might point to... */
257      symbol **new_symbol;          /* (if needed) A new actual symbol */
258      /* ...which, if possible, will be bound to... */
259      value *new_fvalue;            /* (if needed) A new function value */
260      /* ...which will point to... */
261      void *funcptr;                /* A function pointer */
262    
263      static void *handle= NULL;    /* Dynamic linker handle */
264      const char *dlerr;            /* Dynamic linker error */
265      char *mangled;                /* Mangled function name */
266    
267      /* Create a new stack item containing a new value */
268      new_item= malloc(sizeof(stackitem));
269      new_value= malloc(sizeof(value));
270      new_item->item=new_value;
271    
272      /* The new value is a symbol */
273      new_value->type= symb;
274      new_value->refcount= 1;
275    
276      /* Look up the symbol name in the hash table */
277      new_symbol= hash(env->symbols, in_string);
278      new_value->content.ptr= *new_symbol;
279    
280      if(*new_symbol==NULL) { /* If symbol was undefined */
281    
282        /* Create a new symbol */
283        (*new_symbol)= malloc(sizeof(symbol));
284        (*new_symbol)->val= NULL;   /* undefined value */
285        (*new_symbol)->next= NULL;
286        (*new_symbol)->id= malloc(strlen(in_string)+1);
287        strcpy((*new_symbol)->id, in_string);
288    
289        /* Intern the new symbol in the hash table */
290        new_value->content.ptr= *new_symbol;
291    
292        /* Try to load the symbol name as an external function, to see if
293           we should bind the symbol to a new function pointer value */
294        if(handle==NULL)            /* If no handle */
295          handle= dlopen(NULL, RTLD_LAZY);
296    
297        funcptr= dlsym(handle, in_string); /* Get function pointer */
298        dlerr=dlerror();
299        if(dlerr != NULL) {         /* If no function was found */
300          mangled=mangle_str(in_string);
301          funcptr= dlsym(handle, mangled); /* try mangling it */
302          free(mangled);
303          dlerr=dlerror();
304        }
305        if(dlerr==NULL) {           /* If a function was found */
306          new_fvalue= malloc(sizeof(value)); /* Create a new value */
307          new_fvalue->type=func;    /* The new value is a function pointer */
308          new_fvalue->content.ptr=funcptr; /* Store function pointer */
309          (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
310                                             function value */
311          new_fvalue->refcount= 1;
312        }
313      }
314      push(&(env->head), new_item);
315  }  }
316    
317  /* Print newline. */  /* Print newline. */
# Line 176  extern void nl() Line 320  extern void nl()
320    printf("\n");    printf("\n");
321  }  }
322    
323  /* Prints the top element of the stack. */  /* Gets the type of a value */
324  void print_(stackitem** stack_head)  extern void type(environment *env){
325  {    int typenum;
326    if((*stack_head)==NULL)  
327      if((env->head)==NULL) {
328        printerr("Too Few Arguments");
329        env->err=1;
330      return;      return;
331      }
332      typenum=env->head->item->type;
333      toss(env);
334      switch(typenum){
335      case integer:
336        push_sym(env, "integer");
337        break;
338      case string:
339        push_sym(env, "string");
340        break;
341      case symb:
342        push_sym(env, "symbol");
343        break;
344      case func:
345        push_sym(env, "function");
346        break;
347      case list:
348        push_sym(env, "list");
349        break;
350      }
351    }    
352    
353    switch((*stack_head)->type) {  /* Prints the top element of the stack. */
354    case value:  void print_h(stackitem *stack_head)
355      printf("%d", (*stack_head)->content.val);  {
356      switch(stack_head->item->type) {
357      case integer:
358        printf("%d", stack_head->item->content.val);
359      break;      break;
360    case string:    case string:
361      printf("%s", (char*)(*stack_head)->content.ptr);      printf("%s", (char*)stack_head->item->content.ptr);
362        break;
363      case symb:
364        printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
365      break;      break;
366    case ref:    case func:
367      printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);      printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
368      break;      break;
369    case symbol:    case list:
370    default:      /* A list is just a stack, so make stack_head point to it */
371      printf("%p", (*stack_head)->content.ptr);      stack_head=(stackitem *)(stack_head->item->content.ptr);
372        printf("[ ");
373        while(stack_head != NULL) {
374          print_h(stack_head);
375          printf(" ");
376          stack_head=stack_head->next;
377        }
378        printf("]");
379      break;      break;
380    }    }
381  }  }
382    
383    extern void print_(environment *env) {
384      if(env->head==NULL) {
385        printerr("Too Few Arguments");
386        env->err=1;
387        return;
388      }
389      print_h(env->head);
390    }
391    
392  /* Prints the top element of the stack and then discards it. */  /* Prints the top element of the stack and then discards it. */
393  extern void print(stackitem** stack_head)  extern void print(environment *env)
394  {  {
395    print_(stack_head);    print_(env);
396    toss(stack_head);    if(env->err) return;
397      toss(env);
398  }  }
399    
400  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
401  void print_st(stackitem* stack_head, long counter)  void print_st(stackitem *stack_head, long counter)
402  {  {
403    if(stack_head->next != NULL)    if(stack_head->next != NULL)
404      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
   
405    printf("%ld: ", counter);    printf("%ld: ", counter);
406    print_(&stack_head);    print_h(stack_head);
407    nl();    nl();
408  }  }
409    
410  /* Prints the stack. */  /* Prints the stack. */
411  extern void printstack(stackitem** stack_head)  extern void printstack(environment *env)
412    {
413      if(env->head == NULL) {
414        return;
415      }
416      print_st(env->head, 1);
417      nl();
418    }
419    
420    /* Swap the two top elements on the stack. */
421    extern void swap(environment *env)
422    {
423      stackitem *temp= env->head;
424      
425      if(env->head==NULL || env->head->next==NULL) {
426        printerr("Too Few Arguments");
427        env->err=1;
428        return;
429      }
430    
431      env->head= env->head->next;
432      temp->next= env->head->next;
433      env->head->next= temp;
434    }
435    
436    /* Rotate the first three elements on the stack. */
437    extern void rot(environment *env)
438  {  {
439    if(*stack_head != NULL) {    stackitem *temp= env->head;
440      print_st(*stack_head, 1);    
441      printf("\n");    if(env->head==NULL || env->head->next==NULL
442          || env->head->next->next==NULL) {
443        printerr("Too Few Arguments");
444        env->err=1;
445        return;
446    }    }
447    
448      env->head= env->head->next->next;
449      temp->next->next= env->head->next;
450      env->head->next= temp;
451  }  }
452    
453  /* If the top element is a reference, determine if it's a reference to a  /* Recall a value from a symbol, if bound */
454     function, and if it is execute and pop the reference and execute the  extern void rcl(environment *env)
455    {
456      value *val;
457    
458      if(env->head == NULL) {
459        printerr("Too Few Arguments");
460        env->err=1;
461        return;
462      }
463    
464      if(env->head->item->type!=symb) {
465        printerr("Bad Argument Type");
466        env->err=2;
467        return;
468      }
469    
470      val=((symbol *)(env->head->item->content.ptr))->val;
471      if(val == NULL){
472        printerr("Unbound Variable");
473        env->err=3;
474        return;
475      }
476      toss(env);            /* toss the symbol */
477      if(env->err) return;
478      push_val(&(env->head), val); /* Return its bound value */
479    }
480    
481    void stack_read(environment*, char*);
482    
483    /* If the top element is a symbol, determine if it's bound to a
484       function value, and if it is, toss the symbol and execute the
485     function. */     function. */
486  extern void eval(stackitem** stack_head)  extern void eval(environment *env)
487  {  {
488    funcp in_func;    funcp in_func;
489      value* temp_val;
490      stackitem* iterator;
491      char* temp_string;
492    
493      if(env->head==NULL) {
494        printerr("Too Few Arguments");
495        env->err=1;
496        return;
497      }
498    
499    if((*stack_head)==NULL || (*stack_head)->type!=ref)    switch(env->head->item->type) {
500        /* if it's a symbol */
501      case symb:
502        rcl(env);                   /* get its contents */
503        if(env->err) return;
504        if(env->head->item->type!=symb){ /* don't recurse symbols */
505          return eval(env);         /* evaluate the value */
506        }
507      return;      return;
508    
509    if(((stackitem*)(*stack_head)->content.ptr)->type==func) {      /* If it's a lone function value, run it */
510      in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;    case func:
511      toss(stack_head);      in_func= (funcp)(env->head->item->content.ptr);
512      (*in_func)(stack_head);      toss(env);
513        if(env->err) return;
514        return (*in_func)(env);
515    
516        /* If it's a list */
517      case list:
518        temp_val= env->head->item;
519        env->head->item->refcount++;
520        toss(env);
521        if(env->err) return;
522        iterator= (stackitem*)temp_val->content.ptr;
523        while(iterator!=NULL) {
524          push_val(&(env->head), iterator->item);
525          if(env->head->item->type==symb
526            && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
527            toss(env);
528            if(env->err) return;
529            if(iterator->next == NULL){
530              free_val(temp_val);
531              return eval(env);
532            }
533            eval(env);
534            if(env->err) return;
535          }
536          iterator= iterator->next;
537        }
538        free_val(temp_val);
539      return;      return;
540    
541        /* If it's a string */
542      case string:
543        temp_val= env->head->item;
544        env->head->item->refcount++;
545        toss(env);
546        if(env->err) return;
547        temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
548        strcpy(temp_string, "[ ");
549        strcpy(temp_string+2, (char*)temp_val->content.ptr);
550        free_val(temp_val);
551        strcat(temp_string, " ]");
552        stack_read(env, temp_string);
553        free(temp_string);
554        return eval(env);
555    
556      case integer:
557        return;
558      }
559    }
560    
561    /* Reverse (flip) a list */
562    extern void rev(environment *env){
563      stackitem *old_head, *new_head, *item;
564    
565      if((env->head)==NULL) {
566        printerr("Too Few Arguments");
567        env->err=1;
568        return;
569      }
570    
571      if(env->head->item->type!=list) {
572        printerr("Bad Argument Type");
573        env->err=2;
574        return;
575      }
576    
577      old_head=(stackitem *)(env->head->item->content.ptr);
578      new_head=NULL;
579      while(old_head != NULL){
580        item=old_head;
581        old_head=old_head->next;
582        item->next=new_head;
583        new_head=item;
584    }    }
585      env->head->item->content.ptr=new_head;
586    }
587    
588    /* Make a list. */
589    extern void pack(environment *env)
590    {
591      void* delimiter;
592      stackitem *iterator, *temp;
593      value *pack;
594    
595      delimiter= env->head->item->content.ptr; /* Get delimiter */
596      toss(env);
597    
598      iterator= env->head;
599    
600      if(iterator==NULL || iterator->item->content.ptr==delimiter) {
601        temp= NULL;
602        toss(env);
603      } else {
604        /* Search for first delimiter */
605        while(iterator->next!=NULL
606              && iterator->next->item->content.ptr!=delimiter)
607          iterator= iterator->next;
608        
609        /* Extract list */
610        temp= env->head;
611        env->head= iterator->next;
612        iterator->next= NULL;
613        
614        if(env->head!=NULL)
615          toss(env);
616      }
617    
618      /* Push list */
619      pack= malloc(sizeof(value));
620      pack->type= list;
621      pack->content.ptr= temp;
622      pack->refcount= 1;
623    
624      temp= malloc(sizeof(stackitem));
625      temp->item= pack;
626    
627      push(&(env->head), temp);
628      rev(env);
629  }  }
630    
631  /* Parse input. */  /* Parse input. */
632  int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)  void stack_read(environment *env, char *in_line)
633  {  {
634    char *temp, *rest;    char *temp, *rest;
635    int itemp;    int itemp;
# Line 256  int stack_read(stackitem** stack_head, h Line 640  int stack_read(stackitem** stack_head, h
640    rest= malloc(inlength);    rest= malloc(inlength);
641    
642    do {    do {
643        /* If comment */
644        if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {
645          free(temp); free(rest);
646          return;
647        }
648    
649        /* If string */
650      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
651        push_cstring(stack_head, temp);        push_cstring(&(env->head), temp);
652        break;        break;
653      }      }
654        /* If integer */
655      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {      if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
656        push_val(stack_head, itemp);        push_int(&(env->head), itemp);
657        break;        break;
658      }      }
659        /* Escape ';' with '\' */
660      if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
661        temp[1]= '\0';        temp[1]= '\0';
662        push_ref(stack_head, in_hash, temp);        push_sym(env, temp);
663        break;        break;
664      }      }
665              /* If symbol */
666      if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
667          push_ref(stack_head, in_hash, temp);          push_sym(env, temp);
668          break;          break;
669      }      }
670          /* If single char */
671      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
672        eval(stack_head);        if(*temp==';') {
673        break;          if(!env->non_eval_flag) {
674              eval(env);            /* Evaluate top element */
675              break;
676            }
677            
678            push_sym(env, ";");
679            break;
680          }
681    
682          if(*temp==']') {
683            push_sym(env, "[");
684            pack(env);
685            if(env->non_eval_flag)
686              env->non_eval_flag--;
687            break;
688          }
689    
690          if(*temp=='[') {
691            push_sym(env, "[");
692            env->non_eval_flag++;
693            break;
694          }
695      }      }
696    } while(0);    } while(0);
697    
   
698    free(temp);    free(temp);
699    
700    if(convert<2) {    if(convert<2) {
701      free(rest);      free(rest);
702      return 0;      return;
703    }    }
704        
705    stack_read(stack_head, in_hash, rest);    stack_read(env, rest);
706        
707    free(rest);    free(rest);
   return 1;  
708  }  }
709    
710  /* Make a list. */  /* Relocate elements of the list on the stack. */
711  extern void pack(stackitem** stack_head)  extern void expand(environment *env)
712  {  {
713    void* delimiter;    stackitem *temp, *new_head;
   stackitem *iterator, *temp, *pack;  
714    
715    if((*stack_head)==NULL)    /* Is top element a list? */
716      if(env->head==NULL) {
717        printerr("Too Few Arguments");
718        env->err=1;
719      return;      return;
720      }
721      if(env->head->item->type!=list) {
722        printerr("Bad Argument Type");
723        env->err=2;
724        return;
725      }
726    
727    delimiter= (*stack_head)->content.ptr;    rev(env);
   toss(stack_head);  
   
   iterator= *stack_head;  
   
   while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)  
     iterator= iterator->next;  
   
   temp= *stack_head;  
   *stack_head= iterator->next;  
   iterator->next= NULL;  
     
   if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)  
     toss(stack_head);  
   
   pack= malloc(sizeof(stackitem));  
   pack->type= list;  
   pack->content.ptr= temp;  
   
   push(stack_head, pack);  
 }  
728    
729  /* Push elements of the list on the stack. */    if(env->err)
 extern void expand(stackitem** stack_head)  
 {  
   stackitem *temp, *new_head;  
   
   if((*stack_head)==NULL || (*stack_head)->type!=list)  
730      return;      return;
731    
732    new_head= temp= (*stack_head)->content.ptr;    /* The first list element is the new stack head */
733    toss(stack_head);    new_head= temp= env->head->item->content.ptr;
734    
735      env->head->item->refcount++;
736      toss(env);
737    
738      /* Find the end of the list */
739    while(temp->next!=NULL)    while(temp->next!=NULL)
740      temp= temp->next;      temp= temp->next;
741    
742    temp->next= *stack_head;    /* Connect the tail of the list with the old stack head */
743    *stack_head= new_head;    temp->next= env->head;
744  }    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 || (*stack_head)->next==NULL)  
     return;  
745    
746    *stack_head= (*stack_head)->next;  }
   temp->next= (*stack_head)->next;  
   (*stack_head)->next= temp;  
 }  
747    
748  /* Compares two elements by reference. */  /* Compares two elements by reference. */
749  extern void eq(stackitem** stack_head)  extern void eq(environment *env)
750  {  {
751    void *left, *right;    void *left, *right;
752    int result;    int result;
753    
754    if((*stack_head)==NULL || (*stack_head)->next==NULL)    if((env->head)==NULL || env->head->next==NULL) {
755        printerr("Too Few Arguments");
756        env->err=1;
757      return;      return;
758      }
759    
760    left= (*stack_head)->content.ptr;    left= env->head->item->content.ptr;
761    swap(stack_head);    swap(env);
762    right= (*stack_head)->content.ptr;    right= env->head->item->content.ptr;
763    result= (left==right);    result= (left==right);
764        
765    toss(stack_head); toss(stack_head);    toss(env); toss(env);
766    push_val(stack_head, (left==right));    push_int(&(env->head), result);
767  }  }
768    
769  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
770  extern void not(stackitem** stack_head)  extern void not(environment *env)
771  {  {
772    int value;    int val;
773    
774    if((*stack_head)==NULL)    if((env->head)==NULL) {
775        printerr("Too Few Arguments");
776        env->err=1;
777      return;      return;
778      }
779    
780    value= (*stack_head)->content.val;    if(env->head->item->type!=integer) {
781    toss(stack_head);      printerr("Bad Argument Type");
782    push_val(stack_head, !value);      env->err=2;
783        return;
784      }
785    
786      val= env->head->item->content.val;
787      toss(env);
788      push_int(&(env->head), !val);
789  }  }
790    
791  /* 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
792     same. */     same. */
793  extern void neq(stackitem** stack_head)  extern void neq(environment *env)
794  {  {
795    eq(stack_head);    eq(env);
796    not(stack_head);    not(env);
797  }  }
798    
799  /* Give a symbol some content. */  /* Give a symbol some content. */
800  extern void def(stackitem** stack_head)  extern void def(environment *env)
801  {  {
802    stackitem *temp, *value;    symbol *sym;
803    
804    if(*stack_head==NULL || (*stack_head)->next==NULL    /* Needs two values on the stack, the top one must be a symbol */
805       || (*stack_head)->type!=ref)    if(env->head==NULL || env->head->next==NULL) {
806        printerr("Too Few Arguments");
807        env->err=1;
808      return;      return;
809      }
810    
811    temp= (*stack_head)->content.ptr;    if(env->head->item->type!=symb) {
812    value= (*stack_head)->next;      printerr("Bad Argument Type");
813    temp->content= value->content;      env->err=2;
814    value->content.ptr=NULL;      return;
815    temp->type= value->type;    }
816    
817      /* long names are a pain */
818      sym=env->head->item->content.ptr;
819    
820      /* if the symbol was bound to something else, throw it away */
821      if(sym->val != NULL)
822        free_val(sym->val);
823    
824      /* Bind the symbol to the value */
825      sym->val= env->head->next->item;
826      sym->val->refcount++;         /* Increase the reference counter */
827    
828    toss(stack_head); toss(stack_head);    toss(env); toss(env);
829  }  }
830    
831  /* Quit stack. */  /* Quit stack. */
832  extern void quit()  extern void quit(environment *env)
833  {  {
834    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
835  }  }
836    
837    /* Clear stack */
838    extern void clear(environment *env)
839    {
840      while(env->head!=NULL)
841        toss(env);
842    }
843    
844    /* List all defined words */
845    extern void words(environment *env)
846    {
847      symbol *temp;
848      int i;
849      
850      for(i= 0; i<HASHTBLSIZE; i++) {
851        temp= env->symbols[i];
852        while(temp!=NULL) {
853          printf("%s\n", temp->id);
854          temp= temp->next;
855        }
856      }
857    }
858    
859    /* Forgets a symbol (remove it from the hash table) */
860    extern void forget(environment *env)
861    {
862      char* sym_id;
863      stackitem *stack_head= env->head;
864      symbol **hash_entry, *temp;
865    
866      if(stack_head==NULL) {
867        printerr("Too Few Arguments");
868        env->err=1;
869        return;
870      }
871      
872      if(stack_head->item->type!=symb) {
873        printerr("Bad Argument Type");
874        env->err=2;
875        return;
876      }
877    
878      sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
879      toss(env);
880    
881      hash_entry= hash(env->symbols, sym_id);
882      temp= *hash_entry;
883      *hash_entry= (*hash_entry)->next;
884      
885      if(temp->val!=NULL) {
886        free_val(temp->val);
887      }
888      free(temp->id);
889      free(temp);
890    }
891    
892    /* Returns the current error number to the stack */
893    extern void errn(environment *env){
894      push_int(&(env->head), env->err);
895    }
896    
897  int main()  int main()
898  {  {
899    stackitem* s= NULL;    environment myenv;
   hashtbl myhash;  
900    char in_string[100];    char in_string[100];
901    
902    init_hashtbl(myhash);    init_env(&myenv);
903    
904    printf("okidok\n ");    printf("okidok\n ");
905    
906    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
907      stack_read(&s, myhash, in_string);      stack_read(&myenv, in_string);
908        if(myenv.err) {
909          printf("(error %d) ", myenv.err);
910          myenv.err=0;
911        }
912      printf("okidok\n ");      printf("okidok\n ");
913    }    }
914      quit(&myenv);
915      return EXIT_FAILURE;
916    }
917    
918    exit(EXIT_SUCCESS);  /* + */
919    extern void sx_2b(environment *env) {
920      int a, b;
921      size_t len;
922      char* new_string;
923      value *a_val, *b_val;
924    
925      if((env->head)==NULL || env->head->next==NULL) {
926        printerr("Too Few Arguments");
927        env->err=1;
928        return;
929      }
930    
931      if(env->head->item->type==string
932         && env->head->next->item->type==string) {
933        a_val= env->head->item;
934        b_val= env->head->next->item;
935        a_val->refcount++;
936        b_val->refcount++;
937        toss(env); if(env->err) return;
938        toss(env); if(env->err) return;
939        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
940        new_string= malloc(len);
941        strcpy(new_string, b_val->content.ptr);
942        strcat(new_string, a_val->content.ptr);
943        free_val(a_val); free_val(b_val);
944        push_cstring(&(env->head), new_string);
945        free(new_string);
946        return;
947      }
948      
949      if(env->head->item->type!=integer
950         || env->head->next->item->type!=integer) {
951        printerr("Bad Argument Type");
952        env->err=2;
953        return;
954      }
955      a=env->head->item->content.val;
956      toss(env);
957      if(env->err) return;
958      b=env->head->item->content.val;
959      toss(env);
960      if(env->err) return;
961      push_int(&(env->head), a+b);
962    }
963    
964    /* Return copy of a value */
965    value *copy_val(value *old_value){
966      stackitem *old_item, *new_item, *prev_item;
967    
968      value *new_value=malloc(sizeof(value));
969    
970      new_value->type=old_value->type;
971      new_value->refcount=0;        /* This is increased if/when this
972                                       value is referenced somewhere, like
973                                       in a stack item or a variable */
974      switch(old_value->type){
975      case integer:
976        new_value->content.val=old_value->content.val;
977        break;
978      case string:
979        (char *)(new_value->content.ptr)
980          = strdup((char *)(old_value->content.ptr));
981        break;
982      case func:
983      case symb:
984        new_value->content.ptr=old_value->content.ptr;
985        break;
986      case list:
987        new_value->content.ptr=NULL;
988    
989        prev_item=NULL;
990        old_item=(stackitem *)(old_value->content.ptr);
991    
992        while(old_item != NULL) {   /* While list is not empty */
993          new_item= malloc(sizeof(stackitem));
994          new_item->item=copy_val(old_item->item); /* recurse */
995          new_item->next=NULL;
996          if(prev_item != NULL)     /* If this wasn't the first item */
997            prev_item->next=new_item; /* point the previous item to the
998                                         new item */
999          else
1000            new_value->content.ptr=new_item;
1001          old_item=old_item->next;
1002          prev_item=new_item;
1003        }    
1004        break;
1005      }
1006      return new_value;
1007    }
1008    
1009    /* duplicates an item on the stack */
1010    extern void dup(environment *env) {
1011      if((env->head)==NULL) {
1012        printerr("Too Few Arguments");
1013        env->err=1;
1014        return;
1015      }
1016      push_val(&(env->head), copy_val(env->head->item));
1017    }
1018    
1019    /* "if", If-Then */
1020    extern void sx_6966(environment *env) {
1021    
1022      int truth;
1023    
1024      if((env->head)==NULL || env->head->next==NULL) {
1025        printerr("Too Few Arguments");
1026        env->err=1;
1027        return;
1028      }
1029    
1030      if(env->head->next->item->type != integer) {
1031        printerr("Bad Argument Type");
1032        env->err=2;
1033        return;
1034      }
1035      
1036      swap(env);
1037      if(env->err) return;
1038      
1039      truth=env->head->item->content.val;
1040    
1041      toss(env);
1042      if(env->err) return;
1043    
1044      if(truth)
1045        eval(env);
1046      else
1047        toss(env);
1048    }
1049    
1050    /* If-Then-Else */
1051    extern void ifelse(environment *env) {
1052    
1053      int truth;
1054    
1055      if((env->head)==NULL || env->head->next==NULL
1056         || env->head->next->next==NULL) {
1057        printerr("Too Few Arguments");
1058        env->err=1;
1059        return;
1060      }
1061    
1062      if(env->head->next->next->item->type != integer) {
1063        printerr("Bad Argument Type");
1064        env->err=2;
1065        return;
1066      }
1067      
1068      rot(env);
1069      if(env->err) return;
1070      
1071      truth=env->head->item->content.val;
1072    
1073      toss(env);
1074      if(env->err) return;
1075    
1076      if(!truth)
1077        swap(env);
1078      if(env->err) return;
1079    
1080      toss(env);
1081      if(env->err) return;
1082    
1083      eval(env);
1084  }  }
1085    
1086  /* Local Variables: */  /* while */
1087  /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */  extern void sx_7768696c65(environment *env) {
1088  /* End: */  
1089      int truth;
1090    
1091      if((env->head)==NULL || env->head->next==NULL) {
1092        printerr("Too Few Arguments");
1093        env->err=1;
1094        return;
1095      }
1096    
1097      do {
1098        swap(env); if(env->err) return;
1099        dup(env); if(env->err) return;
1100        eval(env); if(env->err) return;
1101        
1102        if(env->head->item->type != integer) {
1103          printerr("Bad Argument Type");
1104          env->err=2;
1105          return;
1106        }
1107        
1108        truth= env->head->item->content.val;
1109        
1110        toss(env); if(env->err) return;
1111        swap(env); if(env->err) return;
1112        
1113        if(truth) {
1114          dup(env);
1115          eval(env);
1116        } else {
1117          toss(env);
1118          toss(env);
1119        }
1120      
1121      } while(truth);
1122    }

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.59

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26