2017-02-09 20:02:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-03-08 20:08:46 +00:00
|
|
|
#include "gst.h"
|
2017-03-10 05:09:42 +00:00
|
|
|
#include "disasm.h"
|
2017-02-09 20:02:59 +00:00
|
|
|
|
|
|
|
/* A simple repl for debugging */
|
2017-02-23 22:21:13 +00:00
|
|
|
void debug_repl(FILE *in, FILE *out) {
|
2017-02-13 05:11:30 +00:00
|
|
|
char buffer[1024] = {0};
|
2017-02-13 04:45:52 +00:00
|
|
|
const char * reader = buffer;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue func;
|
|
|
|
Gst vm;
|
|
|
|
GstParser p;
|
|
|
|
GstCompiler c;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_init(&vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-03-14 23:13:17 +00:00
|
|
|
vm.rootenv.type = GST_OBJECT;
|
|
|
|
vm.rootenv.data.object = gst_object(&vm, 10);
|
|
|
|
gst_object_put(&vm, vm.rootenv.data.object, gst_load_cstring(&vm, "_ENV"), vm.rootenv);
|
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
for (;;) {
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Reset state */
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_parser(&p, &vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Get and parse input until we have a full form */
|
2017-02-16 02:02:00 +00:00
|
|
|
while (p.status == GST_PARSER_PENDING) {
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Get some input if we are done */
|
|
|
|
if (*reader == '\0') {
|
2017-02-23 22:21:13 +00:00
|
|
|
if (out)
|
|
|
|
fprintf(out, ">> ");
|
|
|
|
if (!fgets(buffer, sizeof(buffer), in)) {
|
2017-02-13 04:45:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
p.index = 0;
|
|
|
|
reader = buffer;
|
|
|
|
}
|
2017-02-16 02:02:00 +00:00
|
|
|
reader += gst_parse_cstring(&p, reader);
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Check for parsing errors */
|
|
|
|
if (p.error) {
|
|
|
|
unsigned i;
|
2017-02-23 22:21:13 +00:00
|
|
|
if (out) {
|
|
|
|
fprintf(out, "\n");
|
|
|
|
fprintf(out, "%s\n", buffer);
|
2017-03-12 22:23:27 +00:00
|
|
|
for (i = 1; i < p.index; ++i) {
|
2017-02-23 22:21:13 +00:00
|
|
|
fprintf(out, " ");
|
|
|
|
}
|
|
|
|
fprintf(out, "^\n");
|
|
|
|
fprintf(out, "\nParse error: %s\n", p.error);
|
2017-02-13 04:45:52 +00:00
|
|
|
}
|
|
|
|
reader = buffer; /* Flush the input buffer */
|
|
|
|
buffer[0] = '\0';
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Try to compile generated AST */
|
2017-02-16 02:02:00 +00:00
|
|
|
gst_compiler(&c, &vm);
|
2017-03-14 23:13:17 +00:00
|
|
|
func.type = GST_NIL;
|
|
|
|
gst_compiler_add_global(&c, "ans", func);
|
2017-03-09 18:49:46 +00:00
|
|
|
gst_stl_load(&c);
|
2017-03-14 23:13:17 +00:00
|
|
|
gst_compiler_env(&c, vm.rootenv);
|
2017-02-16 02:02:00 +00:00
|
|
|
func.type = GST_FUNCTION;
|
|
|
|
func.data.function = gst_compiler_compile(&c, p.value);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Check for compilation errors */
|
|
|
|
if (c.error) {
|
2017-02-23 22:21:13 +00:00
|
|
|
if (out) {
|
|
|
|
fprintf(out, "Compiler error: %s\n", c.error);
|
|
|
|
}
|
2017-02-13 04:45:52 +00:00
|
|
|
reader = buffer;
|
|
|
|
buffer[0] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-11 19:01:06 +00:00
|
|
|
|
2017-03-10 05:09:42 +00:00
|
|
|
/* Print asm */
|
2017-03-12 22:23:27 +00:00
|
|
|
//if (out) {
|
|
|
|
//fprintf(out, "\n");
|
|
|
|
//gst_dasm_function(out, func.data.function);
|
|
|
|
//}
|
2017-03-10 05:09:42 +00:00
|
|
|
|
2017-02-13 04:45:52 +00:00
|
|
|
/* Execute function */
|
2017-03-10 05:09:42 +00:00
|
|
|
if (gst_run(&vm, func)) {
|
2017-02-23 22:21:13 +00:00
|
|
|
if (out) {
|
|
|
|
if (vm.crash) {
|
|
|
|
fprintf(out, "VM crash: %s\n", vm.crash);
|
|
|
|
} else {
|
|
|
|
fprintf(out, "VM error: ");
|
2017-03-14 23:13:17 +00:00
|
|
|
fprintf(out, "%s\n", (char *)gst_to_string(&vm, vm.ret));
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-02-22 23:19:46 +00:00
|
|
|
}
|
2017-02-13 04:45:52 +00:00
|
|
|
reader = buffer;
|
|
|
|
buffer[0] = 0;
|
|
|
|
continue;
|
2017-02-23 22:21:13 +00:00
|
|
|
} else if (out) {
|
2017-03-14 23:13:17 +00:00
|
|
|
fprintf(out, "%s\n", (char *)gst_to_string(&vm, vm.ret));
|
|
|
|
gst_object_put(&vm, vm.rootenv.data.object, gst_load_cstring(&vm, "ans"), vm.ret);
|
2017-02-13 04:45:52 +00:00
|
|
|
}
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2017-02-13 04:45:52 +00:00
|
|
|
printf("Super cool interpreter v0.0\n");
|
2017-02-23 22:21:13 +00:00
|
|
|
debug_repl(stdin, stdout);
|
2017-02-13 04:45:52 +00:00
|
|
|
return 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|