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-09 18:49:46 +00:00
|
|
|
#include "stl.h"
|
2017-03-08 22:34:25 +00:00
|
|
|
#include "gst.h"
|
|
|
|
|
|
|
|
/****/
|
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) {
|
|
|
|
GstValue class = gst_get_class(gst_arg(vm, 0));
|
|
|
|
gst_c_return(vm, class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set class value */
|
|
|
|
int gst_stl_setclass(Gst *vm) {
|
|
|
|
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-10 05:09:42 +00:00
|
|
|
/* Call a function */
|
|
|
|
int gst_stl_callforeach(Gst *vm) {
|
|
|
|
GstValue func = gst_arg(vm, 0);
|
|
|
|
uint32_t argCount = gst_count_args(vm);
|
|
|
|
uint32_t i;
|
|
|
|
if (argCount) {
|
|
|
|
for (i = 0; i < argCount - 1; ++i)
|
|
|
|
gst_call(vm, func, 1, vm->thread->data + vm->thread->count + 1 + i);
|
|
|
|
return GST_RETURN_OK;
|
|
|
|
} else {
|
|
|
|
gst_c_throwc(vm, "expected at least one argument");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit */
|
|
|
|
int gst_stl_exit(Gst *vm) {
|
|
|
|
int ret;
|
|
|
|
GstValue exitValue = gst_arg(vm, 0);
|
|
|
|
ret = (exitValue.type == GST_NUMBER) ? exitValue.data.number : 0;
|
|
|
|
exit(ret);
|
|
|
|
return GST_RETURN_OK;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
uint8_t *source = gst_to_string(vm, gst_arg(vm, 0));
|
|
|
|
GstParser p;
|
|
|
|
/* init state */
|
|
|
|
gst_parser(&p, vm);
|
|
|
|
|
|
|
|
/* Get and parse input until we have a full form */
|
|
|
|
gst_parse_string(&p, source);
|
|
|
|
if (p.status == GST_PARSER_PENDING) {
|
|
|
|
gst_c_throwc(vm, "incomplete source");
|
|
|
|
} else if (p.status == GST_PARSER_ERROR) {
|
|
|
|
gst_c_throwc(vm, p.error);
|
|
|
|
} else {
|
|
|
|
gst_c_return(vm, p.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Load parsing */
|
|
|
|
void gst_stl_load_parse(GstCompiler *c) {
|
|
|
|
gst_compiler_add_global_cfunction(c, "parse", gst_stl_parse);
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
gst_compiler(&c, vm);
|
|
|
|
/* Check for environment variables */
|
|
|
|
if (env.type == GST_OBJECT) {
|
|
|
|
/* Iterate through environment, adding globals */
|
2017-03-09 18:49:46 +00:00
|
|
|
} else if (env.type != GST_NIL) {
|
2017-03-08 22:34:25 +00:00
|
|
|
gst_c_throwc(vm, "invalid type for environment");
|
2017-03-09 18:49:46 +00:00
|
|
|
}
|
2017-03-08 22:34:25 +00:00
|
|
|
/* Prepare return value */
|
|
|
|
ret.type = GST_FUNCTION;
|
|
|
|
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-08 22:34:25 +00:00
|
|
|
gst_c_return(vm, ret);
|
|
|
|
} else {
|
|
|
|
gst_c_throwc(vm, c.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:49:46 +00:00
|
|
|
/* Load compilation */
|
|
|
|
void gst_stl_load_compile(GstCompiler *c) {
|
|
|
|
gst_compiler_add_global_cfunction(c, "compile", gst_stl_compile);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
gst_stl_load_core(c);
|
|
|
|
gst_stl_load_parse(c);
|
|
|
|
gst_stl_load_compile(c);
|
|
|
|
}
|