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

Annotation of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.65 - (hide annotations)
Sat Feb 9 00:20:02 2002 UTC (22 years, 2 months ago) by masse
Branch: MAIN
Changes since 1.64: +39 -0 lines
File MIME type: text/plain
(sx_666f72 'for') New function.

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     }
351     }
352    
353 masse 1.14 /* Prints the top element of the stack. */
354 teddy 1.28 void print_h(stackitem *stack_head)
355 masse 1.8 {
356 teddy 1.28 switch(stack_head->item->type) {
357     case integer:
358     printf("%d", stack_head->item->content.val);
359 teddy 1.2 break;
360     case string:
361 masse 1.58 printf("%s", (char*)stack_head->item->content.ptr);
362 teddy 1.2 break;
363 teddy 1.28 case symb:
364 teddy 1.45 printf("%s", ((symbol *)(stack_head->item->content.ptr))->id);
365 masse 1.6 break;
366 teddy 1.35 case func:
367     printf("#<function %p>", (funcp)(stack_head->item->content.ptr));
368     break;
369     case list:
370 teddy 1.38 /* A list is just a stack, so make stack_head point to it */
371     stack_head=(stackitem *)(stack_head->item->content.ptr);
372     printf("[ ");
373     while(stack_head != NULL) {
374     print_h(stack_head);
375     printf(" ");
376     stack_head=stack_head->next;
377     }
378 teddy 1.39 printf("]");
379 teddy 1.35 break;
380 teddy 1.2 }
381 masse 1.1 }
382    
383 teddy 1.28 extern void print_(environment *env) {
384 teddy 1.35 if(env->head==NULL) {
385 teddy 1.36 printerr("Too Few Arguments");
386 teddy 1.35 env->err=1;
387     return;
388     }
389 teddy 1.28 print_h(env->head);
390     }
391    
392 masse 1.14 /* Prints the top element of the stack and then discards it. */
393 teddy 1.28 extern void print(environment *env)
394 masse 1.8 {
395 teddy 1.28 print_(env);
396 teddy 1.35 if(env->err) return;
397 teddy 1.28 toss(env);
398 masse 1.8 }
399    
400 masse 1.14 /* Only to be called by function printstack. */
401 teddy 1.28 void print_st(stackitem *stack_head, long counter)
402 masse 1.8 {
403     if(stack_head->next != NULL)
404     print_st(stack_head->next, counter+1);
405     printf("%ld: ", counter);
406 teddy 1.28 print_h(stack_head);
407 masse 1.8 nl();
408     }
409    
410 masse 1.14 /* Prints the stack. */
411 teddy 1.28 extern void printstack(environment *env)
412 masse 1.1 {
413 teddy 1.35 if(env->head == NULL) {
414     return;
415 masse 1.1 }
416 teddy 1.35 print_st(env->head, 1);
417     nl();
418 masse 1.1 }
419    
420 masse 1.26 /* Swap the two top elements on the stack. */
421 teddy 1.28 extern void swap(environment *env)
422 masse 1.26 {
423 teddy 1.28 stackitem *temp= env->head;
424 masse 1.26
425 masse 1.46 if(env->head==NULL || env->head->next==NULL) {
426 teddy 1.36 printerr("Too Few Arguments");
427 teddy 1.35 env->err=1;
428 masse 1.26 return;
429 teddy 1.28 }
430 masse 1.26
431 teddy 1.28 env->head= env->head->next;
432     temp->next= env->head->next;
433     env->head->next= temp;
434 masse 1.26 }
435    
436 teddy 1.56 /* Rotate the first three elements on the stack. */
437     extern void rot(environment *env)
438     {
439     stackitem *temp= env->head;
440    
441     if(env->head==NULL || env->head->next==NULL
442     || env->head->next->next==NULL) {
443     printerr("Too Few Arguments");
444     env->err=1;
445     return;
446     }
447    
448     env->head= env->head->next->next;
449     temp->next->next= env->head->next;
450     env->head->next= temp;
451     }
452    
453 teddy 1.33 /* Recall a value from a symbol, if bound */
454 teddy 1.31 extern void rcl(environment *env)
455     {
456     value *val;
457    
458     if(env->head == NULL) {
459 teddy 1.36 printerr("Too Few Arguments");
460 teddy 1.35 env->err=1;
461 teddy 1.31 return;
462     }
463    
464     if(env->head->item->type!=symb) {
465 teddy 1.36 printerr("Bad Argument Type");
466     env->err=2;
467 teddy 1.31 return;
468     }
469 teddy 1.35
470 teddy 1.31 val=((symbol *)(env->head->item->content.ptr))->val;
471 teddy 1.33 if(val == NULL){
472 teddy 1.36 printerr("Unbound Variable");
473     env->err=3;
474 teddy 1.33 return;
475     }
476 teddy 1.31 toss(env); /* toss the symbol */
477 teddy 1.35 if(env->err) return;
478 teddy 1.31 push_val(&(env->head), val); /* Return its bound value */
479     }
480 masse 1.26
481 masse 1.46 void stack_read(environment*, char*);
482    
483 teddy 1.29 /* If the top element is a symbol, determine if it's bound to a
484     function value, and if it is, toss the symbol and execute the
485     function. */
486 teddy 1.28 extern void eval(environment *env)
487 masse 1.1 {
488     funcp in_func;
489 masse 1.44 value* temp_val;
490     stackitem* iterator;
491 masse 1.47 char* temp_string;
492 masse 1.44
493 teddy 1.29 if(env->head==NULL) {
494 teddy 1.36 printerr("Too Few Arguments");
495 teddy 1.35 env->err=1;
496 masse 1.1 return;
497 masse 1.17 }
498 masse 1.1
499 teddy 1.64 eval_start:
500    
501 masse 1.46 switch(env->head->item->type) {
502     /* if it's a symbol */
503     case symb:
504 teddy 1.35 rcl(env); /* get its contents */
505     if(env->err) return;
506     if(env->head->item->type!=symb){ /* don't recurse symbols */
507 teddy 1.64 goto eval_start;
508 teddy 1.29 }
509 teddy 1.59 return;
510 masse 1.22
511 masse 1.46 /* If it's a lone function value, run it */
512     case func:
513 teddy 1.29 in_func= (funcp)(env->head->item->content.ptr);
514 teddy 1.28 toss(env);
515 teddy 1.35 if(env->err) return;
516 teddy 1.59 return (*in_func)(env);
517 masse 1.44
518 masse 1.46 /* If it's a list */
519     case list:
520 masse 1.44 temp_val= env->head->item;
521     env->head->item->refcount++;
522     toss(env);
523     if(env->err) return;
524     iterator= (stackitem*)temp_val->content.ptr;
525 teddy 1.59 while(iterator!=NULL) {
526 masse 1.44 push_val(&(env->head), iterator->item);
527     if(env->head->item->type==symb
528     && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) {
529     toss(env);
530     if(env->err) return;
531 teddy 1.59 if(iterator->next == NULL){
532     free_val(temp_val);
533 teddy 1.64 goto eval_start;
534 teddy 1.59 }
535 masse 1.44 eval(env);
536 masse 1.46 if(env->err) return;
537 masse 1.44 }
538     iterator= iterator->next;
539     }
540     free_val(temp_val);
541 teddy 1.59 return;
542 masse 1.46
543     /* If it's a string */
544     case string:
545     temp_val= env->head->item;
546     env->head->item->refcount++;
547     toss(env);
548     if(env->err) return;
549 masse 1.47 temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
550 masse 1.49 strcpy(temp_string, "[ ");
551 teddy 1.59 strcpy(temp_string+2, (char*)temp_val->content.ptr);
552     free_val(temp_val);
553 masse 1.47 strcat(temp_string, " ]");
554     stack_read(env, temp_string);
555     free(temp_string);
556 teddy 1.64 goto eval_start;
557 masse 1.46
558 teddy 1.54 case integer:
559 teddy 1.59 return;
560 masse 1.26 }
561 masse 1.1 }
562    
563 masse 1.44 /* Reverse (flip) a list */
564 teddy 1.40 extern void rev(environment *env){
565     stackitem *old_head, *new_head, *item;
566    
567     if((env->head)==NULL) {
568     printerr("Too Few Arguments");
569     env->err=1;
570     return;
571     }
572    
573     if(env->head->item->type!=list) {
574     printerr("Bad Argument Type");
575     env->err=2;
576     return;
577     }
578    
579     old_head=(stackitem *)(env->head->item->content.ptr);
580     new_head=NULL;
581     while(old_head != NULL){
582     item=old_head;
583     old_head=old_head->next;
584     item->next=new_head;
585     new_head=item;
586     }
587     env->head->item->content.ptr=new_head;
588     }
589    
590 masse 1.19 /* Make a list. */
591 teddy 1.28 extern void pack(environment *env)
592 masse 1.19 {
593     void* delimiter;
594 teddy 1.28 stackitem *iterator, *temp;
595     value *pack;
596 masse 1.19
597 teddy 1.28 delimiter= env->head->item->content.ptr; /* Get delimiter */
598     toss(env);
599 masse 1.19
600 teddy 1.28 iterator= env->head;
601 masse 1.19
602 teddy 1.28 if(iterator==NULL || iterator->item->content.ptr==delimiter) {
603 masse 1.24 temp= NULL;
604 teddy 1.28 toss(env);
605 masse 1.24 } else {
606     /* Search for first delimiter */
607 teddy 1.28 while(iterator->next!=NULL
608     && iterator->next->item->content.ptr!=delimiter)
609 masse 1.24 iterator= iterator->next;
610    
611     /* Extract list */
612 teddy 1.28 temp= env->head;
613     env->head= iterator->next;
614 masse 1.24 iterator->next= NULL;
615    
616 teddy 1.28 if(env->head!=NULL)
617     toss(env);
618 masse 1.24 }
619 masse 1.19
620     /* Push list */
621 teddy 1.28 pack= malloc(sizeof(value));
622 masse 1.19 pack->type= list;
623     pack->content.ptr= temp;
624 teddy 1.28 pack->refcount= 1;
625    
626     temp= malloc(sizeof(stackitem));
627     temp->item= pack;
628 masse 1.19
629 teddy 1.28 push(&(env->head), temp);
630 teddy 1.40 rev(env);
631 masse 1.19 }
632    
633 masse 1.14 /* Parse input. */
634 teddy 1.37 void stack_read(environment *env, char *in_line)
635 masse 1.1 {
636     char *temp, *rest;
637     int itemp;
638     size_t inlength= strlen(in_line)+1;
639     int convert= 0;
640    
641     temp= malloc(inlength);
642     rest= malloc(inlength);
643    
644 masse 1.15 do {
645 masse 1.43 /* If comment */
646     if((convert= sscanf(in_line, "#%[^\n\r]", rest))) {
647     free(temp); free(rest);
648     return;
649     }
650    
651 masse 1.16 /* If string */
652 masse 1.15 if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) {
653 teddy 1.28 push_cstring(&(env->head), temp);
654 masse 1.15 break;
655     }
656 teddy 1.28 /* If integer */
657 masse 1.15 if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) {
658 teddy 1.29 push_int(&(env->head), itemp);
659 masse 1.15 break;
660     }
661 masse 1.16 /* Escape ';' with '\' */
662 masse 1.15 if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) {
663 teddy 1.28 temp[1]= '\0';
664     push_sym(env, temp);
665 masse 1.15 break;
666     }
667 masse 1.16 /* If symbol */
668 masse 1.34 if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) {
669 teddy 1.28 push_sym(env, temp);
670 masse 1.15 break;
671     }
672 masse 1.19 /* If single char */
673     if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) {
674     if(*temp==';') {
675 masse 1.46 if(!env->non_eval_flag) {
676 teddy 1.28 eval(env); /* Evaluate top element */
677 masse 1.20 break;
678     }
679    
680 teddy 1.28 push_sym(env, ";");
681 masse 1.19 break;
682     }
683    
684     if(*temp==']') {
685 teddy 1.28 push_sym(env, "[");
686     pack(env);
687 masse 1.46 if(env->non_eval_flag)
688     env->non_eval_flag--;
689 masse 1.20 break;
690     }
691    
692     if(*temp=='[') {
693 teddy 1.28 push_sym(env, "[");
694 masse 1.46 env->non_eval_flag++;
695 masse 1.19 break;
696     }
697 masse 1.15 }
698     } while(0);
699    
700 masse 1.1 free(temp);
701    
702     if(convert<2) {
703     free(rest);
704 teddy 1.37 return;
705 masse 1.1 }
706    
707 teddy 1.28 stack_read(env, rest);
708 masse 1.1
709     free(rest);
710 masse 1.7 }
711 masse 1.1
712 masse 1.16 /* Relocate elements of the list on the stack. */
713 teddy 1.28 extern void expand(environment *env)
714 masse 1.1 {
715 masse 1.8 stackitem *temp, *new_head;
716    
717 masse 1.16 /* Is top element a list? */
718 teddy 1.36 if(env->head==NULL) {
719     printerr("Too Few Arguments");
720 teddy 1.35 env->err=1;
721 masse 1.8 return;
722 masse 1.17 }
723 teddy 1.36 if(env->head->item->type!=list) {
724     printerr("Bad Argument Type");
725     env->err=2;
726     return;
727     }
728 masse 1.43
729     rev(env);
730    
731     if(env->err)
732     return;
733 masse 1.8
734 masse 1.16 /* The first list element is the new stack head */
735 teddy 1.28 new_head= temp= env->head->item->content.ptr;
736 masse 1.8
737 teddy 1.28 env->head->item->refcount++;
738     toss(env);
739 masse 1.24
740 teddy 1.28 /* Find the end of the list */
741 masse 1.8 while(temp->next!=NULL)
742     temp= temp->next;
743    
744 teddy 1.28 /* Connect the tail of the list with the old stack head */
745     temp->next= env->head;
746     env->head= new_head; /* ...and voila! */
747    
748 teddy 1.5 }
749 masse 1.11
750 masse 1.14 /* Compares two elements by reference. */
751 teddy 1.28 extern void eq(environment *env)
752 masse 1.11 {
753     void *left, *right;
754     int result;
755    
756 teddy 1.28 if((env->head)==NULL || env->head->next==NULL) {
757 teddy 1.36 printerr("Too Few Arguments");
758 teddy 1.35 env->err=1;
759 masse 1.11 return;
760 masse 1.17 }
761 masse 1.11
762 teddy 1.28 left= env->head->item->content.ptr;
763     swap(env);
764     right= env->head->item->content.ptr;
765 masse 1.11 result= (left==right);
766    
767 teddy 1.28 toss(env); toss(env);
768 teddy 1.29 push_int(&(env->head), result);
769 masse 1.11 }
770    
771 masse 1.14 /* Negates the top element on the stack. */
772 teddy 1.28 extern void not(environment *env)
773 masse 1.11 {
774 teddy 1.28 int val;
775 masse 1.11
776 teddy 1.36 if((env->head)==NULL) {
777     printerr("Too Few Arguments");
778 teddy 1.35 env->err=1;
779 masse 1.11 return;
780 masse 1.17 }
781 masse 1.11
782 teddy 1.36 if(env->head->item->type!=integer) {
783     printerr("Bad Argument Type");
784     env->err=2;
785     return;
786     }
787    
788 teddy 1.28 val= env->head->item->content.val;
789     toss(env);
790 teddy 1.29 push_int(&(env->head), !val);
791 masse 1.11 }
792    
793 masse 1.14 /* Compares the two top elements on the stack and return 0 if they're the
794     same. */
795 teddy 1.28 extern void neq(environment *env)
796 masse 1.11 {
797 teddy 1.28 eq(env);
798     not(env);
799 masse 1.11 }
800 masse 1.12
801 masse 1.14 /* Give a symbol some content. */
802 teddy 1.28 extern void def(environment *env)
803 masse 1.12 {
804 teddy 1.28 symbol *sym;
805 masse 1.12
806 teddy 1.28 /* Needs two values on the stack, the top one must be a symbol */
807 teddy 1.36 if(env->head==NULL || env->head->next==NULL) {
808     printerr("Too Few Arguments");
809 teddy 1.35 env->err=1;
810 masse 1.12 return;
811 masse 1.17 }
812 masse 1.12
813 teddy 1.36 if(env->head->item->type!=symb) {
814     printerr("Bad Argument Type");
815     env->err=2;
816     return;
817     }
818    
819 teddy 1.28 /* long names are a pain */
820     sym=env->head->item->content.ptr;
821    
822     /* if the symbol was bound to something else, throw it away */
823     if(sym->val != NULL)
824     free_val(sym->val);
825    
826     /* Bind the symbol to the value */
827     sym->val= env->head->next->item;
828     sym->val->refcount++; /* Increase the reference counter */
829 masse 1.12
830 teddy 1.28 toss(env); toss(env);
831 masse 1.12 }
832 masse 1.10
833 masse 1.14 /* Quit stack. */
834 teddy 1.28 extern void quit(environment *env)
835 teddy 1.5 {
836     exit(EXIT_SUCCESS);
837 masse 1.24 }
838    
839     /* Clear stack */
840 teddy 1.28 extern void clear(environment *env)
841 masse 1.24 {
842 teddy 1.28 while(env->head!=NULL)
843     toss(env);
844 masse 1.1 }
845    
846 teddy 1.33 /* List all defined words */
847 masse 1.32 extern void words(environment *env)
848     {
849     symbol *temp;
850     int i;
851    
852     for(i= 0; i<HASHTBLSIZE; i++) {
853     temp= env->symbols[i];
854     while(temp!=NULL) {
855     printf("%s\n", temp->id);
856     temp= temp->next;
857     }
858     }
859     }
860 masse 1.34
861     /* Forgets a symbol (remove it from the hash table) */
862     extern void forget(environment *env)
863     {
864     char* sym_id;
865     stackitem *stack_head= env->head;
866     symbol **hash_entry, *temp;
867    
868 teddy 1.36 if(stack_head==NULL) {
869     printerr("Too Few Arguments");
870     env->err=1;
871     return;
872     }
873    
874     if(stack_head->item->type!=symb) {
875     printerr("Bad Argument Type");
876     env->err=2;
877 masse 1.34 return;
878     }
879    
880     sym_id= ((symbol*)(stack_head->item->content.ptr))->id;
881     toss(env);
882    
883     hash_entry= hash(env->symbols, sym_id);
884     temp= *hash_entry;
885     *hash_entry= (*hash_entry)->next;
886    
887     if(temp->val!=NULL) {
888     free_val(temp->val);
889     }
890     free(temp->id);
891     free(temp);
892 teddy 1.36 }
893    
894     /* Returns the current error number to the stack */
895     extern void errn(environment *env){
896     push_int(&(env->head), env->err);
897     }
898 masse 1.32
899 masse 1.1 int main()
900     {
901 teddy 1.28 environment myenv;
902 masse 1.1 char in_string[100];
903    
904 teddy 1.28 init_env(&myenv);
905 masse 1.1
906     printf("okidok\n ");
907    
908     while(fgets(in_string, 100, stdin) != NULL) {
909 teddy 1.28 stack_read(&myenv, in_string);
910 teddy 1.35 if(myenv.err) {
911     printf("(error %d) ", myenv.err);
912     myenv.err=0;
913     }
914 masse 1.1 printf("okidok\n ");
915     }
916 teddy 1.41 quit(&myenv);
917 teddy 1.42 return EXIT_FAILURE;
918 teddy 1.48 }
919    
920     /* + */
921     extern void sx_2b(environment *env) {
922     int a, b;
923 masse 1.49 size_t len;
924     char* new_string;
925     value *a_val, *b_val;
926 teddy 1.48
927     if((env->head)==NULL || env->head->next==NULL) {
928     printerr("Too Few Arguments");
929     env->err=1;
930 masse 1.49 return;
931     }
932    
933     if(env->head->item->type==string
934     && env->head->next->item->type==string) {
935     a_val= env->head->item;
936     b_val= env->head->next->item;
937     a_val->refcount++;
938     b_val->refcount++;
939     toss(env); if(env->err) return;
940     toss(env); if(env->err) return;
941     len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
942     new_string= malloc(len);
943     strcpy(new_string, b_val->content.ptr);
944     strcat(new_string, a_val->content.ptr);
945     free_val(a_val); free_val(b_val);
946     push_cstring(&(env->head), new_string);
947     free(new_string);
948 teddy 1.48 return;
949     }
950    
951     if(env->head->item->type!=integer
952     || env->head->next->item->type!=integer) {
953     printerr("Bad Argument Type");
954     env->err=2;
955     return;
956     }
957     a=env->head->item->content.val;
958     toss(env);
959     if(env->err) return;
960 teddy 1.62 if(env->head->item->refcount == 1)
961     env->head->item->content.val += a;
962     else {
963     b=env->head->item->content.val;
964     toss(env);
965     if(env->err) return;
966     push_int(&(env->head), a+b);
967     }
968 masse 1.1 }
969 teddy 1.55
970 teddy 1.60 /* - */
971     extern void sx_2d(environment *env) {
972 teddy 1.62 int a, b;
973 teddy 1.60
974     if((env->head)==NULL || env->head->next==NULL) {
975     printerr("Too Few Arguments");
976     env->err=1;
977     return;
978     }
979    
980     if(env->head->item->type!=integer
981     || env->head->next->item->type!=integer) {
982     printerr("Bad Argument Type");
983     env->err=2;
984     return;
985     }
986     a=env->head->item->content.val;
987     toss(env);
988     if(env->err) return;
989 teddy 1.62 if(env->head->item->refcount == 1)
990     env->head->item->content.val -= a;
991     else {
992     b=env->head->item->content.val;
993     toss(env);
994     if(env->err) return;
995     push_int(&(env->head), b-a);
996     }
997 teddy 1.60 }
998    
999 teddy 1.61 /* > */
1000     extern void sx_3e(environment *env) {
1001 teddy 1.62 int a, b;
1002 teddy 1.61
1003     if((env->head)==NULL || env->head->next==NULL) {
1004     printerr("Too Few Arguments");
1005     env->err=1;
1006     return;
1007     }
1008    
1009     if(env->head->item->type!=integer
1010     || env->head->next->item->type!=integer) {
1011     printerr("Bad Argument Type");
1012     env->err=2;
1013     return;
1014     }
1015     a=env->head->item->content.val;
1016     toss(env);
1017     if(env->err) return;
1018 teddy 1.62 if(env->head->item->refcount == 1)
1019     env->head->item->content.val = (env->head->item->content.val > a);
1020     else {
1021     b=env->head->item->content.val;
1022     toss(env);
1023     if(env->err) return;
1024     push_int(&(env->head), b>a);
1025     }
1026 teddy 1.61 }
1027    
1028 teddy 1.55 /* Return copy of a value */
1029     value *copy_val(value *old_value){
1030     stackitem *old_item, *new_item, *prev_item;
1031    
1032     value *new_value=malloc(sizeof(value));
1033    
1034     new_value->type=old_value->type;
1035     new_value->refcount=0; /* This is increased if/when this
1036     value is referenced somewhere, like
1037     in a stack item or a variable */
1038     switch(old_value->type){
1039     case integer:
1040     new_value->content.val=old_value->content.val;
1041     break;
1042     case string:
1043     (char *)(new_value->content.ptr)
1044     = strdup((char *)(old_value->content.ptr));
1045     break;
1046     case func:
1047     case symb:
1048     new_value->content.ptr=old_value->content.ptr;
1049     break;
1050     case list:
1051     new_value->content.ptr=NULL;
1052    
1053     prev_item=NULL;
1054     old_item=(stackitem *)(old_value->content.ptr);
1055    
1056     while(old_item != NULL) { /* While list is not empty */
1057     new_item= malloc(sizeof(stackitem));
1058     new_item->item=copy_val(old_item->item); /* recurse */
1059     new_item->next=NULL;
1060     if(prev_item != NULL) /* If this wasn't the first item */
1061     prev_item->next=new_item; /* point the previous item to the
1062     new item */
1063     else
1064     new_value->content.ptr=new_item;
1065     old_item=old_item->next;
1066     prev_item=new_item;
1067     }
1068     break;
1069     }
1070     return new_value;
1071     }
1072    
1073     /* duplicates an item on the stack */
1074     extern void dup(environment *env) {
1075     if((env->head)==NULL) {
1076     printerr("Too Few Arguments");
1077     env->err=1;
1078     return;
1079     }
1080     push_val(&(env->head), copy_val(env->head->item));
1081     }
1082 teddy 1.56
1083 teddy 1.59 /* "if", If-Then */
1084 masse 1.57 extern void sx_6966(environment *env) {
1085 teddy 1.56
1086     int truth;
1087    
1088     if((env->head)==NULL || env->head->next==NULL) {
1089     printerr("Too Few Arguments");
1090     env->err=1;
1091     return;
1092     }
1093    
1094     if(env->head->next->item->type != integer) {
1095     printerr("Bad Argument Type");
1096     env->err=2;
1097     return;
1098     }
1099    
1100     swap(env);
1101     if(env->err) return;
1102    
1103     truth=env->head->item->content.val;
1104    
1105     toss(env);
1106     if(env->err) return;
1107    
1108     if(truth)
1109     eval(env);
1110     else
1111     toss(env);
1112     }
1113    
1114     /* If-Then-Else */
1115 masse 1.57 extern void ifelse(environment *env) {
1116 teddy 1.56
1117     int truth;
1118    
1119     if((env->head)==NULL || env->head->next==NULL
1120     || env->head->next->next==NULL) {
1121     printerr("Too Few Arguments");
1122     env->err=1;
1123     return;
1124     }
1125    
1126     if(env->head->next->next->item->type != integer) {
1127     printerr("Bad Argument Type");
1128     env->err=2;
1129     return;
1130     }
1131    
1132     rot(env);
1133     if(env->err) return;
1134    
1135     truth=env->head->item->content.val;
1136    
1137     toss(env);
1138     if(env->err) return;
1139    
1140     if(!truth)
1141     swap(env);
1142     if(env->err) return;
1143    
1144     toss(env);
1145     if(env->err) return;
1146    
1147     eval(env);
1148 masse 1.58 }
1149    
1150     /* while */
1151     extern void sx_7768696c65(environment *env) {
1152    
1153     int truth;
1154 masse 1.63 value *loop, *test;
1155 masse 1.58
1156     if((env->head)==NULL || env->head->next==NULL) {
1157     printerr("Too Few Arguments");
1158     env->err=1;
1159     return;
1160     }
1161    
1162 masse 1.63 loop= env->head->item;
1163     loop->refcount++;
1164     toss(env); if(env->err) return;
1165    
1166     test= env->head->item;
1167     test->refcount++;
1168     toss(env); if(env->err) return;
1169    
1170 masse 1.58 do {
1171 masse 1.63 push_val(&(env->head), test);
1172     eval(env);
1173 masse 1.58
1174     if(env->head->item->type != integer) {
1175     printerr("Bad Argument Type");
1176     env->err=2;
1177     return;
1178     }
1179    
1180     truth= env->head->item->content.val;
1181     toss(env); if(env->err) return;
1182    
1183     if(truth) {
1184 masse 1.63 push_val(&(env->head), loop);
1185 masse 1.58 eval(env);
1186     } else {
1187     toss(env);
1188     }
1189    
1190     } while(truth);
1191 masse 1.63
1192     free_val(test);
1193     free_val(loop);
1194 teddy 1.56 }
1195 masse 1.65
1196     /* For-loop */
1197     extern void sx_666f72(environment *env) {
1198    
1199     value *loop, *foo;
1200     stackitem *iterator;
1201    
1202     if((env->head)==NULL || env->head->next==NULL) {
1203     printerr("Too Few Arguments");
1204     env->err=1;
1205     return;
1206     }
1207    
1208     if(env->head->next->item->type != list) {
1209     printerr("Bad Argument Type");
1210     env->err=2;
1211     return;
1212     }
1213    
1214     loop= env->head->item;
1215     loop->refcount++;
1216     toss(env); if(env->err) return;
1217    
1218     foo= env->head->item;
1219     foo->refcount++;
1220     toss(env); if(env->err) return;
1221    
1222     iterator= foo->content.ptr;
1223    
1224     while(iterator!=NULL) {
1225     push_val(&(env->head), iterator->item);
1226     push_val(&(env->head), loop);
1227     eval(env); if(env->err) return;
1228     iterator= iterator->next;
1229     }
1230    
1231     free_val(loop);
1232     free_val(foo);
1233     }

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26