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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
typedef int stackitem;
typedef struct {
int size;
int maxsize;
stackitem* start;
} stack;
stack* newstack() {
stack* s = malloc(sizeof(stack));
s->size = 0;
s->maxsize = 1024;
s->start = malloc(sizeof(stackitem) * s->maxsize);
return s;
}
stackitem pop(stack* s) {
if (s->size > 0) {
s->size = s->size - 1;
stackitem si = s->start[s->size];
return si;
} else {
// tried to pop empty stack
return 0;
}
}
stackitem peek(stack* s) {
if (s->size > 0) {
return s->start[s->size - 1];
} else {
// tried to pop empty stack
return 0;
}
}
void dump(stack* s) {
for (int i = 0; i < s->size; i++) {
printf("%d,", s->start[i]);
}
printf("\n");
}
void push(stack *s, stackitem si) {
// fprintf(stderr, "pushing %d", si);
if (s->size >= s->maxsize) {
fprintf(stderr, "Error Stack Overflow");
exit(1);
} else {
s->start[s->size] = si;
s->size = s->size + 1;
}
}
void add(stack *s) {
int x = pop(s);
int y = pop(s);
// fprintf(stderr, "adding %d + %d",x,y);
push(s, x + y);
}
void sub(stack *s) {
int x = pop(s);
int y = pop(s);
push(s, y - x);
}
void s_dup(stack *s) {
int x = pop(s);
push(s, x);
push(s, x);
}
typedef void (*stackop)(stack *);
typedef struct {
char* word;
bool isscript;
union {
stackop op;
struct {
char* script;
int scriptlen;
};
};
} wordop;
#define OPTABLE_MAX_SIZE 1024
wordop optable[OPTABLE_MAX_SIZE] = {
{"+", false, add},
{"-", false, sub},
{"dup", false, s_dup},
{"dump", false, dump},
};
int optablelen = 4;
const int DEFINED_FUNC_MAX_LENGTH = 1024;
const int WORD_LEN_LIMIT = 255;
int defineop(int starti, char *input) {
// code to be evaluated when function is called
char *funcscript = malloc(sizeof(char) * DEFINED_FUNC_MAX_LENGTH);
int funcscripti = 0;
// name by which the function will be called
char *opcode = malloc(sizeof(char) * WORD_LEN_LIMIT);
int opcodei = 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';
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; //{opcode, true, funcscript};
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;
}
//https://stackoverflow.com/a/58585995
char isnumber(char *text) {
int j;
j = strlen(text);
while(j--) {
if(text[j] >= '0' && text[j] <= '9')
continue;
return 0;
}
return 1;
}
void eval(stack* s, int len, char* line);
void exec(stack *s, char *word) {
if (!strcmp(word, "pop") || !strcmp(word, ".")) {
printf("%d\n", pop(s));
return;
} else if (!strcmp(word, "peek")) {
printf("%d\n", peek(s));
return;
}
wordop* op = getop(word);
if (op) {
if (op->isscript) {
eval(s, op->scriptlen, op->script);
} else {
op->op(s);
}
return;
}
if (isnumber(word)) {
push(s, atoi(word));
return;
}
}
void eval(stack* s, int len, char* line) {
char word[WORD_LEN_LIMIT];
int wordi = 0;
for (int i = 0; i < len; i++) {
// end of input is \n, char after end of input is \0
if (line[i] == '\0') {
return;
}
if (line[i] != ' ' &&
line[i] != '\n' && wordi < WORD_LEN_LIMIT - 1) {
if (line[i] == ':') {
i = defineop(i, line);
wordi = 0;
} else {
word[wordi++] = line[i];
}
} else { // end of word
if (wordi > 0) { // don't exec an empty string
word[wordi] = '\0';
exec(s, word);
}
// start new word
wordi = 0;
}
}
}
int main(int argc, char** argv) {
stack* s = newstack();
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, stdin)) != -1) {
eval(s, len, line);
}
}
|