]> Nishi Git Mirror - serenade.git/commitdiff
scope works
authornishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Sat, 27 Apr 2024 02:12:30 +0000 (02:12 +0000)
committernishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Sat, 27 Apr 2024 02:12:30 +0000 (02:12 +0000)
git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@67 0f02c867-ac3d-714e-8a88-971ba1f6efcf

Serenade/ffi_binding.c
Serenade/interpreter.c
Serenade/run.c

index 13a8323ce52b669ff13417afa5506d94a94f90c5..9ba60e033f5435384986fdc5344f1fc84c6c12aa 100644 (file)
@@ -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]));
index ee9b82a004863d4706aa779ce6a36a22edba8eb0..58c9a30f035540e9b91acb2d89cde8d2fbdef715 100644 (file)
@@ -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;
index 1b3e00840d335225ff18426e591f9b889217fb61..859bd351e41bab07792b545bbe0c31bec7c42ba9 100644 (file)
@@ -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;
 }