2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 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.
|
|
|
|
*/
|
|
|
|
|
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-02-09 20:02:59 +00:00
|
|
|
|
2017-04-19 16:56:29 +00:00
|
|
|
/* Use readline support for now */
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
2017-04-18 02:14:35 +00:00
|
|
|
/* Compile and run an ast */
|
2017-04-18 20:55:03 +00:00
|
|
|
int debug_compile_and_run(Gst *vm, GstValue ast, GstValue env) {
|
2017-04-18 02:14:35 +00:00
|
|
|
GstCompiler c;
|
|
|
|
GstValue func;
|
|
|
|
/* Try to compile generated AST */
|
|
|
|
gst_compiler(&c, vm);
|
|
|
|
gst_compiler_usemodule(&c, "std");
|
2017-04-30 15:15:44 +00:00
|
|
|
gst_compiler_usemodule(&c, "std.io");
|
2017-04-18 20:55:03 +00:00
|
|
|
gst_compiler_globals(&c, env);
|
2017-04-18 02:14:35 +00:00
|
|
|
func = gst_wrap_function(gst_compiler_compile(&c, ast));
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
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-04-18 02:14:35 +00:00
|
|
|
GstValue ast;
|
|
|
|
GstValue *tup;
|
|
|
|
GstArray *arr;
|
2017-02-16 02:02:00 +00:00
|
|
|
GstParser p;
|
2017-04-18 02:14:35 +00:00
|
|
|
/* Init parser */
|
2017-04-17 04:15:18 +00:00
|
|
|
gst_parser(&p, vm);
|
|
|
|
/* Create do struct */
|
2017-04-18 02:14:35 +00:00
|
|
|
arr = gst_array(vm, 10);
|
2017-04-17 04:15:18 +00:00
|
|
|
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;
|
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));
|
|
|
|
}
|
|
|
|
/* Turn array into tuple */
|
2017-04-18 02:14:35 +00:00
|
|
|
tup = gst_tuple_begin(vm, arr->count);
|
2017-04-17 04:15:18 +00:00
|
|
|
gst_memcpy(tup, arr->data, arr->count * sizeof(GstValue));
|
2017-04-18 02:14:35 +00:00
|
|
|
ast = gst_wrap_tuple(gst_tuple_end(vm, tup));
|
2017-04-17 04:15:18 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
2017-04-18 20:55:03 +00:00
|
|
|
return debug_compile_and_run(vm, ast, gst_wrap_nil());
|
2017-04-18 02:14:35 +00:00
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
|
2017-04-18 02:14:35 +00:00
|
|
|
/* A simple repl */
|
|
|
|
int debug_repl(Gst *vm) {
|
2017-04-19 16:56:29 +00:00
|
|
|
const char *buffer, *reader;
|
2017-04-18 02:14:35 +00:00
|
|
|
GstParser p;
|
2017-04-18 20:55:03 +00:00
|
|
|
GstValue *st;
|
2017-04-18 02:14:35 +00:00
|
|
|
for (;;) {
|
|
|
|
/* Init parser */
|
|
|
|
gst_parser(&p, vm);
|
2017-04-19 16:56:29 +00:00
|
|
|
buffer = reader = NULL;
|
2017-04-18 02:14:35 +00:00
|
|
|
while (p.status != GST_PARSER_ERROR && p.status != GST_PARSER_FULL) {
|
2017-04-19 16:56:29 +00:00
|
|
|
gst_parse_cstring(&p, "\n");
|
|
|
|
if (p.status == GST_PARSER_ERROR || p.status == GST_PARSER_FULL)
|
|
|
|
break;
|
|
|
|
if (!reader || *reader == '\0') {
|
|
|
|
buffer = readline(">> ");
|
|
|
|
add_history(buffer);
|
2017-04-18 02:14:35 +00:00
|
|
|
reader = buffer;
|
|
|
|
}
|
|
|
|
reader += gst_parse_cstring(&p, reader);
|
|
|
|
}
|
|
|
|
/* Check if file read in correctly */
|
|
|
|
if (p.error) {
|
|
|
|
printf("Parse error: %s\n", p.error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Check that parser is complete */
|
|
|
|
if (p.status != GST_PARSER_FULL && p.status != GST_PARSER_ROOT) {
|
|
|
|
printf("Unexpected end of source\n");
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-18 20:55:03 +00:00
|
|
|
/* Add _ to environemt */
|
|
|
|
st = gst_struct_begin(vm, 1);
|
|
|
|
gst_struct_put(st, gst_string_cv(vm, "_"), vm->ret);
|
2017-04-19 13:02:12 +00:00
|
|
|
if (!debug_compile_and_run(vm, gst_parse_consume(&p), gst_wrap_struct(gst_struct_end(vm, st)))) {
|
2017-04-18 02:14:35 +00:00
|
|
|
printf("%s\n", gst_to_string(vm, vm->ret));
|
2017-04-17 04:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
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;
|
2017-04-18 02:14:35 +00:00
|
|
|
|
2017-04-17 04:15:18 +00:00
|
|
|
gst_init(&vm);
|
|
|
|
gst_stl_load(&vm);
|
2017-04-18 02:14:35 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
const char *filename;
|
|
|
|
FILE *f;
|
|
|
|
filename = argv[1];
|
|
|
|
f = fopen(filename, "rb");
|
|
|
|
status = debug_run(&vm, f);
|
|
|
|
} else {
|
|
|
|
status = debug_repl(&vm);
|
|
|
|
}
|
2017-04-17 04:15:18 +00:00
|
|
|
|
|
|
|
gst_deinit(&vm);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|