diff options
Diffstat (limited to 'cmds.c')
-rw-r--r-- | cmds.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/cmds.c b/cmds.c index 0148c7b..2a88041 100644 --- a/cmds.c +++ b/cmds.c @@ -4,6 +4,7 @@ #include <math.h> #include <stdlib.h> #include <err.h> +#include <string.h> stack fceil(stack stack) { stack.val[stack.count-1] = ceil(stack.val[stack.count-1]); @@ -21,10 +22,26 @@ command CMD_LIST[] = { {0, 0, 0} }; -void initstate(state *s) { +void init_state(state *s) { int numel = 0; for (int i = 0; CMD_LIST[i].name != 0; i++) numel = i; - char *sorted = malloc(sizeof(char *)*numel); + command *sorted = malloc(sizeof(command *)*numel); if (sorted == NULL) err(1, "could not allocate function names"); /* todo: (merge, heap, quick) sort? */ + for (int i = 0; CMD_LIST[i].name != 0; i++) { + sorted[i] = CMD_LIST[i]; + } + s->sorted = sorted; +} + +void exec(char *buf, state *s) { + /* change this to a binary tree search */ + for (int i = 0; s->sorted[i].name != 0; i++) { + if (strcmp(buf, s->sorted[i].name) == 0) { + fprintf(s->defout, "executing function: %s\n", s->sorted[i].name); + s->stk = s->sorted[i].exec(s->stk); + return; + } + } + fprintf(s->defout, "error: function '%s' not found\n", buf); } |