about summary refs log tree commit diff stats
path: root/cmds.c
blob: da5720b8e798ad2d2f858abb06d35af79d229c13 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "state.h"
#include "cmds.h"
#include <err.h>
#include <math.h>
#include <stdlib.h>
#include <err.h>
#include <string.h>

#define LAST stack.val[stack.count-1]
#define PREV stack.val[stack.count-2]
#define REDUCE stack.val[--stack.count] = 0
#define NEW stack.val[stack.count++]

stack fceil(stack stack) {
  LAST = ceil(LAST);
  return stack;
}

stack ffloor(stack stack) {
  LAST = floor(LAST);
  return stack;
}

stack flogB(stack stack) {
  PREV = log2(PREV)/log2(LAST);
  REDUCE;
  return stack;
}

stack fnegate(stack stack) {
  LAST *= -1;
  return stack;
}

stack fpop(stack stack) {
  REDUCE;
  return stack;
}

stack fsqrt(stack stack) {
  LAST = sqrt(LAST);
  return stack;
}

stack finv(stack stack) {
  LAST = 1/LAST;
  return stack;
}

stack fpi(stack stack) {
  NEW = M_PI;
  return stack;
}

stack fe(stack stack) {
  NEW = M_E;
  return stack;
}

stack fsin(stack stack) {
  LAST = sin(LAST);
  return stack;
}

stack fcos(stack stack) {
  LAST = cos(LAST);
  return stack;
}

stack fadd(stack stack) {
  PREV = LAST + PREV;
  REDUCE;
  return stack;
}

stack fsub(stack stack) {
  PREV = PREV - LAST;
  REDUCE;
  return stack;
}

stack fmul(stack stack) {
  PREV = PREV*LAST;
  REDUCE;
  return stack;
}

stack fdiv(stack stack) {
  PREV = PREV / LAST;
  REDUCE;
  return stack;
}

stack fpow(stack stack) {
  PREV = pow(PREV, LAST);
  REDUCE;
  return stack;
}

command CMD_LIST[] = {
  {"*", &fmul, "multiply last two numbers", 2},
  {"+", &fadd, "add last two numbers", 2},
  {"-", &fsub, "substract last two numbers", 2},
  {"/", &fdiv, "divide last two numbers", 2},
  {"^", &fpow, "power last two numbers", 2},
  {"ceil", &fceil, "truncate to the next integer", 1},
  {"cos", &fcos, "calculate the cosine", 1},
  {"e", &fe, "return number e", 0},
  {"floor", &ffloor, "truncate to the previous integer", 1},
  {"inv", &finv, "invert last number", 1},
  {"log", &flogB, "calculate logarithm using last element as a base and the previous as the argument", 2},
  {"neg", &fnegate, "change last element's sign", 1},
  {"p", &fpop, "pop last element", 1},
  {"pi", &fpi, "return number pi", 0},
  {"sin", &fsin, "calculate the sine", 1},
  {"sqrt", &fsqrt, "calculate the square root", 1},
};

int compare(const void *s1, const void *s2) {
  char *left = *(char **)s1;
  char *right = *(char **)s2;
  return strcmp(left, right);
}

int search(const void *s1, const void *s2) {
  char *name = *(char **)s1;
  command c = *(command *)s2;
  return (strcmp(name, c.name));
}

void init_state(state *s) {
  int numel = sizeof(CMD_LIST)/sizeof(CMD_LIST[0]);
  qsort(&CMD_LIST, numel, sizeof(CMD_LIST[0]), compare);
  s->numel = numel;
  s->sorted = CMD_LIST;
}

/*
exec tries to find a function name matching the one provided in buf. If it
fails to find one, it returns -2. If it finds one, but number of arguments in
the stack is less than the specified limit, it returns the number of arguments
needed. If it succeeds, it returns -1, in order not to conflict with the integer
returned when an incorrect number of arguments have been passed.
*/
int exec(char *buf, state *s) {
  command *c = bsearch(&buf, s->sorted, s->numel, sizeof(s->sorted[0]), search);
  if (c == NULL) {
    return -2; // not found
  }
  if (c->required_args > s->stk.count) {
    return c->required_args; // return number of required arguments
  }
  fprintf(s->defout, "executing function: %s\n", c->name);
  s->stk = c->exec(s->stk);
  return -1; // no error
}