Simplistic REPL.

This commit is contained in:
Tor Andersson
2014-01-21 15:15:22 +01:00
parent ca79fc1497
commit 5cdcc20936
6 changed files with 33 additions and 16 deletions

2
js.h
View File

@@ -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);

View File

@@ -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);

View File

@@ -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("<unknown %p>", 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;
}

View File

@@ -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;

View File

@@ -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;

18
main.c
View File

@@ -1,15 +1,29 @@
#include <stdio.h>
#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);
}