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
157
158
159
|
#include "optable.h"
#include <string.h>
static void dump(stack* s);
static void not(stack *s);
static void drop(stack *s);
static void over(stack *s);
static void rot(stack *s);
static void swap(stack *s);
static void eq(stack *s);
static void add(stack *s);
static void mult(stack *s);
static void s_div(stack *s);
static void sub(stack *s);
static void dup(stack *s);
static wordop optable[OPTABLE_MAX_SIZE] = {
{"+", false, add},
{"-", false, sub},
{"*", false, mult},
{"/", false, s_div},
{"dup", false, dup},
{"dump", false, dump},
{"not", false, not},
{"=", false, eq},
{"swap", false, swap},
{"drop", false, drop},
{"over", false, over},
{"rot", false, rot},
};
static int optablelen = 12;
int defineop(int starti, char *input) {
// name by which the function will be called
char *opcode = malloc(sizeof(char) * WORD_LEN_LIMIT);
int opcodei = 0;
// code to be evaluated when function is called
char *funcscript = malloc(sizeof(char) * DEFINED_FUNC_MAX_LENGTH);
int funcscripti = 0;
// skip ' ' and ':'
while (input[starti] == ' ' || input[starti] == ':') {
starti++;
}
// get name
while (input[starti] != ' ' && opcodei < WORD_LEN_LIMIT) {
opcode[opcodei++] = input[starti++];
}
opcode[opcodei] = '\0';
// get code
while (input[starti] != ';' && funcscripti < DEFINED_FUNC_MAX_LENGTH) {
funcscript[funcscripti++] = input[starti++];
}
funcscript[funcscripti] = '\0';
// optable bounds check
if (optablelen >= OPTABLE_MAX_SIZE) {
// Error
fprintf(stderr, "Error: optable reached max size, failed to create new user defined operation");
exit(1);
}
// add op to end of table, and increment size
optable[optablelen].word = opcode;
optable[optablelen].isscript = true;
optable[optablelen].script = funcscript;
optable[optablelen].scriptlen = funcscripti;
optablelen++;
return starti;
}
wordop* getop(char *word) {
for (int i = 0; i < optablelen; i++) {
if (!strcmp(optable[i].word, word)) {
return &optable[i];
}
}
return 0;
}
/* Implementations of builtin functions */
static void dump(stack* s) {
for (int i = 0; i < s->size; i++) {
printf("%d,", s->start[i]);
}
printf("\n");
}
static void not(stack *s) {
int x = pop(s);
push(s, !x);
}
static void drop(stack *s) {
pop(s);
}
static void over(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, y);
push(s, x);
push(s, y);
}
static void rot(stack *s) {
int x = pop(s);
int y = pop(s);
int z = pop(s);
push(s, y);
push(s, x);
push(s, z);
}
static void swap(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, x);
push(s, y);
}
static void eq(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, x == y);
}
static void add(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, x + y);
}
static void mult(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, x * y);
}
static void s_div(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, y / x);
}
static void sub(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, y - x);
}
static void dup(stack *s) {
int x = pop(s);
push(s, x);
push(s, x);
}
|