commit f1abc045165d594a775fbf932c87a888ecbaa95f
parent a0e0978b6044a69fc3c28759fbbcc45da7bfac7f
Author: Tomas Hlavaty <tom@logand.com>
Date: Sun, 18 Mar 2012 11:38:40 +0100
print2cell generalized/reusable for different output formats
Diffstat:
3 files changed, 28 insertions(+), 29 deletions(-)
diff --git a/unoidl2.c b/unoidl2.c
@@ -344,6 +344,27 @@ Any run1(Any x) {
return x;
}
+void (*print2cell_fn)(Any k, Any p, Any v);
+
+Any print2cell(Any x) {
+ Any c = cdr(x);
+ Any kpv = car(x);
+ Any k = car(kpv);
+ Any p = cadr(kpv);
+ Any v = cddr(kpv);
+ if(NIL == v) {
+ mapc(print2cell, c);
+ } else if(MODULE == kind(v)) {
+ mapc(print2cell, c);
+ } else if(NIL == c) {
+ print2cell_fn(k, p, v);
+ } else {
+ printf("@@@ print2cell error: unexpected children\n");
+ print(x);
+ }
+ return x;
+}
+
void init() {
heap = calloc(HSIZE, sizeof(struct any));
NIL = xalloc();
diff --git a/unoidl2.h b/unoidl2.h
@@ -21,7 +21,6 @@ typedef struct any *Any;
enum yytokentype;
typedef enum yytokentype Kind;
-
typedef Any (*Fn1)(Any a);
typedef Any (*Fn2)(Any a, Any b);
@@ -66,4 +65,6 @@ Any find(Any elt, Any lst, Fn2 cmp, Fn1 key);
Any reverse(Any x, Any a);
Any id(Any x);
Any run1(Any x);
+extern void (*print2cell_fn)(Any k, Any p, Any v);
+Any print2cell(Any x);
void init();
diff --git a/unoidl2ast.c b/unoidl2ast.c
@@ -25,40 +25,17 @@ extern int yyparse();
extern const Any NIL;
extern const Any ast;
-
-static Any pr1(Any x) {
- print(x);
- printf("\n\n");
-}
-
-static Any print2cell(Any x) {
- Any c = cdr(x);
- Any kpv = car(x);
- Any k = car(kpv);
- Any p = cadr(kpv);
- Any v = cddr(kpv);
- if(NIL == v) {
- mapc(print2cell, c);
- } else if(MODULE == kind(v)) {
- mapc(print2cell, c);
- } else if(NIL == c) {
- print(v);
- } else {
- printf("@@@ print2cell error: unexpected children\n");
- print(x);
- }
- return x;
-}
-
extern const Any root2;
+static Any pr1(Any x) {print(x); printf("\n\n");}
+static void cell2ast(Any k, Any p, Any v) {print(v);}
+
int main() {
init();
yyparse();
- //mapc(NULL, pr1, ast);
+ //mapc(pr1, ast);
mapc(run1, ast);
+ print2cell_fn = cell2ast;
print2cell(root2);
- //print(ast);
- //printf("\n");
return 0;
}