channels working
[watForth.git] / c / switch_split.c
1 extern int sys_read(int,int);
2 extern int sys_request(int,int);
3 extern int sys_write(int,int);
4 extern int vocab_get(int);
5 extern int vocab_set(int,int);
6 extern void push(int);
7 extern int pop(void);
8 extern void rpush(int);
9 extern int rpop(void);
10 extern void rinit(void);
11 static char memseg[1024] =
12 { 0, 0, 0, 9, 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 0, 0, 0,
13 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 16, 0, 0, 0, 13, 0, 0, 0, 1
14 };
15 static int interpret(int,int);
16 int main(void) {
17 /* int start, here, eax, ebx, esi, mode;
18 here = (int) memseg + 512;
19 mode = here;
20 *(int*) mode = 11; //executing
21 here += 4;
22 //string "interpret"
23 eax = here;
24 *(int*)here++ = 9;
25 *(char*)here++ = 'i';
26 *(char*)here++ = 'n';
27 *(char*)here++ = 't';
28 *(char*)here++ = 'e';
29 *(char*)here++ = 'r';
30 *(char*)here++ = 'p';
31 *(char*)here++ = 'r';
32 *(char*)here++ = 'e';
33 *(char*)here++ = 't';
34 here += 3; //align
35 //initial definition of "interpret"
36 esi = here;
37 *(int*)here++ = 4; //WORD
38 *(int*)here++ = 2; //LIT
39 *(int*)here++ = mode; //addr of mode var
40 *(int*)here++ = 10; //FETCH
41 *(int*)here++ = 12; //EXECUTE
42 *(int*)here++ = 13; //NOOP
43 *(int*)here++ = 1; //RET
44 //insert into vocab
45 vocab_set(eax, esi);
46 //string "quit"
47 ebx = esi;
48 eax = here;
49 *(int*)here++ = 4;
50 *(char*)here++ = 'q';
51 *(char*)here++ = 'u';
52 *(char*)here++ = 'i';
53 *(char*)here++ = 't';
54 //initial definition of "quit"
55 esi = here;
56 *(int*)here++ = 3; //rinit
57 *(int*)here++ = ebx; //interpret
58 *(int*)here++ = 9; //JMP
59 *(int*)here++ = esi; //jmp addr 0 (interpret)
60 //insert into vocab
61 vocab_set(eax, esi); */
62 return interpret((int)memseg, (int)memseg);
63 }
64 static int interpret(int esi, int here) {
65 int eax = 0, ebx = 0, ecx = 0, edi = 0;
66 next:
67 eax = *(int*) esi;
68 esi += 4;
69 exec:
70 switch(eax) {
71 case 0: //interpret
72 esi = eax;
73 eax = *(int*)eax;
74 goto next;
75 case 1: // ret (exit)
76 esi = rpop();
77 goto next;
78 case 2: // pushnext (lit)
79 push(*(int*)esi);
80 esi += 4;
81 goto next;
82 case 3: // rinit
83 rinit();
84 goto next;
85 case 4: // word
86 esi += eax;
87 eax += esi;
88 goto next;
89 case 5: // sys_read
90 sys_read(pop(),pop());
91 goto next;
92 case 6: // sys_request
93 sys_request(pop(),pop());
94 goto next;
95 case 7: // sys_write
96 sys_write(pop(),pop());
97 goto next;
98 case 8: // bye
99 break;
100 case 9: // goto
101 esi = *(int*) esi;
102 goto next;
103 case 10: // @ fetch
104 push(*(int*)pop());
105 goto next;
106 case 11: // ! set
107 *(int*)pop() = pop();
108 goto next;
109 case 12: // EXECUTE
110 eax = pop();
111 goto exec;
112 case 13: // NOOP
113 push(1);
114 goto next;
115 default: // eax is an addr, jump to it and push esi
116 rpush(esi);
117 esi = eax;
118 goto next;
119 }
120 return 0;
121 }