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

Diff of /stack/stack.c

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

revision 1.36 by teddy, Wed Feb 6 00:52:31 2002 UTC revision 1.42 by teddy, Wed Feb 6 21:26:46 2002 UTC
# Line 218  void free_val(value *val){ Line 218  void free_val(value *val){
218      switch (val->type){         /* and free the contents if necessary */      switch (val->type){         /* and free the contents if necessary */
219      case string:      case string:
220        free(val->content.ptr);        free(val->content.ptr);
221          break;
222      case list:                  /* lists needs to be freed recursively */      case list:                  /* lists needs to be freed recursively */
223        item=val->content.ptr;        item=val->content.ptr;
224        while(item != NULL) {     /* for all stack items */        while(item != NULL) {     /* for all stack items */
# Line 256  extern void nl() Line 257  extern void nl()
257    printf("\n");    printf("\n");
258  }  }
259    
260    /* Gets the type of a value */
261    extern void type(environment *env){
262      int typenum;
263    
264      if((env->head)==NULL) {
265        printerr("Too Few Arguments");
266        env->err=1;
267        return;
268      }
269      typenum=env->head->item->type;
270      toss(env);
271      switch(typenum){
272      case integer:
273        push_sym(env, "integer");
274        break;
275      case string:
276        push_sym(env, "string");
277        break;
278      case symb:
279        push_sym(env, "symbol");
280        break;
281      case func:
282        push_sym(env, "function");
283        break;
284      case list:
285        push_sym(env, "list");
286        break;
287      default:
288        push_sym(env, "unknown");
289        break;
290      }
291    }    
292    
293  /* Prints the top element of the stack. */  /* Prints the top element of the stack. */
294  void print_h(stackitem *stack_head)  void print_h(stackitem *stack_head)
295  {  {
# Line 273  void print_h(stackitem *stack_head) Line 307  void print_h(stackitem *stack_head)
307      printf("#<function %p>", (funcp)(stack_head->item->content.ptr));      printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
308      break;      break;
309    case list:    case list:
310      printf("#<list %p>", (funcp)(stack_head->item->content.ptr));      /* A list is just a stack, so make stack_head point to it */
311        stack_head=(stackitem *)(stack_head->item->content.ptr);
312        printf("[ ");
313        while(stack_head != NULL) {
314          print_h(stack_head);
315          printf(" ");
316          stack_head=stack_head->next;
317        }
318        printf("]");
319      break;      break;
320    default:    default:
321      printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr));      printf("#<unknown %p>", (stack_head->item->content.ptr));
322      break;      break;
323    }    }
324  }  }
# Line 314  void print_st(stackitem *stack_head, lon Line 356  void print_st(stackitem *stack_head, lon
356  extern void printstack(environment *env)  extern void printstack(environment *env)
357  {  {
358    if(env->head == NULL) {    if(env->head == NULL) {
     printerr("Too Few Arguments");  
     env->err=1;  
359      return;      return;
360    }    }
361    print_st(env->head, 1);    print_st(env->head, 1);
# Line 414  extern void eval(environment *env) Line 454  extern void eval(environment *env)
454    }    }
455  }  }
456    
457    /* Reverse a list */
458    extern void rev(environment *env){
459      stackitem *old_head, *new_head, *item;
460    
461      if((env->head)==NULL) {
462        printerr("Too Few Arguments");
463        env->err=1;
464        return;
465      }
466    
467      if(env->head->item->type!=list) {
468        printerr("Bad Argument Type");
469        env->err=2;
470        return;
471      }
472    
473      old_head=(stackitem *)(env->head->item->content.ptr);
474      new_head=NULL;
475      while(old_head != NULL){
476        item=old_head;
477        old_head=old_head->next;
478        item->next=new_head;
479        new_head=item;
480      }
481      env->head->item->content.ptr=new_head;
482    }
483    
484  /* Make a list. */  /* Make a list. */
485  extern void pack(environment *env)  extern void pack(environment *env)
486  {  {
# Line 454  extern void pack(environment *env) Line 521  extern void pack(environment *env)
521    temp->item= pack;    temp->item= pack;
522    
523    push(&(env->head), temp);    push(&(env->head), temp);
524      rev(env);
525  }  }
526    
527  /* Parse input. */  /* Parse input. */
528  int stack_read(environment *env, char *in_line)  void stack_read(environment *env, char *in_line)
529  {  {
530    char *temp, *rest;    char *temp, *rest;
531    int itemp;    int itemp;
# Line 518  int stack_read(environment *env, char *i Line 586  int stack_read(environment *env, char *i
586      }      }
587    } while(0);    } while(0);
588    
   
589    free(temp);    free(temp);
590    
591    if(convert<2) {    if(convert<2) {
592      free(rest);      free(rest);
593      return 0;      return;
594    }    }
595        
596    stack_read(env, rest);    stack_read(env, rest);
597        
598    free(rest);    free(rest);
   return 1;  
599  }  }
600    
601  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
# Line 731  int main() Line 797  int main()
797      }      }
798      printf("okidok\n ");      printf("okidok\n ");
799    }    }
800      quit(&myenv);
801    exit(EXIT_SUCCESS);    return EXIT_FAILURE;
802  }  }

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.42

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26