commit b3147f2eeea78f979417c945bc2f645ad3cdf936
parent 0cc11334c93bfb095fb88fe06d7f808888d9c89f
Author: Tomas Hlavaty <tom@logand.com>
Date: Wed, 14 Mar 2012 00:44:01 +0100
CHARA has identity, simpler and reuses cells
Diffstat:
2 files changed, 15 insertions(+), 19 deletions(-)
diff --git a/unoidl2.c b/unoidl2.c
@@ -24,6 +24,8 @@
#define HSIZE 3000000
+#define NCHARA 256
+
struct any {
enum {CONS, TOKEN, INUM, CHARA} tag;
union {
@@ -36,7 +38,6 @@ struct any {
struct any *sym1;
} t;
int inum;
- char chara;
} u;
};
@@ -64,16 +65,18 @@ Any cons(Any car, Any cdr) {
return x;
}
+static Any chara_from;
static Any root1;
-static Any intern1_k;
+
+static Any chara(int x) {return &chara_from[x];}
Any intern1(char *x) {
Any p = root1;
for(; *x; x++) {
- intern1_k->u.chara = *x;
- Any r = find(intern1_k, cdr(p), equal, caar);
+ Any k = chara(*x);
+ Any r = find(k, cdr(p), equal, caar);
if(NIL == r)
- p->u.c.cdr = cons(r = list1(cons(mkchara(*x), p)), cdr(p));
+ p->u.c.cdr = cons(r = list1(cons(k, p)), cdr(p));
p = r;
}
return p;
@@ -94,20 +97,12 @@ Any mkinum(int n) {
return x;
}
-Any mkchara(char c) {
- Any x = xalloc();
- x->tag = CHARA;
- x->u.chara = c;
- return x;
-}
-
int consp(Any x) {return CONS == x->tag;}
Any car(Any x) {return x->u.c.car;}
Any cdr(Any x) {return x->u.c.cdr;}
Kind kind(Any x) {return x->u.t.kind;}
Any sym1(Any x) {return x->u.t.sym1;}
int inum(Any x) {return x->u.inum;}
-char chara(Any x) {return x->u.chara;}
Any cons3(Any a, Any b, Any c) {return cons(a, cons(b, c));}
Any cons4(Any a, Any b, Any c, Any d) {return cons(a, cons(b, cons(c, d)));}
@@ -154,6 +149,7 @@ Any cdddddr(Any x) {return cdr(cdr(cdr(cdr(cdr(x)))));}
Any caar(Any x) {return car(car(x));}
int equal_(Any x, Any y) {
+ if(x == y) return 1;
if(NIL == x) return NIL == y;
switch(x->tag) {
case CONS:
@@ -164,8 +160,6 @@ int equal_(Any x, Any y) {
return TOKEN == y->tag && kind(x) == kind(y) && sym1(x) == sym1(y);
case INUM:
return INUM == y->tag && inum(x) == inum(y);
- case CHARA:
- return CHARA == y->tag && chara(x) == chara(y);
}
return 0;
}
@@ -209,7 +203,7 @@ Any print(Any x) {
printf("%d", x->u.inum);
break;
case CHARA:
- printf("%c", x->u.chara);
+ printf("%c", x->u.inum);
break;
/* default: error? */
}
@@ -257,8 +251,12 @@ void init() {
NIL->tag = CONS;
NIL->u.c.car = NIL;
NIL->u.c.cdr = NIL;
+ for(int i = 0; i < NCHARA; i++) {
+ Any x = mkinum(i);
+ x->tag = CHARA;
+ if(0 == i) chara_from = x;
+ }
root1 = list1(NIL);
- intern1_k = mkchara(0);
T = xalloc();
T->tag = TOKEN;
T->u.t.kind = 0;
diff --git a/unoidl2.h b/unoidl2.h
@@ -28,14 +28,12 @@ typedef Any (*Fn2)(Any a, Any b);
Any cons(Any car, Any cdr);
Any mk(Kind kind, char *token);
Any mkinum(int n);
-Any mkchara(char c);
int consp(Any x);
Any car(Any x);
Any cdr(Any x);
Kind kind(Any x);
Any sym1(Any x);
int inum(Any x);
-char chara(Any x);
Any cons3(Any a, Any b, Any c);
Any cons4(Any a, Any b, Any c, Any d);
Any cons5(Any a, Any b, Any c, Any d, Any e);