about summary refs log tree commit diff stats
path: root/cmds.c
blob: 3b44110b521f3fdcc86eaf9e3563327faebf7dc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "state.h"
#include "cmds.h"
#include <err.h>
#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]);
  return stack;
}

stack ffloor(stack stack) {
  stack.val[stack.count-1] = floor(stack.val[stack.count-1]);
  return stack;
}

stack flogB(stack stack) {
  stack.val[stack.count-2] = log2(stack.val[stack.count-2])/log2(stack.val[stack.count-1]);
  stack.val[--stack.count] = 0;
  return stack;
}

stack fnegate(stack stack) {
  stack.val[stack.count-1] *= -1;
  return stack;
}

stack fpop(stack stack) {
  stack.val[--stack.count] = 0;
  return stack;
}

command CMD_LIST[] = {
  {"p", &fpop, "pop last element", 1},
  {"ceil", &fceil, "truncate to the next integer", 1},
  {"floor", &ffloor, "truncate to the previous integer", 1},
  {"log", &flogB, "calculate logarithm in base LAST_NUM of LAST_NUM-1", 2},
  {"neg", &fnegate, "change last element's sign", 1},
  {0, 0, 0, 0}
};

void init_state(state *s) {
  int numel = 0;
  for (int i = 0; CMD_LIST[i].name != 0; i++) numel = i;
  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) {
      if (s->sorted[i].required_args > s->stk.count) {
	fprintf(s->defout,"this function requires %d arguments\n", s->sorted[i].required_args);
	return;
      }
      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);
}