-
Notifications
You must be signed in to change notification settings - Fork 0
/
forth.c
executable file
·608 lines (534 loc) · 13 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
#include "forth.h"
int compiling = FALSE;
forth_obj* pc;
forth_stack rstack, pstack;
forth_word dict[FORTH_DICT_SIZE];
int dict_index = 0;
forth_obj forth_thread_code[FORTH_THREAD_SIZE];
int thread_index = 0;
char name_space[NAME_SPACE_SIZE];
int ns_index = 0;
char* lookup_symbol(char* name)
{
int i;
for(i = 0; i < ns_index; i++){
if(!strcmp(name, name_space + i)){
return name_space + i;
}
}
return NULL;
}
char* add_name(char* name)
{
char* ret = name_space + ns_index;
int i, tmp = 0;
for(i = 0; i < ns_index; i += tmp){
if(!strcmp(name, name_space + i)){
return name_space + i;
}
tmp = strlen(name_space + i) + 1;
}
tmp = (strlen(name) + 4) / 4;
ns_index += tmp * 4;
if(ns_index >= NAME_SPACE_SIZE){
fprintf(stderr, "name space over flower\n");
exit(1);
}
strcpy(ret, name);
dprintf(("add_name(%s)(%p)\n", ret, ret));
return ret;
}
forth_obj str_to_exp(char* str)
{
int tmp = atoi(str);
forth_obj ret;
if((str[0] != '0') && (tmp == 0)){//string
set_sym(&ret, add_name(str));
}else{//num
set_num(&ret, tmp);
}
return ret;
}
forth_word* forth_lookup(char* w)
{
int i;
for(i = dict_index - 1; i >= 0; i--){
dprintf(("compare(%s),(%s)\n",w, dict[i].name));
if(!strcmp(w, dict[i].name)){
return dict + i;
}
}
return NULL;
}
char* forth_dict_name(forth_obj* exp)
{
int i;
for(i = dict_index - 1; i >= 0; i--){
forth_obj *tmp = dict[i].thread;
//dprintf(("comp(%p),(%p)\n", dict[i].thread, exp));
if(tmp == exp ||
(prim_p(*tmp) && prim_p(*exp)
&& (get_prim(*tmp) == get_prim(*exp)))){
return dict[i].name;
}
}
return "NULL";
}
void forth_inner_interpreter(void)
{
// dprintf(("begin inner_interpreter\n"));
do{
dprintf(("before exec.pc=%p,rstack.index=%d\n", (int*)pc, rstack.index));
if(pc_end_p(*pc)){
pc = get_thread(pop(&rstack));
}else if(prim_p(*pc)){
dprintf(("prim_p\n"));
exec_prim(pc);
}else if(thread_p(*pc)){
forth_obj tmp;
set_thread(&tmp, pc + 1);
dprintf(("thread_p\n"));
push(tmp, &rstack);
pc = get_thread((forth_obj)pc->thread);
}else{
dprintf(("push pstack\n"));
push(*pc, &pstack);
pc++;
}
dprintf(("after exec.pc=%p, pc_end_p=(%d),rstack.index=%d\n",
(int*)pc, pc_end_p(*pc), rstack.index));
}while((!pc_end_p(*pc)) || (rstack.index != 0));
// dprintf(("end innner_interpreter\n"));
}
void add_forth_prim(char* name, int immediate, void (*func)())
{
forth_obj tmp;
dict[dict_index].name = add_name(name);
dict[dict_index].immediate = immediate;
dict[dict_index].thread = forth_thread_code + thread_index;
dprintf(("add(%s)pointer=%x\n", name, (int)func));
if(thread_index + 2 >= FORTH_THREAD_SIZE){
fprintf(stderr, "too few threaded code memory\n");
exit(1);
}
set_prim(&tmp, func);
forth_thread_code[thread_index] = tmp;
thread_index++;
set_end(forth_thread_code + thread_index);
thread_index++;
dict_index++;
if(dict_index >= FORTH_DICT_SIZE){
fprintf(stderr, "dict over flower\n");
exit(1);
}
}
void F_nop(void)
{
pc++;
}
void F_mul(void) /* * */
{
int arg1, arg2;
forth_obj ret;
arg1 = get_num(pop(&pstack));
arg2 = get_num(pop(&pstack));
set_num(&ret, arg1 * arg2);
push(ret, &pstack);
pc++;
}
void F_drop(void)
{
pop(&pstack);
pc++;
}
void F_dup(void)
{
forth_obj d = peek(&pstack);
push(d, &pstack);
pc++;
}
void F_swap(void)
{
forth_obj arg1, arg2;
arg1 = pop(&pstack);
arg2 = pop(&pstack);
push(arg1, &pstack);
push(arg2, &pstack);
pc++;
}
void F_print(void)
{
forth_obj e = pop(&pstack);
dprintf(("print(%x)\n", e.num));
if(get_type(e) == TYPE_FIXNUM){
printf("%d\n", get_num(e));
}else if (get_type(e) == TYPE_STRING){
printf("%s\n", get_sym(e));
}else{
printf("#<unprintable %x>\n", e.num & ~TYPE_MASK);
}
dprintf(("print end\n"));
pc++;
}
void F_to_r(void) /* >r */
{
forth_obj tmp = pop(&pstack);
push(tmp, &rstack);
pc++;
}
void F_r_to(void) /* r> */
{
forth_obj tmp = pop(&rstack);
push(tmp, &pstack);
pc++;
dprintf(("r>p\n"));
}
void F_l_bracket(void) /* [ */
{
compiling = FALSE;
dprintf(("([)compiling = FALSE\n"));
pc++;
}
void F_r_bracket(void) /* ] */
{
compiling = TRUE;
dprintf(("(])compiling = TRUE\n"));
pc++;
}
void F_create(void)
{
forth_obj tmp;
dprintf(("create! thread=(%p)\n", forth_thread_code + thread_index));
thread_index++;
dict[dict_index].name = "";
dict[dict_index].immediate = FALSE;
dict[dict_index].thread = forth_thread_code + thread_index;
set_thread(&tmp, forth_thread_code + thread_index + 2);
forth_compile_in(tmp);
thread_index++;
set_end(forth_thread_code + thread_index);
dict_index++;
if(dict_index >= FORTH_DICT_SIZE){
fprintf(stderr, "dict over flower\n");
exit(1);
}
pc++;
}
void F_name(void)
{
dict[dict_index - 1].name = get_sym(pop(&pstack));
pc++;
}
void F_immediate(void)
{
dict[dict_index - 1].immediate = TRUE;
pc++;
}
void F_fetch(void) /* @ */
{
forth_obj ret, *tmp = get_thread(pop(&pstack));
set_thread(&ret, tmp);
push(ret, &pstack);
pc++;
}
void F_store(void)/* ! */
{
forth_obj tmp1 = pop(&pstack),
tmp2 = pop(&pstack),
*loc = get_thread(tmp1),
*thread = get_thread(tmp2);
//print_thread();
dprintf(("loc=(%p),loc->(%p),th=(%p),th->(%p)\n",tmp1,loc,tmp2,thread));
set_thread(loc, thread);
dprintf(("loc=(%p),loc->(%p),th=(%p),th->(%p)\n",tmp1,loc,tmp2,thread));
pc++;
}
void F_not(void)
{
forth_obj tmp = pop(&pstack);
set_num(&tmp, !get_num(tmp));
push(tmp, &pstack);
pc++;
}
void F_oddp(void)
{
forth_obj tmp = pop(&pstack);
set_num(&tmp, !(get_num(tmp) % 2));
push(tmp, &pstack);
pc++;
}
void F_evenp(void)
{
forth_obj tmp = pop(&pstack);
set_num(&tmp, get_num(tmp) % 2);
push(tmp, &pstack);
pc++;
}
#define def_bin(name, sym) void F_##name(void){ \
forth_obj ret, arg1 = pop(&pstack), arg2 = pop(&pstack); \
set_num(&ret, get_num(arg1) sym get_num(arg2)); \
push(ret, &pstack); \
pc++; \
}
def_bin(eq, ==)
def_bin(plus, +)
def_bin(minus, -)
def_bin(div, /)
def_bin(lt, <)
def_bin(gt, >)
def_bin(le, <=)
def_bin(ge, >=)
def_bin(and, &&)
def_bin(or, ||)
void F_max(void)
{
forth_obj ret, arg1 = pop(&pstack), arg2 = pop(&pstack);
int a1 = get_num(arg1), a2 = get_num(arg2);
set_num(&ret, (a1 > a2) ? a1 : a2);
push(ret, &pstack);
pc++;
}
void F_min(void)
{
forth_obj ret, arg1 = pop(&pstack), arg2 = pop(&pstack);
int a1 = get_num(arg1), a2 = get_num(arg2);
set_num(&ret, (a1 < a2) ? a1 : a2);
push(ret, &pstack);
pc++;
}
/* equal = */
void F_branch_if(void)
{
forth_obj tmp = pop(&pstack);
dprintf(("branch:(%p)(%p)\n", pc+1,get_thread(pc[1])));
if(pc_end_p(pc[1])){
pc++;
}else{
pc = (get_num(tmp) != 0) ? get_thread(pc[1]) : pc + 2;
}
}
void F_compile(void)
{
forth_compile_in(pc[1]);
pc += 2;
}
void F_here(void)
{
forth_obj tmp;
set_thread(&tmp, forth_thread_code + thread_index - 1);
push(tmp, &pstack);
pc++;
}
void F_print_pstack(void)
{
print_stack(&pstack);
pc++;
}
void F_print_rstack(void)
{
print_stack(&rstack);
pc++;
}
void F_print_thread(void)
{
print_thread();
pc++;
}
void F_print_dict(void)
{
print_dict();
pc++;
}
void forth_install_prims(void)
{
add_forth_prim("nop", FALSE, F_nop);
add_forth_prim("*", FALSE, F_mul);
add_forth_prim("drop", FALSE, F_drop);
add_forth_prim("dup", FALSE, F_dup);
add_forth_prim("swap", FALSE, F_swap);
add_forth_prim("print", FALSE, F_print);
add_forth_prim(">r", FALSE, F_to_r);
add_forth_prim("r>", FALSE, F_r_to);
add_forth_prim("[", TRUE, F_l_bracket);
add_forth_prim("]", FALSE, F_r_bracket);
add_forth_prim("create", FALSE, F_create);
add_forth_prim("name", FALSE, F_name);
add_forth_prim("immediate", FALSE, F_immediate);
add_forth_prim("@", FALSE, F_fetch);
add_forth_prim("!", FALSE, F_store);
add_forth_prim("not", FALSE, F_not);
add_forth_prim("oddp", FALSE, F_oddp);
add_forth_prim("evenp", FALSE, F_evenp);
add_forth_prim("eq", FALSE, F_eq);
add_forth_prim("+", FALSE, F_plus);
add_forth_prim("-", FALSE, F_minus);
add_forth_prim("/", FALSE, F_div);
add_forth_prim("<", FALSE, F_lt);
add_forth_prim(">", FALSE, F_gt);
add_forth_prim("<=", FALSE, F_le);
add_forth_prim(">=", FALSE, F_ge);
add_forth_prim("max", FALSE, F_max);
add_forth_prim("min", FALSE, F_min);
add_forth_prim("and", FALSE, F_and);
add_forth_prim("or", FALSE, F_or);
/* equal = */
add_forth_prim("branch-if", FALSE, F_branch_if);
add_forth_prim("compile", FALSE, F_compile);
add_forth_prim("here", FALSE, F_here);
add_forth_prim("print-thread", FALSE, F_print_thread);
add_forth_prim("print-pstack", FALSE, F_print_pstack);
add_forth_prim("print-rstack", FALSE, F_print_rstack);
add_forth_prim("print-dict", FALSE, F_print_dict);
}
void forth_compile_in(forth_obj exp)
{
dprintf(("compile(%4x)type=%d\n",
((int)exp.num & ~TYPE_MASK), get_type(exp)));
forth_thread_code[thread_index] = exp;
thread_index++;
set_end(forth_thread_code + thread_index);
if(thread_index >= FORTH_THREAD_SIZE){
fprintf(stderr, "threaded code over flower\n");
exit(1);
}
}
void forth_handle_found(forth_word* word)
{
dprintf(("begin handle_found,compiling=%d\n",compiling));
if(compiling && !(word->immediate)){
forth_compile_in(*word->thread);
dprintf(("after compile_in\n"));
}else{
pc = word->thread;
dprintf(("before inner_interpreter pc=(%p)*pc=(%p)\n", pc, pc->c));
forth_inner_interpreter();
dprintf(("after inner_interpreter\n"));
}
dprintf(("end handle_found\n"));
}
void forth_handle_not_found(char* v)
{
if(v[0] == '\''){
dprintf(("get quote(%s)\n", v + 1));
if(compiling){
forth_compile_in(str_to_exp(v + 1));
}else{
push(str_to_exp(v + 1), &pstack);
}
}else if(v[0] == ','){
forth_word* word;
dprintf(("get postpone(%s)\n", v + 1));
word = forth_lookup(v + 1);
if(word == NULL){
fprintf(stderr, "postpone failed %s\n", v + 1);
}else{
forth_compile_in(*word->thread);
}
}else if(compiling){
dprintf(("compile word\n"));
forth_compile_in(str_to_exp(v));
}else{
int tmp = atoi(v);
if((tmp != 0) || v[0] == '0'){
forth_obj e;
set_num(&e, tmp);
push(e, &pstack);
}else{
fprintf(stderr, "not found word(%s)\n", v);
exit(1);
}
}
}
int main(void)
{
char v[BUF_SIZE], *ret, *last;
init_stack(&rstack);
init_stack(&pstack);
forth_install_prims();
forth_install_stdlib();
while(1){
fgets(v, BUF_SIZE, stdin);
// dprintf(("fgets:(%s)\n",v));
for(ret = v; ret != NULL; ret = last){
ret = strtok_r(ret, " ", &last);
my_forth(ret);
}
}
return 0;
}
void forth_install_stdlib(void)
{
FILE* fp = fopen(FORTH_LIBRARY_NAME, "r");
int ret;
char sym[FORTH_WORD_NAME_SIZE], tmp[65535];
if(fp == NULL){
fprintf(stderr, "can't find " FORTH_LIBRARY_NAME "\n");
return;
}
do{
ret = fscanf(fp, "%s", sym);
if(ret == 0 || ret == EOF){ break;}
if(sym[0] == '#'){ fgets(tmp,65535,fp); continue;}
dprintf(("lib load(%s)\n", sym));
my_forth(sym);
}while(1);
fclose(fp);
}
void my_forth(char* v)
{
forth_word* word;
if(!strcmp(v, "quit")){ exit(0);}
// dprintf(("my_forth:(%s)\n", v));
word = forth_lookup(v);
if(word != NULL){
// dprintf(("forth_handle_found:(%s)\n", v));
forth_handle_found(word);
}else{
// dprintf(("forth_handle_not_found:(%s)\n", v));
forth_handle_not_found(v);
}
}
void print_stack(forth_stack* st)
{
int i;
printf("print_stack:%s\n", (st == &pstack) ? "pstack" : "rstack");
for(i = 0; i< st->index; i++){
printf("%2x", i);
print_forth_obj(st->stack + i);
}
}
void print_dict(void)
{
int i;
printf("print_dict\n");
for(i = 0; i < dict_index; i++){
printf("(%16s)imm=%d,thread=%p\n", dict[i].name,
dict[i].immediate, dict[i].thread);
}
}
void print_thread(void)
{
int i;
printf("print_thread\n");
for(i = 0; i <= thread_index; i++){
print_forth_obj(forth_thread_code + i);
}
}
void print_forth_obj(forth_obj* e)
{
printf("%p(%04x):", e, get_type(*e));
switch(get_type(*e)){
case TYPE_FIXNUM:
printf("FIXNUM:%8d\n", get_num(*e));
break;
case TYPE_PRIM:
printf("PRIM :%p\t%s\n", get_prim(*e),forth_dict_name(e));
break;
case TYPE_THREAD:
printf("THREAD:%p\t%s\n", get_prim(*e),forth_dict_name(e));
break;
case TYPE_STRING:
printf("STRING:%s\n", get_sym(*e));
break;
}
}