From 4f810450af0cb18382a67b60222e36dd44c6886a Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 27 Apr 2024 02:12:30 +0000 Subject: [PATCH] scope works git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@67 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- Serenade/ffi_binding.c | 2 +- Serenade/interpreter.c | 36 ++++++++++++++++++++++++++++++++++++ Serenade/run.c | 4 ++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Serenade/ffi_binding.c b/Serenade/ffi_binding.c index 13a8323..9ba60e0 100644 --- a/Serenade/ffi_binding.c +++ b/Serenade/ffi_binding.c @@ -137,7 +137,7 @@ struct sn_generic* function_caller_handler(struct sn_interpreter* sn, int args, } int i; - if(fargs != NULL){ + if(fargs != NULL) { for(i = 0; i < info->argc; i++) { if(strcmp(info->argtypes[i + 1], "string") == 0) { free(*((void**)fargs[i])); diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index ee9b82a..58c9a30 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -192,6 +192,42 @@ void sn_interpreter_free(struct sn_interpreter* sn) { struct sn_interpreter_kv* sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen) { int i; bool replaced = false; + bool global = true; + if(sn->local_variables != NULL) { + int i; + global = false; + for(i = 0; sn->variables[i] != NULL; i++) { + if(strcmp(sn->variables[i]->key, name) == 0) { + global = true; + break; + } + } + } + if(!global) { + for(i = 0; sn->local_variables[i] != NULL; i++) { + if(strcmp(sn->local_variables[i]->key, name) == 0) { + sn->local_variables[i]->value = gen; + replaced = true; + return sn->local_variables[i]; + } + } + if(!replaced) { + struct sn_interpreter_kv** oldvariables = sn->local_variables; + for(i = 0; oldvariables[i] != NULL; i++) + ; + sn->local_variables = malloc(sizeof(struct sn_interpreter_kv*) * (i + 2)); + for(i = 0; oldvariables[i] != NULL; i++) { + sn->local_variables[i] = oldvariables[i]; + } + sn->local_variables[i] = malloc(sizeof(struct sn_generic)); + sn->local_variables[i]->key = sn_strdup(name); + sn->local_variables[i]->value = gen; + sn->local_variables[i]->handler = NULL; + if(gen != NULL && gen->type == SN_TYPE_FUNCTION) sn->local_variables[i]->handler = gen->handler; + sn->local_variables[i + 1] = NULL; + return sn->local_variables[i]; + } + } for(i = 0; sn->variables[i] != NULL; i++) { if(strcmp(sn->variables[i]->key, name) == 0) { sn->variables[i]->value = gen; diff --git a/Serenade/run.c b/Serenade/run.c index 1b3e008..859bd35 100644 --- a/Serenade/run.c +++ b/Serenade/run.c @@ -46,9 +46,13 @@ struct sn_generic* run_code(struct sn_interpreter* sn, int argc, struct sn_gener r->type = SN_TYPE_VOID; struct sn_generic** gens = (struct sn_generic**)args[0]->argvalue; int i; + struct sn_interpreter_kv** old_kv = sn->local_variables; + sn->local_variables = malloc(sizeof(struct sn_interpreter_kv*)); + sn->local_variables[i] = NULL; for(i = 0; gens[i] != NULL; i++) { _sn_run(sn, gens[i]); } + sn->local_variables = old_kv; return r; } -- 2.43.0