]> Nishi Git Mirror - serenade.git/commitdiff
if works
authornishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Sun, 5 May 2024 16:46:44 +0000 (16:46 +0000)
committernishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Sun, 5 May 2024 16:46:44 +0000 (16:46 +0000)
git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@111 0f02c867-ac3d-714e-8a88-971ba1f6efcf

Serenade/interpreter.c
Serenade/run.c

index 4cc8375311a94f19fcc23be7cf24e7ee0945973a..d47d6a69faf9686532c57e3934e40bce65cb5ed3 100644 (file)
@@ -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;
                                }
index 670a508a48fed939ce25e01be67b5a52a7e83089..a32031e3bcb1bb740471314742892883253b0096 100644 (file)
@@ -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;