From 5cdcc209367f3497006fe43a51e187f6ae7d2e86 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 21 Jan 2014 15:15:22 +0100 Subject: [PATCH] Simplistic REPL. --- js.h | 2 +- jscompile.c | 2 +- jsdump.c | 20 ++++++++++---------- jsparse.c | 3 ++- jsstate.c | 4 +++- main.c | 18 ++++++++++++++++-- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/js.h b/js.h index ce3febc..d37616a 100644 --- a/js.h +++ b/js.h @@ -10,7 +10,7 @@ typedef int (*js_CFunction)(js_State *J, int argc); /* Basic functions */ js_State *js_newstate(void); void js_freestate(js_State *J); -int js_dostring(js_State *J, const char *source); +int js_dostring(js_State *J, const char *source, int report); int js_dofile(js_State *J, const char *filename); void js_gc(js_State *J, int report); diff --git a/jscompile.c b/jscompile.c index 33f7b11..ffaebe8 100644 --- a/jscompile.c +++ b/jscompile.c @@ -39,7 +39,7 @@ static js_Function *newfun(js_State *J, js_Ast *name, js_Ast *params, js_Ast *bo ++J->gccounter; F->filename = js_intern(J, J->filename); - F->line = name ? name->line : params ? params->line : body->line; + F->line = name ? name->line : params ? params->line : body ? body->line : 1; cfunbody(J, F, name, params, body); diff --git a/jsdump.c b/jsdump.c index b592bec..5751acc 100644 --- a/jsdump.c +++ b/jsdump.c @@ -685,22 +685,22 @@ void js_dumpvalue(js_State *J, js_Value v) case JS_TSTRING: printf("'%s'", v.u.string); break; case JS_TOBJECT: switch (v.u.object->type) { - case JS_COBJECT: printf("object(%p)", v.u.object); break; - case JS_CARRAY: printf("array(%p)", v.u.object); break; + case JS_COBJECT: printf("[Object %p]", v.u.object); break; + case JS_CARRAY: printf("[Array %p]", v.u.object); break; case JS_CFUNCTION: - printf("function(%p, %s, %s:%d)", + printf("[Function %p, %s, %s:%d]", v.u.object, v.u.object->u.f.function->name, v.u.object->u.f.function->filename, v.u.object->u.f.function->line); break; - case JS_CSCRIPT: printf("script(%s)", v.u.object->u.f.function->filename); break; - case JS_CCFUNCTION: printf("cfunction(%p)", v.u.object->u.c.function); break; - case JS_CBOOLEAN: printf("boolean(%d)", v.u.object->u.boolean); break; - case JS_CNUMBER: printf("number(%g)", v.u.object->u.number); break; - case JS_CSTRING: printf("string('%s')", v.u.object->u.string); break; - case JS_CERROR: printf("error()"); break; - default: printf("", v.u.object); break; + case JS_CSCRIPT: printf("[Function %s]", v.u.object->u.f.function->filename); break; + case JS_CCFUNCTION: printf("[Function %p]", v.u.object->u.c.function); break; + case JS_CBOOLEAN: printf("[Boolean %d]", v.u.object->u.boolean); break; + case JS_CNUMBER: printf("[Number %g]", v.u.object->u.number); break; + case JS_CSTRING: printf("[String'%s']", v.u.object->u.string); break; + case JS_CERROR: printf("[Error %s]", v.u.object->u.string); break; + default: printf("[Object %p]", v.u.object); break; } break; } diff --git a/jsparse.c b/jsparse.c index cafa5bb..b7897ea 100644 --- a/jsparse.c +++ b/jsparse.c @@ -950,7 +950,8 @@ js_Ast *jsP_parse(js_State *J, const char *filename, const char *source) next(J); p = script(J); - jsP_foldconst(p); + if (p) + jsP_foldconst(p); /* patch up global and eval code to return value of last expression */ last = p; diff --git a/jsstate.c b/jsstate.c index b2d695c..17fb19f 100644 --- a/jsstate.c +++ b/jsstate.c @@ -69,7 +69,7 @@ void js_loadfile(js_State *J, const char *filename) js_endtry(J); } -int js_dostring(js_State *J, const char *source) +int js_dostring(js_State *J, const char *source, int report) { if (js_try(J)) { fprintf(stderr, "libjs: %s\n", js_tostring(J, -1)); @@ -78,6 +78,8 @@ int js_dostring(js_State *J, const char *source) js_loadstring(J, "(string)", source); js_pushglobal(J); js_call(J, 0); + if (report) + printf("%s\n", js_tostring(J, -1)); js_pop(J, 1); js_endtry(J); return 0; diff --git a/main.c b/main.c index 449d1c4..d92d9e6 100644 --- a/main.c +++ b/main.c @@ -1,15 +1,29 @@ +#include + #include "js.h" +#define PS1 "> " + int main(int argc, char **argv) { + char line[256]; js_State *J; int i; J = js_newstate(); - for (i = 1; i < argc; ++i) { - js_dofile(J, argv[i]); + if (argc > 1) { + for (i = 1; i < argc; ++i) { + js_dofile(J, argv[i]); + js_gc(J, 1); + } + } else { + fputs(PS1, stdout); + while (fgets(line, sizeof line, stdin)) { + js_dostring(J, line, 1); + fputs(PS1, stdout); + } js_gc(J, 1); }