]> Nishi Git Mirror - serenade.git/commitdiff
can do math now
authornishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 14:03:25 +0000 (14:03 +0000)
committernishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 14:03:25 +0000 (14:03 +0000)
git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@22 0f02c867-ac3d-714e-8a88-971ba1f6efcf

Serenade/interpreter.c
Serenade/run.c

index c1b94b089d8c726af8f640fb5b4ac73fa71bf6c3..9c042d5a37b85158d904a4515eeab5d50e73f1da 100644 (file)
 #include "util.h"
 
 #include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 struct sn_generic* math_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) {
        struct sn_generic* gen = malloc(sizeof(struct sn_generic));
-       gen->type = SN_TYPE_VOID;
+       gen->type = SN_TYPE_DOUBLE;
+       gen->number = 0;
+       if(strcmp(gens[0]->name, "*") == 0) gen->number = 1;
+       int i;
+       for(i = 1; i < args; i++) {
+               if(gens[i]->type == SN_TYPE_DOUBLE) {
+                       if(strcmp(gens[0]->name, "*") == 0) {
+                               gen->number *= gens[i]->number;
+                       } else if(strcmp(gens[0]->name, "+") == 0) {
+                               gen->number += gens[i]->number;
+                       } else if(strcmp(gens[0]->name, "-") == 0) {
+                               if(i == 1) {
+                                       gen->number += gens[i]->number;
+                               } else {
+                                       gen->number -= gens[i]->number;
+                               }
+                       } else if(strcmp(gens[0]->name, "/") == 0) {
+                               if(i == 1) {
+                                       gen->number += gens[i]->number;
+                               } else {
+                                       gen->number /= gens[i]->number;
+                               }
+                       }
+               }
+       }
+       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;
+       for(i = 1; i < args; i++) {
+               if(i > 1) printf(" ");
+               fflush(stdout);
+               if(gens[i]->type == SN_TYPE_DOUBLE) {
+                       printf("%f", gens[i]->number);
+               } else if(gens[i]->type == SN_TYPE_STRING) {
+                       fwrite(gens[i]->string, 1, gens[i]->string_length, stdout);
+               } else if(gens[i]->type == SN_TYPE_VOID) {
+                       printf("<void>");
+               }
+               fflush(stdout);
+       }
+       printf("\n");
+       gen->type = SN_TYPE_FUNCTION;
+       gen->name = sn_strdup("print");
        return gen;
 }
 
@@ -51,6 +97,7 @@ struct sn_interpreter* sn_create_interpreter(void) {
        sn_set_handler(sn, "-", math_handler);
        sn_set_handler(sn, "*", math_handler);
        sn_set_handler(sn, "/", math_handler);
+       sn_set_handler(sn, "print", print_handler);
 
        return sn;
 }
index d9dd72ab43543e62345778e022e05f48b7ba9679..283051014cad0a117b4788f49814d85b5aab5432 100644 (file)
@@ -44,12 +44,12 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
                        op = _sn_run(sn, op);
                }
                int j;
-               for(j = 1; gen->tree->args[j]; j++)
+               for(j = 0; gen->tree->args[j]; j++)
                        ;
-               struct sn_generic** args = malloc(sizeof(struct sn_generic*) * (j - 1));
-               int argc = j - 1;
-               for(j = 1; gen->tree->args[j]; j++) {
-                       args[j - 1] = _sn_run(sn, gen->tree->args[j]);
+               struct sn_generic** args = malloc(sizeof(struct sn_generic*) * (j));
+               int argc = j;
+               for(j = 0; gen->tree->args[j]; j++) {
+                       args[j] = _sn_run(sn, gen->tree->args[j]);
                }
 
                struct sn_generic* r = malloc(sizeof(struct sn_generic));
@@ -60,6 +60,26 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
                        free(args);
                        return NULL;
                } else {
+                       for(j = 0; sn->variables[j] != NULL; j++) {
+                               if(strcmp(sn->variables[j]->key, op->name) == 0) {
+                                       struct sn_generic* op_result = NULL;
+                                       if(sn->variables[j]->handler != NULL) {
+                                               op_result = sn->variables[j]->handler(sn, argc, args);
+                                       } else {
+                                               struct sn_generic* build = malloc(sizeof(struct sn_generic));
+                                               build->tree = malloc(sizeof(struct sn_tree));
+                                               build->tree->args = args;
+                                               build->tree->args[0] = sn->variables[j]->value;
+                                               op_result = _sn_run(sn, build);
+                                               free(build->tree);
+                                       }
+                                       if(op_result != NULL) {
+                                               free(r);
+                                               r = op_result;
+                                       }
+                                       break;
+                               }
+                       }
                }
                free(args);
                return r;