2017-03-10 05:09:42 +00:00
|
|
|
/* This implements a standard library in gst. Some of this
|
2017-03-08 22:34:25 +00:00
|
|
|
* will eventually be ported over to gst if possible */
|
2017-03-16 00:56:37 +00:00
|
|
|
#include <gst/stl.h>
|
|
|
|
#include <gst/gst.h>
|
2017-03-08 22:34:25 +00:00
|
|
|
|
|
|
|
/****/
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Core */
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Print values for inspection */
|
2017-03-09 18:49:46 +00:00
|
|
|
int gst_stl_print(Gst *vm) {
|
2017-03-08 22:34:25 +00:00
|
|
|
uint32_t j, count;
|
|
|
|
count = gst_count_args(vm);
|
|
|
|
for (j = 0; j < count; ++j) {
|
2017-03-09 18:49:46 +00:00
|
|
|
uint32_t i;
|
|
|
|
uint8_t *string = gst_to_string(vm, gst_arg(vm, j));
|
|
|
|
uint32_t len = gst_string_length(string);
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
fputc(string[i], stdout);
|
2017-03-08 22:34:25 +00:00
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
|
|
|
return GST_RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Get class value */
|
|
|
|
int gst_stl_getclass(Gst *vm) {
|
2017-03-10 05:17:34 +00:00
|
|
|
GstValue class = gst_get_class(gst_arg(vm, 0));
|
|
|
|
gst_c_return(vm, class);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set class value */
|
|
|
|
int gst_stl_setclass(Gst *vm) {
|
2017-03-10 05:17:34 +00:00
|
|
|
GstValue x = gst_arg(vm, 0);
|
|
|
|
GstValue class = gst_arg(vm, 1);
|
|
|
|
const char *err = gst_set_class(x, class);
|
|
|
|
if (err != NULL)
|
|
|
|
gst_c_throwc(vm, err);
|
|
|
|
gst_c_return(vm, x);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 05:09:42 +00:00
|
|
|
/* Call a function */
|
|
|
|
int gst_stl_callforeach(Gst *vm) {
|
2017-03-10 05:17:34 +00:00
|
|
|
GstValue func = gst_arg(vm, 0);
|
|
|
|
uint32_t argCount = gst_count_args(vm);
|
|
|
|
uint32_t i;
|
|
|
|
if (argCount) {
|
2017-03-12 22:23:27 +00:00
|
|
|
for (i = 1; i < argCount; ++i)
|
|
|
|
gst_call(vm, func, 1, vm->thread->data + vm->thread->count + i);
|
2017-03-14 19:55:50 +00:00
|
|
|
vm->ret.type = GST_NIL;
|
2017-03-10 05:17:34 +00:00
|
|
|
return GST_RETURN_OK;
|
2017-03-10 05:09:42 +00:00
|
|
|
} else {
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_c_throwc(vm, "expected at least one argument");
|
2017-03-10 05:09:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit */
|
|
|
|
int gst_stl_exit(Gst *vm) {
|
|
|
|
int ret;
|
2017-03-10 05:17:34 +00:00
|
|
|
GstValue exitValue = gst_arg(vm, 0);
|
|
|
|
ret = (exitValue.type == GST_NUMBER) ? exitValue.data.number : 0;
|
|
|
|
exit(ret);
|
|
|
|
return GST_RETURN_OK;
|
2017-03-10 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Load core */
|
|
|
|
void gst_stl_load_core(GstCompiler *c) {
|
|
|
|
gst_compiler_add_global_cfunction(c, "print", gst_stl_print);
|
|
|
|
gst_compiler_add_global_cfunction(c, "get-class", gst_stl_getclass);
|
|
|
|
gst_compiler_add_global_cfunction(c, "set-class", gst_stl_setclass);
|
2017-03-10 05:09:42 +00:00
|
|
|
gst_compiler_add_global_cfunction(c, "call-for-each", gst_stl_callforeach);
|
|
|
|
gst_compiler_add_global_cfunction(c, "exit", gst_stl_exit);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
|
|
|
/* Parsing */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Parse a source string into an AST */
|
|
|
|
int gst_stl_parse(Gst *vm) {
|
2017-03-10 05:17:34 +00:00
|
|
|
uint8_t *source = gst_to_string(vm, gst_arg(vm, 0));
|
|
|
|
GstParser p;
|
2017-03-08 22:34:25 +00:00
|
|
|
/* init state */
|
|
|
|
gst_parser(&p, vm);
|
|
|
|
|
|
|
|
/* Get and parse input until we have a full form */
|
|
|
|
gst_parse_string(&p, source);
|
2017-03-10 05:17:34 +00:00
|
|
|
if (p.status == GST_PARSER_PENDING) {
|
|
|
|
gst_c_throwc(vm, "incomplete source");
|
|
|
|
} else if (p.status == GST_PARSER_ERROR) {
|
2017-03-08 22:34:25 +00:00
|
|
|
gst_c_throwc(vm, p.error);
|
2017-03-10 05:17:34 +00:00
|
|
|
} else {
|
|
|
|
gst_c_return(vm, p.value);
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Load parsing */
|
|
|
|
void gst_stl_load_parse(GstCompiler *c) {
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_compiler_add_global_cfunction(c, "parse", gst_stl_parse);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
|
|
|
/* Compiling */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Compile an ast */
|
|
|
|
int gst_stl_compile(Gst *vm) {
|
|
|
|
GstValue ast = gst_arg(vm, 0);
|
|
|
|
GstValue env = gst_arg(vm, 1);
|
|
|
|
GstValue ret;
|
|
|
|
GstCompiler c;
|
|
|
|
/* init state */
|
2017-03-14 23:13:17 +00:00
|
|
|
if (env.type == GST_NIL) {
|
|
|
|
env = vm->rootenv;
|
2017-03-10 05:17:34 +00:00
|
|
|
}
|
2017-03-14 23:13:17 +00:00
|
|
|
gst_compiler(&c, vm);
|
|
|
|
gst_compiler_env(&c, env);
|
2017-03-10 05:17:34 +00:00
|
|
|
/* Prepare return value */
|
|
|
|
ret.type = GST_FUNCTION;
|
2017-03-08 22:34:25 +00:00
|
|
|
ret.data.function = gst_compiler_compile(&c, ast);
|
|
|
|
/* Check for errors */
|
2017-03-09 18:49:46 +00:00
|
|
|
if (c.error == NULL) {
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_c_return(vm, ret);
|
2017-03-08 22:34:25 +00:00
|
|
|
} else {
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_c_throwc(vm, c.error);
|
2017-03-08 22:34:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Load compilation */
|
|
|
|
void gst_stl_load_compile(GstCompiler *c) {
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_compiler_add_global_cfunction(c, "compile", gst_stl_compile);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 22:34:25 +00:00
|
|
|
/****/
|
|
|
|
/* IO */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* TODO - add userdata to allow for manipulation of FILE pointers. */
|
2017-03-09 18:49:46 +00:00
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Bootstraping */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/* Load all libraries */
|
|
|
|
void gst_stl_load(GstCompiler *c) {
|
2017-03-10 05:17:34 +00:00
|
|
|
gst_stl_load_core(c);
|
|
|
|
gst_stl_load_parse(c);
|
|
|
|
gst_stl_load_compile(c);
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|