-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaartforth.cpp
499 lines (461 loc) · 11 KB
/
aartforth.cpp
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <iostream>
#include <string>
#include <stack>
#include <cstring>
#include <stdio.h> // legacy c lib because me stupid
#include <unistd.h>
#include <time.h>
#ifdef _WIN32
#include <conio.h>
#else
#include <ncurses.h>
int kbhit() {
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
#endif
#define var_mem 24576 // 24k var and array mem
#define word_mem 2048 // word mem 16k
std::string strings[16384];
std::string strings_temp[16384];
std::string remq(std::string a) {
std::string str;
for (int o = 0; a[o]!='\"'&&a[o]!='\0'; o++) str = str+a[o];
return str;
}
int len(std::string str) {
int length = 0;
for (int i = 0; str[i] != '\0'; i++) length++;
return length;
}
void split(std::string str, char seperator) {
int currIndex = 0, i = 0;
int startIndex = 0, endIndex = 0;
while (i <= len(str))
{
if (str[i] == seperator || i == len(str)) {
endIndex = i;
std::string subStr = "";
subStr.append(str, startIndex, endIndex - startIndex);
strings[currIndex] = subStr;
currIndex += 1;
startIndex = endIndex + 1;
}
i++;
}
}
int isNumber(std::string text) {
if (text=="-") return 0;
int j;
j = len(text);
while(j--) {
if ((text[j] >= 48 && text[j] <= 48+9) || text[j] == 45) continue;
return 0;
}
return 1;
}
int custom_word = 0;
int word_def_length = 0;
struct word {
std::string name;
int pos;
};
std::stack<int> stack;
std::stack<int> word_ret;
std::stack<int> loop_ret;
std::stack<int> loop_index;
std::stack<int> loop_control;
struct word words[word_mem];
struct word words_temp[word_mem];
std::string var_name[var_mem];
int var_content[var_mem];
int is_array[var_mem];
int var_length = 0;
int pc = 0;
int getVarPos(std::string a) {
for (int i = 0; i < var_mem; i++) if (var_name[i]==a) return i;
return -1;
}
int op(std::string win) {
// exit program
if (win=="bye") {
return 2;
}
// custom words
if (win==":") {
pc++;
struct word word_on_array;
word_on_array.name = strings[pc];
word_on_array.pos = pc;
words[word_def_length] = word_on_array;
word_def_length++;
while (strings[pc]!=";") {
pc++;
};
return 0;
}
std::string temp[word_mem];
for (int k=0;k<word_mem;k++) {
temp[k] = words[k].name;
}
for (int k=0;k<word_mem;k++) {
if (temp[k]==win) {
word_ret.push(pc);
pc = words[k].pos;
return 0;
}
}
if (win==";") {
pc = word_ret.top();
word_ret.pop();
return 0;
}
// pushing numbers
if (isNumber(win)) {
stack.push(std::stoi(win));
return 0;
}
// basic math
if (win=="+") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1+op2);
return 0;
}
if (win=="-") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1-op2);
return 0;
}
if (win=="*") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1*op2);
return 0;
}
if (win=="/") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1/op2);
return 0;
}
if (win=="mod") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1%op2);
return 0;
}
// output
if (win==".") {
int op1 = stack.top();
stack.pop();
printf("%d ",op1);
return 0;
}
if (win=="emit") {
int op1 = stack.top();
stack.pop();
printf("%c",op1);
return 0;
}
if (win=="cr") {
printf("\n");
return 0;
}
if (win==".\"") {
std::string print_str;
pc++;
while (strings[pc]==remq(strings[pc])) {
print_str += remq(strings[pc])+" ";
pc++;
}
print_str += remq(strings[pc]);
std::cout<<print_str;
return 0;
}
// stack manipulation
if (win=="dup") {
int op1 = stack.top();
stack.push(op1);
return 0;
}
if (win=="drop") {
stack.pop();
return 0;
}
if (win=="swap") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op2);
stack.push(op1);
return 0;
}
if (win=="over") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1);
stack.push(op2);
stack.push(op1);
return 0;
}
if (win=="rot") {
int op3 = stack.top();
stack.pop();
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op2);
stack.push(op1);
stack.push(op3);
return 0;
}
// conditionals
if (win=="=") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1==op2?-1:0);
return 0;
}
if (win==">") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1>op2?-1:0);
return 0;
}
if (win=="<") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1<op2?-1:0);
return 0;
}
if (win=="and") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1&op2);
return 0;
}
if (win=="or") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1|op2);
return 0;
}
if (win=="xor") {
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
stack.push(op1^op2);
return 0;
}
if (win=="invert") {
int op1 = stack.top();
stack.pop();
stack.push(~op1);
return 0;
}
// if statements
if (win=="if") {
int op1 = stack.top();
stack.pop();
int if_true = 0;
int if_false = 0;
pc++;
if_true = pc;
while (strings[pc]!="else"&&strings[pc]!="then") pc++;
if (strings[pc]=="else") {
pc++;
if_false = pc;
while (strings[pc]!="then") pc++;
}
if (op1==-1) {
pc = if_true-1;
} else if (if_false>0) {
pc = if_false-1;
}
return 0;
}
if (win=="else") {
while (strings[pc]!="then") pc++;
return 0;
}
if (win=="then") {
return 0;
}
// loops
if (win=="do") {
loop_ret.push(pc);
int op2 = stack.top();
stack.pop();
int op1 = stack.top();
stack.pop();
loop_index.push(op2);
loop_control.push(op1);
return 0;
}
if (win=="loop") {
int op1 = loop_index.top();
loop_index.pop();
loop_index.push(op1+1);
if (loop_index.top()<loop_control.top()) {
pc = loop_ret.top();
} else {
loop_control.pop();
loop_index.pop();
loop_ret.pop();
}
return 0;
}
if (win=="i") {
stack.push(loop_index.top());
return 0;
}
if (win=="leave") {
loop_control.pop();
loop_index.pop();
loop_ret.pop();
while (strings[pc]!="loop") pc++;
return 0;
}
// variables
if (win=="variable") {
pc++;
var_name[var_length] = strings[pc];
var_content[var_length] = 0;
is_array[var_length] = 0;
var_length++;
return 0;
}
if (win=="!") {
int address = stack.top();
stack.pop();
int value = stack.top();
stack.pop();
var_content[address] = value;
return 0;
}
if (win=="+!") {
int address = stack.top();
stack.pop();
int value = stack.top();
stack.pop();
var_content[address] += value;
return 0;
}
if (getVarPos(strings[pc])>=0) {
stack.push(getVarPos(strings[pc]));
return 0;
}
if (win=="@") {
int op1 = stack.top();
stack.pop();
stack.push(var_content[op1]);
return 0;
}
if (win=="?") {
int op1 = stack.top();
stack.pop();
printf("%d ",var_content[op1]);
return 0;
}
// arrays
if (win=="cells"&&strings[pc+1]=="allot") {
int op1 = stack.top();
stack.pop();
is_array[var_length-1] = 1;
var_length += op1+1;
pc++;
return 0;
}
if (win=="cells") { // it does nothing because me stupid
return 0;
}
// random number generation
if (win=="rand") {
int op1 = stack.top();
stack.pop();
stack.push(rand()%(op1+1));
return 0;
}
// key input
if (win=="key?") {
stack.push(kbhit()?-1:0);
return 0;
}
if (win=="key") {
while (!kbhit());
stack.push(getch());
return 0;
}
// sleep function
if (win=="sleep") {
int op1 = stack.top();
stack.pop();
usleep(op1*1000);
return 0;
}
return 1;
}
int run_forth(std::string input) {
custom_word = 0;
word_def_length = 0;
memcpy(words_temp,words,sizeof(struct word)*word_mem);
for (int i=0;i<16384;i++) {
strings[i] = strings_temp[i];
}
split(input,' ');
for (pc=0;len(strings[pc])>0;pc++) {
int ret_code = op(strings[pc]);
if (ret_code==2) {
return 2;
};
if (ret_code==1) {
std::cout<<"\nERROR: "<<strings[pc]<<"?";
break;
};
}
return 0;
}
int main() {
srand(time(NULL));
std::string prog;
std::cout<<"AArtForth C version by AArt1256 made in 2022!";
while (1) {
std::cout<<"\n> ";
getline(std::cin, prog, '\n');
if (run_forth(prog)==2) return 0;
std::cout<<"\nok";
}
return 0;
}