2017-02-09 20:02:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-03-16 00:56:37 +00:00
|
|
|
#include <gst/gst.h>
|
2017-03-22 04:27:18 +00:00
|
|
|
#include <gst/parse.h>
|
|
|
|
#include <gst/compile.h>
|
|
|
|
#include <gst/stl.h>
|
2017-03-16 00:56:37 +00:00
|
|
|
#include <gst/disasm.h>
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* Parse a file and execute it */
|
|
|
|
int debug_run(Gst *vm, FILE *in) {
|
2017-02-13 05:11:30 +00:00
|
|
|
char buffer[1024] = {0};
|
2017-04-17 04:15:18 +00:00
|
|
|
const char *reader = buffer;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstValue func;
|
|
|
|
GstParser p;
|
|
|
|
GstCompiler c;
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
gst_parser(&p, vm);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* Create do struct */
|
|
|
|
GstArray *arr = gst_array(vm, 10);
|
|
|
|
gst_array_push(vm, arr, gst_string_cv(vm, "do"));
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* 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;
|
2017-02-23 22:21:13 +00:00
|
|
|
}
|
2017-02-13 04:45:52 +00:00
|
|
|
reader = buffer;
|
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
reader += gst_parse_cstring(&p, reader);
|
|
|
|
if (gst_parse_hasvalue(&p))
|
|
|
|
gst_array_push(vm, arr, gst_parse_consume(&p));
|
|
|
|
}
|
2017-02-11 19:01:06 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* 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;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* 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);
|
2017-02-09 20:02:59 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2017-02-13 04:45:52 +00:00
|
|
|
return 0;
|
2017-02-09 20:02:59 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
|
|
|
|
Gst vm;
|
|
|
|
int status = 0;
|
|
|
|
gst_init(&vm);
|
|
|
|
gst_stl_load(&vm);
|
|
|
|
|
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
// if (argc > 1) {
|
|
|
|
// filename = argv[1];
|
|
|
|
//} else {
|
|
|
|
filename = "libs/stl.gst";
|
|
|
|
//}
|
|
|
|
|
|
|
|
FILE *f = fopen(filename, "rb");
|
|
|
|
status = debug_run(&vm, f);
|
|
|
|
|
|
|
|
gst_deinit(&vm);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|