mirror of
https://github.com/janet-lang/janet
synced 2025-10-30 07:03:02 +00:00
Allow parser to parse files rather than just a repl. I think
there are some memory leak issues (problems with gc).
This commit is contained in:
159
client/main.c
159
client/main.c
@@ -6,96 +6,95 @@
|
||||
#include <gst/stl.h>
|
||||
#include <gst/disasm.h>
|
||||
|
||||
/* A simple repl for debugging */
|
||||
void debug_repl(FILE *in, FILE *out) {
|
||||
/* Parse a file and execute it */
|
||||
int debug_run(Gst *vm, FILE *in) {
|
||||
char buffer[1024] = {0};
|
||||
const char * reader = buffer;
|
||||
const char *reader = buffer;
|
||||
GstValue func;
|
||||
Gst vm;
|
||||
GstParser p;
|
||||
GstCompiler c;
|
||||
|
||||
gst_parser(&p, vm);
|
||||
|
||||
/* Create do struct */
|
||||
GstArray *arr = gst_array(vm, 10);
|
||||
gst_array_push(vm, arr, gst_string_cv(vm, "do"));
|
||||
|
||||
/* Get and parse input until we have a full form */
|
||||
while (p.status != GST_PARSER_ERROR) {
|
||||
if (*reader == '\0') {
|
||||
if (!fgets(buffer, sizeof(buffer), in)) {
|
||||
break;
|
||||
}
|
||||
reader = buffer;
|
||||
}
|
||||
reader += gst_parse_cstring(&p, reader);
|
||||
if (gst_parse_hasvalue(&p))
|
||||
gst_array_push(vm, arr, gst_parse_consume(&p));
|
||||
}
|
||||
|
||||
/* Turn array into tuple */
|
||||
GstValue *tup = gst_tuple_begin(vm, arr->count);
|
||||
gst_memcpy(tup, arr->data, arr->count * sizeof(GstValue));
|
||||
vm->ret.type = GST_TUPLE;
|
||||
vm->ret.data.tuple = gst_tuple_end(vm, tup);
|
||||
|
||||
/* Check if file read in correctly */
|
||||
if (p.error) {
|
||||
printf("Parse error: %s\n", p.error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check that parser is complete */
|
||||
if (p.status != GST_PARSER_FULL && p.status != GST_PARSER_ROOT) {
|
||||
printf("Unexpected end of source\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Try to compile generated AST */
|
||||
gst_compiler(&c, vm);
|
||||
func.type = GST_NIL;
|
||||
gst_compiler_usemodule(&c, "std");
|
||||
func.type = GST_FUNCTION;
|
||||
func.data.function = gst_compiler_compile(&c, vm->ret);
|
||||
|
||||
/* Check for compilation errors */
|
||||
if (c.error) {
|
||||
printf("Compiler error: %s\n", c.error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Execute function */
|
||||
if (gst_run(vm, func)) {
|
||||
if (vm->crash) {
|
||||
printf("VM crash: %s\n", vm->crash);
|
||||
} else {
|
||||
printf("VM error: %s\n", (char *)gst_to_string(vm, vm->ret));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
|
||||
Gst vm;
|
||||
int status = 0;
|
||||
gst_init(&vm);
|
||||
gst_stl_load(&vm);
|
||||
|
||||
for (;;) {
|
||||
const char *filename;
|
||||
|
||||
/* Reset state */
|
||||
gst_parser(&p, &vm);
|
||||
// if (argc > 1) {
|
||||
// filename = argv[1];
|
||||
//} else {
|
||||
filename = "libs/stl.gst";
|
||||
//}
|
||||
|
||||
/* Get and parse input until we have a full form */
|
||||
while (p.status == GST_PARSER_PENDING) {
|
||||
/* Get some input if we are done */
|
||||
if (*reader == '\0') {
|
||||
if (out)
|
||||
fprintf(out, ">> ");
|
||||
if (!fgets(buffer, sizeof(buffer), in)) {
|
||||
return;
|
||||
}
|
||||
p.index = 0;
|
||||
reader = buffer;
|
||||
}
|
||||
reader += gst_parse_cstring(&p, reader);
|
||||
}
|
||||
FILE *f = fopen(filename, "rb");
|
||||
status = debug_run(&vm, f);
|
||||
|
||||
/* Check for parsing errors */
|
||||
if (p.error) {
|
||||
unsigned i;
|
||||
if (out) {
|
||||
fprintf(out, "\n");
|
||||
fprintf(out, "%s\n", buffer);
|
||||
for (i = 1; i < p.index; ++i) {
|
||||
fprintf(out, " ");
|
||||
}
|
||||
fprintf(out, "^\n");
|
||||
fprintf(out, "\nParse error: %s\n", p.error);
|
||||
}
|
||||
reader = buffer; /* Flush the input buffer */
|
||||
buffer[0] = '\0';
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Try to compile generated AST */
|
||||
gst_compiler(&c, &vm);
|
||||
func.type = GST_NIL;
|
||||
gst_compiler_usemodule(&c, "std");
|
||||
gst_compiler_global(&c, "ans", gst_object_get(vm.rootenv, gst_string_cv(&vm, "ans")));
|
||||
func.type = GST_FUNCTION;
|
||||
func.data.function = gst_compiler_compile(&c, p.value);
|
||||
|
||||
/* Check for compilation errors */
|
||||
if (c.error) {
|
||||
if (out) {
|
||||
fprintf(out, "Compiler error: %s\n", c.error);
|
||||
}
|
||||
reader = buffer;
|
||||
buffer[0] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Execute function */
|
||||
if (gst_run(&vm, func)) {
|
||||
if (out) {
|
||||
if (vm.crash) {
|
||||
fprintf(out, "VM crash: %s\n", vm.crash);
|
||||
} else {
|
||||
fprintf(out, "VM error: ");
|
||||
fprintf(out, "%s\n", (char *)gst_to_string(&vm, vm.ret));
|
||||
}
|
||||
}
|
||||
reader = buffer;
|
||||
buffer[0] = 0;
|
||||
continue;
|
||||
} else if (out) {
|
||||
fprintf(out, "%s\n", (char *)gst_to_string(&vm, vm.ret));
|
||||
gst_object_put(&vm, vm.rootenv, gst_string_cv(&vm, "ans"), vm.ret);
|
||||
}
|
||||
}
|
||||
gst_deinit(&vm);
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("GST v0.0 repl\nCopyright 2017 Calvin Rose\n");
|
||||
debug_repl(stdin, stdout);
|
||||
return 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user