mirror of
https://github.com/ccxvii/mujs.git
synced 2026-09-22 15:13:35 +08:00
Clean split between loader, parser and pretty-printer.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+9
-46
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user