commit 43a17a00bb496641e1f79bc4544af6c635c1017a
parent 0b4ef8a6fd73f8d580e5edffbf035ec67fc96d18
Author: Tomas Hlavaty <tom@logand.com>
Date: Fri, 9 Mar 2012 23:01:24 +0100
intern strings
Diffstat:
6 files changed, 97 insertions(+), 23 deletions(-)
diff --git a/scan.ll b/scan.ll
@@ -24,8 +24,6 @@
#include "parse.c.h"
#include <stdio.h>
-//char *strdup(char *);
-
int yycolumn = 1;
Kind tok(Kind kind, char *token) {
@@ -33,6 +31,7 @@ Kind tok(Kind kind, char *token) {
yylval = mk(kind, token);
return kind;
}
+
%}
%option noyywrap
@@ -147,11 +146,11 @@ published return tok(PUBLISHED, "published");
"..." return tok(ELLIPSIS, "ellipsis");
-{INT} return tok(INT, strdup(yytext));
-{OCT} return tok(OCT, strdup(yytext));
-{HEX} return tok(HEX, strdup(yytext));
-{REAL} return tok(REAL, strdup(yytext));
+{INT} return tok(INT, yytext);
+{OCT} return tok(OCT, yytext);
+{HEX} return tok(HEX, yytext);
+{REAL} return tok(REAL, yytext);
-{IDENTIFIER} return tok(ID, strdup(yytext));
+{IDENTIFIER} return tok(ID, yytext);
%%
diff --git a/unoidl2.c b/unoidl2.c
@@ -22,8 +22,10 @@
#include <stdio.h>
#include <string.h>
+#define HSIZE 3000000
+
struct any {
- enum {CONS, TOKEN, INUM} tag;
+ enum {CONS, TOKEN, INUM, CHARA} tag;
union {
struct {
struct any *car;
@@ -31,46 +33,83 @@ struct any {
} c;
struct {
Kind kind;
- char *token;
+ struct any *sym1;
} t;
int inum;
+ char chara;
} u;
};
-static struct any _NIL = {.tag = CONS, .u = {.c = {.car = &_NIL, .cdr = &_NIL}}};
-static struct any _T = {.tag = TOKEN, .u = {.t = {.kind = 0, .token = "T"}}};
-const Any NIL = &_NIL;
-const Any T = &_T;
+Any heap;
+int ncells = 0;
+
+Any xalloc() {
+ Any x = &heap[ncells];
+ ncells++;
+ if(HSIZE <= ncells) {
+ fprintf(stderr, "Error: heap exhausted\n");
+ exit(-1);
+ }
+ return x;
+}
+
+Any NIL;
+Any T;
Any cons(Any car, Any cdr) {
- Any x = malloc(sizeof(struct any));
+ Any x = xalloc();
x->tag = CONS;
x->u.c.car = car;
x->u.c.cdr = cdr;
return x;
}
+static Any root1 = NULL;
+
+Any intern1(char *x) {
+ if(x) {
+ Any root = root1;
+ while(*x) {
+ Any c = mkchara(*x++); // TODO no consing
+ Any root2 = find(c, cdr(root), equal, caar);
+ if(NIL == root2)
+ root->u.c.cdr = cons(root2 = list1(cons(c, root)), cdr(root));
+ root = root2;
+ }
+ return root;
+ }
+ return NIL;
+}
+
Any mk(Kind kind, char *token) {
- Any x = malloc(sizeof(struct any));
+ Any x = xalloc();
x->tag = TOKEN;
x->u.t.kind = kind;
- x->u.t.token = token;
+ x->u.t.sym1 = intern1(token);
return x;
}
Any mkinum(int n) {
- Any x = malloc(sizeof(struct any));
+ Any x = xalloc();
x->tag = INUM;
x->u.inum = 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;}
-char *token(Any x) {return x->u.t.token;}
+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)));}
@@ -114,6 +153,8 @@ Any cddddr(Any x) {return cdr(cdr(cdr(cdr(x))));}
Any caddddr(Any x) {return car(cdr(cdr(cdr(cdr(x)))));}
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(NIL == x) return NIL == y;
switch(x->tag) {
@@ -122,15 +163,27 @@ int equal_(Any x, Any y) {
if(!equal_(car(x), car(y))) return 0;
return NIL == x && NIL == y;
case TOKEN:
- return TOKEN == y->tag && kind(x) == kind(y) && 0 == strcmp(token(x), token(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;
}
Any equal(Any x, Any y) {return equal_(x, y) ? T : NIL;}
+void print_sym1(Any x) {
+ Any kp = car(x);
+ if(NIL != kp) {
+ Any k = car(kp);
+ Any p = cdr(kp);
+ print_sym1(p);
+ print(k);
+ }
+}
+
Any print(Any x) {
if(NIL == x)
printf("NIL");
@@ -152,11 +205,14 @@ Any print(Any x) {
printf(")");
break;
case TOKEN:
- printf("%s", x->u.t.token);
+ print_sym1(sym1(x));
break;
case INUM:
printf("%d", x->u.inum);
break;
+ case CHARA:
+ printf("%c", x->u.chara);
+ break;
/* default: error? */
}
return x;
@@ -196,3 +252,16 @@ Any mk_relative(Any x) {
return cons(h, x);
//return cons(mk(RELATIVE, "relative"), $1);
}
+
+void init() {
+ heap = calloc(HSIZE, sizeof(struct any));
+ NIL = xalloc();
+ NIL->tag = CONS;
+ NIL->u.c.car = NIL;
+ NIL->u.c.cdr = NIL;
+ root1 = list1(NIL);
+ T = xalloc();
+ T->tag = TOKEN;
+ T->u.t.kind = 0;
+ T->u.t.sym1 = intern1("T");
+}
diff --git a/unoidl2.h b/unoidl2.h
@@ -21,8 +21,6 @@ typedef struct any *Any;
enum yytokentype;
typedef enum yytokentype Kind;
-extern const Any NIL;
-extern const Any T;
typedef Any (*Fn1)(Any a);
typedef Any (*Fn2)(Any a, Any b);
@@ -30,12 +28,14 @@ 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);
-char *token(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);
@@ -56,6 +56,7 @@ Any cadddr(Any x);
Any cddddr(Any x);
Any caddddr(Any x);
Any cdddddr(Any x);
+Any caar(Any x);
int equal_(Any x, Any y);
Any equal(Any x, Any y);
Any print(Any x);
@@ -64,3 +65,4 @@ Any mapc(void *env, Any (*fn)(void *env, Any e), Any x);
Any find(Any elt, Any lst, Fn2 cmp, Fn1 key);
Any reverse(Any x, Any a);
Any id(Any x);
+void init();
diff --git a/unoidl2ast.c b/unoidl2ast.c
@@ -23,6 +23,7 @@
extern int yyparse();
+extern const Any NIL;
extern Any ast;
static void pr_all(Any x) {
@@ -33,6 +34,7 @@ static void pr_all(Any x) {
}
int main() {
+ init();
yyparse();
pr_all(ast);
//print(ast);
diff --git a/unoidl2java.c b/unoidl2java.c
@@ -834,6 +834,7 @@ static void pp(Any x) {
}
int main() {
+ init();
yyparse();
module = NIL;
typedefs = NIL;
diff --git a/unoidl2xml.c b/unoidl2xml.c
@@ -52,6 +52,7 @@ void pr(Any x) {
}
int main() {
+ init();
yyparse();
pr(ast);
return 0;