From 3f564db259b89d87cf87ff6691c4ef0f47b55f09 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 10 Jan 2014 14:37:39 +0100 Subject: [PATCH] Clean split between loader, parser and pretty-printer. --- jsload.c | 8 +++++++- jsparse.c | 58 ++++++++++++++++++++++++++++++++++++++++++++---------- jsparse.h | 11 +++-------- jspretty.c | 55 +++++++++------------------------------------------ 4 files changed, 67 insertions(+), 65 deletions(-) diff --git a/jsload.c b/jsload.c index d590810..6988459 100644 --- a/jsload.c +++ b/jsload.c @@ -3,7 +3,13 @@ static int jsP_loadstring(js_State *J, const char *filename, const char *source) { - return jsP_parse(J, filename, source); + js_Ast *prog = jsP_parse(J, filename, source); + if (prog) { + jsP_pretty(J, prog); + jsP_freeparse(J); + return 0; + } + return 1; } int js_loadstring(js_State *J, const char *source) diff --git a/jsparse.c b/jsparse.c index b1066a1..e28dc41 100644 --- a/jsparse.c +++ b/jsparse.c @@ -24,6 +24,50 @@ static js_Ast *memberexp(js_State *J); static js_Ast *statement(js_State *J); static js_Ast *funcbody(js_State *J); +js_Ast *jsP_newnode(js_State *J, int type, js_Ast *a, js_Ast *b, js_Ast *c, js_Ast *d) +{ + js_Ast *node = malloc(sizeof(js_Ast)); + + node->type = type; + node->line = J->line; + node->a = a; + node->b = b; + node->c = c; + node->d = d; + node->n = 0; + node->s = NULL; + + node->next = J->ast; + J->ast = node; + + return node; +} + +js_Ast *jsP_newstrnode(js_State *J, int type, const char *s) +{ + js_Ast *node = jsP_newnode(J, type, 0, 0, 0, 0); + node->s = s; + return node; +} + +js_Ast *jsP_newnumnode(js_State *J, int type, double n) +{ + js_Ast *node = jsP_newnode(J, type, 0, 0, 0, 0); + node->n = n; + return node; +} + +void jsP_freeparse(js_State *J) +{ + js_Ast *node = J->ast; + while (node) { + js_Ast *next = node->next; + free(node); + node = next; + } + J->ast = NULL; +} + static inline void next(js_State *J) { J->lookahead = jsP_lex(J); @@ -741,21 +785,15 @@ int jsP_error(js_State *J, const char *fmt, ...) return 0; } -int jsP_parse(js_State *J, const char *filename, const char *source) +js_Ast *jsP_parse(js_State *J, const char *filename, const char *source) { jsP_initlex(J, filename, source); if (setjmp(J->jb)) { - jsP_freeast(J); - return 1; + jsP_freeparse(J); + return NULL; } next(J); - printblock(chunklist(J), 0); - putchar('\n'); - - // TODO: compile to bytecode - - jsP_freeast(J); - return 0; + return chunklist(J); } diff --git a/jsparse.h b/jsparse.h index 4661043..9e6efbe 100644 --- a/jsparse.h +++ b/jsparse.h @@ -121,14 +121,9 @@ enum STM_DEBUGGER, }; -int jsP_parse(js_State *J, const char *filename, const char *source); +js_Ast *jsP_parse(js_State *J, const char *filename, const char *source); +void jsP_freeparse(js_State *J); -js_Ast *jsP_newnode(js_State *J, int type, js_Ast *a, js_Ast *b, js_Ast *c, js_Ast *d); -js_Ast *jsP_newstrnode(js_State *J, int type, const char *s); -js_Ast *jsP_newnumnode(js_State *J, int type, double n); -void jsP_freeast(js_State *J); - -void printast(js_Ast *n, int level); -void printblock(js_Ast *n, int level); +void jsP_pretty(js_State *J, js_Ast *prog); #endif diff --git a/jspretty.c b/jspretty.c index ce8222f..3475891 100644 --- a/jspretty.c +++ b/jspretty.c @@ -1,50 +1,7 @@ #include "js.h" #include "jsparse.h" -#include "jsstate.h" -js_Ast *jsP_newnode(js_State *J, int type, js_Ast *a, js_Ast *b, js_Ast *c, js_Ast *d) -{ - js_Ast *node = malloc(sizeof(js_Ast)); - - node->type = type; - node->line = J->line; - node->a = a; - node->b = b; - node->c = c; - node->d = d; - node->n = 0; - node->s = NULL; - - node->next = J->ast; - J->ast = node; - - return node; -} - -js_Ast *jsP_newstrnode(js_State *J, int type, const char *s) -{ - js_Ast *node = jsP_newnode(J, type, 0, 0, 0, 0); - node->s = s; - return node; -} - -js_Ast *jsP_newnumnode(js_State *J, int type, double n) -{ - js_Ast *node = jsP_newnode(J, type, 0, 0, 0, 0); - node->n = n; - return node; -} - -void jsP_freeast(js_State *J) -{ - js_Ast *node = J->ast; - while (node) { - js_Ast *next = node->next; - free(node); - node = next; - } - J->ast = NULL; -} +static void printast(js_Ast *n, int level); static const char *strast(int type) { @@ -159,7 +116,7 @@ static void printlist(js_Ast *n, int level, const char *sep) } } -void printblock(js_Ast *n, int level) +static void printblock(js_Ast *n, int level) { while (n) { indent(level); @@ -205,7 +162,7 @@ static void printbinary(int level, js_Ast *a, js_Ast *b, const char *op) printf(")"); } -void printast(js_Ast *n, int level) +static void printast(js_Ast *n, int level) { switch (n->type) { case AST_IDENTIFIER: printf("%s", n->s); return; @@ -553,3 +510,9 @@ void printast(js_Ast *n, int level) break; } } + +void jsP_pretty(js_State *J, js_Ast *prog) +{ + printblock(prog, 0); + putchar('\n'); +}