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

Diff of /stack/stack.c

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

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

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.48

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26