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

Diff of /stack/stack.c

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

revision 1.67 by masse, Sat Feb 9 00:57:05 2002 UTC revision 1.114 by teddy, Sun Mar 17 02:15:01 2002 UTC
# Line 1  Line 1 
1  /* printf, sscanf, fgets, fprintf */  /*
2        stack - an interactive interpreter for a stack-based language
3        Copyright (C) 2002  Mats Alritzson and Teddy Hogeborn
4    
5        This program is free software; you can redistribute it and/or modify
6        it under the terms of the GNU General Public License as published by
7        the Free Software Foundation; either version 2 of the License, or
8        (at your option) any later version.
9    
10        This program is distributed in the hope that it will be useful,
11        but WITHOUT ANY WARRANTY; without even the implied warranty of
12        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13        GNU General Public License for more details.
14    
15        You should have received a copy of the GNU General Public License
16        along with this program; if not, write to the Free Software
17        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18    
19        Authors: Mats Alritzson <masse@fukt.bth.se>
20                 Teddy Hogeborn <teddy@fukt.bth.se>
21    */
22    
23    #define CAR(X) (X->content.c->car)
24    #define CDR(X) (X->content.c->cdr)
25    
26    /* printf, sscanf, fgets, fprintf, fopen, perror */
27  #include <stdio.h>  #include <stdio.h>
28  /* exit, EXIT_SUCCESS, malloc, free */  /* exit, EXIT_SUCCESS, malloc, free */
29  #include <stdlib.h>  #include <stdlib.h>
# Line 8  Line 33 
33  #include <dlfcn.h>  #include <dlfcn.h>
34  /* strcmp, strcpy, strlen, strcat, strdup */  /* strcmp, strcpy, strlen, strcat, strdup */
35  #include <string.h>  #include <string.h>
36    /* getopt, STDIN_FILENO, STDOUT_FILENO, usleep */
37    #include <unistd.h>
38    /* EX_NOINPUT, EX_USAGE */
39    #include <sysexits.h>
40    /* assert */
41    #include <assert.h>
42    
43    #ifdef __linux__
44    /* mtrace, muntrace */
45    #include <mcheck.h>
46    /* ioctl */
47    #include <sys/ioctl.h>
48    /* KDMKTONE */
49    #include <linux/kd.h>
50    #endif /* __linux__ */
51    
52  #define HASHTBLSIZE 65536  #include "stack.h"
   
 /* First, define some types. */  
   
 /* A value of some type */  
 typedef struct {  
   enum {  
     integer,  
     string,  
     func,                       /* Function pointer */  
     symb,  
     list  
   } type;                       /* Type of stack element */  
   
   union {  
     void *ptr;                  /* Pointer to the content */  
     int val;                    /* ...or an integer */  
   } content;                    /* Stores a pointer or an integer */  
   
   int refcount;                 /* Reference counter */  
   
 } value;  
   
 /* A symbol with a name and possible value */  
 /* (These do not need reference counters, they are kept unique by  
    hashing.) */  
 typedef struct symbol_struct {  
   char *id;                     /* Symbol name */  
   value *val;                   /* The value (if any) bound to it */  
   struct symbol_struct *next;   /* In case of hashing conflicts, a */  
 } symbol;                       /* symbol is a kind of stack item. */  
   
 /* A type for a hash table for symbols */  
 typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */  
   
 /* An item (value) on a stack */  
 typedef struct stackitem_struct  
 {  
   value *item;                  /* The value on the stack */  
                                 /* (This is never NULL) */  
   struct stackitem_struct *next; /* Next item */  
 } stackitem;  
   
 /* An environment; gives access to the stack and a hash table of  
    defined symbols */  
 typedef struct {  
   stackitem *head;              /* Head of the stack */  
   hashtbl symbols;              /* Hash table of all variable bindings */  
   int err;                      /* Error flag */  
   int non_eval_flag;  
 } environment;  
   
 /* A type for pointers to external functions */  
 typedef void (*funcp)(environment *); /* funcp is a pointer to a void  
                                          function (environment *) */  
