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*));
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;
}
#include "util.h"
+#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
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);
break;
}
}
+ if(!called) {
+ fprintf(stderr, "Undefined reference to the function `%s`\n", op->name);
+ free(args);
+ free(r);
+ return NULL;
+ }
}
free(args);
return r;