1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 07:03:16 +00:00

More work on compiler. Start defining more dynamic

module system than originally planned.
This commit is contained in:
Calvin Rose 2017-05-06 12:41:24 -04:00
parent d47ee18b1a
commit 6adc2a5268
3 changed files with 30 additions and 7 deletions

View File

@ -42,6 +42,7 @@ int debug_compile_and_run(Gst *vm, GstValue ast, GstValue last) {
gst_compiler_usemodule(&c, "std.parse"); gst_compiler_usemodule(&c, "std.parse");
gst_compiler_usemodule(&c, "std.compile"); gst_compiler_usemodule(&c, "std.compile");
gst_compiler_global(&c, "_", last); gst_compiler_global(&c, "_", last);
gst_compiler_globals(&c, gst_wrap_table(vm->registry));
func = gst_wrap_function(gst_compiler_compile(&c, ast)); func = gst_wrap_function(gst_compiler_compile(&c, ast));
/* Check for compilation errors */ /* Check for compilation errors */
if (c.error) { if (c.error) {

View File

@ -1120,7 +1120,6 @@ GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form) {
} }
/* Create a scope */ /* Create a scope */
opts.isTail = 1; opts.isTail = 1;
compiler_push_scope(c, 0);
compiler_return(c, compile_value(c, opts, form)); compiler_return(c, compile_value(c, opts, form));
def = compiler_gen_funcdef(c, c->buffer->count, 0); def = compiler_gen_funcdef(c, c->buffer->count, 0);
{ {

View File

@ -508,12 +508,34 @@ int gst_stl_serialize(Gst *vm) {
/* Registry */ /* Registry */
/****/ /****/
int gst_stl_global(Gst *vm) { /* Export a symbol definition to the current namespace. Used to implement
gst_c_return(vm, gst_table_get(vm->registry, gst_arg(vm, 0))); * def */
int gst_stl_export(Gst *vm) {
gst_table_put(vm, vm->registry, gst_arg(vm, 0), gst_arg(vm, 1));
gst_c_return(vm, gst_wrap_nil());
} }
int gst_stl_setglobal(Gst *vm) { /* Get everything in the current namespace */
gst_table_put(vm, vm->registry, gst_arg(vm, 0), gst_arg(vm, 1)); int gst_stl_namespace(Gst *vm) {
gst_c_return(vm, gst_wrap_table(vm->registry));
}
/* Switch to a new namespace */
int gst_stl_namespace_set(Gst *vm) {
GstValue name = gst_arg(vm, 0);
GstValue check;
if (name.type != GST_STRING)
gst_c_throwc(vm, "expected string");
check = gst_table_get(vm->modules, name);
if (check.type == GST_TABLE) {
vm->registry = check.data.table;
} else if (check.type == GST_NIL) {
check = gst_wrap_table(gst_table(vm, 10));
gst_table_put(vm, vm->modules, name, check);
vm->registry = check.data.table;
} else {
gst_c_throwc(vm, "invalid module found");
}
gst_c_return(vm, gst_wrap_nil()); gst_c_return(vm, gst_wrap_nil());
} }
@ -690,8 +712,9 @@ static const GstModuleItem const std_module[] = {
{"next", gst_stl_next}, {"next", gst_stl_next},
{"error", gst_stl_error}, {"error", gst_stl_error},
{"serialize", gst_stl_serialize}, {"serialize", gst_stl_serialize},
{"global", gst_stl_global}, {"export!", gst_stl_export},
{"setglobal!", gst_stl_setglobal}, {"namespace", gst_stl_namespace},
{"namespace-set!", gst_stl_namespace_set},
{"push!", gst_stl_push}, {"push!", gst_stl_push},
{"pop!", gst_stl_pop}, {"pop!", gst_stl_pop},
{"peek", gst_stl_peek}, {"peek", gst_stl_peek},