ref: 3f564db259b89d87cf87ff6691c4ef0f47b55f09
parent: 8342452b13a5df602dbb85ecc6602e8eaa4f0206
author: Tor Andersson <tor@ccxvii.net>
date: Fri Jan 10 09:37:39 EST 2014
Clean split between loader, parser and pretty-printer.
--- 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)
--- a/jsparse.c
+++ b/jsparse.c
@@ -24,6 +24,50 @@
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 @@
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);
}
--- a/jsparse.h
+++ b/jsparse.h
@@ -121,14 +121,9 @@
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
--- a/jspretty.c
+++ b/jspretty.c
@@ -1,51 +1,8 @@
#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));
+static void printast(js_Ast *n, int level);
- 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 const char *strast(int type)
{
switch (type) {
@@ -159,7 +116,7 @@
}
}
-void printblock(js_Ast *n, int level)
+static void printblock(js_Ast *n, int level)
{
while (n) {
indent(level);
@@ -205,7 +162,7 @@
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;
@@ -552,4 +509,10 @@
putchar(')');
break;
}
+}
+
+void jsP_pretty(js_State *J, js_Ast *prog)
+{
+ printblock(prog, 0);
+ putchar('\n');
}