53    
54  /* Initialize a newly created environment */  /* Initialize a newly created environment */
55  void init_env(environment *env)  void init_env(environment *env)
56  {  {
57    int i;    int i;
58    
59    env->err= 0;    env->gc_limit= 400000;
60    env->non_eval_flag= 0;    env->gc_count= 0;
61      env->gc_ref= NULL;
62    
63      env->head= new_val(env);
64      env->head->type= empty;
65    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
66      env->symbols[i]= NULL;      env->symbols[i]= NULL;
67      env->err= 0;
68      env->in_string= NULL;
69      env->free_string= NULL;
70      env->inputstream= stdin;
71      env->interactive= 1;
72  }  }
73    
74  void printerr(const char* in_string) {  void printerr(const char* in_string)
75    {
76    fprintf(stderr, "Err: %s\n", in_string);    fprintf(stderr, "Err: %s\n", in_string);
77  }  }
78    
 /* 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);  
       break;  
     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;  
     case integer:  
     case func:  
     case symb:  
       break;  
     }  
   }  
 }  
   
79  /* Discard the top element of the stack. */  /* Discard the top element of the stack. */
80  extern void toss(environment *env)  extern void toss(environment *env)
81  {  {
82    stackitem *temp= env->head;    if(env->head->type==empty) {
   
   if((env->head)==NULL) {  
83      printerr("Too Few Arguments");      printerr("Too Few Arguments");
84      env->err=1;      env->err= 1;
85      return;      return;
86    }    }
87        
88    free_val(env->head->item);    /* Free the value */    env->head= CDR(env->head); /* Remove the top stack item */
   env->head= env->head->next;   /* Remove the top stack item */  
   free(temp);                   /* Free the old top stack item */  
89  }  }
90    
91  /* 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. */
# Line 153  symbol **hash(hashtbl in_hashtbl, const Line 117  symbol **hash(hashtbl in_hashtbl, const
117    }    }
118  }  }
119    
120  /* Generic push function. */  /* Create new value */
121  void push(stackitem** stack_head, stackitem* in_item)  value* new_val(environment *env)
122    {
123      value *nval= malloc(sizeof(value));
124      stackitem *nitem= malloc(sizeof(stackitem));
125    
126      nval->content.ptr= NULL;
127      nval->type= integer;
128    
129      nitem->item= nval;
130      nitem->next= env->gc_ref;
131    
132      env->gc_ref= nitem;
133    
134      env->gc_count += sizeof(value);
135      nval->gc.flag.mark= 0;
136      nval->gc.flag.protect= 0;
137    
138      return nval;
139    }
140    
141    /* Mark values recursively.
142       Marked values are not collected by the GC. */
143    inline void gc_mark(value *val)
144    {
145      if(val==NULL || val->gc.flag.mark)
146        return;
147    
148      val->gc.flag.mark= 1;
149    
150      if(val->type==tcons) {
151        gc_mark(CAR(val));
152        gc_mark(CDR(val));
153      }
154    }
155    
156    inline void gc_maybe(environment *env)
157    {
158      if(env->gc_count < env->gc_limit)
159        return;
160      else
161        return gc_init(env);
162    }
163    
164    /* Start GC */
165    extern void gc_init(environment *env)
166    {
167      stackitem *new_head= NULL, *titem;
168      cons *iterator;
169      symbol *tsymb;
170      int i;
171    
172      if(env->interactive)
173        printf("Garbage collecting.");
174    
175      /* Mark values on stack */
176      gc_mark(env->head);
177    
178      if(env->interactive)
179        printf(".");
180    
181    
182      /* Mark values in hashtable */
183      for(i= 0; i<HASHTBLSIZE; i++)
184        for(tsymb= env->symbols[i]; tsymb!=NULL; tsymb= tsymb->next)
185          if (tsymb->val != NULL)
186            gc_mark(tsymb->val);
187    
188    
189      if(env->interactive)
190        printf(".");
191    
192      env->gc_count= 0;
193    
194      while(env->gc_ref!=NULL) {    /* Sweep unused values */
195    
196        if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */
197    
198          if(env->gc_ref->item->type==string) /* Remove content */
199            free(env->gc_ref->item->content.ptr);
200    
201          free(env->gc_ref->item);  /* Remove from gc_ref */
202          titem= env->gc_ref->next;
203          free(env->gc_ref);        /* Remove value */
204          env->gc_ref= titem;
205          continue;
206        }
207    #ifdef DEBUG
208        printf("Kept value (%p)", env->gc_ref->item);
209        if(env->gc_ref->item->gc.flag.mark)
210          printf(" (marked)");
211        if(env->gc_ref->item->gc.flag.protect)
212          printf(" (protected)");
213        switch(env->gc_ref->item->type){
214        case integer:
215          printf(" integer: %d", env->gc_ref->item->content.i);
216          break;
217        case func:
218          printf(" func: %p", env->gc_ref->item->content.ptr);
219          break;
220        case symb:
221          printf(" symb: %s", env->gc_ref->item->content.sym->id);
222          break;
223        case tcons:
224          printf(" tcons: %p\t%p", env->gc_ref->item->content.c->car,
225                 env->gc_ref->item->content.c->cdr);
226          break;
227        default:
228          printf(" <unknown %d>", (env->gc_ref->item->type));
229        }
230        printf("\n");
231    #endif /* DEBUG */
232    
233        /* Keep values */    
234        env->gc_count += sizeof(value);
235        if(env->gc_ref->item->type==string)
236          env->gc_count += strlen(env->gc_ref->item->content.ptr)+1;
237        
238        titem= env->gc_ref->next;
239        env->gc_ref->next= new_head;
240        new_head= env->gc_ref;
241        new_head->item->gc.flag.mark= 0;
242        env->gc_ref= titem;
243      }
244    
245      if (env->gc_limit < env->gc_count*2)
246        env->gc_limit= env->gc_count*2;
247    
248      env->gc_ref= new_head;
249    
250      if(env->interactive)
251        printf("done (%d bytes still allocated)\n", env->gc_count);
252    
253    }
254    
255    /* Protect values from GC */
256    void protect(value *val)
257    {
258      if(val==NULL || val->gc.flag.protect)
259        return;
260    
261      val->gc.flag.protect= 1;
262    
263      if(val->type==tcons) {
264        protect(CAR(val));
265        protect(CDR(val));
266      }
267    }
268    
269    /* Unprotect values from GC */
270    void unprotect(value *val)
271  {  {
272    in_item->next= *stack_head;    if(val==NULL || !(val->gc.flag.protect))
273    *stack_head= in_item;      return;
274    
275      val->gc.flag.protect= 0;
276    
277      if(val->type==tcons) {
278        unprotect(CAR(val));
279        unprotect(CDR(val));
280      }
281  }  }
282    
283  /* Push a value onto the stack */  /* Push a value onto the stack */
284  void push_val(stackitem **stack_head, value *val)  void push_val(environment *env, value *val)
285  {  {
286    stackitem *new_item= malloc(sizeof(stackitem));    value *new_value= new_val(env);
287    new_item->item= val;  
288    val->refcount++;    new_value->content.c= malloc(sizeof(cons));
289    push(stack_head, new_item);    assert(new_value->content.c!=NULL);
290      new_value->type= tcons;
291      CAR(new_value)= val;
292      CDR(new_value)= env->head;
293      env->head= new_value;
294  }  }
295    
296  /* Push an integer onto the stack. */  /* Push an integer onto the stack */
297  void push_int(stackitem **stack_head, int in_val)  void push_int(environment *env, int in_val)
298  {  {
299    value *new_value= malloc(sizeof(value));    value *new_value= new_val(env);
   stackitem *new_item= malloc(sizeof(stackitem));  
   new_item->item= new_value;  
300        
301    new_value->content.val= in_val;    new_value->content.i= in_val;
302    new_value->type= integer;    new_value->type= integer;
   new_value->refcount=1;  
303    
304    push(stack_head, new_item);    push_val(env, new_value);
305    }
306    
307    /* Push a floating point number onto the stack */
308    void push_float(environment *env, float in_val)
309    {
310      value *new_value= new_val(env);
311    
312      new_value->content.f= in_val;
313      new_value->type= tfloat;
314    
315      push_val(env, new_value);
316  }  }
317    
318  /* Copy a string onto the stack. */  /* Copy a string onto the stack. */
319  void push_cstring(stackitem **stack_head, const char *in_string)  void push_cstring(environment *env, const char *in_string)
320  {  {
321    value *new_value= malloc(sizeof(value));    value *new_value= new_val(env);
322    stackitem *new_item= malloc(sizeof(stackitem));    int length= strlen(in_string)+1;
   new_item->item=new_value;  
323    
324    new_value->content.ptr= malloc(strlen(in_string)+1);    new_value->content.ptr= malloc(length);
325      env->gc_count += length;
326    strcpy(new_value->content.ptr, in_string);    strcpy(new_value->content.ptr, in_string);
327    new_value->type= string;    new_value->type= string;
   new_value->refcount=1;  
328    
329    push(stack_head, new_item);    push_val(env, new_value);
330  }  }
331    
332  /* Mangle a symbol name to a valid C identifier name */  /* Mangle a symbol name to a valid C identifier name */
333  char *mangle_str(const char *old_string){  char *mangle_str(const char *old_string)
334    char validchars[]  {
335      ="0123456789abcdef";    char validchars[]= "0123456789abcdef";
336    char *new_string, *current;    char *new_string, *current;
337    
338    new_string=malloc((strlen(old_string)*2)+4);    new_string= malloc((strlen(old_string)*2)+4);
339    strcpy(new_string, "sx_");    /* Stack eXternal */    strcpy(new_string, "sx_");    /* Stack eXternal */
340    current=new_string+3;    current= new_string+3;
341    while(old_string[0] != '\0'){    while(old_string[0] != '\0'){
342      current[0]=validchars[(unsigned char)(old_string[0])/16];      current[0]= validchars[(unsigned char)(old_string[0])/16];
343      current[1]=validchars[(unsigned char)(old_string[0])%16];      current[1]= validchars[(unsigned char)(old_string[0])%16];
344      current+=2;      current+= 2;
345      old_string++;      old_string++;
346    }    }
347    current[0]='\0';    current[0]= '\0';
348    
349    return new_string;            /* The caller must free() it */    return new_string;            /* The caller must free() it */
350  }  }
351    
352  extern void mangle(environment *env){  extern void mangle(environment *env)
353    value *new_value;  {
354    char *new_string;    char *new_string;
355    
356    if((env->head)==NULL) {    if(env->head->type==empty) {
357      printerr("Too Few Arguments");      printerr("Too Few Arguments");
358      env->err=1;      env->err= 1;
359      return;      return;
360    }    }
361    
362    if(env->head->item->type!=string) {    if(CAR(env->head)->type!=string) {
363      printerr("Bad Argument Type");      printerr("Bad Argument Type");
364      env->err=2;      env->err= 2;
365      return;      return;
366    }    }
367    
368    new_string= mangle_str((const char *)(env->head->item->content.ptr));    new_string=
369        mangle_str((const char *)(CAR(env->head)->content.ptr));
370    
371    toss(env);    toss(env);
372    if(env->err) return;    if(env->err) return;
373    
374    new_value= malloc(sizeof(value));    push_cstring(env, new_string);
   new_value->content.ptr= new_string;  
   new_value->type= string;  
   new_value->refcount=1;  
   
   push_val(&(env->head), new_value);  
375  }  }
376    
377  /* Push a symbol onto the stack. */  /* Push a symbol onto the stack. */
378  void push_sym(environment *env, const char *in_string)  void push_sym(environment *env, const char *in_string)
379  {  {
   stackitem *new_item;          /* The new stack item */  
   /* ...which will contain... */  
380    value *new_value;             /* A new symbol value */    value *new_value;             /* A new symbol value */
381    /* ...which might point to... */    /* ...which might point to... */
382    symbol **new_symbol;          /* (if needed) A new actual symbol */    symbol **new_symbol;          /* (if needed) A new actual symbol */
# Line 264  void push_sym(environment *env, const ch Line 389  void push_sym(environment *env, const ch
389    const char *dlerr;            /* Dynamic linker error */    const char *dlerr;            /* Dynamic linker error */
390    char *mangled;                /* Mangled function name */    char *mangled;                /* Mangled function name */
391    
392    /* Create a new stack item containing a new value */    new_value= new_val(env);
393    new_item= malloc(sizeof(stackitem));    protect(new_value);
394    new_value= malloc(sizeof(value));    new_fvalue= new_val(env);
395    new_item->item=new_value;    protect(new_fvalue);
396    
397    /* The new value is a symbol */    /* The new value is a symbol */
398    new_value->type= symb;    new_value->type= symb;
   new_value->refcount= 1;  
399    
400    /* Look up the symbol name in the hash table */    /* Look up the symbol name in the hash table */
401    new_symbol= hash(env->symbols, in_string);    new_symbol= hash(env->symbols, in_string);
# Line 294  void push_sym(environment *env, const ch Line 418  void push_sym(environment *env, const ch
418      if(handle==NULL)            /* If no handle */      if(handle==NULL)            /* If no handle */
419        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
420    
421      funcptr= dlsym(handle, in_string); /* Get function pointer */      mangled= mangle_str(in_string); /* mangle the name */
422      dlerr=dlerror();      funcptr= dlsym(handle, mangled); /* and try to find it */
423    
424        dlerr= dlerror();
425      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
426        mangled=mangle_str(in_string);        funcptr= dlsym(handle, in_string); /* Get function pointer */
427        funcptr= dlsym(handle, mangled); /* try mangling it */        dlerr= dlerror();
       free(mangled);  
       dlerr=dlerror();  
428      }      }
429    
430      if(dlerr==NULL) {           /* If a function was found */      if(dlerr==NULL) {           /* If a function was found */
431        new_fvalue= malloc(sizeof(value)); /* Create a new value */        new_fvalue->type= func;   /* The new value is a function pointer */
432        new_fvalue->type=func;    /* The new value is a function pointer */        new_fvalue->content.ptr= funcptr; /* Store function pointer */
       new_fvalue->content.ptr=funcptr; /* Store function pointer */  
433        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new        (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
434                                           function value */                                           function value */
       new_fvalue->refcount= 1;  
435      }      }
436    
437        free(mangled);
438    }    }
439    push(&(env->head), new_item);  
440      push_val(env, new_value);
441      unprotect(new_value); unprotect(new_fvalue);
442  }  }
443    
444  /* Print newline. */  /* Print newline. */
# Line 321  extern void nl() Line 448  extern void nl()
448  }  }
449    
450  /* Gets the type of a value */  /* Gets the type of a value */
451  extern void type(environment *env){  extern void type(environment *env)
452    int typenum;  {
453      if(env->head->type==empty) {
   if((env->head)==NULL) {  
454      printerr("Too Few Arguments");      printerr("Too Few Arguments");
455      env->err=1;      env->err= 1;
456      return;      return;
457    }    }
458    typenum=env->head->item->type;  
459    toss(env);    switch(CAR(env->head)->type){
460    switch(typenum){    case empty:
461        push_sym(env, "empty");
462        break;
463    case integer:    case integer:
464      push_sym(env, "integer");      push_sym(env, "integer");
465      break;      break;
466      case tfloat:
467        push_sym(env, "float");
468        break;
469    case string:    case string:
470      push_sym(env, "string");      push_sym(env, "string");
471      break;      break;
# Line 344  extern void type(environment *env){ Line 475  extern void type(environment *env){
475    case func:    case func:
476      push_sym(env, "function");      push_sym(env, "function");
477      break;      break;
478    case list:    case tcons:
479      push_sym(env, "list");      push_sym(env, "pair");
480      break;      break;
481    }    }
482      swap(env);
483      if (env->err) return;
484      toss(env);
485  }      }    
486    
487  /* Prints the top element of the stack. */  /* Print a value */
488  void print_h(stackitem *stack_head)  void print_val(value *val, int noquote)
489  {  {
490    switch(stack_head->item->type) {    switch(val->type) {
491      case empty:
492        printf("[]");
493        break;
494    case integer:    case integer:
495      printf("%d", stack_head->item->content.val);      printf("%d", val->content.i);
496        break;
497      case tfloat:
498        printf("%f", val->content.f);
499      break;      break;
500    case string:    case string:
501      printf("%s", (char*)stack_head->item->content.ptr);      if(noquote)
502          printf("%s", (char*)(val->content.ptr));
503        else
504          printf("\"%s\"", (char*)(val->content.ptr));
505      break;      break;
506    case symb:    case symb:
507      printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);      printf("%s", val->content.sym->id);
508      break;      break;
509    case func:    case func:
510      printf("#<function %p>", (funcp)(stack_head->item->content.ptr));      printf("#<function %p>", (funcp)(val->content.ptr));
511      break;      break;
512    case list:    case tcons:
     /* A list is just a stack, so make stack_head point to it */  
     stack_head=(stackitem *)(stack_head->item->content.ptr);  
513      printf("[ ");      printf("[ ");
514      while(stack_head != NULL) {      do {
515        print_h(stack_head);        print_val(CAR(val), noquote);
516        printf(" ");        val= CDR(val);
517        stack_head=stack_head->next;        switch(val->type){
518      }        case empty:
519      printf("]");          break;
520          case tcons:
521            printf(" ");
522            break;
523          default:
524            printf(" . ");          /* Improper list */
525            print_val(val, noquote);
526          }
527        } while(val->type == tcons);
528        printf(" ]");
529      break;      break;
530    }    }
531  }  }
532    
533  extern void print_(environment *env) {  extern void print_(environment *env)
534    if(env->head==NULL) {  {
535      if(env->head->type==empty) {
536      printerr("Too Few Arguments");      printerr("Too Few Arguments");
537      env->err=1;      env->err= 1;
538      return;      return;
539    }    }
540    print_h(env->head);    print_val(CAR(env->head), 0);
541      nl();
542  }  }
543    
544  /* Prints the top element of the stack and then discards it. */  /* Prints the top element of the stack and then discards it. */
# Line 397  extern void print(environment *env) Line 549  extern void print(environment *env)
549    toss(env);    toss(env);
550  }  }
551    
552    extern void princ_(environment *env)
553    {
554      if(env->head->type==empty) {
555        printerr("Too Few Arguments");
556        env->err= 1;
557        return;
558      }
559      print_val(CAR(env->head), 1);
560    }
561    
562    /* Prints the top element of the stack and then discards it. */
563    extern void princ(environment *env)
564    {
565      princ_(env);
566      if(env->err) return;
567      toss(env);
568    }
569    
570  /* Only to be called by function printstack. */  /* Only to be called by function printstack. */
571  void print_st(stackitem *stack_head, long counter)  void print_st(value *stack_head, long counter)
572  {  {
573    if(stack_head->next != NULL)    if(CDR(stack_head)->type != empty)
574      print_st(stack_head->next, counter+1);      print_st(CDR(stack_head), counter+1);
575    printf("%ld: ", counter);    printf("%ld: ", counter);
576    print_h(stack_head);    print_val(CAR(stack_head), 0);
577    nl();    nl();
578  }  }
579    
580  /* Prints the stack. */  /* Prints the stack. */
581  extern void printstack(environment *env)  extern void printstack(environment *env)
582  {  {
583    if(env->head == NULL) {    if(env->head->type == empty) {
584        printf("Stack Empty\n");
585      return;      return;
586    }    }
587    
588    print_st(env->head, 1);    print_st(env->head, 1);
   nl();  
589  }  }
590    
591  /* Swap the two top elements on the stack. */  /* Swap the two top elements on the stack. */
592  extern void swap(environment *env)  extern void swap(environment *env)
593  {  {
594    stackitem *temp= env->head;    value *temp= env->head;
595        
596    if(env->head==NULL || env->head->next==NULL) {    if(env->head->type == empty || CDR(env->head)->type == empty) {
597      printerr("Too Few Arguments");      printerr("Too Few Arguments");
598      env->err=1;      env->err=1;
599      return;      return;
600    }    }
601    
602    env->head= env->head->next;    env->head= CDR(env->head);
603    temp->next= env->head->next;    CDR(temp)= CDR(env->head);
604    env->head->next= temp;    CDR(env->head)= temp;
605  }  }
606    
607  /* Rotate the first three elements on the stack. */  /* Rotate the first three elements on the stack. */
608  extern void rot(environment *env)  extern void rot(environment *env)
609  {  {
610    stackitem *temp= env->head;    value *temp= env->head;
611        
612    if(env->head==NULL || env->head->next==NULL    if(env->head->type == empty || CDR(env->head)->type == empty
613        || env->head->next->next==NULL) {       || CDR(CDR(env->head))->type == empty) {
614      printerr("Too Few Arguments");      printerr("Too Few Arguments");
615      env->err=1;      env->err= 1;
616      return;      return;
617    }    }
618      
619    env->head= env->head->next->next;    env->head= CDR(CDR(env->head));
620    temp->next->next= env->head->next;    CDR(CDR(temp))= CDR(env->head);
621    env->head->next= temp;    CDR(env->head)= temp;
622  }  }
623    
624  /* Recall a value from a symbol, if bound */  /* Recall a value from a symbol, if bound */
# Line 455  extern void rcl(environment *env) Line 626  extern void rcl(environment *env)
626  {  {
627    value *val;    value *val;
628    
629    if(env->head == NULL) {    if(env->head->type==empty) {
630      printerr("Too Few Arguments");      printerr("Too Few Arguments");
631      env->err=1;      env->err= 1;
632      return;      return;
633    }    }
634    
635    if(env->head->item->type!=symb) {    if(CAR(env->head)->type!=symb) {
636      printerr("Bad Argument Type");      printerr("Bad Argument Type");
637      env->err=2;      env->err= 2;
638      return;      return;
639    }    }
640    
641    val=((symbol *)(env->head->item->content.ptr))->val;    val= CAR(env->head)->content.sym->val;
642    if(val == NULL){    if(val == NULL){
643      printerr("Unbound Variable");      printerr("Unbound Variable");
644      env->err=3;      env->err= 3;
645      return;      return;
646    }    }
647    toss(env);            /* toss the symbol */    push_val(env, val);           /* Return the symbol's bound value */
648      swap(env);
649      if(env->err) return;
650      toss(env);                    /* toss the symbol */
651    if(env->err) return;    if(env->err) return;
   push_val(&(env->head), val); /* Return its bound value */  
652  }  }
653    
 void stack_read(environment*, char*);  
   
