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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include "optable.h"
#include <stdio.h>
#include <string.h>
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 void popout(stack *s);
static void peekout(stack *s);
static void ifdirective(stack *s, int len, char* line, int* i);
static void defineop(stack *s, int len, char* line, int* i);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
// clang seems to not realise that the union can contain fp types beyond the first
static wordop optable[OPTABLE_MAX_SIZE] = {
{".", builtin, {popout}},
{"peek", builtin, {peekout}},
{"+", builtin, {add}},
{"-", builtin, {sub}},
{"*", builtin, {mult}},
{"/", builtin, {s_div}},
{"dup", builtin, {dup}},
{"not", builtin, {not}},
{"=", builtin, {eq}},
{"swap", builtin, {swap}},
{"drop", builtin, {drop}},
{"over", builtin, {over}},
{"rot", builtin, {rot}},
{"if", directive, {ifdirective}},
{":", directive, {defineop}},
};
static int optablelen = 15;
#pragma clang diagnostic pop
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 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);
}
static void popout(stack *s) {
printf("%d\n", pop(s));
}
static void peekout(stack *s) {
int x = pop(s);
push(s, x);
printf("%d\n", x);
}
/* Directives */
static void ifdirective(stack *s, int len, char* line, int* i) {
stackitem predicate = pop(s);
if (!predicate) {
while (len > *i && !(
line[*i-3] == 't' &&
line[*i-2] == 'h' &&
line[*i-1] == 'e' &&
line[*i] == 'n'
)) {
(*i)++;
}
}
}
/**
* defineop reads a function identifier, followed by the commands to run when the function
* is called, stopping when a semicolon is reached.
* Reading is done from the input string.
*
* returns new position of input index
*
*/
static void defineop(stack* s_IGNORED, int len_IGNORED, char *input, int* starti) {
// value easier to deal with (than pointer)
int i = *starti;
// 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[i] == ' ' || input[i] == ':') {
i++;
}
// get name
while (input[i] != ' ' && opcodei < WORD_LEN_LIMIT) {
opcode[opcodei++] = input[i++];
}
opcode[opcodei] = '\0';
// get code
while (input[i] != ';' && funcscripti < DEFINED_FUNC_MAX_LENGTH) {
funcscript[funcscripti++] = input[i++];
}
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].optype = script;
optable[optablelen].script = funcscript;
optable[optablelen].scriptlen = funcscripti;
optablelen++;
// move read position forwards
*starti = i;
}
|