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

Annotation of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56 - (hide annotations)
Fri Feb 8 02:28:41 2002 UTC (22 years, 2 months ago) by teddy
Branch: MAIN
Changes since 1.55: +87 -0 lines
File MIME type: text/plain
(rot, ift, ifte): New functions.

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     #define HASHTBLSIZE 65536
13    
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 teddy 1.28 } environment;
63    
64     /* A type for pointers to external functions */
65     typedef void (*funcp)(environment *); /* funcp is a pointer to a void
66     function (environment *) */
67 masse 1.1
68 teddy 1.28 /* Initialize a newly created environment */
69     void init_env(environment *env)
70 masse 1.1 {
71 masse 1.46 int i;
72 masse 1.1
73 masse 1.46 env->err= 0;
74     env->non_eval_flag= 0;
75 masse 1.1 for(i= 0; i<HASHTBLSIZE; i++)
76 teddy 1.28 env->symbols[i]= NULL;
77 masse 1.1 }
78    
79 teddy 1.48 void printerr(const char* in_string) {
80     fprintf(stderr, "Err: %s\n", in_string);
81     }
82    
83     /* Throw away a value */
84     void free_val(value *val){
85     stackitem *item, *temp;
86    
87     val->refcount--; /* Decrease the reference count */
88     if(val->refcount == 0){
89     switch (val->type){ /* and free the contents if necessary */
90     case string:
91     free(val->content.ptr);
92     break;
93     case list: /* lists needs to be freed recursively */
94     item=val->content.ptr;
95     while(item != NULL) { /* for all stack items */
96     free_val(item->item); /* free the value */
97     temp=item->next; /* save next ptr */
98     free(item); /* free the stackitem */
99     item=temp; /* go to next stackitem */
100     }
101     free(val); /* Free the actual list value */
102     break;
103 teddy 1.54 case integer:
104     case func:
105     case symb:
106 teddy 1.48 break;
107     }
108     }
109     }
110    
111     /* Discard the top element of the stack. */
112     extern void toss(environment *env)
113     {
114     stackitem *temp= env->head;
115    
116     if((env->head)==NULL) {
117     printerr("Too Few Arguments");
118     env->err=1;
119     return;
120     }
121    
122     free_val(env->head->item); /* Free the value */
123     env->head= env->head->next; /* Remove the top stack item */
124     free(temp); /* Free the old top stack item */
125     }
126    
127 teddy 1.27 /* Returns a pointer to a pointer to an element in the hash table. */
128 teddy 1.28 symbol **hash(hashtbl in_hashtbl, const char *in_string)
129 masse 1.1 {
130 masse 1.46 int i= 0;
131     unsigned int out_hash= 0;
132 teddy 1.18 char key= '\0';
133 teddy 1.28 symbol **position;
134 masse 1.1
135 masse 1.16 while(1){ /* Hash in_string */
136 masse 1.1 key= in_string[i++];
137     if(key=='\0')
138     break;
139     out_hash= out_hash*32+key;
140     }
141    
142     out_hash= out_hash%HASHTBLSIZE;
143     position= &(in_hashtbl[out_hash]);
144    
145 masse 1.25 while(1){
146 teddy 1.18 if(*position==NULL) /* If empty */
147 masse 1.1 return position;
148    
149 teddy 1.18 if(strcmp(in_string, (*position)->id)==0) /* If match */
150 masse 1.1 return position;
151    
152 masse 1.16 position= &((*position)->next); /* Try next */
153 masse 1.1 }
154     }
155    
156 masse 1.14 /* Generic push function. */
157 teddy 1.35 void push(stackitem** stack_head, stackitem* in_item)
158 masse 1.1 {
159     in_item->next= *stack_head;
160     *stack_head= in_item;
161     }
162    
163 teddy 1.29 /* Push a value onto the stack */
164     void push_val(stackitem **stack_head, value *val)
165     {
166     stackitem *new_item= malloc(sizeof(stackitem));
167     new_item->item= val;
168     val->refcount++;
169     push(stack_head, new_item);
170     }
171    
172 teddy 1.28 /* Push an integer onto the stack. */
173 teddy 1.35 void push_int(stackitem **stack_head, int in_val)
174 masse 1.1 {
175 teddy 1.28 value *new_value= malloc(sizeof(value));
176     stackitem *new_item= malloc(sizeof(stackitem));
177     new_item->item= new_value;
178    
179     new_value->content.val= in_val;
180     new_value->type= integer;
181     new_value->refcount=1;
182 masse 1.1
183     push(stack_head, new_item);
184     }
185    
186 masse 1.14 /* Copy a string onto the stack. */
187 teddy 1.35 void push_cstring(stackitem **stack_head, const char *in_string)
188 masse 1.1 {
189 teddy 1.28 value *new_value= malloc(sizeof(value));
190     stackitem *new_item= malloc(sizeof(stackitem));
191     new_item->item=new_value;
192    
193     new_value->content.ptr= malloc(strlen(in_string)+1);
194     strcpy(new_value->content.ptr, in_string);
195     new_value->type= string;
196     new_value->refcount=1;
197 masse 1.1
198     push(stack_head, new_item);
199     }
200    
201 teddy 1.48 /* Mangle a symbol name to a valid C identifier name */
202 teddy 1.51 char *mangle_str(const char *old_string){
203 teddy 1.48 char validchars[]
204     ="0123456789abcdef";
205     char *new_string, *current;
206    
207 teddy 1.50 new_string=malloc((strlen(old_string)*2)+4);
208 teddy 1.48 strcpy(new_string, "sx_"); /* Stack eXternal */
209     current=new_string+3;
210     while(old_string[0] != '\0'){
211 teddy 1.53 current[0]=validchars[(unsigned char)(old_string[0])/16];
212     current[1]=validchars[(unsigned char)(old_string[0])%16];
213 teddy 1.48 current+=2;
214     old_string++;
215     }
216     current[0]='\0';
217    
218     return new_string; /* The caller must free() it */
219     }
220    
221     extern void mangle(environment *env){
222     value *new_value;
223     char *new_string;
224    
225     if((env->head)==NULL) {
226     printerr("Too Few Arguments");
227     env->err=1;
228     return;
229     }
230    
231     if(env->head->item->type!=string) {
232     printerr("Bad Argument Type");
233     env->err=2;
234     return;
235     }
236    
237 teddy 1.51 new_string= mangle_str((const char *)(env->head->item->content.ptr));
238 teddy 1.48
239     toss(env);
240     if(env->err) return;
241    
242     new_value= malloc(sizeof(value));
243     new_value->content.ptr= new_string;
244     new_value->type= string;
245     new_value->refcount=1;
246    
247     push_val(&(env->head), new_value);
248     }
249    
250 teddy 1.28 /* Push a symbol onto the stack. */
251 teddy 1.35 void push_sym(environment *env, const char *in_string)
252 masse 1.1 {
253 teddy 1.28 stackitem *new_item; /* The new stack item */
254     /* ...which will contain... */
255     value *new_value; /* A new symbol value */
256     /* ...which might point to... */
257 teddy 1.29 symbol **new_symbol; /* (if needed) A new actual symbol */
258 teddy 1.28 /* ...which, if possible, will be bound to... */
259     value *new_fvalue; /* (if needed) A new function value */
260     /* ...which will point to... */
261     void *funcptr; /* A function pointer */
262    
263     static void *handle= NULL; /* Dynamic linker handle */
264 teddy 1.48 const char *dlerr; /* Dynamic linker error */
265     char *mangled; /* Mangled function name */
266 teddy 1.28
267     /* Create a new stack item containing a new value */
268     new_item= malloc(sizeof(stackitem));
269     new_value= malloc(sizeof(value));
270     new_item->item=new_value;
271    
272     /* The new value is a symbol */
273     new_value->type= symb;
274     new_value->refcount= 1;
275    
276     /* Look up the symbol name in the hash table */
277 teddy 1.29 new_symbol= hash(env->symbols, in_string);
278     new_value->content.ptr= *new_symbol;
279 teddy 1.28
280 teddy 1.30 if(*new_symbol==NULL) { /* If symbol was undefined */
281 teddy 1.28
282     /* Create a new symbol */
283 teddy 1.30 (*new_symbol)= malloc(sizeof(symbol));
284 teddy 1.29 (*new_symbol)->val= NULL; /* undefined value */
285     (*new_symbol)->next= NULL;
286     (*new_symbol)->id= malloc(strlen(in_string)+1);
287     strcpy((*new_symbol)->id, in_string);
288 masse 1.1
289 teddy 1.28 /* Intern the new symbol in the hash table */
290 teddy 1.29 new_value->content.ptr= *new_symbol;
291 masse 1.1
292 teddy 1.28 /* Try to load the symbol name as an external function, to see if
293     we should bind the symbol to a new function pointer value */
294 masse 1.16 if(handle==NULL) /* If no handle */
295 teddy 1.28 handle= dlopen(NULL, RTLD_LAZY);
296 masse 1.6
297 teddy 1.28 funcptr= dlsym(handle, in_string); /* Get function pointer */
298 teddy 1.48 dlerr=dlerror();
299     if(dlerr != NULL) { /* If no function was found */
300 teddy 1.51 mangled=mangle_str(in_string);
301 teddy 1.48 funcptr= dlsym(handle, mangled); /* try mangling it */
302     free(mangled);
303     dlerr=dlerror();
304     }
305     if(dlerr==NULL) { /* If a function was found */
306 teddy 1.28 new_fvalue= malloc(sizeof(value)); /* Create a new value */
307     new_fvalue->type=func; /* The new value is a function pointer */
308     new_fvalue->content.ptr=funcptr; /* Store function pointer */
309 teddy 1.29 (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new
310     function value */
311 teddy 1.28 new_fvalue->refcount= 1;
312     }
313 masse 1.1 }
314 teddy 1.28 push(&(env->head), new_item);
315 masse 1.1 }
316    
317 masse 1.14 /* Print newline. */
318 masse 1.34 extern void nl()
319 masse 1.8 {
320     printf("\n");
321     }
322 masse 1.1
323 teddy 1.37 /* Gets the type of a value */
324     extern void type(environment *env){
325     int typenum;
326    
327     if((env->head)==NULL) {
328     printerr("Too Few Arguments");
329     env->err=1;
330     return;
331     }
332     typenum=env->head->item->type;
333     toss(env);
334     switch(typenum){
335     case integer:
336     push_sym(env, "integer");
337     break;
338     case string:
339     push_sym(env, "string");
340     break;
341     case symb:
342     push_sym(env, "symbol");
343     break;
344     case func:
345     push_sym(env, "function");
346     break;
347     case list:
348     push_sym(env, "list");
349     break;
350     default:
351     push_sym(env, "unknown");
352     break;
353     }
354     }
355    
356 masse 1.14 /* Prints the top element of the stack. */
357 teddy 1.28 void print_h(stackitem *stack_head)
358 masse 1.8 {
359 teddy 1.28 switch(stack_head->item->type) {
360     case integer:
361     printf("%d", stack_head->item->content.val);
362 teddy 1.2 break;
363     case string:
364 teddy 1.28 printf("\"%s\"", (char*)stack_head->item->content.ptr);
365 teddy 1.2 break;
366 teddy 1.28 case symb:
367 teddy 1.45 printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
368 masse 1.6 break;
369 teddy 1.35 case func:
370     printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
371     break;
372     case list:
373 teddy 1.38 /* A list is just a stack, so make stack_head point to it */
374     stack_head=(stackitem *)(stack_head->item->content.ptr);
375     printf("[ ");
376     while(stack_head != NULL) {
377     print_h(stack_head);
378     printf(" ");
379     stack_head=stack_head->next;
380     }
381 teddy 1.39 printf("]");
382 teddy 1.35 break;
383 masse 1.7 default:
384 teddy 1.38 printf("#<unknown %p>", (stack_head->item->content.ptr));
385 teddy 1.2 break;
386     }
387 masse 1.1 }
388    
389 teddy 1.28 extern void print_(environment *env) {
390 teddy 1.35 if(env->head==NULL) {
391 teddy 1.36 printerr("Too Few Arguments");
392 teddy 1.35 env->err=1;
393     return;
394     }
395 teddy 1.28 print_h(env->head);
396     }
397    
398 masse 1.14 /* Prints the top element of the stack and then discards it. */
399 teddy 1.28 extern void print(environment *env)
400 masse 1.8 {
401 teddy 1.28 print_(env);
402 teddy 1.35 if(env->err) return;
403 teddy 1.28 toss(env);
404 masse 1.8 }
405    
406 masse 1.14 /* Only to be called by function printstack. */
407 teddy 1.28 void print_st(stackitem *stack_head, long counter)
408 masse 1.8 {
409     if(stack_head->next != NULL)
410     print_st(stack_head->next, counter+1);
411     printf("%ld: ", counter);
412 teddy 1.28 print_h(stack_head);
413 masse 1.8 nl();
414     }
415    
416 masse 1.14 /* Prints the stack. */
417 teddy 1.28 extern void printstack(environment *env)
418 masse 1.1 {
419 teddy 1.35 if(env->head == NULL) {
420     return;
421 masse 1.1 }
422 teddy 1.35 print_st(env->head, 1);
423     nl();
424 masse 1.1 }
425    
426 masse 1.26 /* Swap the two top elements on the stack. */
427 teddy 1.28 extern void swap(environment *env)
428 masse 1.26 {
429 teddy 1.28 stackitem *temp= env->head;
430 masse 1.26
431 masse 1.46 if(env->head==NULL || env->head->next==NULL) {
432 teddy 1.36 printerr("Too Few Arguments");
433 teddy 1.35 env->err=1;
434 masse 1.26 return;
435 teddy 1.28 }
436 masse 1.26
437 teddy 1.28 env->head= env->head->next;
438     temp->next= env->head->next;
439     env->head->next= temp;
440 masse 1.26 }
441    
442 teddy 1.56 /* Rotate the first three elements on the stack. */
443     extern void rot(environment *env)
444     {
445     stackitem *temp= env->head;
446    
447     if(env->head==NULL || env->head->next==NULL
448     || env->head->next->next==NULL) {
449     printerr("Too Few Arguments");
450     env->err=1;
451     return;
452     }
453    
454     env->head= env->head->next->next;
455     temp->next->next= env->head->next;
456     env->head->next= temp;
457     }
458    
459 teddy 1.33 /* Recall a value from a symbol, if bound */
460 teddy 1.31 extern void rcl(environment *env)
461     {
462     value *val;
463    
464     if(env->head == NULL) {
465 teddy 1.36 printerr("Too Few Arguments");
466 teddy 1.35 env->err=1;
467 teddy 1.31 return;
468     }
469    
470     if(env->head->item->type!=symb) {
471 teddy 1.36 printerr("Bad Argument Type");
472     env->err=2;
473 teddy 1.31 return;
474     }
475 teddy 1.35
476 teddy 1.31 val=((symbol *)(env->head->item->content.ptr))->val;
477 teddy 1.33 if(val == NULL){
478 teddy 1.36 printerr("Unbound Variable");
479     env->err=3;
480 teddy 1.33 return;
481     }
482 teddy 1.31 toss(env); /* toss the symbol */
483 teddy 1.35 if(env->err) return;
484 teddy 1.31 push_val(&(env->head), val); /* Return its bound value */
485     }
486 masse 1.26
487 masse 1.46 void stack_read(environment*, char*);
488    
489 teddy 1.29 /* If the top element is a symbol, determine if it's bound to a
490     function value, and if it is, toss the symbol and execute the
491     function. */
492 teddy 1.28 extern void eval(environment *env)
493 masse 1.1 {
494     funcp in_func;
495 masse 1.44 value* temp_val;
496     stackitem* iterator;
497 masse 1.47 char* temp_string;
498 masse 1.44
499 teddy 1.29 if(env->head==NULL) {
500 teddy 1.36 printerr("Too Few Arguments");
501 teddy 1.35 env->err=1;
502 masse 1.1 return;
503 masse 1.17 }
504 masse 1.1
505 masse 1.46 switch(env->head->item->type) {
506     /* if it's a symbol */
507     case symb:
508 teddy 1.35 rcl(env); /* get its contents */
509     if(env->err) return;
510     if(env->head->item->type!=symb){ /* don't recurse symbols */
511     eval(env); /* evaluate the value */
512 teddy 1.29 return;
513     }
514 masse 1.46 break;
515 masse 1.22
516 masse 1.46 /* If it's a lone function value, run it */
517     case func:
518 teddy 1.29 in_func= (funcp)(env->head->item->content.ptr);
519 teddy 1.28 toss(env);
520 teddy 1.35 if(env->err) return;
521 teddy 1.28 (*in_func)(env);
522 masse 1.46 break;
523 masse 1.44
524 masse 1.46 /* If it's a list */
525     case list:
526 masse 1.44 temp_val= env->head->item;
527     env->head->item->refcount++;
528     toss(env);
529     if(env->err) return;
530     iterator= (stackitem*)temp_val->content.ptr;
531     while(iterator!=NULL && iterator->item!=NULL) {
532     push_val(&(env->head), iterator->item);
533     if(env->head->item->type==symb
534     && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
535     toss(env);
536     if(env->err) return;
537     eval(env);
538 masse 1.46 if(env->err) return;
539 masse 1.44 }
540     iterator= iterator->next;
541     }
542     free_val(temp_val);
543 masse 1.46 break;
544    
545     /* If it's a string */
546     case string:
547     temp_val= env->head->item;
548     env->head->item->refcount++;
549     toss(env);
550     if(env->err) return;
551 masse 1.47 temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
552 masse 1.49 strcpy(temp_string, "[ ");
553 masse 1.47 strcat(temp_string, (char*)temp_val->content.ptr);
554     strcat(temp_string, " ]");
555     stack_read(env, temp_string);
556 masse 1.46 eval(env);
557     if(env->err) return;
558     free_val(temp_val);
559 masse 1.47 free(temp_string);
560 masse 1.46 break;
561    
562 teddy 1.54 case integer:
563     break;
564 masse 1.26 }
565 masse 1.1 }
566    
567 masse 1.44 /* Reverse (flip) a list */
568 teddy 1.40 extern void rev(environment *env){
569     stackitem *old_head, *new_head, *item;
570    
571     if((env->head)==NULL) {
572     printerr("Too Few Arguments");
573     env->err=1;
574     return;
575     }
576    
577     if(env->head->item->type!=list) {
578     printerr("Bad Argument Type");
579     env->err=2;
580     return;
581     }
582    
583     old_head=(stackitem *)(env->head->item->content.ptr);
584     new_head=NULL;
585     while(old_head != NULL){
586     item=old_head;
587     old_head=old_head->next;
588     item->next=new_head;
589     new_head=item;
590     }
591     env->head->item->content.ptr=new_head;
592     }
593    
594 masse 1.19 /* Make a list. */
595 teddy 1.28 extern void pack(environment *env)
596 masse 1.19 {
597     void* delimiter;
598 teddy 1.28 stackitem *iterator, *temp;
599     value *pack;
600 masse 1.19
601 teddy 1.28 delimiter= env->head->item->content.ptr; /* Get delimiter */
602     toss(env);
603 masse 1.19
604 teddy 1.28 iterator= env->head;
605 masse 1.19
606 teddy 1.28 if(iterator==NULL || iterator->item->content.ptr==delimiter) {
607 masse 1.24 temp= NULL;
608 teddy 1.28 toss(env);
609 masse 1.24 } else {
610     /* Search for first delimiter */
611 teddy 1.28 while(iterator->next!=NULL
612     && iterator->next->item->content.ptr!=delimiter)
613 masse 1.24 iterator= iterator->next;
614    
615     /* Extract list */
616 teddy 1.28 temp= env->head;
617     env->head= iterator->next;
618 masse 1.24 iterator->next= NULL;
619    
620 teddy 1.28 if(env->head!=NULL)
621     toss(env);
622 masse 1.24 }
623 masse 1.19
624     /* Push list */
625 teddy 1.28 pack= malloc(sizeof(value));
626 masse 1.19 pack->type= list;
627     pack->content.ptr= temp;
628 teddy 1.28 pack->refcount= 1;
629    
630     temp= malloc(sizeof(stackitem));
631     temp->item= pack;
632 masse 1.19
633 teddy 1.28 push(&(env->head), temp);
634 teddy 1.40 rev(env);
635 masse 1.19 }
636    
637 masse 1.14 /* Parse input. */
638 teddy 1.37 void stack_read(environment *env, char *in_line)
639 masse 1.1 {
640     char *temp, *rest;
641     int itemp;
642     size_t inlength= strlen(in_line)+1;
643     int convert= 0;
644    
645     temp= malloc(inlength);
646     rest= malloc(inlength);
647    
648 masse 1.15 do {
649 masse 1.43 /* If comment */
650     if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {
651     free(temp); free(rest);
652     return;
653     }
654    
655 masse 1.16 /* If string */
656 masse 1.15 if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
657 teddy 1.28 push_cstring(&(env->head), temp);
658 masse 1.15 break;
659     }
660 teddy 1.28 /* If integer */
661 masse 1.15 if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
662 teddy 1.29 push_int(&(env->head), itemp);
663 masse 1.15 break;
664     }
665 masse 1.16 /* Escape ';' with '\' */
666 masse 1.15 if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
667 teddy 1.28 temp[1]= '\0';
668     push_sym(env, temp);
669 masse 1.15 break;
670     }
671 masse 1.16 /* If symbol */
672 masse 1.34 if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
673 teddy 1.28 push_sym(env, temp);
674 masse 1.15 break;
675     }
676 masse 1.19 /* If single char */
677     if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
678     if(*temp==';') {
679 masse 1.46 if(!env->non_eval_flag) {
680 teddy 1.28 eval(env); /* Evaluate top element */
681 masse 1.20 break;
682     }
683    
684 teddy 1.28 push_sym(env, ";");
685 masse 1.19 break;
686     }
687    
688     if(*temp==']') {
689 teddy 1.28 push_sym(env, "[");
690     pack(env);
691 masse 1.46 if(env->non_eval_flag)
692     env->non_eval_flag--;
693 masse 1.20 break;
694     }
695    
696     if(*temp=='[') {
697 teddy 1.28 push_sym(env, "[");
698 masse 1.46 env->non_eval_flag++;
699 masse 1.19 break;
700     }
701 masse 1.15 }
702     } while(0);
703    
704 masse 1.1 free(temp);
705    
706     if(convert<2) {
707     free(rest);
708 teddy 1.37 return;
709 masse 1.1 }
710    
711 teddy 1.28 stack_read(env, rest);
712 masse 1.1
713     free(rest);
714 masse 1.7 }
715 masse 1.1
716 masse 1.16 /* Relocate elements of the list on the stack. */
717 teddy 1.28 extern void expand(environment *env)
718 masse 1.1 {
719 masse 1.8 stackitem *temp, *new_head;
720    
721 masse 1.16 /* Is top element a list? */
722 teddy 1.36 if(env->head==NULL) {
723     printerr("Too Few Arguments");
724 teddy 1.35 env->err=1;
725 masse 1.8 return;
726 masse 1.17 }
727 teddy 1.36 if(env->head->item->type!=list) {
728     printerr("Bad Argument Type");
729     env->err=2;
730     return;
731     }
732 masse 1.43
733     rev(env);
734    
735     if(env->err)
736     return;
737 masse 1.8
738 masse 1.16 /* The first list element is the new stack head */
739 teddy 1.28 new_head= temp= env->head->item->content.ptr;
740 masse 1.8
741 teddy 1.28 env->head->item->refcount++;
742     toss(env);
743 masse 1.24
744 teddy 1.28 /* Find the end of the list */
745 masse 1.8 while(temp->next!=NULL)
746     temp= temp->next;
747    
748 teddy 1.28 /* Connect the tail of the list with the old stack head */
749     temp->next= env->head;
750     env->head= new_head; /* ...and voila! */
751    
752 teddy 1.5 }
753 masse 1.11
754 masse 1.14 /* Compares two elements by reference. */
755 teddy 1.28 extern void eq(environment *env)
756 masse 1.11 {
757     void *left, *right;
758     int result;
759    
760 teddy 1.28 if((env->head)==NULL || env->head->next==NULL) {
761 teddy 1.36 printerr("Too Few Arguments");
762 teddy 1.35 env->err=1;
763 masse 1.11 return;
764 masse 1.17 }
765 masse 1.11
766 teddy 1.28 left= env->head->item->content.ptr;
767     swap(env);
768     right= env->head->item->content.ptr;
769 masse 1.11 result= (left==right);
770    
771 teddy 1.28 toss(env); toss(env);
772 teddy 1.29 push_int(&(env->head), result);
773 masse 1.11 }
774    
775 masse 1.14 /* Negates the top element on the stack. */
776 teddy 1.28 extern void not(environment *env)
777 masse 1.11 {
778 teddy 1.28 int val;
779 masse 1.11
780 teddy 1.36 if((env->head)==NULL) {
781     printerr("Too Few Arguments");
782 teddy 1.35 env->err=1;
783 masse 1.11 return;
784 masse 1.17 }
785 masse 1.11
786 teddy 1.36 if(env->head->item->type!=integer) {
787     printerr("Bad Argument Type");
788     env->err=2;
789     return;
790     }
791    
792 teddy 1.28 val= env->head->item->content.val;
793     toss(env);
794 teddy 1.29 push_int(&(env->head), !val);
795 masse 1.11 }
796    
797 masse 1.14 /* Compares the two top elements on the stack and return 0 if they're the
798     same. */
799 teddy 1.28 extern void neq(environment *env)
800 masse 1.11 {
801 teddy 1.28 eq(env);
802     not(env);
803 masse 1.11 }
804 masse 1.12
805 masse 1.14 /* Give a symbol some content. */
806 teddy 1.28 extern void def(environment *env)
807 masse 1.12 {
808 teddy 1.28 symbol *sym;
809 masse 1.12
810 teddy 1.28 /* Needs two values on the stack, the top one must be a symbol */
811 teddy 1.36 if(env->head==NULL || env->head->next==NULL) {
812     printerr("Too Few Arguments");
813 teddy 1.35 env->err=1;
814 masse 1.12 return;
815 masse 1.17 }
816 masse 1.12
817 teddy 1.36 if(env->head->item->type!=symb) {
818     printerr("Bad Argument Type");
819     env->err=2;
820     return;
821     }
822    
823 teddy 1.28 /* long names are a pain */
824     sym=env->head->item->content.ptr;
825    
826     /* if the symbol was bound to something else, throw it away */
827     if(sym->val != NULL)
828     free_val(sym->val);
829    
830     /* Bind the symbol to the value */
831     sym->val= env->head->next->item;
832     sym->val->refcount++; /* Increase the reference counter */
833 masse 1.12
834 teddy 1.28 toss(env); toss(env);
835 masse 1.12 }
836 masse 1.10
837 masse 1.14 /* Quit stack. */
838 teddy 1.28 extern void quit(environment *env)
839 teddy 1.5 {
840     exit(EXIT_SUCCESS);
841 masse 1.24 }
842    
843     /* Clear stack */
844 teddy 1.28 extern void clear(environment *env)
845 masse 1.24 {
846 teddy 1.28 while(env->head!=NULL)
847     toss(env);
848 masse 1.1 }
849    
850 teddy 1.33 /* List all defined words */
851 masse 1.32 extern void words(environment *env)
852     {
853     symbol *temp;
854     int i;
855    
856     for(i= 0; i<HASHTBLSIZE; i++) {
857     temp= env->symbols[i];
858     while(temp!=NULL) {
859     printf("%s\n", temp->id);
860     temp= temp->next;
861     }
862     }
863     }
864 masse 1.34
865     /* Forgets a symbol (remove it from the hash table) */
866     extern void forget(environment *env)
867     {
868     char* sym_id;
869     stackitem *stack_head= env->head;
870     symbol **hash_entry, *temp;
871    
872 teddy 1.36 if(stack_head==NULL) {
873     printerr("Too Few Arguments");
874     env->err=1;
875     return;
876     }
877    
878     if(stack_head->item->type!=symb) {
879     printerr("Bad Argument Type");
880     env->err=2;
881 masse 1.34 return;
882     }
883    
884     sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
885     toss(env);
886    
887     hash_entry= hash(env->symbols, sym_id);
888     temp= *hash_entry;
889     *hash_entry= (*hash_entry)->next;
890    
891     if(temp->val!=NULL) {
892     free_val(temp->val);
893     }
894     free(temp->id);
895     free(temp);
896 teddy 1.36 }
897    
898     /* Returns the current error number to the stack */
899     extern void errn(environment *env){
900     push_int(&(env->head), env->err);
901     }
902 masse 1.32
903 masse 1.1 int main()
904     {
905 teddy 1.28 environment myenv;
906 masse 1.1 char in_string[100];
907    
908 teddy 1.28 init_env(&myenv);
909 masse 1.1
910     printf("okidok\n ");
911    
912     while(fgets(in_string, 100, stdin) != NULL) {
913 teddy 1.28 stack_read(&myenv, in_string);
914 teddy 1.35 if(myenv.err) {
915     printf("(error %d) ", myenv.err);
916     myenv.err=0;
917     }
918 masse 1.1 printf("okidok\n ");
919     }
920 teddy 1.41 quit(&myenv);
921 teddy 1.42 return EXIT_FAILURE;
922 teddy 1.48 }
923    
924     /* + */
925     extern void sx_2b(environment *env) {
926     int a, b;
927 masse 1.49 size_t len;
928     char* new_string;
929     value *a_val, *b_val;
930 teddy 1.48
931     if((env->head)==NULL || env->head->next==NULL) {
932     printerr("Too Few Arguments");
933     env->err=1;
934 masse 1.49 return;
935     }
936    
937     if(env->head->item->type==string
938     && env->head->next->item->type==string) {
939     a_val= env->head->item;
940     b_val= env->head->next->item;
941     a_val->refcount++;
942     b_val->refcount++;
943     toss(env); if(env->err) return;
944     toss(env); if(env->err) return;
945     len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
946     new_string= malloc(len);
947     strcpy(new_string, b_val->content.ptr);
948     strcat(new_string, a_val->content.ptr);
949     free_val(a_val); free_val(b_val);
950     push_cstring(&(env->head), new_string);
951     free(new_string);
952 teddy 1.48 return;
953     }
954    
955     if(env->head->item->type!=integer
956     || env->head->next->item->type!=integer) {
957     printerr("Bad Argument Type");
958     env->err=2;
959     return;
960     }
961     a=env->head->item->content.val;
962     toss(env);
963     if(env->err) return;
964     b=env->head->item->content.val;
965     toss(env);
966     if(env->err) return;
967     push_int(&(env->head), a+b);
968 masse 1.1 }
969 teddy 1.55
970     /* Return copy of a value */
971     value *copy_val(value *old_value){
972     stackitem *old_item, *new_item, *prev_item;
973    
974     value *new_value=malloc(sizeof(value));
975    
976     new_value->type=old_value->type;
977     new_value->refcount=0; /* This is increased if/when this
978     value is referenced somewhere, like
979     in a stack item or a variable */
980     switch(old_value->type){
981     case integer:
982     new_value->content.val=old_value->content.val;
983     break;
984     case string:
985     (char *)(new_value->content.ptr)
986     = strdup((char *)(old_value->content.ptr));
987     break;
988     case func:
989     case symb:
990     new_value->content.ptr=old_value->content.ptr;
991     break;
992     case list:
993     new_value->content.ptr=NULL;
994    
995     prev_item=NULL;
996     old_item=(stackitem *)(old_value->content.ptr);
997    
998     while(old_item != NULL) { /* While list is not empty */
999     new_item= malloc(sizeof(stackitem));
1000     new_item->item=copy_val(old_item->item); /* recurse */
1001     new_item->next=NULL;
1002     if(prev_item != NULL) /* If this wasn't the first item */
1003     prev_item->next=new_item; /* point the previous item to the
1004     new item */
1005     else
1006     new_value->content.ptr=new_item;
1007     old_item=old_item->next;
1008     prev_item=new_item;
1009     }
1010     break;
1011     }
1012     return new_value;
1013     }
1014    
1015     /* duplicates an item on the stack */
1016     extern void dup(environment *env) {
1017     if((env->head)==NULL) {
1018     printerr("Too Few Arguments");
1019     env->err=1;
1020     return;
1021     }
1022     push_val(&(env->head), copy_val(env->head->item));
1023     }
1024 teddy 1.56
1025     /* If-Then */
1026     extern void ift(environment *env) {
1027    
1028     int truth;
1029    
1030     if((env->head)==NULL || env->head->next==NULL) {
1031     printerr("Too Few Arguments");
1032     env->err=1;
1033     return;
1034     }
1035    
1036     if(env->head->next->item->type != integer) {
1037     printerr("Bad Argument Type");
1038     env->err=2;
1039     return;
1040     }
1041    
1042     swap(env);
1043     if(env->err) return;
1044    
1045     truth=env->head->item->content.val;
1046    
1047     toss(env);
1048     if(env->err) return;
1049    
1050     if(truth)
1051     eval(env);
1052     else
1053     toss(env);
1054     }
1055    
1056    
1057     /* If-Then-Else */
1058     extern void ifte(environment *env) {
1059    
1060     int truth;
1061    
1062     if((env->head)==NULL || env->head->next==NULL
1063     || env->head->next->next==NULL) {
1064     printerr("Too Few Arguments");
1065     env->err=1;
1066     return;
1067     }
1068    
1069     if(env->head->next->next->item->type != integer) {
1070     printerr("Bad Argument Type");
1071     env->err=2;
1072     return;
1073     }
1074    
1075     rot(env);
1076     if(env->err) return;
1077    
1078     truth=env->head->item->content.val;
1079    
1080     toss(env);
1081     if(env->err) return;
1082    
1083     if(!truth)
1084     swap(env);
1085     if(env->err) return;
1086    
1087     toss(env);
1088     if(env->err) return;
1089    
1090     eval(env);
1091     }
1092    

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26