654  /* If the top element is a symbol, determine if it's bound to a  /* If the top element is a symbol, determine if it's bound to a
655     function value, and if it is, toss the symbol and execute the     function value, and if it is, toss the symbol and execute the
656     function. */     function. */
# Line 487  extern void eval(environment *env) Line 658  extern void eval(environment *env)
658  {  {
659    funcp in_func;    funcp in_func;
660    value* temp_val;    value* temp_val;
661    stackitem* iterator;    value* iterator;
662    char* temp_string;  
663     eval_start:
664    
665    if(env->head==NULL) {    gc_maybe(env);
666    
667      if(env->head->type==empty) {
668      printerr("Too Few Arguments");      printerr("Too Few Arguments");
669      env->err=1;      env->err= 1;
670      return;      return;
671    }    }
672    
673   eval_start:    switch(CAR(env->head)->type) {
   
   switch(env->head->item->type) {  
674      /* if it's a symbol */      /* if it's a symbol */
675    case symb:    case symb:
676      rcl(env);                   /* get its contents */      rcl(env);                   /* get its contents */
677      if(env->err) return;      if(env->err) return;
678      if(env->head->item->type!=symb){ /* don't recurse symbols */      if(CAR(env->head)->type!=symb){ /* don't recurse symbols */
679        goto eval_start;        goto eval_start;
680      }      }
681      return;      return;
682    
683      /* If it's a lone function value, run it */      /* If it's a lone function value, run it */
684    case func:    case func:
685      in_func= (funcp)(env->head->item->content.ptr);      in_func= (funcp)(CAR(env->head)->content.ptr);
686      toss(env);      toss(env);
687      if(env->err) return;      if(env->err) return;
688      return (*in_func)(env);      return in_func(env);
689    
690      /* If it's a list */      /* If it's a list */
691    case list:    case tcons:
692      temp_val= env->head->item;      temp_val= CAR(env->head);
693      env->head->item->refcount++;      protect(temp_val);
694      toss(env);  
695      if(env->err) return;      toss(env); if(env->err) return;
696      iterator= (stackitem*)temp_val->content.ptr;      iterator= temp_val;
697      while(iterator!=NULL) {      
698        push_val(&(env->head), iterator->item);      while(iterator->type != empty) {
699        if(env->head->item->type==symb        push_val(env, CAR(iterator));
700          && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {        
701          if(CAR(env->head)->type==symb
702             && CAR(env->head)->content.sym->id[0]==';') {
703          toss(env);          toss(env);
704          if(env->err) return;          if(env->err) return;
705          if(iterator->next == NULL){          
706            free_val(temp_val);          if(CDR(iterator)->type == empty){
707            goto eval_start;            goto eval_start;
708          }          }
709          eval(env);          eval(env);
710          if(env->err) return;          if(env->err) return;
711        }        }
712        iterator= iterator->next;        if (CDR(iterator)->type == empty || CDR(iterator)->type == tcons)
713            iterator= CDR(iterator);
714          else {
715            printerr("Bad Argument Type"); /* Improper list */
716            env->err= 2;
717            return;
718          }
719      }      }
720      free_val(temp_val);      unprotect(temp_val);
721      return;      return;
722    
723      /* If it's a string */    default:
   case string:  
     temp_val= env->head->item;  
     env->head->item->refcount++;  
     toss(env);  
     if(env->err) return;  
     temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);  
     strcpy(temp_string, "[ ");  
     strcpy(temp_string+2, (char*)temp_val->content.ptr);  
     free_val(temp_val);  
     strcat(temp_string, " ]");  
     stack_read(env, temp_string);  
     free(temp_string);  
     goto eval_start;  
   
   case integer:  
724      return;      return;
725    }    }
726  }  }
727    
728  /* Reverse (flip) a list */  /* Reverse (flip) a list */
729  extern void rev(environment *env){  extern void rev(environment *env)
730    stackitem *old_head, *new_head, *item;  {
731      value *old_head, *new_head, *item;
732    
733    if((env->head)==NULL) {    if(env->head->type==empty) {
734      printerr("Too Few Arguments");      printerr("Too Few Arguments");
735      env->err=1;      env->err= 1;
736      return;      return;
737    }    }
738    
739    if(env->head->item->type!=list) {    if(CAR(env->head)->type==empty)
740        return;                     /* Don't reverse an empty list */
741    
742      if(CAR(env->head)->type!=tcons) {
743      printerr("Bad Argument Type");      printerr("Bad Argument Type");
744      env->err=2;      env->err= 2;
745      return;      return;
746    }    }
747    
748    old_head=(stackitem *)(env->head->item->content.ptr);    old_head= CAR(env->head);
749    new_head=NULL;    new_head= new_val(env);
750    while(old_head != NULL){    new_head->type= empty;
751      item=old_head;    while(old_head->type != empty) {
752      old_head=old_head->next;      item= old_head;
753      item->next=new_head;      old_head= CDR(old_head);
754      new_head=item;      CDR(item)= new_head;
755        new_head= item;
756    }    }
757    env->head->item->content.ptr=new_head;    CAR(env->head)= new_head;
758  }  }
759    
760  /* Make a list. */  /* Make a list. */
761  extern void pack(environment *env)  extern void pack(environment *env)
762  {  {
763    void* delimiter;    value *iterator, *temp, *ending;
   stackitem *iterator, *temp;  
   value *pack;  
764    
765    delimiter= env->head->item->content.ptr; /* Get delimiter */    ending=new_val(env);
766    toss(env);    ending->type=empty;
767    
768    iterator= env->head;    iterator= env->head;
769      if(iterator->type == empty
770    if(iterator==NULL || iterator->item->content.ptr==delimiter) {       || (CAR(iterator)->type==symb
771      temp= NULL;       && CAR(iterator)->content.sym->id[0]=='[')) {
772        temp= ending;
773      toss(env);      toss(env);
774    } else {    } else {
775      /* Search for first delimiter */      /* Search for first delimiter */
776      while(iterator->next!=NULL      while(CDR(iterator)->type != empty
777            && iterator->next->item->content.ptr!=delimiter)            && (CAR(CDR(iterator))->type!=symb
778        iterator= iterator->next;             || CAR(CDR(iterator))->content.sym->id[0]!='['))
779          iterator= CDR(iterator);
780            
781      /* Extract list */      /* Extract list */
782      temp= env->head;      temp= env->head;
783      env->head= iterator->next;      env->head= CDR(iterator);
784      iterator->next= NULL;      CDR(iterator)= ending;
785        
786      if(env->head!=NULL)      if(env->head->type != empty)
787        toss(env);        toss(env);
788    }    }
789    
790    /* Push list */    /* Push list */
   pack= malloc(sizeof(value));  
   pack->type= list;  
   pack->content.ptr= temp;  
   pack->refcount= 1;  
   
   temp= malloc(sizeof(stackitem));  
   temp->item= pack;  
791    
792    push(&(env->head), temp);    push_val(env, temp);
793    rev(env);    rev(env);
794  }  }
795    
 /* Parse input. */  
 void stack_read(environment *env, char *in_line)  
 {  
   char *temp, *rest;  
   int itemp;  
   size_t inlength= strlen(in_line)+1;  
   int convert= 0;  
   
   temp= malloc(inlength);  
   rest= malloc(inlength);  
   
   do {  
     /* If comment */  
     if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {  
       free(temp); free(rest);  
       return;  
     }  
   
     /* If string */  
     if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {  
       push_cstring(&(env->head), temp);  
       break;  
     }  
     /* If integer */  
     if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {  
       push_int(&(env->head), itemp);  
       break;  
     }  
     /* Escape ';' with '\' */  
     if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {  
       temp[1]= '\0';  
       push_sym(env, temp);  
       break;  
     }  
     /* If symbol */  
     if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {  
         push_sym(env, temp);  
         break;  
     }  
     /* If single char */  
     if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {  
       if(*temp==';') {  
         if(!env->non_eval_flag) {  
           eval(env);            /* Evaluate top element */  
           break;  
         }  
           
         push_sym(env, ";");  
         break;  
       }  
   
       if(*temp==']') {  
         push_sym(env, "[");  
         pack(env);  
         if(env->non_eval_flag)  
           env->non_eval_flag--;  
         break;  
       }  
   
       if(*temp=='[') {  
         push_sym(env, "[");  
         env->non_eval_flag++;  
         break;  
       }  
     }  
   } while(0);  
   
   free(temp);  
   
   if(convert<2) {  
     free(rest);  
     return;  
   }  
     
   stack_read(env, rest);  
     
   free(rest);  
 }  
   
