2018-02-07 05:44:51 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2018-02-07 05:44:51 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "state.h"
|
2018-11-26 14:02:07 +00:00
|
|
|
#include "vector.h"
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Error reporting */
|
2018-09-10 01:20:33 +00:00
|
|
|
void janet_stacktrace(JanetFiber *fiber, const char *errtype, Janet err) {
|
2018-11-26 14:02:07 +00:00
|
|
|
int32_t fi;
|
2018-09-06 02:18:42 +00:00
|
|
|
const char *errstr = (const char *)janet_to_string(err);
|
2018-11-26 14:02:07 +00:00
|
|
|
JanetFiber **fibers = NULL;
|
|
|
|
fprintf(stderr, "%s error: %s\n", errtype, errstr);
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2018-11-26 14:02:07 +00:00
|
|
|
while (fiber) {
|
|
|
|
janet_v_push(fibers, fiber);
|
|
|
|
fiber = fiber->child;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (fi = janet_v_count(fibers) - 1; fi >= 0; fi--) {
|
|
|
|
fiber = fibers[fi];
|
|
|
|
int32_t i = fiber->frame;
|
|
|
|
if (i > 0) fprintf(stderr, " (fiber)\n");
|
|
|
|
while (i > 0) {
|
|
|
|
JanetStackFrame *frame = (JanetStackFrame *)(fiber->data + i - JANET_FRAME_SIZE);
|
|
|
|
JanetFuncDef *def = NULL;
|
|
|
|
i = frame->prevframe;
|
|
|
|
fprintf(stderr, " in");
|
|
|
|
if (frame->func) {
|
|
|
|
def = frame->func->def;
|
|
|
|
fprintf(stderr, " %s", def->name ? (const char *)def->name : "<anonymous>");
|
|
|
|
if (def->source) {
|
|
|
|
fprintf(stderr, " [%s]", (const char *)def->source);
|
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
} else {
|
2018-11-26 14:02:07 +00:00
|
|
|
JanetCFunction cfun = (JanetCFunction)(frame->pc);
|
|
|
|
if (cfun) {
|
|
|
|
Janet name = janet_table_get(janet_vm_registry, janet_wrap_cfunction(cfun));
|
|
|
|
if (!janet_checktype(name, JANET_NIL))
|
|
|
|
fprintf(stderr, " %s", (const char *)janet_to_string(name));
|
|
|
|
else
|
|
|
|
fprintf(stderr, " <cfunction>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (frame->flags & JANET_STACKFRAME_TAILCALL)
|
|
|
|
fprintf(stderr, " (tailcall)");
|
|
|
|
if (frame->func && frame->pc) {
|
|
|
|
int32_t off = (int32_t) (frame->pc - def->bytecode);
|
|
|
|
if (def->sourcemap) {
|
|
|
|
JanetSourceMapping mapping = def->sourcemap[off];
|
2018-12-13 23:46:53 +00:00
|
|
|
fprintf(stderr, " at (%d:%d)", mapping.start, mapping.end);
|
2018-11-26 14:02:07 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, " pc=%d", off);
|
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2018-11-26 14:02:07 +00:00
|
|
|
fprintf(stderr, "\n");
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-13 23:46:53 +00:00
|
|
|
|
|
|
|
janet_v_free(fibers);
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2018-02-07 05:44:51 +00:00
|
|
|
|
|
|
|
/* Run a string */
|
2018-10-17 03:08:26 +00:00
|
|
|
int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char *sourcePath, Janet *out) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetParser parser;
|
2018-02-07 05:44:51 +00:00
|
|
|
int errflags = 0;
|
|
|
|
int32_t index = 0;
|
2018-02-07 18:19:34 +00:00
|
|
|
int dudeol = 0;
|
2018-05-16 02:03:45 +00:00
|
|
|
int done = 0;
|
2018-10-17 03:08:26 +00:00
|
|
|
Janet ret = janet_wrap_nil();
|
2018-09-06 02:18:42 +00:00
|
|
|
const uint8_t *where = sourcePath ? janet_cstring(sourcePath) : NULL;
|
|
|
|
if (where) janet_gcroot(janet_wrap_string(where));
|
|
|
|
janet_parser_init(&parser);
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-05-16 02:03:45 +00:00
|
|
|
while (!errflags && !done) {
|
2019-01-04 01:44:58 +00:00
|
|
|
|
|
|
|
/* Evaluate parsed values */
|
|
|
|
while (janet_parser_has_more(&parser)) {
|
|
|
|
Janet form = janet_parser_produce(&parser);
|
|
|
|
JanetCompileResult cres = janet_compile(form, env, where);
|
|
|
|
if (cres.status == JANET_COMPILE_OK) {
|
|
|
|
JanetFunction *f = janet_thunk(cres.funcdef);
|
2019-01-12 22:31:15 +00:00
|
|
|
JanetFiber *fiber = janet_fiber(f, 64, 0, NULL);
|
2019-01-04 01:44:58 +00:00
|
|
|
JanetSignal status = janet_continue(fiber, janet_wrap_nil(), &ret);
|
|
|
|
if (status != JANET_SIGNAL_OK) {
|
|
|
|
janet_stacktrace(fiber, "runtime", ret);
|
|
|
|
errflags |= 0x01;
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
2019-01-04 01:44:58 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "source path: %s\n", sourcePath);
|
|
|
|
janet_stacktrace(cres.macrofiber, "compile",
|
|
|
|
janet_wrap_string(cres.error));
|
|
|
|
errflags |= 0x02;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch based on parse state */
|
|
|
|
switch (janet_parser_status(&parser)) {
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_PARSE_ERROR:
|
2018-02-07 05:44:51 +00:00
|
|
|
errflags |= 0x04;
|
2018-11-26 14:02:07 +00:00
|
|
|
fprintf(stderr, "parse error: %s\n", janet_parser_error(&parser));
|
2018-02-07 05:44:51 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_PARSE_PENDING:
|
2018-02-07 18:19:34 +00:00
|
|
|
if (index >= len) {
|
|
|
|
if (dudeol) {
|
|
|
|
errflags |= 0x04;
|
2018-11-26 14:02:07 +00:00
|
|
|
fprintf(stderr, "internal parse error: unexpected end of source\n");
|
2018-02-07 18:19:34 +00:00
|
|
|
} else {
|
|
|
|
dudeol = 1;
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_consume(&parser, '\n');
|
2018-02-07 18:19:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_consume(&parser, bytes[index++]);
|
2018-02-07 18:19:34 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_PARSE_ROOT:
|
2018-05-16 02:03:45 +00:00
|
|
|
if (index >= len) {
|
|
|
|
done = 1;
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_consume(&parser, bytes[index++]);
|
2018-05-16 02:03:45 +00:00
|
|
|
}
|
2018-02-07 05:44:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-04 01:44:58 +00:00
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_parser_deinit(&parser);
|
|
|
|
if (where) janet_gcunroot(janet_wrap_string(where));
|
2018-10-17 03:08:26 +00:00
|
|
|
if (out) *out = ret;
|
2018-05-16 02:03:45 +00:00
|
|
|
return errflags;
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 03:08:26 +00:00
|
|
|
int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out) {
|
2018-02-07 05:44:51 +00:00
|
|
|
int32_t len = 0;
|
|
|
|
while (str[len]) ++len;
|
2018-10-17 03:08:26 +00:00
|
|
|
return janet_dobytes(env, (const uint8_t *)str, len, sourcePath, out);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|