-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforth.c
753 lines (700 loc) · 13.9 KB
/
forth.c
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Railforth \\
// \\
// A simple Reference implementation
// for the Raillisp Forth Specification.
#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <stdint.h>
#include <sys/syscall.h>
typedef long long int cell;
typedef long long unsigned int u_cell;
cell tos=0; // top of stack value
cell* sp0; // stack base pointer
cell* sp; // stack location pointer
cell* rp0; // return stack
cell* rp;
cell* dp0; // dictionary
cell* dp;
cell* ip=0; // instruction pointer
//int here = 0; // top of dictionary
cell* latest = 0; // last word
#define COMPILE 1
#define INTERPRET 0
#define bytes_per_cell sizeof(cell)
cell state = INTERPRET; // forth compiler state
#define IMM_BIT 0x100000 //TODO
#define CODE_BIT 0x1000000 //TODO
#define HIDDEN_BIT 0x10000000 //TODO
#define LEN_MASK 0xffff
#define false 0
#define true 1
cell* LIT_XT;
cell* DOCOL_XT;
cell* EXIT_XT;
cell* EXEC_XT;
// stack pointers point at next item
#define push(x) *sp++ = tos; tos = (cell)(x)
#define pop() tos; tos = *--sp
#define rpush(x) *rp++ = (cell)(x)
#define rpop() *--rp
#define dict_append(x) *dp++ = (cell)(x)
#define tib_max 128
static char *tib; // Text Input Buffer
int tib_i = 0;
FILE *input_device;
int lineno = 0;
int colno = 0;
uint8_t base = 10;
void error(char* msg){
printf("ERROR: %s\n", msg);
exit(1);
}
void print_word(char* a, int c){
for (int i =0; i < c; i ++){
printf("%c", a[i]);
}
printf("\n");
}
cell digit(unsigned char c) {
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'z') return 10 + c - 'a';
if ('A' <= c && c <= 'Z') return 10 + c - 'A';
return -1;
}
int parse_num(char *a, cell len, cell* res) {
cell b = base;
int isminus = 0;
cell n=0;
if (len <= 0) {
return false;
}
switch (*a) {
case '%': b = 2; len--; a++; break;
case '#': b = 10; len--; a++; break;
case '$': b = 16; len--; a++; break;
}
if( *a == '-' && len > 1) {
isminus = 1;
len--;
a++;
}
for( unsigned char c; len > 0; len-- ) {
c = digit(*a++);
if (c >= b ) return false;
n = (n * b) + c;
}
*res = isminus ? -n : n;
return true;
}
static char *_line = (char *)NULL;
int read_stdin(){ // read a line from stdin into tib
printf(" ok\r\n");
char* in = readline("");
if (!in) {
exit(0);
}
strcpy(tib, in);
free(in);
return 1;
}
int read_line() {
lineno++;
colno = 0;
tib_i = 0;
if (input_device == stdin ) {
return read_stdin();
}
return fgets(tib, 128, input_device) != NULL;
}
char next_char(){
colno++;
if (tib_i >= tib_max) {
return 0;
}
return tib[tib_i++];
}
char key(){
char c = next_char();
if (c) {
return c;
}
read_line();
c = next_char();
if (c){
return c;
}
exit(1);
}
char* word_a = 0;
int word_c = 0;
int parse_name(){ // return true on success
char c;
word_c = 0;
do{
c = next_char();
} while (c && isspace(c));
if (c == 0) {
return 0;
}
word_a = tib + tib_i - 1;
word_c = 1;
while(!isspace(c = next_char()) && c) {
++word_c;
}
tib_i--;
return 1;
}
void immediate() {
*(cell*)(latest) |= IMM_BIT;
}
int is_immediate(cell *word) {
return *word & IMM_BIT ;
}
void hide(){
*(cell*)(latest) |= HIDDEN_BIT;
}
cell aligned(cell x){
return (x + sizeof(cell)-1) & ~(sizeof(cell)-1);
}
void dict_append_str(char *a, int c){
strncpy((char*)(dp), a, c);
dp += c / bytes_per_cell + c % bytes_per_cell;
}
void create(char* a, cell c){
dict_append(latest);
latest = (cell*)dp;
dict_append(c);
dict_append_str(a, c);
}
void code(char* name, void* addr){
create(name, strlen(name));
dict_append(addr);
*latest |= CODE_BIT;
}
int str_eq(char *a1, u_cell c1, char *a2, u_cell c2){
if ( c1 != c2 ) {
return false;
}
for (; c1 && c2; a1++, a2++, c1--, c2--) {
if (*a1 != *a2) {
return false;
}
}
return true;
}
int compare(char *a1, u_cell c1, char *a2, u_cell c2) {
for (;c1 && c2; a1++, a2++, c1--, c2--) {
if (*a1 != *a2) {
return (*a1 < *a2) ? -1 : 1;
}
}
return c1 == c2 ? 0 : ((c1 < c2) ? -1 : 1);
}
cell* cfa(cell* word){
int len = (*word) & LEN_MASK;
return word + 1 + len/bytes_per_cell + len % bytes_per_cell;
}
cell* find_word(char* a, int c){
cell *word = latest;
while (word){
cell this = *word;
int len = this & LEN_MASK;
char* name = (char*)(word+1);
if (str_eq(a, c, name, len)){
return word;
}
word = (cell*)*(word-1);
}
return false;
}
void cr(){ printf("\n"); };
void type(char* a, int c){
for ( ; c>0; c--, a++) {
printf("%c", *a);
}
}
void words(){
cell *word = latest;
int col=0;
while (word){
cell this = *word;
int len = this & LEN_MASK;
char* name = (char*)(word+1);
type(name, len);
col += len + 1;
if (col > 64) {
cr();
col = 0;
} else {
printf(" ");
}
word = (cell*)*(word-1);
}
cr();
}
cell* handle_word(cell* word){
if (state == INTERPRET || *word & IMM_BIT){
return (cell*)cfa(word); // interpret
}
dict_append(cfa(word)); // compile
return 0;
}
void literal(cell num){
dict_append((cell)LIT_XT);
dict_append((cell)num);
}
cell* handle_num(cell num){
if (state == INTERPRET) {
push(num);
} else {
literal(num);
}
return 0;
}
cell* do_interpret(){
cell num;
cell* word = find_word(word_a, word_c);
if (word) {
return handle_word(word);
}
int ok = parse_num(word_a, word_c, &num);
if (ok) {
return handle_num(num);
}
printf("Undefined word: ");
print_word(word_a, word_c);
exit(1);
}
cell* get_xt(char* word){
cell* x = find_word(word, strlen(word));
if (!x){
error("get_xt undefined");
}
return cfa(x);
}
int depth(){
return sp - sp0;
}
void print_stack(){
printf("<%d> ", depth());
for (cell* p = sp0+1; p < sp; p++) {
printf("%llu ", (cell)*p);
}
if (depth()){
printf("%llu\n", tos);
}
}
void cmove(char *from, char *to, u_cell length) {
while ((length--) != 0) {
*to++ = *from++;
}
}
void see(char* a, int c){
cell* word = find_word(word_a, word_c);
if (! word) {
error("undefined");
}
int len = *word & LEN_MASK;
char* name = (char*)(word+1);
if (*word & CODE_BIT) {
printf("codeword\n");
return;
}
printf(": "); print_word(name, len);
cell* code = cfa(word);
for(; (cell*)(*code) != EXIT_XT; code++) {
printf(" %llu: %llu\n", (cell)code, *code);
}
if (is_immediate(word)) {
printf(" ; immediate\n");
}
else printf(" ;\n");
}
cell utime(){
struct timeval time;
gettimeofday(&time, NULL);
return time.tv_sec * 1000000 + time.tv_usec;
}
#define CODE(name, label) label
#define iCODE(name, label) label
#define NEXT goto **(cell*)(*ip++)
void forth(){
cell* xt;
cell x;
char* str, c;
#include "_forthwords.c"
LIT_XT = get_xt("lit");
DOCOL_XT = &&docol;
EXIT_XT = get_xt("exit");
EXEC_XT = get_xt("exec");
// Reserve space used by INTERPRET for executing words.
dict_append(0); // space for destination address
cell* interpret_ip = dp;
dict_append(get_xt("interpret")); // return addres
CODE("quit", quit):
rp = rp0;
while (true) {
if (!read_line()){
if(input_device != stdin) { // actually, this will always be true
fclose(input_device);
input_device = stdin;
lineno = 0;
continue;
}
exit(1);
}
CODE("interpret", interpret):
while (parse_name()){
xt = do_interpret();
if (xt) {
goto execute_xt;
}
}
}
docol:
rpush(ip);
ip = (cell*)(*(ip-1)) + 1;
NEXT;
CODE("exit", exit):
ip = (cell*)(*--rp);
NEXT;
CODE("drop", drop):
tos = *--sp;
NEXT;
CODE("2drop", _2drop):
sp--;
tos = *--sp;
NEXT;
CODE("dup", dup):
*sp++ = tos;
NEXT;
CODE("2dup", _2dup):
*sp++ = tos;
*sp++ = *(sp-3);
NEXT;
CODE("over", over):
push(*(sp-2));
NEXT;
CODE("nip", nip):
--sp;
NEXT;
CODE("swap", swap):
x = *(sp-1);
*(sp-1) = tos;
tos = x;
NEXT;
CODE("pick", pick):
tos = *(sp-1-tos);
NEXT;
CODE("rpick", rpick):
tos = *(rp-1-tos);
NEXT;
CODE("rot", rot):
x = *(sp-2);
*(sp-2) = *(sp-1);
*(sp-1) = tos;
tos = x;
NEXT;
CODE("-rot", _rot):
x = *(sp-2);
*(sp-2) = tos;
tos=*(sp-1);
*(sp-1) = x;
NEXT;
#define BINOP(name, label, op) CODE(name, label): \
tos = *--sp op tos; \
NEXT;
BINOP("+", plus, +);
BINOP("-", minus, -);
BINOP("*", mul, *);
BINOP("/", div, /);
BINOP("and", and, &);
BINOP("or", or, |);
BINOP("xor", xor, ^);
BINOP("=", equal, ==);
BINOP("<>", not_equal, !=);
BINOP("<", less_than, <);
BINOP(">", greater_than, >);
BINOP(">=", greater_or_eq, >=);
BINOP("<=", less_or_eq, <=);
CODE("1+", one_plus):
tos += 1;
NEXT;
CODE("1-", one_minus):
tos -= 1;
NEXT;
CODE("0=", zero_eq):
tos = (tos == 0);
NEXT;
CODE("0<>", zero_not_eq):
tos = (tos != 0);
NEXT;
CODE("0<", zero_less_than):
tos = (tos < 0);
NEXT;
CODE("0>", zero_greater_than):
tos = (tos > 0);
NEXT;
CODE("d<>", d_not_eq):
tos = tos != *(sp-2) || *(sp-1) != *(sp-3);
sp -= 3;
NEXT;
CODE("invert", invert):
tos = ~tos;
NEXT;
CODE("2/", two_div):
tos >>= 1;
NEXT;
CODE("2*", two_times):
tos <<= 1;
NEXT;
CODE("lshift", lshift):
tos = *--sp << tos;
NEXT;
CODE("rshift", rshift):
tos = *--sp >> tos;
NEXT;
CODE("!", store):
*((cell*)tos) = *--sp;
pop();
NEXT;
CODE("@", fetch):
tos = *((cell*)tos);
NEXT;
CODE("+!", addstore):
*((cell*)tos) += *--sp;
pop();
NEXT;
CODE("c!", store_byte):
*((char*)tos) = (char)*--sp;
pop();
NEXT;
CODE("c@", fetch_byte):
tos = (cell)(*((char*)tos));
NEXT;
CODE("cmove", cmove):
str = (char*)(*--sp);
cmove((char*)(*--sp), str, (u_cell)tos);
pop(); NEXT;
CODE(">r", to_r):
rpush(tos);
pop();
NEXT;
CODE("r>", from_r):
push(rpop());
NEXT;
CODE("r@", read_r):
push(*(rp - 1));
NEXT;
CODE("rdrop", r_drop):
rp--;
NEXT;
CODE("sp@", sp_fetch):
push(sp);
NEXT;
CODE("emit", emit):
x = pop();
printf("%c", (char)x);
NEXT;
CODE("create", _create):
parse_name();
create(word_a, word_c);
NEXT;
CODE("allot", allot):
dp += pop();
NEXT;
iCODE("[", lbrack):
state = INTERPRET;
NEXT;
CODE("]", rbrack):
state = COMPILE;
NEXT;
CODE("parse-name", _parse_name):
parse_name();
push(word_a);
push(word_c);
NEXT;
CODE("find-name", find_name):
x = pop();
str = (char*)pop();
push(find_word(str, x));
NEXT;
CODE("name>int", name_to_int):
tos = (cell)cfa((cell*)tos);
NEXT;
CODE("'", tick):
parse_name();
xt = find_word(word_a, word_c);
push(cfa(xt));
NEXT;
CODE("compile,", compile_comma):
CODE(",", comma):
x = pop();
dict_append(x);
NEXT;
CODE("[']", btickb):
push(*ip++);
NEXT;
#define do_branch ip = (cell*)((cell)ip + (cell)(*ip))
CODE("branch", branch):
do_branch;
NEXT;
CODE("0branch", zero_branch):
x = pop();
if (x==0) {
do_branch;
}else{
ip++;
}
NEXT;
CODE("litstring", litstring):
x = *ip++; // len
push(((cell)(ip))+1); // addr
push(x);
ip = (cell*)aligned((cell)(ip) + x);
NEXT;
CODE("char", _char):
parse_name();
push(*word_a);
NEXT;
CODE(".", dot):
xt=(cell*)pop();
printf("%llu ", (cell)xt);
NEXT;
CODE(".s", print_stack):
print_stack();
NEXT;
CODE("type", _type):
type(((char*)*--sp), tos);
tos = *--sp;
NEXT;
iCODE("execute", execute):
if (state==COMPILE){
dict_append(EXEC_XT);
dict_append(0);
} else {
xt = (cell*)pop();
execute_xt:
*(interpret_ip - 1) = (cell)xt;
ip = interpret_ip;
goto **xt;
}
NEXT;
CODE("exec", exec):
xt = (cell*)pop();
*ip++ = (cell)xt;
goto **xt;
CODE("bye", bye):
exit(0);
CODE("literal", _literal):
x = pop();
literal(x);
NEXT;
CODE("lit", lit):
push(*ip++);
NEXT;
CODE("words", _words):
words();
NEXT;
iCODE("immediate", _immediate):
immediate();
NEXT;
CODE("reveal", reveal):
*(cell*)(tos) ^= HIDDEN_BIT;
pop();
NEXT;
CODE(":", colon):
parse_name();
create(word_a, word_c);
dict_append(DOCOL_XT);
hide();
state = COMPILE;
NEXT;
CODE("see", _see):
parse_name();
see(word_a, word_c);
NEXT;
iCODE(";", semi):
dict_append(EXIT_XT);
state = INTERPRET;
NEXT;
CODE("docol", _docol):
push(DOCOL_XT);
NEXT;
iCODE("\\", _slash):
tib_i = tib_max;
NEXT;
iCODE("(", _open_paren):
while((c = next_char()) && c != ')');
NEXT;
CODE("key", _key):
push(key());
NEXT;
CODE("aligned", _aligned):
tos = aligned(tos);
NEXT;
CODE("ms", ms):
x = pop();
usleep(x*1000);
NEXT;
CODE("utime", utime):
push(utime());
push(0);
NEXT;
CODE("allocate", allocate):
x = pop();
xt = (cell*)malloc(sizeof(cell)*x);
push(xt);
push(xt == NULL);
NEXT;
CODE("free", free):
x = pop();
free((cell*)x);
NEXT;
CODE("clearstack", clearstack):
sp = sp0;
NEXT;
CODE("compare", compare):
tos = compare((char*)*(sp-3), *(sp-2), (char*)*(sp-1), tos);
sp -= 3;
NEXT;
CODE("refill", refill):
read_line();
NEXT;
CODE("syscall1", syscall1):
tos = syscall(tos, 1, *--sp);
NEXT;
CODE("syscall2", syscall2):
tos = syscall(tos, 2, *--sp, *--sp);
NEXT;
CODE("syscall3", syscall3):
tos = syscall(tos, 3, *--sp, *--sp, *--sp);
NEXT;
#define VAR(name, label, var) CODE(name, label): push( & var ); NEXT
VAR("latest", _latest, latest);
VAR("base", _base, base);
VAR("dp", _dp, dp);
VAR("state", _state, state);
#define CONST(name, label, val) CODE(name, label): push( val ); NEXT
CONST("sys_open", sys_open, __NR_open);
CONST("sys_close", sys_close, __NR_close);
CONST("sys_read", sys_read, __NR_read);
CONST("sys_write", sys_write, __NR_write);
CONST("sys_exit", sys_exit, __NR_exit);
CONST("sys_brk", sys_brk, __NR_brk);
}
void init(){
input_device = fopen("forth.fth", "r");
tib = (char*)malloc(sizeof(char)*tib_max);
sp0 = sp = (cell*)malloc(sizeof(cell)*1000);
rp0 = rp = (cell*)malloc(sizeof(cell)*1000);
dp0 = dp = (cell*)malloc(sizeof(cell)*100000);
}
int main (){
init();
forth();
}