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

Diff of /stack/stack.c

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

revision 1.28 by teddy, Mon Feb 4 21:47:26 2002 UTC revision 1.49 by masse, Thu Feb 7 05:24:19 2002 UTC
# Line 8  Line 8 
8  #include <dlfcn.h>  #include <dlfcn.h>
9  /* assert */  /* assert */
10  #include <assert.h>  #include <assert.h>
11    /* strcat */
12    #include <string.h>
13    
14  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
15    
# Line 18  typedef struct { Line 20  typedef struct {
20    enum {    enum {
21      integer,      integer,
22      string,      string,
     ref,                        /* Reference (to an element in the  
                                    hash table) */  
23      func,                       /* Function pointer */      func,                       /* Function pointer */
24      symb,      symb,
25      list      list
# Line 58  typedef struct stackitem_struct Line 58  typedef struct stackitem_struct
58  typedef struct {  typedef struct {
59    stackitem *head;              /* Head of the stack */    stackitem *head;              /* Head of the stack */
60    hashtbl symbols;              /* Hash table of all variable bindings */    hashtbl symbols;              /* Hash table of all variable bindings */
61      int err;                      /* Error flag */
62      int non_eval_flag;
63  } environment;  } environment;
64    
65  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 67  typedef void (*funcp)(environment *); /* Line 69  typedef void (*funcp)(environment *); /*
69  /* Initialize a newly created environment */  /* Initialize a newly created environment */
70  void init_env(environment *env)  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      env->symbols[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    /* Discard the top element of the stack. */
111    extern void toss(environment *env)
112    {
113      stackitem *temp= env->head;
114    
115      if((env->head)==NULL) {
116        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. */  /* Returns a pointer to a pointer to an element in the hash table. */
127  symbol **hash(hashtbl in_hashtbl, const char *in_string)  symbol **hash(hashtbl in_hashtbl, const char *in_string)
128  {  {
129    long i= 0;    int i= 0;
130    unsigned long out_hash= 0;    unsigned int out_hash= 0;
131    char key= '\0';    char key= '\0';
132    symbol **position;    symbol **position;
133        
# Line 103  symbol **hash(hashtbl in_hashtbl, const Line 153  symbol **hash(hashtbl in_hashtbl, const
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;
160    return 1;  }
161    
162    /* Push a value onto the stack */
163    void push_val(stackitem **stack_head, value *val)
164    {
165      stackitem *new_item= malloc(sizeof(stackitem));
166      new_item->item= val;
167      val->refcount++;
168      push(stack_head, new_item);
169  }  }
170    
171  /* Push an integer onto the stack. */  /* Push an integer onto the stack. */
172  int push_val(stackitem **stack_head, int in_val)  void push_int(stackitem **stack_head, int in_val)
173  {  {
174    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
175    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
# Line 122  int push_val(stackitem **stack_head, int Line 180  int push_val(stackitem **stack_head, int
180    new_value->refcount=1;    new_value->refcount=1;
181    
182    push(stack_head, new_item);    push(stack_head, new_item);
   return 1;  
183  }  }
184    
185  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
186  int push_cstring(stackitem **stack_head, const char *in_string)  void push_cstring(stackitem **stack_head, const char *in_string)
187  {  {
188    value *new_value= malloc(sizeof(value));    value *new_value= malloc(sizeof(value));
189    stackitem *new_item= malloc(sizeof(stackitem));    stackitem *new_item= malloc(sizeof(stackitem));
# Line 138  int push_cstring(stackitem **stack_head, Line 195  int push_cstring(stackitem **stack_head,
195    new_value->refcount=1;    new_value->refcount=1;
196    
197    push(stack_head, new_item);    push(stack_head, new_item);
198    return 1;  }
199    
200    /* Mangle a symbol name to a valid C identifier name */
201    char *mangle_(const char *old_string){
202      char validchars[]
203        ="0123456789abcdef";
204      char *new_string, *current;
205    
206      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      return new_string;            /* The caller must free() it */
218    }
219    
220    extern void mangle(environment *env){
221      value *new_value;
222      char *new_string;
223    
224      if((env->head)==NULL) {
225        printerr("Too Few Arguments");
226        env->err=1;
227        return;
228      }
229    
230      if(env->head->item->type!=string) {
231        printerr("Bad Argument Type");
232        env->err=2;
233        return;
234      }
235    
236      new_string= mangle_((const char *)(env->head->item->content.ptr));
237    
238      toss(env);
239      if(env->err) return;
240    
241      new_value= malloc(sizeof(value));
242      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. */  /* Push a symbol onto the stack. */
250  int push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
251  {  {
252    stackitem *new_item;          /* The new stack item */    stackitem *new_item;          /* The new stack item */
253    /* ...which will contain... */    /* ...which will contain... */
254    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
255    /* ...which might point to... */    /* ...which might point to... */
256    symbol *new_symbol;           /* (if needed) A new actual symbol */    symbol **new_symbol;          /* (if needed) A new actual symbol */
257    /* ...which, if possible, will be bound to... */    /* ...which, if possible, will be bound to... */
258    value *new_fvalue;            /* (if needed) A new function value */    value *new_fvalue;            /* (if needed) A new function value */
259    /* ...which will point to... */    /* ...which will point to... */
260    void *funcptr;                /* A function pointer */    void *funcptr;                /* A function pointer */
261    
262    static void *handle= NULL;    /* Dynamic linker handle */    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 */    /* Create a new stack item containing a new value */
267    new_item= malloc(sizeof(stackitem));    new_item= malloc(sizeof(stackitem));
# Line 166  int push_sym(environment *env, const cha Line 273  int push_sym(environment *env, const cha
273    new_value->refcount= 1;    new_value->refcount= 1;
274    
275    /* Look up the symbol name in the hash table */    /* Look up the symbol name in the hash table */
276    new_value->content.ptr= *hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
277      new_value->content.ptr= *new_symbol;
278    
279    if(new_value->content.ptr==NULL) { /* If symbol was undefined */    if(*new_symbol==NULL) { /* If symbol was undefined */
280    
281      /* Create a new symbol */      /* Create a new symbol */
282      new_symbol= malloc(sizeof(symbol));      (*new_symbol)= malloc(sizeof(symbol));
283      new_symbol->val= NULL;      /* undefined value */      (*new_symbol)->val= NULL;   /* undefined value */
284      new_symbol->next= NULL;      (*new_symbol)->next= NULL;
285      new_symbol->id= malloc(strlen(in_string)+1);      (*new_symbol)->id= malloc(strlen(in_string)+1);
286      strcpy(new_symbol->id, in_string);      strcpy((*new_symbol)->id, in_string);
287    
288      /* Intern the new symbol in the hash table */      /* Intern the new symbol in the hash table */
289      new_value->content.ptr= new_symbol;      new_value->content.ptr= *new_symbol;
290    
291      /* Try to load the symbol name as an external function, to see if      /* Try to load the symbol name as an external function, to see if
292         we should bind the symbol to a new function pointer value */         we should bind the symbol to a new function pointer value */
# Line 186  int push_sym(environment *env, const cha Line 294  int push_sym(environment *env, const cha
294        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
295    
296      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
297      if(dlerror()==NULL) {       /* If a function was found */      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 */        new_fvalue= malloc(sizeof(value)); /* Create a new value */
306        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->type=func;    /* The new value is a function pointer */
307        new_fvalue->content.ptr=funcptr; /* Store function pointer */        new_fvalue->content.ptr=funcptr; /* Store function pointer */
308        new_symbol->val= new_fvalue;      /* Bind the symbol to the new        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
309                                             function value */                                           function value */
310        new_fvalue->refcount= 1;        new_fvalue->refcount= 1;
311      }      }
312    }    }
313    push(&(env->head), new_item);    push(&(env->head), new_item);
   return 1;  
 }  
   
 void printerr(const char* in_string) {  
   fprintf(stderr, "Err: %s\n", in_string);  
 }  
   
 /* Throw away a value */  
 void free_val(value *val){  
   stackitem *item, *temp;  
   
   val->refcount--;              /* Decrease the reference count */  
   if(val->refcount == 0){  
     switch (val->type){         /* and free the contents if necessary */  
     case string:  
       free(val->content.ptr);  
     case list:                  /* lists needs to be freed recursively */  
       item=val->content.ptr;  
       while(item != NULL) {     /* for all stack items */  
         free_val(item->item);   /* free the value */  
         temp=item->next;        /* save next ptr */  
         free(item);             /* free the stackitem */  
         item=temp;              /* go to next stackitem */  
       }  
       free(val);                /* Free the actual list value */  
       break;  
     default:  
       break;  
     }  
   }  
 }  
   
 /* Discard the top element of the stack. */  
 extern void toss(environment *env)  
 {  
   stackitem *temp= env->head;  
   
   if((env->head)==NULL) {  
     printerr("Stack empty");  
     return;  
   }  
     
   free_val(env->head->item);    /* Free the value */  
   env->head= env->head->next;   /* Remove the top stack item */  
   free(temp);                   /* Free the old top stack item */  
314  }  }
315    
316  /* Print newline. */  /* Print newline. */
# Line 249  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_h(stackitem *stack_head)  extern void type(environment *env){
324  {    int typenum;
325    
326    if(stack_head==NULL) {    if((env->head)==NULL) {
327      printerr("Stack empty");      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    /* Prints the top element of the stack. */
356    void print_h(stackitem *stack_head)
357    {
358    switch(stack_head->item->type) {    switch(stack_head->item->type) {
359    case integer:    case integer:
360      printf("%d", stack_head->item->content.val);      printf("%d", stack_head->item->content.val);
# Line 266  void print_h(stackitem *stack_head) Line 363  void print_h(stackitem *stack_head)
363      printf("\"%s\"", (char*)stack_head->item->content.ptr);      printf("\"%s\"", (char*)stack_head->item->content.ptr);
364      break;      break;
365    case symb:    case symb:
366      printf("'%s'", ((symbol *)(stack_head->item->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;
382    default:    default:
383      printf("%p", (funcp)(stack_head->item->content.ptr));      printf("#<unknown %p>", (stack_head->item->content.ptr));
384      break;      break;
385    }    }
386  }  }
387    
388  extern void print_(environment *env) {  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);    print_h(env->head);
395  }  }
396    
# Line 282  extern void print_(environment *env) { Line 398  extern void print_(environment *env) {
398  extern void print(environment *env)  extern void print(environment *env)
399  {  {
400    print_(env);    print_(env);
401      if(env->err) return;
402    toss(env);    toss(env);
403  }  }
404    
# Line 295  void print_st(stackitem *stack_head, lon Line 412  void print_st(stackitem *stack_head, lon
412    nl();    nl();
413  }  }
414    
   
   
415  /* Prints the stack. */  /* Prints the stack. */
416  extern void printstack(environment *env)  extern void printstack(environment *env)
417  {  {
418    if(env->head != NULL) {    if(env->head == NULL) {
419      print_st(env->head, 1);      return;
     nl();  
   } else {  
     printerr("Stack empty");  
420    }    }
421      print_st(env->head, 1);
422      nl();
423  }  }
424    
425  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
# Line 313  extern void swap(environment *env) Line 427  extern void swap(environment *env)
427  {  {
428    stackitem *temp= env->head;    stackitem *temp= env->head;
429        
430    if((env->head)==NULL) {    if(env->head==NULL || env->head->next==NULL) {
431      printerr("Stack empty");      printerr("Too Few Arguments");
432      return;      env->err=1;
   }  
   
   if(env->head->next==NULL) {  
     printerr("Not enough arguments");  
433      return;      return;
434    }    }
435    
# Line 328  extern void swap(environment *env) Line 438  extern void swap(environment *env)
438    env->head->next= temp;    env->head->next= temp;
439  }  }
440    
441  stackitem* copy(stackitem* in_item)  /* Recall a value from a symbol, if bound */
442    extern void rcl(environment *env)
443  {  {
444    stackitem *out_item= malloc(sizeof(stackitem));    value *val;
445    
446    memcpy(out_item, in_item, sizeof(stackitem));    if(env->head == NULL) {
447    out_item->next= NULL;      printerr("Too Few Arguments");
448        env->err=1;
449        return;
450      }
451    
452    return out_item;    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    void stack_read(environment*, char*);
470    
471  /* If the top element is a reference, determine if it's a reference to a  /* If the top element is a symbol, determine if it's bound to a
472     function, and if it is, toss the reference and execute the function. */     function value, and if it is, toss the symbol and execute the
473       function. */
474  extern void eval(environment *env)  extern void eval(environment *env)
475  {  {
476    funcp in_func;    funcp in_func;
477    stackitem* temp= env->head;    value* temp_val;
478      stackitem* iterator;
479      char* temp_string;
480    
481    if(temp==NULL) {    if(env->head==NULL) {
482      printerr("Stack empty");      printerr("Too Few Arguments");
483        env->err=1;
484      return;      return;
485    }    }
486    
487    if(temp->item->type==symb    switch(env->head->item->type) {
488       && ((symbol *)(temp->item->content.ptr))->val != NULL      /* if it's a symbol */
489       && ((symbol *)(temp->item->content.ptr))->val->type == func) {    case symb:
490      in_func= (funcp)(((symbol *)(temp->item->content.ptr))->val->content.ptr);      rcl(env);                   /* get its contents */
491      toss(env);      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);      (*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        strcpy(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      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(temp->item->type==func) {    if(env->head->item->type!=list) {
559      in_func= (funcp)(temp->item->content.ptr);      printerr("Bad Argument Type");
560      toss(env);      env->err=2;
     (*in_func)(env);  
561      return;      return;
562    }    }
563    
564    push(&(env->head), copy(temp));    old_head=(stackitem *)(env->head->item->content.ptr);
565    swap(env);    new_head=NULL;
566    toss(env);    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. */  /* Make a list. */
# Line 413  extern void pack(environment *env) Line 612  extern void pack(environment *env)
612    temp->item= pack;    temp->item= pack;
613    
614    push(&(env->head), temp);    push(&(env->head), temp);
615      rev(env);
616  }  }
617    
618  /* Parse input. */  /* Parse input. */
619  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
620  {  {
621    char *temp, *rest;    char *temp, *rest;
622    int itemp;    int itemp;
623    size_t inlength= strlen(in_line)+1;    size_t inlength= strlen(in_line)+1;
624    int convert= 0;    int convert= 0;
   static int non_eval_flag= 0;  
625    
626    temp= malloc(inlength);    temp= malloc(inlength);
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(&(env->head), temp);        push_cstring(&(env->head), temp);
# Line 435  int stack_read(environment *env, char *i Line 640  int stack_read(environment *env, char *i
640      }      }
641      /* If integer */      /* 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(&(env->head), itemp);        push_int(&(env->head), itemp);
644        break;        break;
645      }      }
646      /* Escape ';' with '\' */      /* Escape ';' with '\' */
# Line 445  int stack_read(environment *env, char *i Line 650  int stack_read(environment *env, char *i
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_sym(env, temp);          push_sym(env, temp);
655          break;          break;
656      }      }
657      /* If single char */      /* If single char */
658      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {      if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
659        if(*temp==';') {        if(*temp==';') {
660          if(!non_eval_flag) {          if(!env->non_eval_flag) {
661            eval(env);            /* Evaluate top element */            eval(env);            /* Evaluate top element */
662            break;            break;
663          }          }
# Line 464  int stack_read(environment *env, char *i Line 669  int stack_read(environment *env, char *i
669        if(*temp==']') {        if(*temp==']') {
670          push_sym(env, "[");          push_sym(env, "[");
671          pack(env);          pack(env);
672          if(non_eval_flag!=0)          if(env->non_eval_flag)
673            non_eval_flag--;            env->non_eval_flag--;
674          break;          break;
675        }        }
676    
677        if(*temp=='[') {        if(*temp=='[') {
678          push_sym(env, "[");          push_sym(env, "[");
679          non_eval_flag++;          env->non_eval_flag++;
680          break;          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(env, rest);    stack_read(env, rest);
693        
694    free(rest);    free(rest);
   return 1;  
695  }  }
696    
697  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 497  extern void expand(environment *env) Line 700  extern void expand(environment *env)
700    stackitem *temp, *new_head;    stackitem *temp, *new_head;
701    
702    /* Is top element a list? */    /* Is top element a list? */
703    if(env->head==NULL || env->head->item->type!=list) {    if(env->head==NULL) {
704      printerr("Stack empty or not a list");      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;      return;
712    }    }
713    
714      rev(env);
715    
716      if(env->err)
717        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= env->head->item->content.ptr;    new_head= temp= env->head->item->content.ptr;
721    
# Line 525  extern void eq(environment *env) Line 739  extern void eq(environment *env)
739    int result;    int result;
740    
741    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
742      printerr("Not enough elements to compare");      printerr("Too Few Arguments");
743        env->err=1;
744      return;      return;
745    }    }
746    
# Line 535  extern void eq(environment *env) Line 750  extern void eq(environment *env)
750    result= (left==right);    result= (left==right);
751        
752    toss(env); toss(env);    toss(env); toss(env);
753    push_val(&(env->head), result);    push_int(&(env->head), result);
754  }  }
755    
756  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 543  extern void not(environment *env) Line 758  extern void not(environment *env)
758  {  {
759    int val;    int val;
760    
761    if((env->head)==NULL || env->head->item->type!=integer) {    if((env->head)==NULL) {
762      printerr("Stack empty or element is not a integer");      printerr("Too Few Arguments");
763        env->err=1;
764        return;
765      }
766    
767      if(env->head->item->type!=integer) {
768        printerr("Bad Argument Type");
769        env->err=2;
770      return;      return;
771    }    }
772    
773    val= env->head->item->content.val;    val= env->head->item->content.val;
774    toss(env);    toss(env);
775    push_val(&(env->head), !val);    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
# Line 567  extern void def(environment *env) Line 789  extern void def(environment *env)
789    symbol *sym;    symbol *sym;
790    
791    /* Needs two values on the stack, the top one must be a symbol */    /* Needs two values on the stack, the top one must be a symbol */
792    if(env->head==NULL || env->head->next==NULL    if(env->head==NULL || env->head->next==NULL) {
793       || env->head->item->type!=symb) {      printerr("Too Few Arguments");
794      printerr("Define what?");      env->err=1;
795        return;
796      }
797    
798      if(env->head->item->type!=symb) {
799        printerr("Bad Argument Type");
800        env->err=2;
801      return;      return;
802    }    }
803    
# Line 600  extern void clear(environment *env) Line 828  extern void clear(environment *env)
828      toss(env);      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    environment myenv;    environment myenv;
# Line 611  int main() Line 892  int main()
892    
893    while(fgets(in_string, 100, stdin) != NULL) {    while(fgets(in_string, 100, stdin) != NULL) {
894      stack_read(&myenv, 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      return EXIT_FAILURE;
903    }
904    
905    exit(EXIT_SUCCESS);  /* + */
906    extern void sx_2b(environment *env) {
907      int a, b;
908      size_t len;
909      char* new_string;
910      value *a_val, *b_val;
911    
912      if((env->head)==NULL || env->head->next==NULL) {
913        printerr("Too Few Arguments");
914        env->err=1;
915        return;
916      }
917    
918      if(env->head->item->type==string
919         && env->head->next->item->type==string) {
920        a_val= env->head->item;
921        b_val= env->head->next->item;
922        a_val->refcount++;
923        b_val->refcount++;
924        toss(env); if(env->err) return;
925        toss(env); if(env->err) return;
926        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
927        new_string= malloc(len);
928        strcpy(new_string, b_val->content.ptr);
929        strcat(new_string, a_val->content.ptr);
930        free_val(a_val); free_val(b_val);
931        push_cstring(&(env->head), new_string);
932        free(new_string);
933        return;
934      }
935      
936      if(env->head->item->type!=integer
937         || env->head->next->item->type!=integer) {
938        printerr("Bad Argument Type");
939        env->err=2;
940        return;
941      }
942      a=env->head->item->content.val;
943      toss(env);
944      if(env->err) return;
945      b=env->head->item->content.val;
946      toss(env);
947      if(env->err) return;
948      push_int(&(env->head), a+b);
949  }  }

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.49

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26