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

Annotation of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.80 - (hide annotations)
Thu Feb 14 12:20:09 2002 UTC (22 years, 2 months ago) by teddy
Branch: MAIN
Changes since 1.79: +37 -10 lines
File MIME type: text/plain
(print_h): New argument "noquote"; used by "print_" and "print_st",
	   which are changed.
(princ, princ_): New; print strings unquoted.
(printstack): Print message instead of nothing when stack is empty.
(eval): Bug fix: Move label above error check to fix "[;];".
(quit): Bug fix: Correctly forget all symbols.
(main): Print a newline before the stack, and a prompt after it.
(read): Print a prompt of our own if we are reading a list.

1 teddy 1.52 /* printf, sscanf, fgets, fprintf */
2 masse 1.1 #include <stdio.h>
3 teddy 1.52 /* exit, EXIT_SUCCESS, malloc, free */
4 masse 1.1 #include <stdlib.h>
5     /* NULL */
6     #include <stddef.h>
7 teddy 1.3 /* dlopen, dlsym, dlerror */
8 masse 1.1 #include <dlfcn.h>
9 teddy 1.52 /* strcmp, strcpy, strlen, strcat, strdup */
10 masse 1.47 #include <string.h>
11 masse 1.1
12 masse 1.72 #define HASHTBLSIZE 2048
13 masse 1.1
14 teddy 1.28 /* First, define some types. */
15    
16     /* A value of some type */
17     typedef struct {
18 masse 1.16 enum {
19 teddy 1.28 integer,
20 teddy 1.18 string,
21 masse 1.16 func, /* Function pointer */
22 teddy 1.28 symb,
23 masse 1.16 list
24 teddy 1.18 } type; /* Type of stack element */
25    
26 masse 1.1 union {
27 teddy 1.28 void *ptr; /* Pointer to the content */
28 masse 1.16 int val; /* ...or an integer */
29     } content; /* Stores a pointer or an integer */
30 masse 1.1
31 teddy 1.28 int refcount; /* Reference counter */
32    
33     } value;
34    
35     /* A symbol with a name and possible value */
36     /* (These do not need reference counters, they are kept unique by
37     hashing.) */
38     typedef struct symbol_struct {
39     char *id; /* Symbol name */
40     value *val; /* The value (if any) bound to it */
41     struct symbol_struct *next; /* In case of hashing conflicts, a */
42     } symbol; /* symbol is a kind of stack item. */
43    
44     /* A type for a hash table for symbols */
45     typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */
46    
47     /* An item (value) on a stack */
48     typedef struct stackitem_struct
49     {
50     value *item; /* The value on the stack */
51 teddy 1.56 /* (This is never NULL) */
52 teddy 1.28 struct stackitem_struct *next; /* Next item */
53 masse 1.1 } stackitem;
54    
55 teddy 1.28 /* An environment; gives access to the stack and a hash table of
56     defined symbols */
57     typedef struct {
58     stackitem *head; /* Head of the stack */
59     hashtbl symbols; /* Hash table of all variable bindings */
60 teddy 1.33 int err; /* Error flag */
61 masse 1.46 int non_eval_flag;
62 masse 1.70 char *in_string; /* Input pending to be read */
63 teddy 1.78 char *free_string; /* Free this string when all input is
64     read from in_string */
65 teddy 1.28 } environment;
66    
67     /* A type for pointers to external functions */
68     typedef void (*funcp)(environment *); /* funcp is a pointer to a void
69     function (environment *) */
70 masse 1.1
71 teddy 1.28 /* Initialize a newly created environment */
72     void init_env(environment *env)
73 masse 1.1 {
74 masse 1.46 int i;
75 masse 1.1
76 masse 1.70 env->in_string= NULL;
77 masse 1.46 env->err= 0;
78     env->non_eval_flag= 0;
79 masse 1.1 for(i= 0; i<HASHTBLSIZE; i++)
80 teddy 1.28 env->symbols[i]= NULL;
81 masse 1.1 }
82    
83 teddy 1.48 void printerr(const char* in_string) {
84     fprintf(stderr, "Err: %s\n", in_string);
85     }
86    
87     /* Throw away a value */
88     void free_val(value *val){
89     stackitem *item, *temp;
90    
91     val->refcount--; /* Decrease the reference count */
92     if(val->refcount == 0){
93     switch (val->type){ /* and free the contents if necessary */
94     case string:
95     free(val->content.ptr);
96     break;
97     case list: /* lists needs to be freed recursively */
98     item=val->content.ptr;
99     while(item != NULL) { /* for all stack items */
100     free_val(item->item); /* free the value */
101     temp=item->next; /* save next ptr */
102     free(item); /* free the stackitem */
103     item=temp; /* go to next stackitem */
104     }
105     free(val); /* Free the actual list value */
106     break;
107 teddy 1.54 case integer:
108     case func:
109     case symb:
110 teddy 1.48 break;
111     }
112     }
113     }
114    
115     /* Discard the top element of the stack. */
116     extern void toss(environment *env)
117     {
118     stackitem *temp= env->head;
119    
120     if((env->head)==NULL) {
121     printerr("Too Few Arguments");
122     env->err=1;
123     return;
124     }
125    
126     free_val(env->head->item); /* Free the value */
127     env->head= env->head->next; /* Remove the top stack item */
128     free(temp); /* Free the old top stack item */
129     }
130    
131 teddy 1.27 /* Returns a pointer to a pointer to an element in the hash table. */
132 teddy 1.28 symbol **hash(hashtbl in_hashtbl, const char *in_string)
133 masse 1.1 {
134 masse 1.46 int i= 0;
135     unsigned int out_hash= 0;
136 teddy 1.18 char key= '\0';
137 teddy 1.28 symbol **position;
138 masse 1.1
139 masse 1.16 while(1){ /* Hash in_string */
140 masse 1.1 key= in_string[i++];
141     if(key=='\0')
142     break;
143     out_hash= out_hash*32+key;
144     }
145    
146     out_hash= out_hash%HASHTBLSIZE;
147     position= &(in_hashtbl[out_hash]);
148    
149 masse 1.25 while(1){
150 teddy 1.18 if(*position==NULL) /* If empty */
151 masse 1.1 return position;
152    
153 teddy 1.18 if(strcmp(in_string, (*position)->id)==0) /* If match */
154 masse 1.1 return position;
155    
156 masse 1.16 position= &((*position)->next); /* Try next */
157 masse 1.1 }
158     }
159    
160 teddy 1.29 /* Push a value onto the stack */
161 masse 1.72 void push_val(environment *env, value *val)
162 teddy 1.29 {
163     stackitem *new_item= malloc(sizeof(stackitem));
164     new_item->item= val;
165     val->refcount++;
166 masse 1.75 new_item->next= env->head;
167     env->head= new_item;
168 teddy 1.29 }
169    
170 teddy 1.28 /* Push an integer onto the stack. */
171 masse 1.72 void push_int(environment *env, int in_val)
172 masse 1.1 {
173 teddy 1.28 value *new_value= malloc(sizeof(value));
174    
175     new_value->content.val= in_val;
176     new_value->type= integer;
177     new_value->refcount=1;
178 masse 1.1
179 masse 1.75 push_val(env, new_value);
180 masse 1.1 }
181    
182 masse 1.14 /* Copy a string onto the stack. */
183 masse 1.72 void push_cstring(environment *env, const char *in_string)
184 masse 1.1 {
185 teddy 1.28 value *new_value= malloc(sizeof(value));
186    
187     new_value->content.ptr= malloc(strlen(in_string)+1);
188     strcpy(new_value->content.ptr, in_string);
189     new_value->type= string;
190     new_value->refcount=1;
191 masse 1.1
192 masse 1.75 push_val(env, new_value);
193 masse 1.1 }
194    
195 teddy 1.48 /* Mangle a symbol name to a valid C identifier name */
196 teddy 1.51 char *mangle_str(const char *old_string){
197 teddy 1.48 char validchars[]
198     ="0123456789abcdef";
199     char *new_string, *current;
200    
201 teddy 1.50 new_string=malloc((strlen(old_string)*2)+4);
202 teddy 1.48 strcpy(new_string, "sx_"); /* Stack eXternal */
203     current=new_string+3;
204     while(old_string[0] != '\0'){
205 teddy 1.53 current[0]=validchars[(unsigned char)(old_string[0])/16];
206     current[1]=validchars[(unsigned char)(old_string[0])%16];
207 teddy 1.48 current+=2;
208     old_string++;
209     }
210     current[0]='\0';
211    
212     return new_string; /* The caller must free() it */
213     }
214    
215     extern void mangle(environment *env){
216     value *new_value;
217     char *new_string;
218    
219     if((env->head)==NULL) {
220     printerr("Too Few Arguments");
221     env->err=1;
222     return;
223     }
224    
225     if(env->head->item->type!=string) {
226     printerr("Bad Argument Type");
227     env->err=2;
228     return;
229     }
230    
231 teddy 1.51 new_string= mangle_str((const char *)(env->head->item->content.ptr));
232 teddy 1.48
233     toss(env);
234     if(env->err) return;
235    
236     new_value= malloc(sizeof(value));
237     new_value->content.ptr= new_string;
238     new_value->type= string;
239     new_value->refcount=1;
240    
241 masse 1.72 push_val(env, new_value);
242 teddy 1.48 }
243    
244 teddy 1.28 /* Push a symbol onto the stack. */
245 teddy 1.35 void push_sym(environment *env, const char *in_string)
246 masse 1.1 {
247 teddy 1.28 value *new_value; /* A new symbol value */
248     /* ...which might point to... */
249 teddy 1.29 symbol **new_symbol; /* (if needed) A new actual symbol */
250 teddy 1.28 /* ...which, if possible, will be bound to... */
251     value *new_fvalue; /* (if needed) A new function value */
252     /* ...which will point to... */
253     void *funcptr; /* A function pointer */
254    
255     static void *handle= NULL; /* Dynamic linker handle */
256 teddy 1.48 const char *dlerr; /* Dynamic linker error */
257     char *mangled; /* Mangled function name */
258 teddy 1.28
259     new_value= malloc(sizeof(value));
260    
261     /* The new value is a symbol */
262     new_value->type= symb;
263     new_value->refcount= 1;
264    
265     /* Look up the symbol name in the hash table */
266 teddy 1.29 new_symbol= hash(env->symbols, in_string);
267     new_value->content.ptr= *new_symbol;
268 teddy 1.28
269 teddy 1.30 if(*new_symbol==NULL) { /* If symbol was undefined */
270 teddy 1.28
271     /* Create a new symbol */
272 teddy 1.30 (*new_symbol)= malloc(sizeof(symbol));
273 teddy 1.29 (*new_symbol)->val= NULL; /* undefined value */
274     (*new_symbol)->next= NULL;
275     (*new_symbol)->id= malloc(strlen(in_string)+1);
276     strcpy((*new_symbol)->id, in_string);
277 masse 1.1
278 teddy 1.28 /* Intern the new symbol in the hash table */
279 teddy 1.29 new_value->content.ptr= *new_symbol;
280 masse 1.1
281 teddy 1.28 /* Try to load the symbol name as an external function, to see if
282     we should bind the symbol to a new function pointer value */
283 masse 1.16 if(handle==NULL) /* If no handle */
284 teddy 1.28 handle= dlopen(NULL, RTLD_LAZY);
285 masse 1.6
286 teddy 1.28 funcptr= dlsym(handle, in_string); /* Get function pointer */
287 teddy 1.48 dlerr=dlerror();
288     if(dlerr != NULL) { /* If no function was found */
289 teddy 1.51 mangled=mangle_str(in_string);
290 teddy 1.48 funcptr= dlsym(handle, mangled); /* try mangling it */
291     free(mangled);
292     dlerr=dlerror();
293     }
294     if(dlerr==NULL) { /* If a function was found */
295 teddy 1.28 new_fvalue= malloc(sizeof(value)); /* Create a new value */
296     new_fvalue->type=func; /* The new value is a function pointer */
297     new_fvalue->content.ptr=funcptr; /* Store function pointer */
298 teddy 1.29 (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
299     function value */
300 teddy 1.28 new_fvalue->refcount= 1;
301     }
302 masse 1.1 }
303 masse 1.75 push_val(env, new_value);
304 masse 1.1 }
305    
306 masse 1.14 /* Print newline. */
307 masse 1.34 extern void nl()
308 masse 1.8 {
309     printf("\n");
310     }
311 masse 1.1
312 teddy 1.37 /* Gets the type of a value */
313     extern void type(environment *env){
314     int typenum;
315    
316     if((env->head)==NULL) {
317     printerr("Too Few Arguments");
318     env->err=1;
319     return;
320     }
321     typenum=env->head->item->type;
322     toss(env);
323     switch(typenum){
324     case integer:
325     push_sym(env, "integer");
326     break;
327     case string:
328     push_sym(env, "string");
329     break;
330     case symb:
331     push_sym(env, "symbol");
332     break;
333     case func:
334     push_sym(env, "function");
335     break;
336     case list:
337     push_sym(env, "list");
338     break;
339     }
340     }
341    
342 masse 1.14 /* Prints the top element of the stack. */
343 teddy 1.80 void print_h(stackitem *stack_head, int noquote)
344 masse 1.8 {
345 teddy 1.28 switch(stack_head->item->type) {
346     case integer:
347     printf("%d", stack_head->item->content.val);
348 teddy 1.2 break;
349     case string:
350 teddy 1.80 if(noquote)
351     printf("%s", (char*)stack_head->item->content.ptr);
352     else
353     printf("\"%s\"", (char*)stack_head->item->content.ptr);
354 teddy 1.2 break;
355 teddy 1.28 case symb:
356 teddy 1.45 printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
357 masse 1.6 break;
358 teddy 1.35 case func:
359     printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
360     break;
361     case list:
362 teddy 1.38 /* A list is just a stack, so make stack_head point to it */
363     stack_head=(stackitem *)(stack_head->item->content.ptr);
364     printf("[ ");
365     while(stack_head != NULL) {
366 teddy 1.80 print_h(stack_head, noquote);
367 teddy 1.38 printf(" ");
368     stack_head=stack_head->next;
369     }
370 teddy 1.39 printf("]");
371 teddy 1.35 break;
372 teddy 1.2 }
373 masse 1.1 }
374    
375 teddy 1.28 extern void print_(environment *env) {
376 teddy 1.35 if(env->head==NULL) {
377 teddy 1.36 printerr("Too Few Arguments");
378 teddy 1.35 env->err=1;
379     return;
380     }
381 teddy 1.80 print_h(env->head, 0);
382     nl();
383 teddy 1.28 }
384    
385 masse 1.14 /* Prints the top element of the stack and then discards it. */
386 teddy 1.28 extern void print(environment *env)
387 masse 1.8 {
388 teddy 1.28 print_(env);
389 teddy 1.35 if(env->err) return;
390 teddy 1.28 toss(env);
391 masse 1.8 }
392    
393 teddy 1.80 extern void princ_(environment *env) {
394     if(env->head==NULL) {
395     printerr("Too Few Arguments");
396     env->err=1;
397     return;
398     }
399     print_h(env->head, 1);
400     }
401    
402     /* Prints the top element of the stack and then discards it. */
403     extern void princ(environment *env)
404     {
405     princ_(env);
406     if(env->err) return;
407     toss(env);
408     }
409    
410 masse 1.14 /* Only to be called by function printstack. */
411 teddy 1.28 void print_st(stackitem *stack_head, long counter)
412 masse 1.8 {
413     if(stack_head->next != NULL)
414     print_st(stack_head->next, counter+1);
415     printf("%ld: ", counter);
416 teddy 1.80 print_h(stack_head, 0);
417 masse 1.8 nl();
418     }
419    
420 masse 1.14 /* Prints the stack. */
421 teddy 1.28 extern void printstack(environment *env)
422 masse 1.1 {
423 teddy 1.35 if(env->head == NULL) {
424 teddy 1.80 printf("Stack Empty\n");
425 teddy 1.35 return;
426 masse 1.1 }
427 teddy 1.35 print_st(env->head, 1);
428 masse 1.1 }
429    
430 masse 1.26 /* Swap the two top elements on the stack. */
431 teddy 1.28 extern void swap(environment *env)
432 masse 1.26 {
433 teddy 1.28 stackitem *temp= env->head;
434 masse 1.26
435 masse 1.46 if(env->head==NULL || env->head->next==NULL) {
436 teddy 1.36 printerr("Too Few Arguments");
437 teddy 1.35 env->err=1;
438 masse 1.26 return;
439 teddy 1.28 }
440 masse 1.26
441 teddy 1.28 env->head= env->head->next;
442     temp->next= env->head->next;
443     env->head->next= temp;
444 masse 1.26 }
445    
446 teddy 1.56 /* Rotate the first three elements on the stack. */
447     extern void rot(environment *env)
448     {
449     stackitem *temp= env->head;
450    
451     if(env->head==NULL || env->head->next==NULL
452     || env->head->next->next==NULL) {
453     printerr("Too Few Arguments");
454     env->err=1;
455     return;
456     }
457    
458     env->head= env->head->next->next;
459     temp->next->next= env->head->next;
460     env->head->next= temp;
461     }
462    
463 teddy 1.33 /* Recall a value from a symbol, if bound */
464 teddy 1.31 extern void rcl(environment *env)
465     {
466     value *val;
467    
468     if(env->head == NULL) {
469 teddy 1.36 printerr("Too Few Arguments");
470 teddy 1.35 env->err=1;
471 teddy 1.31 return;
472     }
473    
474     if(env->head->item->type!=symb) {
475 teddy 1.36 printerr("Bad Argument Type");
476     env->err=2;
477 teddy 1.31 return;
478     }
479 teddy 1.35
480 teddy 1.31 val=((symbol *)(env->head->item->content.ptr))->val;
481 teddy 1.33 if(val == NULL){
482 teddy 1.36 printerr("Unbound Variable");
483     env->err=3;
484 teddy 1.33 return;
485     }
486 teddy 1.31 toss(env); /* toss the symbol */
487 teddy 1.35 if(env->err) return;
488 masse 1.72 push_val(env, val); /* Return its bound value */
489 teddy 1.31 }
490 masse 1.26
491 teddy 1.29 /* If the top element is a symbol, determine if it's bound to a
492     function value, and if it is, toss the symbol and execute the
493     function. */
494 teddy 1.28 extern void eval(environment *env)
495 masse 1.1 {
496     funcp in_func;
497 masse 1.44 value* temp_val;
498     stackitem* iterator;
499    
500 teddy 1.80 eval_start:
501    
502 teddy 1.29 if(env->head==NULL) {
503 teddy 1.36 printerr("Too Few Arguments");
504 teddy 1.35 env->err=1;
505 masse 1.1 return;
506 masse 1.17 }
507 masse 1.1
508 masse 1.46 switch(env->head->item->type) {
509     /* if it's a symbol */
510     case symb:
511 teddy 1.35 rcl(env); /* get its contents */
512     if(env->err) return;
513     if(env->head->item->type!=symb){ /* don't recurse symbols */
514 teddy 1.64 goto eval_start;
515 teddy 1.29 }
516 teddy 1.59 return;
517 masse 1.22
518 masse 1.46 /* If it's a lone function value, run it */
519     case func:
520 teddy 1.29 in_func= (funcp)(env->head->item->content.ptr);
521 teddy 1.28 toss(env);
522 teddy 1.35 if(env->err) return;
523 teddy 1.59 return (*in_func)(env);
524 masse 1.44
525 masse 1.46 /* If it's a list */
526     case list:
527 masse 1.44 temp_val= env->head->item;
528     env->head->item->refcount++;
529     toss(env);
530     if(env->err) return;
531     iterator= (stackitem*)temp_val->content.ptr;
532 teddy 1.59 while(iterator!=NULL) {
533 masse 1.72 push_val(env, iterator->item);
534 masse 1.44 if(env->head->item->type==symb
535     && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
536     toss(env);
537     if(env->err) return;
538 teddy 1.59 if(iterator->next == NULL){
539     free_val(temp_val);
540 teddy 1.64 goto eval_start;
541 teddy 1.59 }
542 masse 1.44 eval(env);
543 masse 1.46 if(env->err) return;
544 masse 1.44 }
545     iterator= iterator->next;
546     }
547     free_val(temp_val);
548 teddy 1.59 return;
549 masse 1.46
550 masse 1.71 default:
551 teddy 1.59 return;
552 masse 1.26 }
553 masse 1.1 }
554    
555 masse 1.44 /* Reverse (flip) a list */
556 teddy 1.40 extern void rev(environment *env){
557 teddy 1.78 stackitem *old_head, *new_head, *item;
558 teddy 1.40
559     if((env->head)==NULL) {
560     printerr("Too Few Arguments");
561     env->err=1;
562     return;
563     }
564    
565     if(env->head->item->type!=list) {
566     printerr("Bad Argument Type");
567     env->err=2;
568     return;
569     }
570    
571 teddy 1.78 old_head=(stackitem *)(env->head->item->content.ptr);
572     new_head=NULL;
573     while(old_head != NULL){
574     item=old_head;
575     old_head=old_head->next;
576     item->next=new_head;
577     new_head=item;
578 teddy 1.40 }
579 teddy 1.78 env->head->item->content.ptr=new_head;
580 teddy 1.40 }
581    
582 masse 1.19 /* Make a list. */
583 teddy 1.28 extern void pack(environment *env)
584 masse 1.19 {
585 teddy 1.28 stackitem *iterator, *temp;
586     value *pack;
587 masse 1.19
588 teddy 1.28 iterator= env->head;
589 masse 1.19
590 masse 1.73 if(iterator==NULL
591     || (iterator->item->type==symb
592     && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) {
593 masse 1.24 temp= NULL;
594 teddy 1.28 toss(env);
595 masse 1.24 } else {
596     /* Search for first delimiter */
597 teddy 1.28 while(iterator->next!=NULL
598 masse 1.73 && (iterator->next->item->type!=symb
599     || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='['))
600 masse 1.24 iterator= iterator->next;
601    
602     /* Extract list */
603 teddy 1.28 temp= env->head;
604     env->head= iterator->next;
605 masse 1.24 iterator->next= NULL;
606    
607 teddy 1.28 if(env->head!=NULL)
608     toss(env);
609 masse 1.24 }
610 masse 1.19
611     /* Push list */
612 teddy 1.28 pack= malloc(sizeof(value));
613 masse 1.19 pack->type= list;
614     pack->content.ptr= temp;
615 teddy 1.28 pack->refcount= 1;
616    
617 masse 1.74 push_val(env, pack);
618 teddy 1.40 rev(env);
619 masse 1.19 }
620    
621 masse 1.16 /* Relocate elements of the list on the stack. */
622 teddy 1.28 extern void expand(environment *env)
623 masse 1.1 {
624 masse 1.8 stackitem *temp, *new_head;
625    
626 masse 1.16 /* Is top element a list? */
627 teddy 1.36 if(env->head==NULL) {
628     printerr("Too Few Arguments");
629 teddy 1.35 env->err=1;
630 masse 1.8 return;
631 masse 1.17 }
632 teddy 1.36 if(env->head->item->type!=list) {
633     printerr("Bad Argument Type");
634     env->err=2;
635     return;
636     }
637 masse 1.43
638     rev(env);
639    
640     if(env->err)
641     return;
642 masse 1.8
643 masse 1.16 /* The first list element is the new stack head */
644 teddy 1.28 new_head= temp= env->head->item->content.ptr;
645 masse 1.8
646 teddy 1.28 env->head->item->refcount++;
647     toss(env);
648 masse 1.24
649 teddy 1.28 /* Find the end of the list */
650 masse 1.8 while(temp->next!=NULL)
651     temp= temp->next;
652    
653 teddy 1.28 /* Connect the tail of the list with the old stack head */
654     temp->next= env->head;
655     env->head= new_head; /* ...and voila! */
656    
657 teddy 1.5 }
658 masse 1.11
659 masse 1.14 /* Compares two elements by reference. */
660 teddy 1.28 extern void eq(environment *env)
661 masse 1.11 {
662     void *left, *right;
663     int result;
664    
665 teddy 1.28 if((env->head)==NULL || env->head->next==NULL) {
666 teddy 1.36 printerr("Too Few Arguments");
667 teddy 1.35 env->err=1;
668 masse 1.11 return;
669 masse 1.17 }
670 masse 1.11
671 teddy 1.28 left= env->head->item->content.ptr;
672     swap(env);
673     right= env->head->item->content.ptr;
674 masse 1.11 result= (left==right);
675    
676 teddy 1.28 toss(env); toss(env);
677 masse 1.72 push_int(env, result);
678 masse 1.11 }
679    
680 masse 1.14 /* Negates the top element on the stack. */
681 teddy 1.28 extern void not(environment *env)
682 masse 1.11 {
683 teddy 1.28 int val;
684 masse 1.11
685 teddy 1.36 if((env->head)==NULL) {
686     printerr("Too Few Arguments");
687 teddy 1.35 env->err=1;
688 masse 1.11 return;
689 masse 1.17 }
690 masse 1.11
691 teddy 1.36 if(env->head->item->type!=integer) {
692     printerr("Bad Argument Type");
693     env->err=2;
694     return;
695     }
696    
697 teddy 1.28 val= env->head->item->content.val;
698     toss(env);
699 masse 1.72 push_int(env, !val);
700 masse 1.11 }
701    
702 masse 1.14 /* Compares the two top elements on the stack and return 0 if they're the
703     same. */
704 teddy 1.28 extern void neq(environment *env)
705 masse 1.11 {
706 teddy 1.28 eq(env);
707     not(env);
708 masse 1.11 }
709 masse 1.12
710 masse 1.14 /* Give a symbol some content. */
711 teddy 1.28 extern void def(environment *env)
712 masse 1.12 {
713 teddy 1.28 symbol *sym;
714 masse 1.12
715 teddy 1.28 /* Needs two values on the stack, the top one must be a symbol */
716 teddy 1.36 if(env->head==NULL || env->head->next==NULL) {
717     printerr("Too Few Arguments");
718 teddy 1.35 env->err=1;
719 masse 1.12 return;
720 masse 1.17 }
721 masse 1.12
722 teddy 1.36 if(env->head->item->type!=symb) {
723     printerr("Bad Argument Type");
724     env->err=2;
725     return;
726     }
727    
728 teddy 1.28 /* long names are a pain */
729     sym=env->head->item->content.ptr;
730    
731     /* if the symbol was bound to something else, throw it away */
732     if(sym->val != NULL)
733     free_val(sym->val);
734    
735     /* Bind the symbol to the value */
736     sym->val= env->head->next->item;
737     sym->val->refcount++; /* Increase the reference counter */
738 masse 1.12
739 teddy 1.28 toss(env); toss(env);
740 masse 1.12 }
741 masse 1.10
742 teddy 1.77 extern void clear(environment *);
743     void forget_sym(symbol **);
744    
745 masse 1.14 /* Quit stack. */
746 teddy 1.28 extern void quit(environment *env)
747 teddy 1.5 {
748 teddy 1.77 long i;
749    
750     clear(env);
751     if (env->err) return;
752     for(i= 0; i<HASHTBLSIZE; i++) {
753 masse 1.79 while(env->symbols[i]!= NULL) {
754 teddy 1.77 forget_sym(&(env->symbols[i]));
755     }
756 teddy 1.80 env->symbols[i]= NULL;
757 teddy 1.77 }
758 teddy 1.5 exit(EXIT_SUCCESS);
759 masse 1.24 }
760    
761     /* Clear stack */
762 teddy 1.28 extern void clear(environment *env)
763 masse 1.24 {
764 teddy 1.28 while(env->head!=NULL)
765     toss(env);
766 masse 1.1 }
767    
768 teddy 1.33 /* List all defined words */
769 masse 1.32 extern void words(environment *env)
770     {
771     symbol *temp;
772     int i;
773    
774     for(i= 0; i<HASHTBLSIZE; i++) {
775     temp= env->symbols[i];
776     while(temp!=NULL) {
777     printf("%s\n", temp->id);
778     temp= temp->next;
779     }
780     }
781     }
782 masse 1.34
783 teddy 1.77 /* Internal forget function */
784     void forget_sym(symbol **hash_entry) {
785     symbol *temp;
786    
787     temp= *hash_entry;
788     *hash_entry= (*hash_entry)->next;
789    
790     if(temp->val!=NULL) {
791     free_val(temp->val);
792     }
793     free(temp->id);
794     free(temp);
795     }
796    
797 masse 1.34 /* Forgets a symbol (remove it from the hash table) */
798     extern void forget(environment *env)
799     {
800     char* sym_id;
801     stackitem *stack_head= env->head;
802    
803 teddy 1.36 if(stack_head==NULL) {
804     printerr("Too Few Arguments");
805     env->err=1;
806     return;
807     }
808    
809     if(stack_head->item->type!=symb) {
810     printerr("Bad Argument Type");
811     env->err=2;
812 masse 1.34 return;
813     }
814    
815     sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
816     toss(env);
817    
818 teddy 1.77 return forget_sym(hash(env->symbols, sym_id));
819 teddy 1.36 }
820    
821     /* Returns the current error number to the stack */
822     extern void errn(environment *env){
823 masse 1.72 push_int(env, env->err);
824 teddy 1.36 }
825 masse 1.32
826 masse 1.69 extern void read(environment*);
827    
828 masse 1.1 int main()
829     {
830 teddy 1.28 environment myenv;
831 masse 1.1
832 teddy 1.28 init_env(&myenv);
833 masse 1.1
834 masse 1.69 while(1) {
835 teddy 1.80 if(myenv.in_string==NULL) {
836     nl();
837 masse 1.71 printstack(&myenv);
838 teddy 1.80 printf("> ");
839     }
840 masse 1.69 read(&myenv);
841 teddy 1.35 if(myenv.err) {
842     printf("(error %d) ", myenv.err);
843     myenv.err=0;
844 masse 1.71 } else if(myenv.head!=NULL
845     && myenv.head->item->type==symb
846 masse 1.69 && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {
847     toss(&myenv); /* No error check in main */
848     eval(&myenv);
849 teddy 1.35 }
850 masse 1.1 }
851 teddy 1.41 quit(&myenv);
852 teddy 1.42 return EXIT_FAILURE;
853 teddy 1.48 }
854    
855     /* + */
856     extern void sx_2b(environment *env) {
857     int a, b;
858 masse 1.49 size_t len;
859     char* new_string;
860     value *a_val, *b_val;
861 teddy 1.48
862     if((env->head)==NULL || env->head->next==NULL) {
863     printerr("Too Few Arguments");
864     env->err=1;
865 masse 1.49 return;
866     }
867    
868     if(env->head->item->type==string
869     && env->head->next->item->type==string) {
870     a_val= env->head->item;
871     b_val= env->head->next->item;
872     a_val->refcount++;
873     b_val->refcount++;
874     toss(env); if(env->err) return;
875     toss(env); if(env->err) return;
876     len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
877     new_string= malloc(len);
878     strcpy(new_string, b_val->content.ptr);
879     strcat(new_string, a_val->content.ptr);
880     free_val(a_val); free_val(b_val);
881 masse 1.72 push_cstring(env, new_string);
882 masse 1.49 free(new_string);
883 teddy 1.48 return;
884     }
885    
886     if(env->head->item->type!=integer
887     || env->head->next->item->type!=integer) {
888     printerr("Bad Argument Type");
889     env->err=2;
890     return;
891     }
892     a=env->head->item->content.val;
893     toss(env);
894     if(env->err) return;
895 teddy 1.62 if(env->head->item->refcount == 1)
896     env->head->item->content.val += a;
897     else {
898     b=env->head->item->content.val;
899     toss(env);
900     if(env->err) return;
901 masse 1.72 push_int(env, a+b);
902 teddy 1.62 }
903 masse 1.1 }
904 teddy 1.55
905 teddy 1.60 /* - */
906     extern void sx_2d(environment *env) {
907 teddy 1.62 int a, b;
908 teddy 1.60
909     if((env->head)==NULL || env->head->next==NULL) {
910     printerr("Too Few Arguments");
911     env->err=1;
912     return;
913     }
914    
915     if(env->head->item->type!=integer
916     || env->head->next->item->type!=integer) {
917     printerr("Bad Argument Type");
918     env->err=2;
919     return;
920     }
921     a=env->head->item->content.val;
922     toss(env);
923     if(env->err) return;
924 teddy 1.62 if(env->head->item->refcount == 1)
925     env->head->item->content.val -= a;
926     else {
927     b=env->head->item->content.val;
928     toss(env);
929     if(env->err) return;
930 masse 1.72 push_int(env, b-a);
931 teddy 1.62 }
932 teddy 1.60 }
933    
934 teddy 1.61 /* > */
935     extern void sx_3e(environment *env) {
936 teddy 1.62 int a, b;
937 teddy 1.61
938     if((env->head)==NULL || env->head->next==NULL) {
939     printerr("Too Few Arguments");
940     env->err=1;
941     return;
942     }
943    
944     if(env->head->item->type!=integer
945     || env->head->next->item->type!=integer) {
946     printerr("Bad Argument Type");
947     env->err=2;
948     return;
949     }
950     a=env->head->item->content.val;
951     toss(env);
952     if(env->err) return;
953 teddy 1.62 if(env->head->item->refcount == 1)
954     env->head->item->content.val = (env->head->item->content.val > a);
955     else {
956     b=env->head->item->content.val;
957     toss(env);
958     if(env->err) return;
959 masse 1.72 push_int(env, b>a);
960 teddy 1.62 }
961 teddy 1.61 }
962    
963 teddy 1.55 /* Return copy of a value */
964     value *copy_val(value *old_value){
965     stackitem *old_item, *new_item, *prev_item;
966    
967     value *new_value=malloc(sizeof(value));
968    
969     new_value->type=old_value->type;
970     new_value->refcount=0; /* This is increased if/when this
971     value is referenced somewhere, like
972     in a stack item or a variable */
973     switch(old_value->type){
974     case integer:
975     new_value->content.val=old_value->content.val;
976     break;
977     case string:
978     (char *)(new_value->content.ptr)
979     = strdup((char *)(old_value->content.ptr));
980     break;
981     case func:
982     case symb:
983     new_value->content.ptr=old_value->content.ptr;
984     break;
985     case list:
986     new_value->content.ptr=NULL;
987    
988     prev_item=NULL;
989     old_item=(stackitem *)(old_value->content.ptr);
990    
991     while(old_item != NULL) { /* While list is not empty */
992     new_item= malloc(sizeof(stackitem));
993     new_item->item=copy_val(old_item->item); /* recurse */
994     new_item->next=NULL;
995     if(prev_item != NULL) /* If this wasn't the first item */
996     prev_item->next=new_item; /* point the previous item to the
997     new item */
998     else
999     new_value->content.ptr=new_item;
1000     old_item=old_item->next;
1001     prev_item=new_item;
1002     }
1003     break;
1004     }
1005     return new_value;
1006     }
1007    
1008     /* duplicates an item on the stack */
1009     extern void dup(environment *env) {
1010     if((env->head)==NULL) {
1011     printerr("Too Few Arguments");
1012     env->err=1;
1013     return;
1014     }
1015 masse 1.72 push_val(env, copy_val(env->head->item));
1016 teddy 1.55 }
1017 teddy 1.56
1018 teddy 1.59 /* "if", If-Then */
1019 masse 1.57 extern void sx_6966(environment *env) {
1020 teddy 1.56
1021     int truth;
1022    
1023     if((env->head)==NULL || env->head->next==NULL) {
1024     printerr("Too Few Arguments");
1025     env->err=1;
1026     return;
1027     }
1028    
1029     if(env->head->next->item->type != integer) {
1030     printerr("Bad Argument Type");
1031     env->err=2;
1032     return;
1033     }
1034    
1035     swap(env);
1036     if(env->err) return;
1037    
1038     truth=env->head->item->content.val;
1039    
1040     toss(env);
1041     if(env->err) return;
1042    
1043     if(truth)
1044     eval(env);
1045     else
1046     toss(env);
1047     }
1048    
1049     /* If-Then-Else */
1050 masse 1.57 extern void ifelse(environment *env) {
1051 teddy 1.56
1052     int truth;
1053    
1054     if((env->head)==NULL || env->head->next==NULL
1055     || env->head->next->next==NULL) {
1056     printerr("Too Few Arguments");
1057     env->err=1;
1058     return;
1059     }
1060    
1061     if(env->head->next->next->item->type != integer) {
1062     printerr("Bad Argument Type");
1063     env->err=2;
1064     return;
1065     }
1066    
1067     rot(env);
1068     if(env->err) return;
1069    
1070     truth=env->head->item->content.val;
1071    
1072     toss(env);
1073     if(env->err) return;
1074    
1075     if(!truth)
1076     swap(env);
1077     if(env->err) return;
1078    
1079     toss(env);
1080     if(env->err) return;
1081    
1082     eval(env);
1083 masse 1.58 }
1084    
1085     /* while */
1086     extern void sx_7768696c65(environment *env) {
1087    
1088     int truth;
1089 masse 1.63 value *loop, *test;
1090 masse 1.58
1091     if((env->head)==NULL || env->head->next==NULL) {
1092     printerr("Too Few Arguments");
1093     env->err=1;
1094     return;
1095     }
1096    
1097 masse 1.63 loop= env->head->item;
1098     loop->refcount++;
1099     toss(env); if(env->err) return;
1100    
1101     test= env->head->item;
1102     test->refcount++;
1103     toss(env); if(env->err) return;
1104    
1105 masse 1.58 do {
1106 masse 1.72 push_val(env, test);
1107 masse 1.63 eval(env);
1108 masse 1.58
1109     if(env->head->item->type != integer) {
1110     printerr("Bad Argument Type");
1111     env->err=2;
1112     return;
1113     }
1114    
1115     truth= env->head->item->content.val;
1116     toss(env); if(env->err) return;
1117    
1118     if(truth) {
1119 masse 1.72 push_val(env, loop);
1120 masse 1.58 eval(env);
1121     } else {
1122     toss(env);
1123     }
1124    
1125     } while(truth);
1126 masse 1.63
1127     free_val(test);
1128     free_val(loop);
1129 teddy 1.56 }
1130 masse 1.65
1131     /* For-loop */
1132     extern void sx_666f72(environment *env) {
1133    
1134     value *loop, *foo;
1135     stackitem *iterator;
1136    
1137     if((env->head)==NULL || env->head->next==NULL) {
1138     printerr("Too Few Arguments");
1139     env->err=1;
1140     return;
1141     }
1142    
1143     if(env->head->next->item->type != list) {
1144     printerr("Bad Argument Type");
1145     env->err=2;
1146     return;
1147     }
1148    
1149     loop= env->head->item;
1150     loop->refcount++;
1151     toss(env); if(env->err) return;
1152    
1153     foo= env->head->item;
1154     foo->refcount++;
1155     toss(env); if(env->err) return;
1156    
1157     iterator= foo->content.ptr;
1158    
1159     while(iterator!=NULL) {
1160 masse 1.72 push_val(env, iterator->item);
1161     push_val(env, loop);
1162 masse 1.65 eval(env); if(env->err) return;
1163     iterator= iterator->next;
1164     }
1165    
1166     free_val(loop);
1167     free_val(foo);
1168     }
1169 masse 1.66
1170     /* 'to' */
1171     extern void to(environment *env) {
1172     int i, start, ending;
1173 masse 1.74 stackitem *temp_head;
1174     value *temp_val;
1175 masse 1.66
1176     if((env->head)==NULL || env->head->next==NULL) {
1177     printerr("Too Few Arguments");
1178     env->err=1;
1179     return;
1180     }
1181    
1182     if(env->head->item->type!=integer
1183     || env->head->next->item->type!=integer) {
1184     printerr("Bad Argument Type");
1185     env->err=2;
1186     return;
1187     }
1188    
1189     ending= env->head->item->content.val;
1190     toss(env); if(env->err) return;
1191     start= env->head->item->content.val;
1192     toss(env); if(env->err) return;
1193    
1194 masse 1.74 temp_head= env->head;
1195     env->head= NULL;
1196 masse 1.66
1197 masse 1.67 if(ending>=start) {
1198 masse 1.74 for(i= ending; i>=start; i--)
1199 masse 1.72 push_int(env, i);
1200 masse 1.67 } else {
1201 masse 1.74 for(i= ending; i<=start; i++)
1202 masse 1.72 push_int(env, i);
1203 masse 1.67 }
1204 masse 1.66
1205 masse 1.74 temp_val= malloc(sizeof(value));
1206     temp_val->content.ptr= env->head;
1207     temp_val->refcount= 1;
1208     temp_val->type= list;
1209     env->head= temp_head;
1210     push_val(env, temp_val);
1211 masse 1.66 }
1212 masse 1.68
1213     /* Read a string */
1214     extern void readline(environment *env) {
1215     char in_string[101];
1216    
1217     fgets(in_string, 100, stdin);
1218 masse 1.72 push_cstring(env, in_string);
1219 masse 1.68 }
1220    
1221     /* Read a value and place on stack */
1222     extern void read(environment *env) {
1223 teddy 1.78 const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1224     const char strform[]= "\"%[^\"]\"%n";
1225     const char intform[]= "%i%n";
1226     const char blankform[]= "%*[ \t]%n";
1227     const char ebrackform[]= "%*1[]]%n";
1228     const char semicform[]= "%*1[;]%n";
1229     const char bbrackform[]= "%*1[[]%n";
1230 masse 1.68
1231 teddy 1.78 int itemp, readlength= -1;
1232 masse 1.68 static int depth= 0;
1233     char *rest, *match;
1234     size_t inlength;
1235    
1236 masse 1.70 if(env->in_string==NULL) {
1237 teddy 1.80 if(depth > 0) {
1238     printf("]> ");
1239     }
1240 masse 1.68 readline(env); if(env->err) return;
1241    
1242 masse 1.70 env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1243 teddy 1.78 env->free_string= env->in_string; /* Save the original pointer */
1244 masse 1.70 strcpy(env->in_string, env->head->item->content.ptr);
1245 masse 1.68 toss(env); if(env->err) return;
1246     }
1247    
1248 masse 1.70 inlength= strlen(env->in_string)+1;
1249 masse 1.68 match= malloc(inlength);
1250     rest= malloc(inlength);
1251    
1252 teddy 1.78 if(sscanf(env->in_string, blankform, &readlength)!=EOF
1253     && readlength != -1) {
1254 masse 1.71 ;
1255 teddy 1.78 } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF
1256     && readlength != -1) {
1257 masse 1.72 push_int(env, itemp);
1258 teddy 1.78 } else if(sscanf(env->in_string, strform, match, &readlength) != EOF
1259     && readlength != -1) {
1260 masse 1.72 push_cstring(env, match);
1261 teddy 1.78 } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF
1262     && readlength != -1) {
1263 masse 1.68 push_sym(env, match);
1264 teddy 1.78 } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF
1265     && readlength != -1) {
1266 masse 1.68 pack(env); if(env->err) return;
1267 teddy 1.78 if(depth != 0) depth--;
1268     } else if(sscanf(env->in_string, semicform, &readlength) != EOF
1269     && readlength != -1) {
1270 masse 1.68 push_sym(env, ";");
1271 teddy 1.78 } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF
1272     && readlength != -1) {
1273 masse 1.68 push_sym(env, "[");
1274     depth++;
1275     } else {
1276 teddy 1.78 free(env->free_string);
1277     env->in_string = env->free_string = NULL;
1278     free(match);
1279     }
1280     if ( env->in_string != NULL) {
1281     env->in_string += readlength;
1282 masse 1.68 }
1283    
1284 masse 1.71 if(depth)
1285 masse 1.68 return read(env);
1286     }

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26