From: nishi Date: Wed, 24 Apr 2024 00:30:01 +0000 (+0000) Subject: eval works X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=beba70806374ca287896f9d7c085455201f4390e;p=serenade.git eval works git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@27 0f02c867-ac3d-714e-8a88-971ba1f6efcf --- diff --git a/Serenade/interpreter.c b/Serenade/interpreter.c index 855c970..2d3f5a4 100644 --- a/Serenade/interpreter.c +++ b/Serenade/interpreter.c @@ -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; } diff --git a/Serenade/run.c b/Serenade/run.c index ec74952..3eae2a7 100644 --- a/Serenade/run.c +++ b/Serenade/run.c @@ -32,6 +32,7 @@ #include "util.h" +#include #include #include #include @@ -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;