796  /* Relocate elements of the list on the stack. */  /* Relocate elements of the list on the stack. */
797  extern void expand(environment *env)  extern void expand(environment *env)
798  {  {
799    stackitem *temp, *new_head;    value *temp, *new_head;
800    
801    /* Is top element a list? */    /* Is top element a list? */
802    if(env->head==NULL) {    if(env->head->type==empty) {
803      printerr("Too Few Arguments");      printerr("Too Few Arguments");
804      env->err=1;      env->err= 1;
805      return;      return;
806    }    }
807    if(env->head->item->type!=list) {  
808      if(CAR(env->head)->type!=tcons) {
809      printerr("Bad Argument Type");      printerr("Bad Argument Type");
810      env->err=2;      env->err= 2;
811      return;      return;
812    }    }
813    
# Line 732  extern void expand(environment *env) Line 817  extern void expand(environment *env)
817      return;      return;
818    
819    /* The first list element is the new stack head */    /* The first list element is the new stack head */
820    new_head= temp= env->head->item->content.ptr;    new_head= temp= CAR(env->head);
821    
   env->head->item->refcount++;  
822    toss(env);    toss(env);
823    
824    /* Find the end of the list */    /* Find the end of the list */
825    while(temp->next!=NULL)    while(CDR(temp)->type != empty) {
826      temp= temp->next;      if (CDR(temp)->type == tcons)
827          temp= CDR(temp);
828        else {
829          printerr("Bad Argument Type"); /* Improper list */
830          env->err= 2;
831          return;
832        }
833      }
834    
835    /* Connect the tail of the list with the old stack head */    /* Connect the tail of the list with the old stack head */
836    temp->next= env->head;    CDR(temp)= env->head;
837    env->head= new_head;          /* ...and voila! */    env->head= new_head;          /* ...and voila! */
838    
839  }  }
# Line 751  extern void expand(environment *env) Line 842  extern void expand(environment *env)
842  extern void eq(environment *env)  extern void eq(environment *env)
843  {  {
844    void *left, *right;    void *left, *right;
   int result;  
845    
846    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
847      printerr("Too Few Arguments");      printerr("Too Few Arguments");
848      env->err=1;      env->err= 1;
849      return;      return;
850    }    }
851    
852    left= env->head->item->content.ptr;    left= CAR(env->head)->content.ptr;
853    swap(env);    right= CAR(CDR(env->head))->content.ptr;
   right= env->head->item->content.ptr;  
   result= (left==right);  
     
854    toss(env); toss(env);    toss(env); toss(env);
855    push_int(&(env->head), result);  
856      push_int(env, left==right);
857  }  }
858    
859  /* Negates the top element on the stack. */  /* Negates the top element on the stack. */
# Line 773  extern void not(environment *env) Line 861  extern void not(environment *env)
861  {  {
862    int val;    int val;
863    
864    if((env->head)==NULL) {    if(env->head->type==empty) {
865      printerr("Too Few Arguments");      printerr("Too Few Arguments");
866      env->err=1;      env->err= 1;
867      return;      return;
868    }    }
869    
870    if(env->head->item->type!=integer) {    if(CAR(env->head)->type!=integer) {
871      printerr("Bad Argument Type");      printerr("Bad Argument Type");
872      env->err=2;      env->err= 2;
873      return;      return;
874    }    }
875    
876    val= env->head->item->content.val;    val= CAR(env->head)->content.i;
877    toss(env);    toss(env);
878    push_int(&(env->head), !val);    push_int(env, !val);
879  }  }
880    
881  /* 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 804  extern void def(environment *env) Line 892  extern void def(environment *env)
892    symbol *sym;    symbol *sym;
893    
894    /* 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 */
895    if(env->head==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
896      printerr("Too Few Arguments");      printerr("Too Few Arguments");
897      env->err=1;      env->err= 1;
898      return;      return;
899    }    }
900    
901    if(env->head->item->type!=symb) {    if(CAR(env->head)->type!=symb) {
902      printerr("Bad Argument Type");      printerr("Bad Argument Type");
903      env->err=2;      env->err= 2;
904      return;      return;
905    }    }
906    
907    /* long names are a pain */    /* long names are a pain */
908    sym=env->head->item->content.ptr;    sym= CAR(env->head)->content.ptr;
   
   /* if the symbol was bound to something else, throw it away */  
   if(sym->val != NULL)  
     free_val(sym->val);  
909    
910    /* Bind the symbol to the value */    /* Bind the symbol to the value */
911    sym->val= env->head->next->item;    sym->val= CAR(CDR(env->head));
   sym->val->refcount++;         /* Increase the reference counter */  
