-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInnerInterpreter.c
208 lines (149 loc) · 3.3 KB
/
InnerInterpreter.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
#include "InnerInterpreter.h"
#include <malloc.h>
// stack storage
#define STACK_SIZE 1024
Object* DataStack;
Object* ReturnStack;
// Dictionary storage
#define DICTIONARY_SIZE 32768
Object* Dictionary;
void InitInterpreter()
{
// create stacks
DataStack = (Object*)malloc(MAX_OBJECT_SIZE * STACK_SIZE);
if (DataStack)
{
memset(DataStack, 0, MAX_OBJECT_SIZE * STACK_SIZE); // effectively fills it will NULLObjects
MakeObjectPtr(&DataStackPtr, DataStack);
}
ReturnStack = (Object*)malloc(MAX_OBJECT_SIZE * STACK_SIZE);
if (ReturnStack)
{
memset(ReturnStack, 0, MAX_OBJECT_SIZE * STACK_SIZE);
MakeObjectPtr(&ReturnStackPtr, ReturnStack);
}
// create Dictionary
Dictionary = (Object*)malloc(DICTIONARY_SIZE);
if (Dictionary)
{
memset(Dictionary, 0, DICTIONARY_SIZE);
MakeObjectPtr(&DictionaryPtr, Dictionary);
}
// init interpreter registers
MakeObjectPtr(&WordAddress, NULL);
MakeObjectPtr(&InstructionAddress, NULL);
}
void Push(Object* Stack, Object* Object)
{
IncPtr(Stack);
CopyObject(Object, Stack->Ptr);
}
void Pop(Object* Stack, Object* Object)
{
CopyObject(Stack->Ptr, Object);
DecPtr(Stack);
}
void LocalPop(Object* Stack, Object* Object)
{
LocalCopyObject(Stack->Ptr, Object);
DecPtr(Stack);
}
void* AllocNative(size_t Size)
{
void* dest = memset(DictionaryPtr.NativePtr, 0, Size);
DictionaryPtr.NativePtr += Size;
return dest;
}
void* _Run() // only used to enter interpreter
{
CodeAddress CodeAddressPtr = WordAddress.Ptr->CA;
IncPtr(&WordAddress);
return CodeAddressPtr(); // execute native code
}
void* _Next()
{
SetObjectPtr(&WordAddress, InstructionAddress.Ptr->Word->CodeBody);
IncPtr(&InstructionAddress);
CodeAddress CodeAddressPtr = WordAddress.Ptr->CA;
IncPtr(&WordAddress);
return CodeAddressPtr(); // execute native code
}
void* _Colon()
{
Push(&ReturnStackPtr, &InstructionAddress);
SetObjectPtr(&InstructionAddress, WordAddress.Ptr);
return _Next;
}
void* _Semi()
{
LocalPop(&ReturnStackPtr, &InstructionAddress);
return _Next;
}
void* _Terminate()
{
return NULL;
}
void* _Constant()
{
Push(&DataStackPtr, WordAddress.Ptr);
return _Next;
}
void* _Variable()
{
Push(&DataStackPtr, &WordAddress);
return _Next;
}
void* _DoRelativeJump()
{
// since everything is fixed, a simple direct jump is possible
InstructionAddress.Ptr += InstructionAddress.Ptr->i;
return _Next;
}
void* _DoLoop()
{
Object index;
Object limit;
LocalPop(&ReturnStackPtr, &index);
LocalPop(&ReturnStackPtr, &limit);
// (the Abs of this is the number of iterations left)
int w = index.i - limit.i;
// increment loop index
index.i += 1;
// check for end of looping (not sure how this works but I got it from another Forth implementation - it handles both inc & dec looping)
if (((index.i - limit.i) ^ w) > 0)
{
Push(&ReturnStackPtr, &limit);
Push(&ReturnStackPtr, &index);
_DoRelativeJump();
}
else
{
IncPtr(&InstructionAddress);
}
return _Next;
}
void* _DoConditionalRelativeJump()
{
Object flag;
LocalPop(&DataStackPtr, &flag);
if (flag.i)
{
IncPtr(&InstructionAddress);
}
else
{
_DoRelativeJump();
}
return _Next;
}
void Execute(Word* Word)
{
// set the word to execute
SetObjectPtr(&WordAddress, Word->CodeBody);
// start the interpreter loop
CodeAddress func = _Run;
do
{
func = func();
} while (func != NULL);
}