#include "util.h"
#include <stdbool.h>
+#include <stdio.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;
+ gen->type = SN_TYPE_DOUBLE;
+ gen->number = 0;
+ if(strcmp(gens[0]->name, "*") == 0) gen->number = 1;
+ int i;
+ for(i = 1; i < args; i++) {
+ if(gens[i]->type == SN_TYPE_DOUBLE) {
+ if(strcmp(gens[0]->name, "*") == 0) {
+ gen->number *= gens[i]->number;
+ } else if(strcmp(gens[0]->name, "+") == 0) {
+ gen->number += gens[i]->number;
+ } else if(strcmp(gens[0]->name, "-") == 0) {
+ if(i == 1) {
+ gen->number += gens[i]->number;
+ } else {
+ gen->number -= gens[i]->number;
+ }
+ } else if(strcmp(gens[0]->name, "/") == 0) {
+ if(i == 1) {
+ gen->number += gens[i]->number;
+ } else {
+ gen->number /= gens[i]->number;
+ }
+ }
+ }
+ }
+ return gen;
+}
+
+struct sn_generic* print_handler(struct sn_interpreter* sn, int args, struct sn_generic** gens) {
+ struct sn_generic* gen = malloc(sizeof(struct sn_generic));
+ int i;
+ for(i = 1; i < args; i++) {
+ if(i > 1) printf(" ");
+ fflush(stdout);
+ if(gens[i]->type == SN_TYPE_DOUBLE) {
+ printf("%f", gens[i]->number);
+ } else if(gens[i]->type == SN_TYPE_STRING) {
+ fwrite(gens[i]->string, 1, gens[i]->string_length, stdout);
+ } else if(gens[i]->type == SN_TYPE_VOID) {
+ printf("<void>");
+ }
+ fflush(stdout);
+ }
+ printf("\n");
+ gen->type = SN_TYPE_FUNCTION;
+ gen->name = sn_strdup("print");
return gen;
}
sn_set_handler(sn, "-", math_handler);
sn_set_handler(sn, "*", math_handler);
sn_set_handler(sn, "/", math_handler);
+ sn_set_handler(sn, "print", print_handler);
return sn;
}
op = _sn_run(sn, op);
}
int j;
- for(j = 1; gen->tree->args[j]; j++)
+ for(j = 0; gen->tree->args[j]; j++)
;
- struct sn_generic** args = malloc(sizeof(struct sn_generic*) * (j - 1));
- int argc = j - 1;
- for(j = 1; gen->tree->args[j]; j++) {
- args[j - 1] = _sn_run(sn, gen->tree->args[j]);
+ struct sn_generic** args = malloc(sizeof(struct sn_generic*) * (j));
+ int argc = j;
+ for(j = 0; gen->tree->args[j]; j++) {
+ args[j] = _sn_run(sn, gen->tree->args[j]);
}
struct sn_generic* r = malloc(sizeof(struct sn_generic));
free(args);
return NULL;
} else {
+ for(j = 0; sn->variables[j] != NULL; j++) {
+ if(strcmp(sn->variables[j]->key, op->name) == 0) {
+ struct sn_generic* op_result = NULL;
+ if(sn->variables[j]->handler != NULL) {
+ op_result = sn->variables[j]->handler(sn, argc, args);
+ } else {
+ struct sn_generic* build = malloc(sizeof(struct sn_generic));
+ build->tree = malloc(sizeof(struct sn_tree));
+ build->tree->args = args;
+ build->tree->args[0] = sn->variables[j]->value;
+ op_result = _sn_run(sn, build);
+ free(build->tree);
+ }
+ if(op_result != NULL) {
+ free(r);
+ r = op_result;
+ }
+ break;
+ }
+ }
}
free(args);
return r;