ref: 08c76bd5343c15ac8e2d0f0b519c1a3ffae45856
parent: 380be8129fadeb358e5475cba08005a08ded9c29
author: Tor Andersson <tor@ccxvii.net>
date: Thu Jan 9 20:33:56 EST 2014
Pretty print and indent STM_BLOCK nodes.
--- a/js-ast.c
+++ b/js-ast.c
@@ -142,18 +142,34 @@
}
}
-void printlist(js_Ast *n)
+static void indent(int level)
{
+ while (level--)
+ putchar('\t');
+}
+
+void printlist(js_Ast *n, int level, const char *sep)
+{
while (n) {
- printast(n->a);
+ printast(n->a, level);
n = n->b;
if (n)
- putchar(' ');
+ fputs(sep, stdout);
}
}
-void printast(js_Ast *n)
+void printblock(js_Ast *n, int level)
{
+ while (n) {
+ indent(level);
+ printast(n->a, level);
+ putchar('\n');
+ n = n->b;
+ }
+}
+
+void printast(js_Ast *n, int level)
+{
switch (n->type) {
case AST_IDENTIFIER: printf("%s", n->s); return;
case AST_NUMBER: printf("%g", n->n); return;
@@ -161,15 +177,22 @@
case AST_REGEXP: printf("/%s/", n->s); return;
case AST_LIST:
putchar('[');
- printlist(n);
+ printlist(n, level, " ");
putchar(']');
break;
+ case STM_BLOCK:
+ putchar('{');
+ putchar('\n');
+ printblock(n->a, level + 1);
+ indent(level);
+ putchar('}');
+ break;
default:
printf("(%s", strast(n->type));
- if (n->a) { putchar(' '); printast(n->a); }
- if (n->b) { putchar(' '); printast(n->b); }
- if (n->c) { putchar(' '); printast(n->c); }
- if (n->d) { putchar(' '); printast(n->d); }
+ if (n->a) { putchar(' '); printast(n->a, level); }
+ if (n->b) { putchar(' '); printast(n->b, level); }
+ if (n->c) { putchar(' '); printast(n->c, level); }
+ if (n->d) { putchar(' '); printast(n->d, level); }
putchar(')');
break;
}
--- a/js-ast.h
+++ b/js-ast.h
@@ -121,6 +121,6 @@
js_Ast *jsP_newnnode(js_State *J, int type, double n);
void jsP_freeast(js_State *J);
-void printast(js_Ast *n);
+void printast(js_Ast *n, int level);
#endif
--- a/js-parse.c
+++ b/js-parse.c
@@ -828,7 +828,7 @@
}
next(J);
- printast(sourcelist(J));
+ printast(sourcelist(J), 0);
putchar('\n');
// TODO: compile to bytecode