-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.c
More file actions
406 lines (335 loc) · 10.1 KB
/
repl.c
File metadata and controls
406 lines (335 loc) · 10.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
// modes
#define NORMAL 0
#define READ_DEF 1
#define NO_IF 0
#define NOT_SKIPPING 1
#define SKIPPING 2
int mode = NORMAL;
int if_mode = NO_IF;
// variables for stack
int stack[1024];
int stack_pos = -1;
// variables for getting input
FILE *current_input;
char buf[BUFSIZ];
const int WORD_SIZE = 256;
char current_word[256];
char current_string[256];
char* buf_pos = NULL;
const char const *START_STRING = "s\"";
struct word_list {
char *word;
struct word_list *next;
};
struct def_list {
struct word_list *def;
struct def_list *next;
};
struct word_list *current_list = NULL;
struct word_list *end_current_list = NULL;
struct def_list *defs_list = NULL;
struct def_list *end_defs_list = NULL;
void get_next_word();
void read_string();
void append_word_to_current_def(const char *string);
void append_current_def();
void print_defs();
void eval_word(const char *word);
struct word_list *find_def(const char *word);
void eval_def(struct word_list *user_def);
int main()
{
*buf = (char) NULL;
buf_pos = buf;
get_next_word ();
while (*current_word != (char) NULL) {
printf ("current_word %s\n", current_word);
if (!strcmp(current_word, ":")) { // starting to read def
if (READ_DEF == mode) {
printf ("Already reading a def.\n");
}
else {
current_list = NULL;
end_current_list = NULL;
mode = READ_DEF;
}
}
else if (!strcmp(current_word, ";")) { // ending reading of def
if (READ_DEF != mode) {
printf ("Not reading a def.\n");
}
else {
append_current_def ();
print_defs ();
mode = NORMAL;
}
}
else if (!strcmp(current_word, START_STRING)) {
read_string ();
printf ("read string: %s\n", current_string);
if (NORMAL == mode) { // push string address
stack_pos++;
stack[stack_pos] = current_string;
}
else if (READ_DEF == mode) {
append_word_to_current_def (START_STRING);
append_word_to_current_def (current_string);
}
}
else if (READ_DEF == mode) {
printf ("read_def mode: %s\n", current_word);
append_word_to_current_def (current_word);
}
else if (NORMAL == mode) { // for now only print word in normal mode
printf ("normal mode: %s\n", current_word);
eval_word (current_word);
}
get_next_word ();
}
return 0;
}
struct word_list *find_def(const char *word) {
struct def_list *temp;
for (temp = defs_list; NULL != temp; temp = temp->next) {
if (!strcmp(temp->def->word, word)) {
return temp->def;
}
}
return NULL;
}
void eval_def(struct word_list *user_def) {
struct word_list *temp = user_def;
struct word_list *user_word;
clock_t start = clock ();
printf ("Eval def: ");
while (NULL != temp) {
printf ("%s ", temp->word);
temp = temp->next;
}
printf ("\n");
temp = user_def;
if (temp != NULL) {
temp = temp->next;
}
while (NULL != temp) {
user_word = find_def(temp->word);
if (NULL != user_word) printf ("Evaling user word %s.\n", user_word->word);
else printf ("Evaling word %s.\n", temp->word);
eval_word (temp->word);
if (NULL != user_word) printf ("Done evaling user word, next %s.\n", temp->next->word);
temp = temp->next;
}
printf ("\n");
printf ("Done evaling %s, %d ms passed.\n", user_def->word, clock () - start);
}
void eval_word(const char *word) {
int temp, old_if_mode;
struct word_list *user_def;
printf ("eval_word got %s\n", word);
if (!strcmp(word, "IF")) {
stack_pos--;
if (stack[stack_pos + 1] > 0) {
if_mode = NOT_SKIPPING;
}
else {
if_mode = SKIPPING;
}
}
else if (!strcmp(word, "ELSE")) {
if (NOT_SKIPPING == if_mode) {
if_mode = SKIPPING;
}
else {
if_mode = NOT_SKIPPING;
}
}
else if (!strcmp(word, "ENDIF")) {
if_mode = NO_IF;
}
else if (SKIPPING == if_mode) {
printf ("skipping %s\n", word);
}
else if (!strcmp(word, "<")) {
stack[stack_pos - 1] = (stack[stack_pos] < stack[stack_pos - 1]) ? 1 : 0;
stack_pos--;
}
else if (!strcmp(word, "<=")) {
stack[stack_pos - 1] = (stack[stack_pos] <= stack[stack_pos - 1]) ? 1 : 0;
stack_pos--;
}
else if (!strcmp(word, "==")) {
stack[stack_pos - 1] = (stack[stack_pos - 1] == stack[stack_pos]) ? 1 : 0;
stack_pos--;
}
else if (!strcmp(word, "!=")) {
stack[stack_pos - 1] = (stack[stack_pos - 1] == stack[stack_pos]) ? 0 : 1;
stack_pos--;
}
else if (!strcmp(word, "pop")) {
printf ("pop: %d\n", stack[stack_pos]);
stack_pos--;
}
else if (!strcmp(word, "over")) {
stack_pos++;
stack[stack_pos] = stack[stack_pos - 2];
}
else if (!strcmp(word, "swap")) {
temp = stack[stack_pos];
stack[stack_pos] = stack[stack_pos - 1];
stack[stack_pos - 1] = temp;
}
else if (!strcmp(word, ".")) {
for (temp = 0; temp <= stack_pos; temp++) {
printf ("%d ", stack[temp]);
}
printf ("\n");
}
else if (!strcmp(word, "s.")) {
printf ("%s\n", (char *)stack[stack_pos]);
}
else if (!strcmp(word, "dup")) {
stack_pos++;
stack[stack_pos] = stack[stack_pos - 1];
}
else if (!strcmp(word, "2dup")) {
stack_pos += 2;
stack[stack_pos] = stack[stack_pos - 2];
stack[stack_pos - 1] = stack[stack_pos - 3];
}
else if (!strcmp(word, "+")) {
printf ("%d + %d\n", stack[stack_pos], stack[stack_pos - 1]);
stack[stack_pos - 1] += stack[stack_pos];
stack_pos--;
}
else if (!strcmp(word, "*")) {
stack[stack_pos - 1] *= stack[stack_pos];
stack_pos--;
}
else if (!strcmp(word, "-")) {
printf ("%d - %d\n", stack[stack_pos], stack[stack_pos - 1]);
stack[stack_pos - 1] = stack[stack_pos] - stack[stack_pos - 1];
stack_pos--;
}
else if (!strcmp(word, "/")) {
stack[stack_pos - 1] = stack[stack_pos] / stack[stack_pos - 1];
stack_pos--;
}
else if (!strcmp(word, "%")) {
stack[stack_pos - 1] = stack[stack_pos] % stack[stack_pos - 1];
stack_pos--;
}
else {
if (push_if_number (word))
printf ("push %d\n", stack[stack_pos]);
else {
user_def = find_def(word);
if (NULL != user_def) {
old_if_mode = if_mode;
if_mode = NO_IF;
eval_def (user_def);
if_mode = old_if_mode;
}
else {
printf ("Unknown word: %s\n", word);
}
}
}
}
int push_if_number(const char* word) {
int num;
int num_read = sscanf (word, "%d", &num);
if (1 == num_read) {
stack_pos++;
stack[stack_pos] = num;
return 1;
}
else
return 0;
}
void print_defs() {
struct def_list *temp;
struct word_list *words;
if (NULL == defs_list) {
printf ("No defs.\n");
}
else {
printf ("Defs:\n");
temp = defs_list;
while (NULL != temp) {
words = temp->def;
while (NULL != words) {
printf ("%s ", words->word);
words = words->next;
}
printf ("\n");
temp = temp->next;
}
}
}
void append_current_def() {
struct def_list *new_node = (struct def_list *) malloc (sizeof (struct def_list));
new_node->def = current_list;
new_node->next = NULL;
if (NULL == defs_list) {
defs_list = new_node;
end_defs_list = new_node;
}
else {
end_defs_list->next = new_node;
end_defs_list = new_node;
}
}
void append_word_to_current_def(const char *string) {
struct word_list *new_node = (struct word_list *) malloc (sizeof (struct word_list));
new_node->word = (char *) malloc (sizeof (string) + 1);
strcpy (new_node->word, string);
new_node->next = NULL;
if (NULL == current_list) {
current_list = new_node;
end_current_list = new_node;
}
else {
end_current_list->next = new_node;
end_current_list = new_node;
}
printf ("new word: %s\n", end_current_list->word);
}
void get_next_word() {
char *p;
int i;
while (*buf_pos != (char) NULL && isspace(*buf_pos)) buf_pos++; // skip all whitespace
// while at end of input buffer, read line into buf and skip whitespace
while (*buf_pos == (char) NULL) {
printf (">");
if (fgets(buf, BUFSIZ, stdin) != NULL)
{
if ((p = strchr(buf, '\n')) != NULL) *p = '\0'; // overwrite trailing newline char
buf_pos = buf; // reset current position
while (*buf_pos != (char) NULL && isspace(*buf_pos)) buf_pos++; // skip all whitespace
}
else
return;
}
// copy up to WORD_SIZE - 1 non-null, non-space characters into current_word and append NULL
for (i = 0; i < WORD_SIZE - 1 && *buf_pos != (char) NULL && !isspace(*buf_pos); i++, buf_pos++) {
current_word[i] = *buf_pos;
}
current_word[i] = (char) NULL;
}
void read_string() {
int i;
if (*buf_pos != (char) NULL && isspace(*buf_pos)) buf_pos++; // skip first whitespace
// read only up to end of buffer/line or first NULL or first " (not \")
for (i = 0;
i < WORD_SIZE - 1 && *buf_pos != (char) NULL && !('"' == *buf_pos && '\\' != *(buf_pos - 1));
i++, buf_pos++) {
current_string[i] = *buf_pos;
}
current_string[i] = (char) NULL;
if ('"' == *buf_pos) buf_pos++; // skip over last "
}