1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-06 00:36:17 +00:00
janet/src/core/run.c

105 lines
3.9 KiB
C
Raw Normal View History

/*
* Copyright (c) 2019 Calvin Rose
*
* 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.
*/
#ifndef JANET_AMALG
#include <janet.h>
#include "state.h"
#endif
/* 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;
int errflags = 0;
int32_t index = 0;
int dudeol = 0;
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));
if (NULL == sourcePath) sourcePath = "<unknown>";
2018-09-06 02:18:42 +00:00
janet_parser_init(&parser);
while (!errflags && !done) {
/* 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);
JanetFiber *fiber = janet_fiber(f, 64, 0, NULL);
JanetSignal status = janet_continue(fiber, janet_wrap_nil(), &ret);
if (status != JANET_SIGNAL_OK) {
janet_stacktrace(fiber, ret);
errflags |= 0x01;
}
} else {
fprintf(stderr, "compile error in %s: %s\n", sourcePath,
(const char *)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:
errflags |= 0x04;
fprintf(stderr, "parse error in %s: %s\n",
sourcePath, janet_parser_error(&parser));
break;
2018-09-06 02:18:42 +00:00
case JANET_PARSE_PENDING:
if (index >= len) {
if (dudeol) {
errflags |= 0x04;
fprintf(stderr, "internal parse error in %s: unexpected end of source\n",
sourcePath);
} else {
dudeol = 1;
2018-09-06 02:18:42 +00:00
janet_parser_consume(&parser, '\n');
}
} else {
2018-09-06 02:18:42 +00:00
janet_parser_consume(&parser, bytes[index++]);
}
break;
2018-09-06 02:18:42 +00:00
case JANET_PARSE_ROOT:
if (index >= len) {
done = 1;
} else {
2018-09-06 02:18:42 +00:00
janet_parser_consume(&parser, bytes[index++]);
}
break;
}
}
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;
return errflags;
}
2018-10-17 03:08:26 +00:00
int janet_dostring(JanetTable *env, const char *str, const char *sourcePath, Janet *out) {
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);
}