From 6adc2a526806b5994711b7f2162553a1da3260db Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 6 May 2017 12:41:24 -0400 Subject: [PATCH] More work on compiler. Start defining more dynamic module system than originally planned. --- client/main.c | 1 + core/compile.c | 1 - core/stl.c | 35 +++++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/client/main.c b/client/main.c index af57591d..9bf21a48 100644 --- a/client/main.c +++ b/client/main.c @@ -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.compile"); gst_compiler_global(&c, "_", last); + gst_compiler_globals(&c, gst_wrap_table(vm->registry)); func = gst_wrap_function(gst_compiler_compile(&c, ast)); /* Check for compilation errors */ if (c.error) { diff --git a/core/compile.c b/core/compile.c index 72e2b2a9..407c6fe1 100644 --- a/core/compile.c +++ b/core/compile.c @@ -1120,7 +1120,6 @@ GstFunction *gst_compiler_compile(GstCompiler *c, GstValue form) { } /* Create a scope */ opts.isTail = 1; - compiler_push_scope(c, 0); compiler_return(c, compile_value(c, opts, form)); def = compiler_gen_funcdef(c, c->buffer->count, 0); { diff --git a/core/stl.c b/core/stl.c index 1a242488..1026bc5a 100644 --- a/core/stl.c +++ b/core/stl.c @@ -508,12 +508,34 @@ int gst_stl_serialize(Gst *vm) { /* Registry */ /****/ -int gst_stl_global(Gst *vm) { - gst_c_return(vm, gst_table_get(vm->registry, gst_arg(vm, 0))); +/* Export a symbol definition to the current namespace. Used to implement + * 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) { - gst_table_put(vm, vm->registry, gst_arg(vm, 0), gst_arg(vm, 1)); +/* Get everything in the current namespace */ +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()); } @@ -690,8 +712,9 @@ static const GstModuleItem const std_module[] = { {"next", gst_stl_next}, {"error", gst_stl_error}, {"serialize", gst_stl_serialize}, - {"global", gst_stl_global}, - {"setglobal!", gst_stl_setglobal}, + {"export!", gst_stl_export}, + {"namespace", gst_stl_namespace}, + {"namespace-set!", gst_stl_namespace_set}, {"push!", gst_stl_push}, {"pop!", gst_stl_pop}, {"peek", gst_stl_peek},