From: nishi Date: Sun, 5 May 2024 16:46:44 +0000 (+0000) Subject: if works X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=5c5866fc3051716e993915fa3f8ee34ce4343158;p=serenade.git if works git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@111 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index 4cc8375..d47d6a6 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -119,23 +119,23 @@ struct sn_generic* condn_handler(struct sn_interpreter* sn, int args, struct sn_ gen->number = -1; } else { int i; - for(i = 1; i < args; i++){ - if(gens[i]->type != SN_TYPE_DOUBLE){ + for(i = 1; i < args; i++) { + if(gens[i]->type != SN_TYPE_DOUBLE) { gen->number = -1; return gen; } } if(strcmp(gens[0]->name, "or") == 0) { - for(i = 1; i < args; i++){ - if(gens[i]->number != 0){ + for(i = 1; i < args; i++) { + if(gens[i]->number != 0) { gen->number = 1; break; } } - }else if(strcmp(gens[0]->name, "and") == 0) { + } else if(strcmp(gens[0]->name, "and") == 0) { gen->number = 1; - for(i = 1; i < args; i++){ - if(gens[i]->number == 0){ + for(i = 1; i < args; i++) { + if(gens[i]->number == 0) { gen->number = 0; break; } diff --git a/Serenade/run.c b/Serenade/run.c index 670a508..a32031e 100644 --- a/Serenade/run.c +++ b/Serenade/run.c @@ -146,6 +146,47 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) { goto do_loop; } return r; + } else if(strcmp(op->name, "if") == 0) { + int i; + for(i = 0; gen->tree->args[i] != NULL; i++) + ; + if(i < 2) { + fprintf(stderr, "Insufficient arguments\n"); + free(r); + return NULL; + } + struct sn_interpreter_kv** old_kv = sn->local_variables; + sn->local_variables = malloc(sizeof(struct sn_interpreter_kv*)); + sn->local_variables[0] = NULL; + + struct sn_generic* cond = _sn_run(sn, gen->tree->args[1]); + if(cond == NULL) { + free(r); + return NULL; + } + + free(sn->local_variables); + sn->local_variables = old_kv; + + r->type = SN_TYPE_VOID; + + if(cond->type != SN_TYPE_DOUBLE) { + fprintf(stderr, "Cannot use non-number (Type %d) for loop condition\n", r->type); + free(r); + return NULL; + } else if(cond->number != 0) { + old_kv = sn->local_variables; + sn->local_variables = malloc(sizeof(struct sn_interpreter_kv*)); + sn->local_variables[0] = NULL; + + for(i = 2; gen->tree->args[i] != NULL; i++) { + _sn_run(sn, gen->tree->args[i]); + } + + free(sn->local_variables); + sn->local_variables = old_kv; + } + return r; } bool called = false; int j;