commit ea3487c3966083843986f5a6bc563cb26a5b3bab
parent 86892ef5c441c186d24c2439246fa118b43b9928
Author: Tomas Hlavaty <tom@logand.com>
Date: Tue, 13 Dec 2011 00:09:22 +0100
unoidl2java: count up default enum values in pr_enum
Diffstat:
3 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/unoidl2.c b/unoidl2.c
@@ -22,7 +22,7 @@
#include <stdio.h>
struct any {
- enum {CONS, TOKEN} tag;
+ enum {CONS, TOKEN, INUM} tag;
union {
struct {
struct any *car;
@@ -32,6 +32,7 @@ struct any {
Kind kind;
char *token;
} t;
+ int inum;
} u;
};
@@ -56,12 +57,20 @@ Any mk(Kind kind, char *token) {
return x;
}
+Any mkinum(int n) {
+ Any x = malloc(sizeof(struct any));
+ x->tag = INUM;
+ x->u.inum = n;
+ return x;
+}
+
int null(Any x) {return NIL == 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;}
+int inum(Any x) {return x->u.inum;}
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)));}
@@ -108,8 +117,9 @@ Any cdddddr(Any x) {return cdr(cdr(cdr(cdr(cdr(x)))));}
void print(Any x) {
if(null(x))
printf("NIL");
- else {
- if(consp(x)) {
+ else
+ switch(x->tag) {
+ case CONS:
printf("(");
print(car(x));
for(Any d = cdr(x); !null(d); d = cdr(d)) {
@@ -123,7 +133,13 @@ void print(Any x) {
}
}
printf(")");
- } else
+ break;
+ case TOKEN:
printf("%s", x->u.t.token);
- }
+ break;
+ case INUM:
+ printf("%d", x->u.inum);
+ break;
+ /* default: error? */
+ }
}
diff --git a/unoidl2.h b/unoidl2.h
@@ -23,12 +23,14 @@ typedef enum yytokentype Kind;
Any cons(Any car, Any cdr);
Any mk(Kind kind, char *token);
+Any mkinum(int n);
int null(Any x);
int consp(Any x);
Any car(Any x);
Any cdr(Any x);
Kind kind(Any x);
char *token(Any x);
+int inum(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);
diff --git a/unoidl2java.c b/unoidl2java.c
@@ -78,18 +78,20 @@ static void pr_enum(Any x) {
pr(" public static "); pp(name); pl(" getDefault() {");
pr(" return "); pp(v0k); pl(";");
pl(" }");
+ int n = 0;
for(Any y = values; !null(y); y = cdr(y)) {
Any value = car(y);
char *k = token(consp(value) ? car(value) : value);
- Any v = consp(value) ? cdr(value) : mk(INT, "?"); // TODO
+ Any v = consp(value) ? cdr(value) : mkinum(n++);
pr(" public static final "); pp(name); pr(" "); pr(k); pr(" = new "); pp(name); pr("("); pp(v); pl(");");
}
pr(" public static "); pp(name); pl(" fromInt(int value) {");
pl(" switch(value) {");
+ n = 0;
for(Any y = values; !null(y); y = cdr(y)) {
Any value = car(y);
char *k = token(consp(value) ? car(value) : value);
- Any v = consp(value) ? cdr(value) : mk(INT, "?"); // TODO
+ Any v = consp(value) ? cdr(value) : mkinum(n++);
pr(" case "); pp(v); pr(": return "); pr(k); pl(";");
}
pl(" }");