912    
913    toss(env); toss(env);    toss(env); toss(env);
914  }  }
# Line 833  extern void def(environment *env) Line 916  extern void def(environment *env)
916  /* Quit stack. */  /* Quit stack. */
917  extern void quit(environment *env)  extern void quit(environment *env)
918  {  {
919      int i;
920    
921      clear(env);
922    
923      if (env->err) return;
924      for(i= 0; i<HASHTBLSIZE; i++) {
925        while(env->symbols[i]!= NULL) {
926          forget_sym(&(env->symbols[i]));
927        }
928        env->symbols[i]= NULL;
929      }
930    
931      env->gc_limit= 0;
932      gc_maybe(env);
933    
934      words(env);
935    
936      if(env->free_string!=NULL)
937        free(env->free_string);
938      
939    #ifdef __linux__
940      muntrace();
941    #endif
942    
943    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
944  }  }
945    
946  /* Clear stack */  /* Clear stack */
947  extern void clear(environment *env)  extern void clear(environment *env)
948  {  {
949    while(env->head!=NULL)    while(env->head->type != empty)
950      toss(env);      toss(env);
951  }  }
952    
# Line 852  extern void words(environment *env) Line 959  extern void words(environment *env)
959    for(i= 0; i<HASHTBLSIZE; i++) {    for(i= 0; i<HASHTBLSIZE; i++) {
960      temp= env->symbols[i];      temp= env->symbols[i];
961      while(temp!=NULL) {      while(temp!=NULL) {
962    #ifdef DEBUG
963          if (temp->val != NULL && temp->val->gc.flag.protect)
964            printf("(protected) ");
965    #endif /* DEBUG */
966        printf("%s\n", temp->id);        printf("%s\n", temp->id);
967        temp= temp->next;        temp= temp->next;
968      }      }
969    }    }
970  }  }
971    
972    /* Internal forget function */
973    void forget_sym(symbol **hash_entry)
974    {
975      symbol *temp;
976    
977      temp= *hash_entry;
978      *hash_entry= (*hash_entry)->next;
979      
980      free(temp->id);
981      free(temp);
982    }
983    
984  /* Forgets a symbol (remove it from the hash table) */  /* Forgets a symbol (remove it from the hash table) */
985  extern void forget(environment *env)  extern void forget(environment *env)
986  {  {
987    char* sym_id;    char* sym_id;
   stackitem *stack_head= env->head;  
   symbol **hash_entry, *temp;  
988    
989    if(stack_head==NULL) {    if(env->head->type==empty) {
990      printerr("Too Few Arguments");      printerr("Too Few Arguments");
991      env->err=1;      env->err= 1;
992      return;      return;
993    }    }
994        
995    if(stack_head->item->type!=symb) {    if(CAR(env->head)->type!=symb) {
996      printerr("Bad Argument Type");      printerr("Bad Argument Type");
997      env->err=2;      env->err= 2;
998      return;      return;
999    }    }
1000    
1001    sym_id= ((symbol*)(stack_head->item->content.ptr))->id;    sym_id= CAR(env->head)->content.sym->id;
1002    toss(env);    toss(env);
1003    
1004    hash_entry= hash(env->symbols, sym_id);    return forget_sym(hash(env->symbols, sym_id));
   temp= *hash_entry;  
   *hash_entry= (*hash_entry)->next;  
     
   if(temp->val!=NULL) {  
     free_val(temp->val);  
   }  
   free(temp->id);  
   free(temp);  
1005  }  }
1006    
1007  /* Returns the current error number to the stack */  /* Returns the current error number to the stack */
1008  extern void errn(environment *env){  extern void errn(environment *env)
1009    push_int(&(env->head), env->err);  {
1010      push_int(env, env->err);
1011  }  }
1012    
1013  int main()  int main(int argc, char **argv)
1014  {  {
1015    environment myenv;    environment myenv;
1016    char in_string[100];  
1017      int c;                        /* getopt option character */
1018    
1019    #ifdef __linux__
1020      mtrace();
1021    #endif
1022    
1023    init_env(&myenv);    init_env(&myenv);
1024    
1025    printf("okidok\n ");    myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
1026    
1027      while ((c = getopt (argc, argv, "i")) != -1)
1028        switch (c)
1029          {
1030          case 'i':
1031            myenv.interactive = 1;
1032            break;
1033          case '?':
1034            fprintf (stderr,
1035                     "Unknown option character '\\x%x'.\n",
1036                     optopt);
1037            return EX_USAGE;
1038          default:
1039            abort ();
1040          }
1041      
1042      if (optind < argc) {
1043        myenv.interactive = 0;
1044        myenv.inputstream= fopen(argv[optind], "r");
1045        if(myenv.inputstream== NULL) {
1046          perror(argv[0]);
1047          exit (EX_NOINPUT);
1048        }
1049      }
1050    
1051      if(myenv.interactive) {
1052        printf("Stack version $Revision$\n\
1053    Copyright (C) 2002  Mats Alritzson and Teddy Hogeborn\n\
1054    Stack comes with ABSOLUTELY NO WARRANTY; for details type 'warranty;'.\n\
1055    This is free software, and you are welcome to redistribute it\n\
1056    under certain conditions; type 'copying;' for details.\n");
1057      }
1058    
1059    while(fgets(in_string, 100, stdin) != NULL) {    while(1) {
1060      stack_read(&myenv, in_string);      if(myenv.in_string==NULL) {
1061      if(myenv.err) {        if (myenv.interactive) {
1062        printf("(error %d) ", myenv.err);          if(myenv.err) {
1063              printf("(error %d)\n", myenv.err);
1064            }
1065            nl();
1066            printstack(&myenv);
1067            printf("> ");
1068          }
1069        myenv.err=0;        myenv.err=0;
1070      }      }
1071      printf("okidok\n ");      sx_72656164(&myenv);        /* "read" */
1072        if (myenv.err==4) {         /* EOF */
1073          myenv.err=0;
1074          quit(&myenv);
1075        } else if(myenv.head->type!=empty
1076                  && CAR(myenv.head)->type==symb
1077                  && CAR(myenv.head)->content.sym->id[0]
1078                  ==';') {
1079          toss(&myenv);             /* No error check in main */
1080          eval(&myenv);
1081        }
1082        gc_maybe(&myenv);
1083    }    }
1084    quit(&myenv);    quit(&myenv);
1085    return EXIT_FAILURE;    return EXIT_FAILURE;
1086  }  }
1087    
1088  /* + */  /* "+" */
1089  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env)
1090    {
1091    int a, b;    int a, b;
1092      float fa, fb;
1093    size_t len;    size_t len;
1094    char* new_string;    char* new_string;
1095    value *a_val, *b_val;    value *a_val, *b_val;
1096    
1097    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
1098      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1099      env->err=1;      env->err= 1;
1100      return;      return;
1101    }    }
1102    
1103    if(env->head->item->type==string    if(CAR(env->head)->type==string
1104       && env->head->next->item->type==string) {       && CAR(CDR(env->head))->type==string) {
1105      a_val= env->head->item;      a_val= CAR(env->head);
1106      b_val= env->head->next->item;      b_val= CAR(CDR(env->head));
1107      a_val->refcount++;      protect(a_val); protect(b_val);
     b_val->refcount++;  
1108      toss(env); if(env->err) return;      toss(env); if(env->err) return;
1109      toss(env); if(env->err) return;      toss(env); if(env->err) return;
1110      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;      len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
1111      new_string= malloc(len);      new_string= malloc(len);
1112      strcpy(new_string, b_val->content.ptr);      strcpy(new_string, b_val->content.ptr);
1113      strcat(new_string, a_val->content.ptr);      strcat(new_string, a_val->content.ptr);
1114      free_val(a_val); free_val(b_val);      push_cstring(env, new_string);
1115      push_cstring(&(env->head), new_string);      unprotect(a_val); unprotect(b_val);
1116      free(new_string);      free(new_string);
1117        
1118      return;      return;
1119    }    }
1120        
1121    if(env->head->item->type!=integer    if(CAR(env->head)->type==integer
1122       || env->head->next->item->type!=integer) {       && CAR(CDR(env->head))->type==integer) {
1123      printerr("Bad Argument Type");      a= CAR(env->head)->content.i;
1124      env->err=2;      toss(env); if(env->err) return;
1125        b= CAR(env->head)->content.i;
1126        toss(env); if(env->err) return;
1127        push_int(env, b+a);
1128    
1129      return;      return;
1130    }    }
1131    a=env->head->item->content.val;  
1132    toss(env);    if(CAR(env->head)->type==tfloat
1133    if(env->err) return;       && CAR(CDR(env->head))->type==tfloat) {
1134    if(env->head->item->refcount == 1)      fa= CAR(env->head)->content.f;
1135      env->head->item->content.val += a;      toss(env); if(env->err) return;
1136    else {      fb= CAR(env->head)->content.f;
1137      b=env->head->item->content.val;      toss(env); if(env->err) return;
1138      toss(env);      push_float(env, fb+fa);
1139      if(env->err) return;      
1140      push_int(&(env->head), a+b);      return;
1141      }
1142    
1143      if(CAR(env->head)->type==tfloat
1144         && CAR(CDR(env->head))->type==integer) {
1145        fa= CAR(env->head)->content.f;
1146        toss(env); if(env->err) return;
1147        b= CAR(env->head)->content.i;
1148        toss(env); if(env->err) return;
1149        push_float(env, b+fa);
1150        
1151        return;
1152      }
1153    
1154      if(CAR(env->head)->type==integer
1155         && CAR(CDR(env->head))->type==tfloat) {
1156        a= CAR(env->head)->content.i;
1157        toss(env); if(env->err) return;
1158        fb= CAR(env->head)->content.f;
1159        toss(env); if(env->err) return;
1160        push_float(env, fb+a);
1161    
1162        return;
1163    }    }
1164    
1165      printerr("Bad Argument Type");
1166      env->err=2;
1167  }  }
1168    
1169  /* - */  /* "-" */
1170  extern void sx_2d(environment *env) {  extern void sx_2d(environment *env)
1171    {
1172    int a, b;    int a, b;
1173      float fa, fb;
1174    
1175    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
1176      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1177      env->err=1;      env->err=1;
1178      return;      return;
1179    }    }
1180        
1181    if(env->head->item->type!=integer    if(CAR(env->head)->type==integer
1182       || env->head->next->item->type!=integer) {       && CAR(CDR(env->head))->type==integer) {
1183      printerr("Bad Argument Type");      a= CAR(env->head)->content.i;
1184      env->err=2;      toss(env); if(env->err) return;
1185        b= CAR(env->head)->content.i;
1186        toss(env); if(env->err) return;
1187        push_int(env, b-a);
1188    
1189      return;      return;
1190    }    }
1191    a=env->head->item->content.val;  
1192    toss(env);    if(CAR(env->head)->type==tfloat
1193    if(env->err) return;       && CAR(CDR(env->head))->type==tfloat) {
1194    if(env->head->item->refcount == 1)      fa= CAR(env->head)->content.f;
1195      env->head->item->content.val -= a;      toss(env); if(env->err) return;
1196    else {      fb= CAR(env->head)->content.f;
1197      b=env->head->item->content.val;      toss(env); if(env->err) return;
1198      toss(env);      push_float(env, fb-fa);
1199      if(env->err) return;      
1200      push_int(&(env->head), b-a);      return;
1201      }
1202    
1203      if(CAR(env->head)->type==tfloat
1204         && CAR(CDR(env->head))->type==integer) {
1205        fa= CAR(env->head)->content.f;
1206        toss(env); if(env->err) return;
1207        b= CAR(env->head)->content.i;
1208        toss(env); if(env->err) return;
1209        push_float(env, b-fa);
1210        
1211        return;
1212      }
1213    
1214      if(CAR(env->head)->type==integer
1215         && CAR(CDR(env->head))->type==tfloat) {
1216        a= CAR(env->head)->content.i;
1217        toss(env); if(env->err) return;
1218        fb= CAR(env->head)->content.f;
1219        toss(env); if(env->err) return;
1220        push_float(env, fb-a);
1221    
1222        return;
1223    }    }
1224    
1225      printerr("Bad Argument Type");
1226      env->err=2;
1227  }  }
1228    
1229  /* > */  /* ">" */
1230  extern void sx_3e(environment *env) {  extern void sx_3e(environment *env)
1231    {
1232    int a, b;    int a, b;
1233      float fa, fb;
1234    
1235    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
1236      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1237      env->err=1;      env->err= 1;
1238      return;      return;
1239    }    }
1240        
1241    if(env->head->item->type!=integer    if(CAR(env->head)->type==integer
1242       || env->head->next->item->type!=integer) {       && CAR(CDR(env->head))->type==integer) {
1243      printerr("Bad Argument Type");      a= CAR(env->head)->content.i;
1244      env->err=2;      toss(env); if(env->err) return;
1245        b= CAR(env->head)->content.i;
1246        toss(env); if(env->err) return;
1247        push_int(env, b>a);
1248    
1249      return;      return;
1250    }    }
1251    a=env->head->item->content.val;  
1252    toss(env);    if(CAR(env->head)->type==tfloat
1253    if(env->err) return;       && CAR(CDR(env->head))->type==tfloat) {
1254    if(env->head->item->refcount == 1)      fa= CAR(env->head)->content.f;
1255      env->head->item->content.val = (env->head->item->content.val > a);      toss(env); if(env->err) return;
1256    else {      fb= CAR(env->head)->content.f;
1257      b=env->head->item->content.val;      toss(env); if(env->err) return;
1258      toss(env);      push_int(env, fb>fa);
1259      if(env->err) return;      
1260      push_int(&(env->head), b>a);      return;
1261      }
1262    
1263      if(CAR(env->head)->type==tfloat
1264         && CAR(CDR(env->head))->type==integer) {
1265        fa= CAR(env->head)->content.f;
1266        toss(env); if(env->err) return;
1267        b= CAR(env->head)->content.i;
1268        toss(env); if(env->err) return;
1269        push_int(env, b>fa);
1270        
1271        return;
1272    }    }
1273    
1274      if(CAR(env->head)->type==integer
1275         && CAR(CDR(env->head))->type==tfloat) {
1276        a= CAR(env->head)->content.i;
1277        toss(env); if(env->err) return;
1278        fb= CAR(env->head)->content.f;
1279        toss(env); if(env->err) return;
1280        push_int(env, fb>a);
1281    
1282        return;
1283      }
1284    
1285      printerr("Bad Argument Type");
1286      env->err= 2;
1287    }
1288    
1289    /* "<" */
1290    extern void sx_3c(environment *env)
1291    {
1292      swap(env); if(env->err) return;
1293      sx_3e(env);
1294    }
1295    
1296    /* "<=" */
1297    extern void sx_3c3d(environment *env)
1298    {
1299      sx_3e(env); if(env->err) return;
1300      not(env);
1301    }
1302    
1303    /* ">=" */
1304    extern void sx_3e3d(environment *env)
1305    {
1306      sx_3c(env); if(env->err) return;
1307      not(env);
1308  }  }
1309    
1310  /* Return copy of a value */  /* Return copy of a value */
1311  value *copy_val(value *old_value){  value *copy_val(environment *env, value *old_value)
1312    stackitem *old_item, *new_item, *prev_item;  {
1313      value *new_value;
1314    
1315    value *new_value=malloc(sizeof(value));    if(old_value==NULL)
1316        return NULL;
1317    
1318      protect(old_value);
1319      new_value= new_val(env);
1320      new_value->type= old_value->type;
1321    
   new_value->type=old_value->type;  
   new_value->refcount=0;        /* This is increased if/when this  
                                    value is referenced somewhere, like  
                                    in a stack item or a variable */  
1322    switch(old_value->type){    switch(old_value->type){
1323      case tfloat:
1324    case integer:    case integer:
     new_value->content.val=old_value->content.val;  
     break;  
   case string:  
     (char *)(new_value->content.ptr)  
       = strdup((char *)(old_value->content.ptr));  
     break;  
1325    case func:    case func:
1326    case symb:    case symb:
1327      new_value->content.ptr=old_value->content.ptr;      new_value->content= old_value->content;
1328        break;
1329      case string:
1330        (char *)(new_value->content.ptr)=
1331          strdup((char *)(old_value->content.ptr));
1332      break;      break;
1333    case list:    case tcons:
     new_value->content.ptr=NULL;  
1334    
1335      prev_item=NULL;      new_value->content.c= malloc(sizeof(cons));
1336      old_item=(stackitem *)(old_value->content.ptr);      assert(new_value->content.c!=NULL);
1337    
1338      while(old_item != NULL) {   /* While list is not empty */      CAR(new_value)= copy_val(env, CAR(old_value)); /* recurse */
1339        new_item= malloc(sizeof(stackitem));      CDR(new_value)= copy_val(env, CDR(old_value)); /* recurse */
       new_item->item=copy_val(old_item->item); /* recurse */  
       new_item->next=NULL;  
       if(prev_item != NULL)     /* If this wasn't the first item */  
         prev_item->next=new_item; /* point the previous item to the  
                                      new item */  
       else  
         new_value->content.ptr=new_item;  
       old_item=old_item->next;  
       prev_item=new_item;  
     }      
1340      break;      break;
1341    }    }
1342    
1343      unprotect(old_value);
1344    
1345    return new_value;    return new_value;
1346  }  }
1347    
1348  /* duplicates an item on the stack */  /* "dup"; duplicates an item on the stack */
1349  extern void dup(environment *env) {  extern void sx_647570(environment *env)
1350    if((env->head)==NULL) {  {
1351      if(env->head->type==empty) {
1352      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1353      env->err=1;      env->err= 1;
1354      return;      return;
1355    }    }
1356    push_val(&(env->head), copy_val(env->head->item));    push_val(env, copy_val(env, CAR(env->head)));
1357  }  }
1358    
1359  /* "if", If-Then */  /* "if", If-Then */
1360  extern void sx_6966(environment *env) {  extern void sx_6966(environment *env)
1361    {
1362    int truth;    int truth;
1363    
1364    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
1365      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1366      env->err=1;      env->err= 1;
1367      return;      return;
1368    }    }
1369    
1370    if(env->head->next->item->type != integer) {    if(CAR(CDR(env->head))->type != integer) {
1371      printerr("Bad Argument Type");      printerr("Bad Argument Type");
1372      env->err=2;      env->err= 2;
1373      return;      return;
1374    }    }
1375        
1376    swap(env);    swap(env);
1377    if(env->err) return;    if(env->err) return;
1378        
1379    truth=env->head->item->content.val;    truth= CAR(env->head)->content.i;
1380    
1381    toss(env);    toss(env);
1382    if(env->err) return;    if(env->err) return;
# Line 1112  extern void sx_6966(environment *env) { Line 1388  extern void sx_6966(environment *env) {
1388  }  }
1389    
1390  /* If-Then-Else */  /* If-Then-Else */
1391  extern void ifelse(environment *env) {  extern void ifelse(environment *env)
1392    {
1393    int truth;    int truth;
1394    
1395    if((env->head)==NULL || env->head->next==NULL    if(env->head->type==empty || CDR(env->head)->type==empty
1396       || env->head->next->next==NULL) {       || CDR(CDR(env->head))->type==empty) {
1397      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1398      env->err=1;      env->err= 1;
1399      return;      return;
1400    }    }
1401    
1402    if(env->head->next->next->item->type != integer) {    if(CAR(CDR(CDR(env->head)))->type!=integer) {
1403      printerr("Bad Argument Type");      printerr("Bad Argument Type");
1404      env->err=2;      env->err= 2;
1405      return;      return;
1406    }    }
1407        
1408    rot(env);    rot(env);
1409    if(env->err) return;    if(env->err) return;
1410        
1411    truth=env->head->item->content.val;    truth= CAR(env->head)->content.i;
1412    
1413    toss(env);    toss(env);
1414    if(env->err) return;    if(env->err) return;
# Line 1147  extern void ifelse(environment *env) { Line 1423  extern void ifelse(environment *env) {
1423    eval(env);    eval(env);
1424  }  }
1425    
1426  /* while */  extern void sx_656c7365(environment *env)
1427  extern void sx_7768696c65(environment *env) {  {
1428      if(env->head->type==empty || CDR(env->head)->type==empty
1429         || CDR(CDR(env->head))->type==empty || CDR(CDR(CDR(env->head)))->type==empty
1430         || CDR(CDR(CDR(CDR(env->head))))->type==empty) {
1431        printerr("Too Few Arguments");
1432        env->err= 1;
1433        return;
1434      }
1435    
1436      if(CAR(CDR(env->head))->type!=symb
1437         || strcmp(CAR(CDR(env->head))->content.sym->id, "then")!=0
1438         || CAR(CDR(CDR(CDR(env->head))))->type!=symb
1439         || strcmp(CAR(CDR(CDR(CDR(env->head))))->content.sym->id, "if")!=0) {
1440        printerr("Bad Argument Type");
1441        env->err= 2;
1442        return;
1443      }
1444    
1445      swap(env); toss(env); rot(env); toss(env);
1446      ifelse(env);
1447    }
1448    
1449    extern void then(environment *env)
1450    {
1451      if(env->head->type==empty || CDR(env->head)->type==empty
1452         || CDR(CDR(env->head))->type==empty) {
1453        printerr("Too Few Arguments");
1454        env->err= 1;
1455        return;
1456      }
1457    
1458      if(CAR(CDR(env->head))->type!=symb
1459         || strcmp(CAR(CDR(env->head))->content.sym->id, "if")!=0) {
1460        printerr("Bad Argument Type");
1461        env->err= 2;
1462        return;
1463      }
1464    
1465      swap(env); toss(env);
1466      sx_6966(env);
1467    }
1468    
1469    /* "while" */
1470    extern void sx_7768696c65(environment *env)
1471    {
1472    int truth;    int truth;
1473    value *loop, *test;    value *loop, *test;
1474    
1475    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
1476      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1477      env->err=1;      env->err= 1;
1478      return;      return;
1479    }    }
1480    
1481    loop= env->head->item;    loop= CAR(env->head);
1482    loop->refcount++;    protect(loop);
1483    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1484    
1485    test= env->head->item;    test= CAR(env->head);
1486    test->refcount++;    protect(test);
1487    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1488    
1489    do {    do {
1490      push_val(&(env->head), test);      push_val(env, test);
1491      eval(env);      eval(env);
1492            
1493      if(env->head->item->type != integer) {      if(CAR(env->head)->type != integer) {
1494        printerr("Bad Argument Type");        printerr("Bad Argument Type");
1495        env->err=2;        env->err= 2;
1496        return;        return;
1497      }      }
1498            
1499      truth= env->head->item->content.val;      truth= CAR(env->head)->content.i;
1500      toss(env); if(env->err) return;      toss(env); if(env->err) return;
1501            
1502      if(truth) {      if(truth) {
1503        push_val(&(env->head), loop);        push_val(env, loop);
1504        eval(env);        eval(env);
1505      } else {      } else {
1506        toss(env);        toss(env);
# Line 1189  extern void sx_7768696c65(environment *e Line 1508  extern void sx_7768696c65(environment *e
1508        
1509    } while(truth);    } while(truth);
1510    
1511    free_val(test);    unprotect(loop); unprotect(test);
   free_val(loop);  
1512  }  }
1513    
1514  /* For-loop */  
1515  extern void sx_666f72(environment *env) {  /* "for"; for-loop */
1516      extern void sx_666f72(environment *env)
1517    {
1518      value *loop;
1519      int foo1, foo2;
1520    
1521      if(env->head->type==empty || CDR(env->head)->type==empty
1522         || CDR(CDR(env->head))->type==empty) {
1523        printerr("Too Few Arguments");
1524        env->err= 1;
1525        return;
1526      }
1527    
1528      if(CAR(CDR(env->head))->type!=integer
1529         || CAR(CDR(CDR(env->head)))->type!=integer) {
1530        printerr("Bad Argument Type");
1531        env->err= 2;
1532        return;
1533      }
1534    
1535      loop= CAR(env->head);
1536      protect(loop);
1537      toss(env); if(env->err) return;
1538    
1539      foo2= CAR(env->head)->content.i;
1540      toss(env); if(env->err) return;
1541    
1542      foo1= CAR(env->head)->content.i;
1543      toss(env); if(env->err) return;
1544    
1545      if(foo1<=foo2) {
1546        while(foo1<=foo2) {
1547          push_int(env, foo1);
1548          push_val(env, loop);
1549          eval(env); if(env->err) return;
1550          foo1++;
1551        }
1552      } else {
1553        while(foo1>=foo2) {
1554          push_int(env, foo1);
1555          push_val(env, loop);
1556          eval(env); if(env->err) return;
1557          foo1--;
1558        }
1559      }
1560      unprotect(loop);
1561    }
1562    
1563    /* Variant of for-loop */
1564    extern void foreach(environment *env)
1565    {  
1566    value *loop, *foo;    value *loop, *foo;
1567    stackitem *iterator;    value *iterator;
1568        
1569    if((env->head)==NULL || env->head->next==NULL) {    if(env->head->type==empty || CDR(env->head)->type==empty) {
1570      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1571      env->err=1;      env->err= 1;
1572      return;      return;
1573    }    }
1574    
1575    if(env->head->next->item->type != list) {    if(CAR(CDR(env->head))->type!=tcons) {
1576      printerr("Bad Argument Type");      printerr("Bad Argument Type");
1577      env->err=2;      env->err= 2;
1578      return;      return;
1579    }    }
1580    
1581    loop= env->head->item;    loop= CAR(env->head);
1582    loop->refcount++;    protect(loop);
1583    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1584    
1585    foo= env->head->item;    foo= CAR(env->head);
1586    foo->refcount++;    protect(foo);
1587    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1588    
1589    iterator= foo->content.ptr;    iterator= foo;
1590    
1591    while(iterator!=NULL) {    while(iterator!=NULL) {
1592      push_val(&(env->head), iterator->item);      push_val(env, CAR(iterator));
1593      push_val(&(env->head), loop);      push_val(env, loop);
1594      eval(env); if(env->err) return;      eval(env); if(env->err) return;
1595      iterator= iterator->next;      if (iterator->type == tcons){
1596          iterator= CDR(iterator);
1597        } else {
1598          printerr("Bad Argument Type"); /* Improper list */
1599          env->err= 2;
1600          break;
1601        }
1602    }    }
1603      unprotect(loop); unprotect(foo);
   free_val(loop);  
   free_val(foo);  
1604  }  }
1605    
1606  /* 'to' */  /* "to" */
1607  extern void to(environment *env) {  extern void to(environment *env)
1608    int i, start, ending;  {
1609        int ending, start, i;
1610    if((env->head)==NULL || env->head->next==NULL) {    value *iterator, *temp;
1611    
1612      if(env->head->type==empty || CDR(env->head)->type==empty) {
1613      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1614      env->err=1;      env->err= 1;
1615      return;      return;
1616    }    }
1617    
1618    if(env->head->item->type!=integer    if(CAR(env->head)->type!=integer
1619       || env->head->next->item->type!=integer) {       || CAR(CDR(env->head))->type!=integer) {
1620      printerr("Bad Argument Type");      printerr("Bad Argument Type");
1621      env->err=2;      env->err= 2;
1622      return;      return;
1623    }    }
1624    
1625    ending= env->head->item->content.val;    ending= CAR(env->head)->content.i;
1626    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1627    start= env->head->item->content.val;    start= CAR(env->head)->content.i;
1628    toss(env); if(env->err) return;    toss(env); if(env->err) return;
1629    
1630    push_sym(env, "[");    push_sym(env, "[");
1631    
1632    if(ending>=start) {    if(ending>=start) {
1633      for(i= start; i<=ending; i++)      for(i= ending; i>=start; i--)
1634        push_int(&(env->head), i);        push_int(env, i);
1635    } else {    } else {
1636      for(i= start; i>=ending; i--)      for(i= ending; i<=start; i++)
1637        push_int(&(env->head), i);        push_int(env, i);
1638    }    }
1639    
1640    push_sym(env, "[");    iterator= env->head;
1641    pack(env); if(env->err) return;  
1642      if(iterator->type==empty
1643         || (CAR(iterator)->type==symb
1644             && CAR(iterator)->content.sym->id[0]=='[')) {
1645        temp= NULL;
1646        toss(env);
1647      } else {
1648        /* Search for first delimiter */
1649        while(CDR(iterator)!=NULL
1650              && (CAR(CDR(iterator))->type!=symb
1651                  || CAR(CDR(iterator))->content.sym->id[0]!='['))
1652          iterator= CDR(iterator);
1653        
1654        /* Extract list */
1655        temp= env->head;
1656        env->head= CDR(iterator);
1657        CDR(iterator)= NULL;
1658    
1659        if(env->head!=NULL)
1660          toss(env);
1661      }
1662    
1663      /* Push list */
1664      push_val(env, temp);
1665    }
1666    
1667    /* Read a string */
1668    extern void readline(environment *env)
1669    {
1670      char in_string[101];
1671    
1672      if(fgets(in_string, 100, env->inputstream)==NULL)
1673        push_cstring(env, "");
1674      else
1675        push_cstring(env, in_string);
1676    }
1677    
1678    /* "read"; Read a value and place on stack */
1679    extern void sx_72656164(environment *env)
1680    {
1681      const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1682      const char strform[]= "\"%[^\"]\"%n";
1683      const char intform[]= "%i%n";
1684      const char fltform[]= "%f%n";
1685      const char blankform[]= "%*[ \t]%n";
1686      const char ebrackform[]= "]%n";
1687      const char semicform[]= ";%n";
1688      const char bbrackform[]= "[%n";
1689    
1690      int itemp, readlength= -1;
1691      int count= -1;
1692      float ftemp;
1693      static int depth= 0;
1694      char *match, *ctemp;
1695      size_t inlength;
1696    
1697      if(env->in_string==NULL) {
1698        if(depth > 0 && env->interactive) {
1699          printf("]> ");
1700        }
1701        readline(env); if(env->err) return;
1702    
1703        if(((char *)(CAR(env->head)->content.ptr))[0]=='\0'){
1704          env->err= 4;              /* "" means EOF */
1705          return;
1706        }
1707        
1708        env->in_string= malloc(strlen(CAR(env->head)->content.ptr)+1);
1709        env->free_string= env->in_string; /* Save the original pointer */
1710        strcpy(env->in_string, CAR(env->head)->content.ptr);
1711        toss(env); if(env->err) return;
1712      }
1713      
1714      inlength= strlen(env->in_string)+1;
1715      match= malloc(inlength);
1716    
1717      if(sscanf(env->in_string, blankform, &readlength) != EOF
1718         && readlength != -1) {
1719        ;
1720      } else if(sscanf(env->in_string, fltform, &ftemp, &readlength) != EOF
1721                && readlength != -1) {
1722        if(sscanf(env->in_string, intform, &itemp, &count) != EOF
1723           && count==readlength) {
1724          push_int(env, itemp);
1725        } else {
1726          push_float(env, ftemp);
1727        }
1728      } else if(sscanf(env->in_string, "\"\"%n", &readlength) != EOF
1729                && readlength != -1) {
1730        push_cstring(env, "");
1731      } else if(sscanf(env->in_string, strform, match, &readlength) != EOF
1732                && readlength != -1) {
1733        push_cstring(env, match);
1734      } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF
1735                && readlength != -1) {
1736        push_sym(env, match);
1737      } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF
1738                && readlength != -1) {
1739        pack(env); if(env->err) return;
1740        if(depth != 0) depth--;
1741      } else if(sscanf(env->in_string, semicform, &readlength) != EOF
1742                && readlength != -1) {
1743        push_sym(env, ";");
1744      } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF
1745                && readlength != -1) {
1746        push_sym(env, "[");
1747        depth++;
1748      } else {
1749        free(env->free_string);
1750        env->in_string = env->free_string = NULL;
1751      }
1752      if (env->in_string != NULL) {
1753        env->in_string += readlength;
1754      }
1755    
1756      free(match);
1757    
1758      if(depth)
1759        return sx_72656164(env);
1760    }
1761    
1762    #ifdef __linux__
1763    extern void beep(environment *env)
1764    {
1765      int freq, dur, period, ticks;
1766    
1767      if(env->head->type==empty || CDR(env->head)->type==empty) {
1768        printerr("Too Few Arguments");
1769        env->err= 1;
1770        return;
1771      }
1772    
1773      if(CAR(env->head)->type!=integer
1774         || CAR(CDR(env->head))->type!=integer) {
1775        printerr("Bad Argument Type");
1776        env->err= 2;
1777        return;
1778      }
1779    
1780      dur= CAR(env->head)->content.i;
1781      toss(env);
1782      freq= CAR(env->head)->content.i;
1783      toss(env);
1784    
1785      period= 1193180/freq;         /* convert freq from Hz to period
1786                                       length */
1787      ticks= dur*.001193180;        /* convert duration from µseconds to
1788                                       timer ticks */
1789    
1790    /*    ticks=dur/1000; */
1791    
1792          /*  if (ioctl(STDOUT_FILENO, KDMKTONE, (125<<16) + 0x637)==0) */
1793      switch (ioctl(STDOUT_FILENO, KDMKTONE, (ticks<<16) | period)){
1794      case 0:
1795        usleep(dur);
1796        return;
1797      case -1:
1798        perror("beep");
1799        env->err= 5;
1800        return;
1801      default:
1802        abort();
1803      }
1804    }
1805    #endif /* __linux__ */
1806    
1807    /* "wait" */
1808    extern void sx_77616974(environment *env)
1809    {
1810      int dur;
1811    
1812      if(env->head->type==empty) {
1813        printerr("Too Few Arguments");
1814        env->err= 1;
1815        return;
1816      }
1817    
1818      if(CAR(env->head)->type!=integer) {
1819        printerr("Bad Argument Type");
1820        env->err= 2;
1821        return;
1822      }
1823    
1824      dur= CAR(env->head)->content.i;
1825      toss(env);
1826    
1827      usleep(dur);
1828    }
1829    
1830    extern void copying(environment *env)
1831    {
1832      printf("                  GNU GENERAL PUBLIC LICENSE\n\
1833                           Version 2, June 1991\n\
1834    \n\
1835     Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n\
1836         59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n\
1837     Everyone is permitted to copy and distribute verbatim copies\n\
1838     of this license document, but changing it is not allowed.\n\
1839    \n\
1840                                Preamble\n\
1841    \n\
1842      The licenses for most software are designed to take away your\n\
1843    freedom to share and change it.  By contrast, the GNU General Public\n\
1844    License is intended to guarantee your freedom to share and change free\n\
1845    software--to make sure the software is free for all its users.  This\n\
1846    General Public License applies to most of the Free Software\n\
1847    Foundation's software and to any other program whose authors commit to\n\
1848    using it.  (Some other Free Software Foundation software is covered by\n\
1849    the GNU Library General Public License instead.)  You can apply it to\n\
1850    your programs, too.\n\
1851    \n\
1852      When we speak of free software, we are referring to freedom, not\n\
1853    price.  Our General Public Licenses are designed to make sure that you\n\
1854    have the freedom to distribute copies of free software (and charge for\n\
1855    this service if you wish), that you receive source code or can get it\n\
1856    if you want it, that you can change the software or use pieces of it\n\
1857    in new free programs; and that you know you can do these things.\n\
1858    \n\
1859      To protect your rights, we need to make restrictions that forbid\n\
1860    anyone to deny you these rights or to ask you to surrender the rights.\n\
1861    These restrictions translate to certain responsibilities for you if you\n\
1862    distribute copies of the software, or if you modify it.\n\
1863    \n\
1864      For example, if you distribute copies of such a program, whether\n\
1865    gratis or for a fee, you must give the recipients all the rights that\n\
1866    you have.  You must make sure that they, too, receive or can get the\n\
1867    source code.  And you must show them these terms so they know their\n\
1868    rights.\n\
1869    \n\
1870      We protect your rights with two steps: (1) copyright the software, and\n\
1871    (2) offer you this license which gives you legal permission to copy,\n\
1872    distribute and/or modify the software.\n\
1873    \n\
1874      Also, for each author's protection and ours, we want to make certain\n\
1875    that everyone understands that there is no warranty for this free\n\
1876    software.  If the software is modified by someone else and passed on, we\n\
1877    want its recipients to know that what they have is not the original, so\n\
1878    that any problems introduced by others will not reflect on the original\n\
1879    authors' reputations.\n\
1880    \n\
1881      Finally, any free program is threatened constantly by software\n\
1882    patents.  We wish to avoid the danger that redistributors of a free\n\
1883    program will individually obtain patent licenses, in effect making the\n\
1884    program proprietary.  To prevent this, we have made it clear that any\n\
1885    patent must be licensed for everyone's free use or not licensed at all.\n\
1886    \n\
1887      The precise terms and conditions for copying, distribution and\n\
1888    modification follow.\n\
1889    \n\
1890                        GNU GENERAL PUBLIC LICENSE\n\
1891       TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n\
1892    \n\
1893      0. This License applies to any program or other work which contains\n\
1894    a notice placed by the copyright holder saying it may be distributed\n\
1895    under the terms of this General Public License.  The \"Program\", below,\n\
1896    refers to any such program or work, and a \"work based on the Program\"\n\
1897    means either the Program or any derivative work under copyright law:\n\
1898    that is to say, a work containing the Program or a portion of it,\n\
1899    either verbatim or with modifications and/or translated into another\n\
1900    language.  (Hereinafter, translation is included without limitation in\n\
1901    the term \"modification\".)  Each licensee is addressed as \"you\".\n\
1902    \n\
1903    Activities other than copying, distribution and modification are not\n\
1904    covered by this License; they are outside its scope.  The act of\n\
1905    running the Program is not restricted, and the output from the Program\n\
1906    is covered only if its contents constitute a work based on the\n\
1907    Program (independent of having been made by running the Program).\n\
1908    Whether that is true depends on what the Program does.\n\
1909    \n\
1910      1. You may copy and distribute verbatim copies of the Program's\n\
1911    source code as you receive it, in any medium, provided that you\n\
1912    conspicuously and appropriately publish on each copy an appropriate\n\
1913    copyright notice and disclaimer of warranty; keep intact all the\n\
1914    notices that refer to this License and to the absence of any warranty;\n\
1915    and give any other recipients of the Program a copy of this License\n\
1916    along with the Program.\n\
1917    \n\
1918    You may charge a fee for the physical act of transferring a copy, and\n\
1919    you may at your option offer warranty protection in exchange for a fee.\n\
1920    \n\
1921      2. You may modify your copy or copies of the Program or any portion\n\
1922    of it, thus forming a work based on the Program, and copy and\n\
1923    distribute such modifications or work under the terms of Section 1\n\
1924    above, provided that you also meet all of these conditions:\n\
1925    \n\
1926        a) You must cause the modified files to carry prominent notices\n\
1927        stating that you changed the files and the date of any change.\n\
1928    \n\
1929        b) You must cause any work that you distribute or publish, that in\n\
1930        whole or in part contains or is derived from the Program or any\n\
1931        part thereof, to be licensed as a whole at no charge to all third\n\
1932        parties under the terms of this License.\n\
1933    \n\
1934        c) If the modified program normally reads commands interactively\n\
1935        when run, you must cause it, when started running for such\n\
1936        interactive use in the most ordinary way, to print or display an\n\
1937        announcement including an appropriate copyright notice and a\n\
1938        notice that there is no warranty (or else, saying that you provide\n\
1939        a warranty) and that users may redistribute the program under\n\
1940        these conditions, and telling the user how to view a copy of this\n\
1941        License.  (Exception: if the Program itself is interactive but\n\
1942        does not normally print such an announcement, your work based on\n\
1943        the Program is not required to print an announcement.)\n\
1944    \n\
1945    These requirements apply to the modified work as a whole.  If\n\
1946    identifiable sections of that work are not derived from the Program,\n\
1947    and can be reasonably considered independent and separate works in\n\
1948    themselves, then this License, and its terms, do not apply to those\n\
1949    sections when you distribute them as separate works.  But when you\n\
1950    distribute the same sections as part of a whole which is a work based\n\
1951    on the Program, the distribution of the whole must be on the terms of\n\
1952    this License, whose permissions for other licensees extend to the\n\
1953    entire whole, and thus to each and every part regardless of who wrote it.\n\
1954    \n\
1955    Thus, it is not the intent of this section to claim rights or contest\n\
1956    your rights to work written entirely by you; rather, the intent is to\n\
1957    exercise the right to control the distribution of derivative or\n\
1958    collective works based on the Program.\n\
1959    \n\
1960    In addition, mere aggregation of another work not based on the Program\n\
1961    with the Program (or with a work based on the Program) on a volume of\n\
1962    a storage or distribution medium does not bring the other work under\n\
1963    the scope of this License.\n\
1964    \n\
1965      3. You may copy and distribute the Program (or a work based on it,\n\
1966    under Section 2) in object code or executable form under the terms of\n\
1967    Sections 1 and 2 above provided that you also do one of the following:\n\
1968    \n\
1969        a) Accompany it with the complete corresponding machine-readable\n\
1970        source code, which must be distributed under the terms of Sections\n\
1971        1 and 2 above on a medium customarily used for software interchange; or,\n\
1972    \n\
1973        b) Accompany it with a written offer, valid for at least three\n\
1974        years, to give any third party, for a charge no more than your\n\
1975        cost of physically performing source distribution, a complete\n\
1976        machine-readable copy of the corresponding source code, to be\n\
1977        distributed under the terms of Sections 1 and 2 above on a medium\n\
1978        customarily used for software interchange; or,\n\
1979    \n\
1980        c) Accompany it with the information you received as to the offer\n\
1981        to distribute corresponding source code.  (This alternative is\n\
1982        allowed only for noncommercial distribution and only if you\n\
1983        received the program in object code or executable form with such\n\
1984        an offer, in accord with Subsection b above.)\n\
1985    \n\
1986    The source code for a work means the preferred form of the work for\n\
1987    making modifications to it.  For an executable work, complete source\n\
1988    code means all the source code for all modules it contains, plus any\n\
1989    associated interface definition files, plus the scripts used to\n\
1990    control compilation and installation of the executable.  However, as a\n\
1991    special exception, the source code distributed need not include\n\
1992    anything that is normally distributed (in either source or binary\n\
1993    form) with the major components (compiler, kernel, and so on) of the\n\
1994    operating system on which the executable runs, unless that component\n\
1995    itself accompanies the executable.\n\
1996    \n\
1997    If distribution of executable or object code is made by offering\n\
1998    access to copy from a designated place, then offering equivalent\n\
1999    access to copy the source code from the same place counts as\n\
2000    distribution of the source code, even though third parties are not\n\
2001    compelled to copy the source along with the object code.\n\
2002    \n\
2003      4. You may not copy, modify, sublicense, or distribute the Program\n\
2004    except as expressly provided under this License.  Any attempt\n\
2005    otherwise to copy, modify, sublicense or distribute the Program is\n\
2006    void, and will automatically terminate your rights under this License.\n\
2007    However, parties who have received copies, or rights, from you under\n\
2008    this License will not have their licenses terminated so long as such\n\
2009    parties remain in full compliance.\n\
2010    \n\
2011      5. You are not required to accept this License, since you have not\n\
2012    signed it.  However, nothing else grants you permission to modify or\n\
2013    distribute the Program or its derivative works.  These actions are\n\
2014    prohibited by law if you do not accept this License.  Therefore, by\n\
2015    modifying or distributing the Program (or any work based on the\n\
2016    Program), you indicate your acceptance of this License to do so, and\n\
2017    all its terms and conditions for copying, distributing or modifying\n\
2018    the Program or works based on it.\n\
2019    \n\
2020      6. Each time you redistribute the Program (or any work based on the\n\
2021    Program), the recipient automatically receives a license from the\n\
2022    original licensor to copy, distribute or modify the Program subject to\n\
2023    these terms and conditions.  You may not impose any further\n\
2024    restrictions on the recipients' exercise of the rights granted herein.\n\
2025    You are not responsible for enforcing compliance by third parties to\n\
2026    this License.\n\
2027    \n\
2028      7. If, as a consequence of a court judgment or allegation of patent\n\
2029    infringement or for any other reason (not limited to patent issues),\n\
2030    conditions are imposed on you (whether by court order, agreement or\n\
2031    otherwise) that contradict the conditions of this License, they do not\n\
2032    excuse you from the conditions of this License.  If you cannot\n\
2033    distribute so as to satisfy simultaneously your obligations under this\n\
2034    License and any other pertinent obligations, then as a consequence you\n\
2035    may not distribute the Program at all.  For example, if a patent\n\
2036    license would not permit royalty-free redistribution of the Program by\n\
2037    all those who receive copies directly or indirectly through you, then\n\
2038    the only way you could satisfy both it and this License would be to\n\
2039    refrain entirely from distribution of the Program.\n\
2040    \n\
2041    If any portion of this section is held invalid or unenforceable under\n\
2042    any particular circumstance, the balance of the section is intended to\n\
2043    apply and the section as a whole is intended to apply in other\n\
2044    circumstances.\n\
2045    \n\
2046    It is not the purpose of this section to induce you to infringe any\n\
2047    patents or other property right claims or to contest validity of any\n\
2048    such claims; this section has the sole purpose of protecting the\n\
2049    integrity of the free software distribution system, which is\n\
2050    implemented by public license practices.  Many people have made\n\
2051    generous contributions to the wide range of software distributed\n\
2052    through that system in reliance on consistent application of that\n\
2053    system; it is up to the author/donor to decide if he or she is willing\n\
2054    to distribute software through any other system and a licensee cannot\n\
2055    impose that choice.\n\
2056    \n\
2057    This section is intended to make thoroughly clear what is believed to\n\
2058    be a consequence of the rest of this License.\n\
2059    \n\
2060      8. If the distribution and/or use of the Program is restricted in\n\
2061    certain countries either by patents or by copyrighted interfaces, the\n\
2062    original copyright holder who places the Program under this License\n\
2063    may add an explicit geographical distribution limitation excluding\n\
2064    those countries, so that distribution is permitted only in or among\n\
2065    countries not thus excluded.  In such case, this License incorporates\n\
2066    the limitation as if written in the body of this License.\n\
2067    \n\
2068      9. The Free Software Foundation may publish revised and/or new versions\n\
2069    of the General Public License from time to time.  Such new versions will\n\
2070    be similar in spirit to the present version, but may differ in detail to\n\
2071    address new problems or concerns.\n\
2072    \n\
2073    Each version is given a distinguishing version number.  If the Program\n\
2074    specifies a version number of this License which applies to it and \"any\n\
2075    later version\", you have the option of following the terms and conditions\n\
2076    either of that version or of any later version published by the Free\n\
2077    Software Foundation.  If the Program does not specify a version number of\n\
2078    this License, you may choose any version ever published by the Free Software\n\
2079    Foundation.\n\
2080    \n\
2081      10. If you wish to incorporate parts of the Program into other free\n\
2082    programs whose distribution conditions are different, write to the author\n\
2083    to ask for permission.  For software which is copyrighted by the Free\n\
2084    Software Foundation, write to the Free Software Foundation; we sometimes\n\
2085    make exceptions for this.  Our decision will be guided by the two goals\n\
2086    of preserving the free status of all derivatives of our free software and\n\
2087    of promoting the sharing and reuse of software generally.\n");
2088    }
2089    
2090    extern void warranty(environment *env)
2091    {
2092      printf("                          NO WARRANTY\n\
2093    \n\
2094      11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n\
2095    FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\n\
2096    OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n\
2097    PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n\
2098    OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n\
2099    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\n\
2100    TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\n\
2101    PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n\
2102    REPAIR OR CORRECTION.\n\
2103    \n\
2104      12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n\
2105    WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n\
2106    REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n\
2107    INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n\
2108    OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n\
2109    TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n\
2110    YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n\
2111    PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n\
2112    POSSIBILITY OF SUCH DAMAGES.\n");
2113    }
2114    
2115    /* "*" */
2116    extern void sx_2a(environment *env)
2117    {
2118      int a, b;
2119      float fa, fb;
2120    
2121      if(env->head->type==empty || CDR(env->head)->type==empty) {
2122        printerr("Too Few Arguments");
2123        env->err= 1;
2124        return;
2125      }
2126      
2127      if(CAR(env->head)->type==integer
2128         && CAR(CDR(env->head))->type==integer) {
2129        a= CAR(env->head)->content.i;
2130        toss(env); if(env->err) return;
2131        b= CAR(env->head)->content.i;
2132        toss(env); if(env->err) return;
2133        push_int(env, b*a);
2134    
2135        return;
2136      }
2137    
2138      if(CAR(env->head)->type==tfloat
2139         && CAR(CDR(env->head))->type==tfloat) {
2140        fa= CAR(env->head)->content.f;
2141        toss(env); if(env->err) return;
2142        fb= CAR(env->head)->content.f;
2143        toss(env); if(env->err) return;
2144        push_float(env, fb*fa);
2145        
2146        return;
2147      }
2148    
2149      if(CAR(env->head)->type==tfloat
2150         && CAR(CDR(env->head))->type==integer) {
2151        fa= CAR(env->head)->content.f;
2152        toss(env); if(env->err) return;
2153        b= CAR(env->head)->content.i;
2154        toss(env); if(env->err) return;
2155        push_float(env, b*fa);
2156        
2157        return;
2158      }
2159    
2160      if(CAR(env->head)->type==integer
2161         && CAR(CDR(env->head))->type==tfloat) {
2162        a= CAR(env->head)->content.i;
2163        toss(env); if(env->err) return;
2164        fb= CAR(env->head)->content.f;
2165        toss(env); if(env->err) return;
2166        push_float(env, fb*a);
2167    
2168        return;
2169      }
2170    
2171      printerr("Bad Argument Type");
2172      env->err= 2;
2173    }
2174    
2175    /* "/" */
2176    extern void sx_2f(environment *env)
2177    {
2178      int a, b;
2179      float fa, fb;
2180    
2181      if(env->head->type==empty || CDR(env->head)->type==empty) {
2182        printerr("Too Few Arguments");
2183        env->err= 1;
2184        return;
2185      }
2186      
2187      if(CAR(env->head)->type==integer
2188         && CAR(CDR(env->head))->type==integer) {
2189        a= CAR(env->head)->content.i;
2190        toss(env); if(env->err) return;
2191        b= CAR(env->head)->content.i;
2192        toss(env); if(env->err) return;
2193        push_float(env, b/a);
2194    
2195        return;
2196      }
2197    
2198      if(CAR(env->head)->type==tfloat
2199         && CAR(CDR(env->head))->type==tfloat) {
2200        fa= CAR(env->head)->content.f;
2201        toss(env); if(env->err) return;
2202        fb= CAR(env->head)->content.f;
2203        toss(env); if(env->err) return;
2204        push_float(env, fb/fa);
2205        
2206        return;
2207      }
2208    
2209      if(CAR(env->head)->type==tfloat
2210         && CAR(CDR(env->head))->type==integer) {
2211        fa= CAR(env->head)->content.f;
2212        toss(env); if(env->err) return;
2213        b= CAR(env->head)->content.i;
2214        toss(env); if(env->err) return;
2215        push_float(env, b/fa);
2216        
2217        return;
2218      }
2219    
2220      if(CAR(env->head)->type==integer
2221         && CAR(CDR(env->head))->type==tfloat) {
2222        a= CAR(env->head)->content.i;
2223        toss(env); if(env->err) return;
2224        fb= CAR(env->head)->content.f;
2225        toss(env); if(env->err) return;
2226        push_float(env, fb/a);
2227    
2228        return;
2229      }
2230    
2231      printerr("Bad Argument Type");
2232      env->err= 2;
2233    }
2234    
2235    /* "mod" */
2236    extern void mod(environment *env)
2237    {
2238      int a, b;
2239    
2240      if(env->head->type==empty || CDR(env->head)->type==empty) {
2241        printerr("Too Few Arguments");
2242        env->err= 1;
2243        return;
2244      }
2245      
2246      if(CAR(env->head)->type==integer
2247         && CAR(CDR(env->head))->type==integer) {
2248        a= CAR(env->head)->content.i;
2249        toss(env); if(env->err) return;
2250        b= CAR(env->head)->content.i;
2251        toss(env); if(env->err) return;
2252        push_int(env, b%a);
2253    
2254        return;
2255      }
2256    
2257      printerr("Bad Argument Type");
2258      env->err= 2;
2259    }
2260    
2261    /* "div" */
2262    extern void sx_646976(environment *env)
2263    {
2264      int a, b;
2265      
2266      if(env->head->type==empty || CDR(env->head)->type==empty) {
2267        printerr("Too Few Arguments");
2268        env->err= 1;
2269        return;
2270      }
2271    
2272      if(CAR(env->head)->type==integer
2273         && CAR(CDR(env->head))->type==integer) {
2274        a= CAR(env->head)->content.i;
2275        toss(env); if(env->err) return;
2276        b= CAR(env->head)->content.i;
2277        toss(env); if(env->err) return;
2278        push_int(env, (int)b/a);
2279    
2280        return;
2281      }
2282    
2283      printerr("Bad Argument Type");
2284      env->err= 2;
2285    }
2286    
2287    extern void setcar(environment *env)
2288    {
2289      if(env->head->type==empty || CDR(env->head)->type==empty) {
2290        printerr("Too Few Arguments");
2291        env->err= 1;
2292        return;
2293      }
2294    
2295      if(CDR(env->head)->type!=tcons) {
2296        printerr("Bad Argument Type");
2297        env->err= 2;
2298        return;
2299      }
2300    
2301      CAR(CAR(CDR(env->head)))=CAR(env->head);
2302      toss(env);
2303    }
2304    
2305    extern void setcdr(environment *env)
2306    {
2307      if(env->head->type==empty || CDR(env->head)->type==empty) {
2308        printerr("Too Few Arguments");
2309        env->err= 1;
2310        return;
2311      }
2312    
2313      if(CDR(env->head)->type!=tcons) {
2314        printerr("Bad Argument Type");
2315        env->err= 2;
2316        return;
2317      }
2318    
2319      CDR(CAR(CDR(env->head)))=CAR(env->head);
2320      toss(env);
2321    }
2322    
2323    extern void car(environment *env)
2324    {
2325      if(env->head->type==empty) {
2326        printerr("Too Few Arguments");
2327        env->err= 1;
2328        return;
2329      }
2330    
2331      if(CAR(env->head)->type!=tcons) {
2332        printerr("Bad Argument Type");
2333        env->err= 2;
2334        return;
2335      }
2336    
2337      CAR(env->head)=CAR(CAR(env->head));
2338    }
2339    
2340    extern void cdr(environment *env)
2341    {
2342      if(env->head->type==empty) {
2343        printerr("Too Few Arguments");
2344        env->err= 1;
2345        return;
2346      }
2347    
2348      if(CAR(env->head)->type!=tcons) {
2349        printerr("Bad Argument Type");
2350        env->err= 2;
2351        return;
2352      }
2353    
2354      CAR(env->head)=CDR(CAR(env->head));
2355  }  }

Legend:
Removed from v.1.67  
changed lines
  Added in v.1.114

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26