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

Serenade/interpreter.c
Serenade/run.c

index 855c9701061affe41c0c4138e99c2a61061c3078..2d3f5a42dfe020596ee260ea83b1c4dda53b010e 100644 (file)
@@ -89,6 +89,22 @@ struct sn_generic* print_handler(struct sn_interpreter* sn, int args, struct sn_
        return gen;
 }
 
+struct sn_generic* eval_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) {
+       int i;
+       int result = 0;
+       int r = 0;
+       for(i = 1; i < args; i++) {
+               if(gens[i]->type == SN_TYPE_STRING) {
+                       r = sn_eval(sn, gens[i]->string, gens[i]->string_length);
+                       if(r != 0) result = 1;
+               }
+       }
+       struct sn_generic* gen = malloc(sizeof(struct sn_generic));
+       gen->type = SN_TYPE_DOUBLE;
+       gen->number = result;
+       return gen;
+}
+
 struct sn_interpreter* sn_create_interpreter(void) {
        struct sn_interpreter* sn = malloc(sizeof(struct sn_interpreter));
        sn->variables = malloc(sizeof(struct sn_interpreter_kv*));
@@ -99,6 +115,7 @@ struct sn_interpreter* sn_create_interpreter(void) {
        sn_set_handler(sn, "*", math_handler);
        sn_set_handler(sn, "/", math_handler);
        sn_set_handler(sn, "print", print_handler);
+       sn_set_handler(sn, "eval", eval_handler);
 
        return sn;
 }
index ec74952bfbec163de6236471fd90cb6cb349fef2..3eae2a7c2e770b307371c6418f6e0cb2a6b9a78a 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "util.h"
 
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -58,10 +59,13 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
                if(op->type != SN_TYPE_FUNCTION) {
                        fprintf(stderr, "Cannot call non-function (Type %d)\n", op->type);
                        free(args);
+                       free(r);
                        return NULL;
                } else {
+                       bool called = false;
                        for(j = 0; sn->variables[j] != NULL; j++) {
                                if(strcmp(sn->variables[j]->key, op->name) == 0) {
+                                       called = true;
                                        struct sn_generic* op_result = NULL;
                                        if(sn->variables[j]->handler != NULL) {
                                                op_result = sn->variables[j]->handler(sn, argc, args);
@@ -80,6 +84,12 @@ struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
                                        break;
                                }
                        }
+                       if(!called) {
+                               fprintf(stderr, "Undefined reference to the function `%s`\n", op->name);
+                               free(args);
+                               free(r);
+                               return NULL;
+                       }
                }
                free(args);
                return r;