free(sn);
}
-void sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen) {
+struct sn_interpreter_kv* 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->variables[i]->value = gen;
replaced = true;
- break;
+ return sn->variables[i];
}
}
if(!replaced) {
sn->variables[i]->handler = NULL;
if(gen->type == SN_TYPE_FUNCTION) sn->variables[i]->handler = gen->handler;
sn->variables[i + 1] = NULL;
+ return sn->variables[i];
}
}
-void sn_set_handler(struct sn_interpreter* sn, const char* name, struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**)) {
+struct sn_interpreter_kv* 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->variables[i]->handler = handler;
replaced = true;
- break;
+ return sn->variables[i];
}
}
if(!replaced) {
sn->variables[i]->value = NULL;
sn->variables[i]->handler = handler;
sn->variables[i + 1] = NULL;
+ return sn->variables[i];
}
}
int i;
for(i = 0; gens[i] != NULL; i++) {
if(r == 0) {
+ sn->local_variables = NULL;
if(sn_run(sn, gens[i]) != 0) {
r = 1;
}
struct sn_interpreter {
struct sn_interpreter_kv** variables;
+ struct sn_interpreter_kv** local_variables;
struct sn_generic*** generics;
};
struct sn_interpreter_kv {
char* key;
struct sn_generic* value;
+ void* argvalue;
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**));
+struct sn_interpreter_kv* sn_set_variable(struct sn_interpreter* sn, const char* name, struct sn_generic* gen);
+struct sn_interpreter_kv* sn_set_handler(struct sn_interpreter* sn, const char* name, struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**));
int sn_eval(struct sn_interpreter* sn, char* data, unsigned long long len);
void sn_stdlib_init(struct sn_interpreter* sn);
void sn_ffi_init(struct sn_interpreter* sn);
char* name;
struct sn_tree* tree;
bool used;
+ void* argvalue;
struct sn_generic* (*handler)(struct sn_interpreter* sn, int, struct sn_generic**);
};
#include <stdlib.h>
#include <string.h>
+struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen);
+
+struct sn_generic* run_code(struct sn_interpreter* sn, int argc, struct sn_generic** args){
+ struct sn_generic* r = malloc(sizeof(struct sn_generic));
+ r->type = SN_TYPE_VOID;
+ struct sn_generic** gens = (struct sn_generic**)args[0]->argvalue;
+ int i;
+ for(i = 0; gens[i] != NULL; i++){
+ _sn_run(sn, gens[i]);
+ }
+ return r;
+}
+
struct sn_generic* _sn_run(struct sn_interpreter* sn, struct sn_generic* gen) {
if(gen->type == SN_TYPE_TREE) {
struct sn_generic* op = gen->tree->args[0];
r->string = sn_strdup(SERENADE_VERSION);
r->string_length = strlen(r->string);
return r;
+ }else if(strcmp(op->name, "define-func") == 0){
+ char* name = malloc(gen->tree->args[1]->string_length + 1);
+ memcpy(name, gen->tree->args[1]->string, gen->tree->args[1]->string_length);
+ name[gen->tree->args[1]->string_length] = 0;
+ struct sn_interpreter_kv* kv = sn_set_handler(sn, name, run_code);
+ kv->argvalue = gen->tree->args + 1;
+ free(name);
+ return r;
}
bool called = false;
int j;
if(sn->variables[j]->value != NULL) {
args[0] = sn->variables[j]->value;
}
+ args[0]->argvalue = sn->variables[j]->argvalue;
op_result = sn->variables[j]->handler(sn, argc, args);
} else {
struct sn_generic* build = malloc(sizeof(struct sn_generic));
} else if(gen->type == SN_TYPE_FUNCTION) {
fprintf(f, "<function %s", gen->name == NULL ? "(anonymous)" : gen->name);
if(gen->name == NULL) {
- fprintf(f, ":%x>", gen->handler);
+ fprintf(f, ":%x", gen->handler);
}
+ fprintf(f, ">");
} else if(gen->type == SN_TYPE_PTR) {
fprintf(f, "<pointer %x>", gen->ptr);
}