-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforth.c
68 lines (55 loc) · 1.76 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
//----------------------------------------------------------------------------//
//#include "io.h"
#include "match.h"
#include <stdio.h>
#include <string.h>
// Main Memory
char xxxx[100000];
char *mem = xxxx;
#define NEXT(xx) goto ***(xx ip)
#define ALLOC(ptr,x) ((ptr+=sizeof(x))-sizeof(x))
#define PUSH(ptr,x) *(--ptr) = x
#define POP(ptr) *(ptr++)
int main() {
/* Memory */
void *** ip; // Instruction Ptr
void **** dict = ALLOC(mem, void ***[100]); dict += 100; // Dictionary
long * stack = ALLOC(mem, long [10]); stack += 10; // Stack
void *** rstack = ALLOC(mem, void *[10]); rstack+= 10; // Return Stack
/* Core Words*/
void * call = && call;
void * retrn = && retrn;
void * quit = && quit;
void * input = && input;
void * print = && print;
/* Secondary Words */
void **indirect[] = { call, &input, &print, &retrn };
void **test[] = { &indirect, &quit };
/* Filling The Dictionary */
PUSH(dict, &test);
/* Run */
goto parse;
// defcall: match(stack, "call") ? ALLOC(mem, ptr) : NEXT(++);
call: puts("-> call"); // Call -> Creates local scope.
PUSH(rstack, ip);
ip = *ip;
NEXT(++);
retrn: puts("-> return"); // Retrn -> Exists local scope.
ip = POP(rstack);
NEXT(++);
quit: puts("-> quit"); // Quit -> Kills program.
return 0;
def: puts("-> def");
PUSH(dict, POP(stack));
NEXT(++);
parse: puts("-> parse");
ip = *dict;
NEXT();
input: puts("-> input");
gets(mem);
PUSH(stack, ALLOC(mem, strlen(mem)+1));
NEXT(++);
print: puts("-> print");
puts(POP(stack));
NEXT(++);
}