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

Serenade/interpreter.c
Serenade/parser.c

index 60faa41d4bee178c5df79f25f8d66bdb3836d6a8..4cc8375311a94f19fcc23be7cf24e7ee0945973a 100644 (file)
@@ -94,6 +94,57 @@ struct sn_generic* cond_handler(struct sn_interpreter* sn, int args, struct sn_g
        return gen;
 }
 
+struct sn_generic* cond1_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) {
+       struct sn_generic* gen = malloc(sizeof(struct sn_generic));
+       gen->type = SN_TYPE_DOUBLE;
+       gen->number = 0;
+       int i;
+       if(args != 2 || !(gens[1]->type == SN_TYPE_DOUBLE)) {
+               gen->number = -1;
+       } else {
+               double a = gens[1]->number;
+               if(strcmp(gens[0]->name, "not") == 0) {
+                       gen->number = a == 0 ? 1 : 0;
+               }
+       }
+       return gen;
+}
+
+struct sn_generic* condn_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) {
+       struct sn_generic* gen = malloc(sizeof(struct sn_generic));
+       gen->type = SN_TYPE_DOUBLE;
+       gen->number = 0;
+       int i;
+       if(args < 2) {
+               gen->number = -1;
+       } else {
+               int i;
+               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){
+                                       gen->number = 1;
+                                       break;
+                               }
+                       }
+               }else if(strcmp(gens[0]->name, "and") == 0) {
+                       gen->number = 1;
+                       for(i = 1; i < args; i++){
+                               if(gens[i]->number == 0){
+                                       gen->number = 0;
+                                       break;
+                               }
+                       }
+               }
+       }
+       return gen;
+}
+
 struct sn_generic* print_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) {
        struct sn_generic* gen = malloc(sizeof(struct sn_generic));
        int i;
@@ -178,6 +229,9 @@ void sn_stdlib_init(struct sn_interpreter* sn) {
        sn_set_handler(sn, ">=", cond_handler);
        sn_set_handler(sn, "<", cond_handler);
        sn_set_handler(sn, ">", cond_handler);
+       sn_set_handler(sn, "not", cond1_handler);
+       sn_set_handler(sn, "or", condn_handler);
+       sn_set_handler(sn, "and", condn_handler);
        sn_set_handler(sn, "print", print_handler);
        sn_set_handler(sn, "eval", eval_handler);
        sn_set_handler(sn, "define-variable", defvar_handler);
index d23a7df23dbd8d50187d6d08e0ddf4d184cd3d7c..930cf3957ad71daee74ee60f814d4fdae8f84c89 100644 (file)
@@ -162,7 +162,7 @@ struct sn_generic* sn_expr_parse(char* data, unsigned long long size) {
                                free(tmp);
 
                                if(argbufmode == SN_TYPE_VOID) {
-                                       if(c == '.' || (c >= '0' && c <= '9')) {
+                                       if(c == '.' || c == '-' || (c >= '0' && c <= '9')) {
                                                argbufmode = SN_TYPE_DOUBLE;
                                        } else if(index_stack[br - 1] == 0) {
                                                argbufmode = SN_TYPE_FUNCTION;