]> Nishi Git Mirror - serenade.git/commitdiff
interpreter
authornishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 13:38:26 +0000 (13:38 +0000)
committernishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 13:38:26 +0000 (13:38 +0000)
git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@21 0f02c867-ac3d-714e-8a88-971ba1f6efcf

Serenade/interpreter.c
Serenade/interpreter.h
Serenade/parser.c
Serenade/run.c

index 5edad85d3d0463842d0c0a9f6740cfbb3a4bd39d..c1b94b089d8c726af8f640fb5b4ac73fa71bf6c3 100644 (file)
 
 #include "interpreter.h"
 
+#include "util.h"
+
+#include <stdbool.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;
+       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->variables[0] = NULL;
+
+       sn_set_handler(sn, "+", math_handler);
+       sn_set_handler(sn, "-", math_handler);
+       sn_set_handler(sn, "*", math_handler);
+       sn_set_handler(sn, "/", math_handler);
+
        return sn;
 }
 
-void sn_interpreter_free(struct sn_interpreter* sn) { free(sn); }
+void sn_interpreter_free(struct sn_interpreter* sn) {
+       int i;
+       for(i = 0; sn->variables[i] != NULL; i++) {
+               free(sn->variables[i]->key);
+               if(sn->variables[i]->value != NULL) sn_generic_free(sn->variables[i]->value);
+               free(sn->variables[i]);
+       }
+       free(sn->variables);
+       free(sn);
+}
+
+void sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen) {
+       int i;
+       bool replaced = false;
+       for(i = 0; sn->variables[i] != NULL; i++) {
+               if(strcmp(sn->variables[i]->key, name) == 0) {
+                       sn_generic_free(sn->variables[i]->value);
+                       sn->variables[i]->value = gen;
+                       replaced = true;
+                       break;
+               }
+       }
+       if(!replaced) {
+               struct sn_interpreter_kv** oldvariables = sn->variables;
+               for(i = 0; oldvariables[i] != NULL; i++)
+                       ;
+               sn->variables = malloc(sizeof(struct sn_interpreter_kv*) * (i + 2));
+               for(i = 0; oldvariables[i] != NULL; i++) {
+                       sn->variables[i] = oldvariables[i];
+               }
+               sn->variables[i] = malloc(sizeof(struct sn_generic));
+               sn->variables[i]->key = sn_strdup(name);
+               sn->variables[i]->value = gen;
+               sn->variables[i]->handler = NULL;
+               sn->variables[i + 1] = NULL;
+       }
+}
+
+void sn_set_handler(struct sn_interpreter* sn, const char* name, struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**)) {
+       int i;
+       bool replaced = false;
+       for(i = 0; sn->variables[i] != NULL; i++) {
+               if(strcmp(sn->variables[i]->key, name) == 0) {
+                       sn_generic_free(sn->variables[i]->value);
+                       sn->variables[i]->handler = handler;
+                       replaced = true;
+                       break;
+               }
+       }
+       if(!replaced) {
+               struct sn_interpreter_kv** oldvariables = sn->variables;
+               for(i = 0; oldvariables[i] != NULL; i++)
+                       ;
+               sn->variables = malloc(sizeof(struct sn_interpreter_kv*) * (i + 2));
+               for(i = 0; oldvariables[i] != NULL; i++) {
+                       sn->variables[i] = oldvariables[i];
+               }
+               sn->variables[i] = malloc(sizeof(struct sn_generic));
+               sn->variables[i]->key = sn_strdup(name);
+               sn->variables[i]->value = NULL;
+               sn->variables[i]->handler = handler;
+               sn->variables[i + 1] = NULL;
+       }
+}
index f52835915d58d8fd32a424135677d155e0ee417b..689ed25f18a933d28e89f95671a13e654386b468 100644 (file)
 
 #include "parser.h"
 
-struct sn_interpreter {};
+struct sn_interpreter {
+       struct sn_interpreter_kv** variables;
+};
+
+struct sn_interpreter_kv {
+       char* key;
+       struct sn_generic* value;
+       struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**);
+};
 
 struct sn_interpreter* sn_create_interpreter(void);
 void sn_interpreter_free(struct sn_interpreter* sn);
+void sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen);
+void sn_set_handler(struct sn_interpreter* sn, const char* name, struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**));
 
 #endif
index 6a89783a2440aafceb8f8d292bee6f4c227cc45d..1e6a7f255e7068d4c734f83d9350a40fbade729c 100644 (file)
@@ -29,6 +29,7 @@
 /* --- END LICENSE --- */
 
 #include "parser.h"
+
 #include "../config.h"
 #include "util.h"
 
index 07a47fe4676c044ab9c4a1afdcd8749ace4fbcb6..d9dd72ab43543e62345778e022e05f48b7ba9679 100644 (file)
@@ -29,6 +29,7 @@
 /* --- END LICENSE --- */
 
 #include "run.h"
+
 #include "util.h"
 
 #include <stddef.h>