-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.c
More file actions
295 lines (256 loc) · 6.5 KB
/
interpreter.c
File metadata and controls
295 lines (256 loc) · 6.5 KB
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
/**
* @file interpreter.c
*
* @brief Forth outer, inner interpreter and related functions.
*/
#include "interpreter.h"
#include "builtins_common.h"
#include "emforth.h"
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
/* Forward declarations */
static int read_token(struct forth_ctx *ctx, char *token, size_t max_len);
static int parse_number(char *token, int token_len, stack_cell_t *number_p);
/* The inner interpreter - this is the heart of the Forth system */
static void inner_interpreter(struct forth_ctx *ctx)
{
while (ctx->ip != NULL) {
ctx->w = ctx->ip++;
word_t xt = *ctx->w;
if (xt != NULL) {
/* Check if this is a compiled reference to a colon
* definition */
word_t *word_ptr = (word_t *)xt;
if (*word_ptr == do_docol) {
/* This is a colon definition - call do_docol */
ctx->w = word_ptr;
do_docol(ctx);
continue;
}
/* Otherwise it's a primitive function pointer */
xt(ctx);
}
}
}
/**
* @brief Execute a word from the outer interpreter
*
* This sets up the initial state and runs the inner interpreter
*/
static void execute_word(struct forth_ctx *ctx, word_t *codeword_addr)
{
/* Save the current IP (should be NULL for top-level execution) */
word_t *saved_ip = ctx->ip;
/* Check if this is a primitive or colon definition */
word_t codeword = *codeword_addr;
if (codeword == do_docol) {
/* Colon definition - set up to run inner interpreter */
ctx->w = codeword_addr;
ctx->ip = codeword_addr + 1;
inner_interpreter(ctx);
} else {
/* Primitive word - execute directly */
ctx->w = codeword_addr;
codeword(ctx);
}
/* Restore IP */
ctx->ip = saved_ip;
}
/**
* @brief This is what is usually called the outer interpreter
*
* What it does:
* 1. Read a token from input
* 2. Try to parse as number
* 3. If number: push (immediate mode) or compile literal (compile mode)
* 4. If not number, then try to find it in the dictionary
* 5. If found: execute (immediate mode) or compile (compile mode)
* 6. Otherwise: error
*/
int outer_interpreter(struct forth_ctx *ctx)
{
char token[MAX_INPUT_LEN + 1];
int token_len;
while (1) {
token_len = read_token(ctx, token, MAX_INPUT_LEN);
if (token_len < 0) {
ctx->plat.puts("Error or EOF. Exiting.\n");
return -1;
}
if (token_len == 0) {
continue;
}
/* try to parse as number */
stack_cell_t number;
if (parse_number(token, token_len, &number) == 0) {
if (ctx->intrp_data.mode == MODE_IMMEDIATE) {
/* push number to stack */
stack_push(ctx, number);
} else {
/* compile literal */
compile_word(ctx, (stack_cell_t)do_lit);
compile_word(ctx, number);
}
} else {
/* Try to find word in dictionary */
dict_header_t *header =
find_word_header(ctx, token, token_len);
if (header) {
/* Calculate codeword pointer from header */
char *word_name = (char *)(header + 1);
word_t *codeword_addr =
(word_t *)(word_name +
ALIGN_UP_WORD_T(
header->flags.f.length));
if (ctx->intrp_data.mode == MODE_IMMEDIATE ||
header->flags.f.immediate) {
/* Execute the word */
execute_word(ctx, codeword_addr);
} else {
/* Compile mode - compile the word's
* address */
word_t codeword = *codeword_addr;
if (codeword == do_docol) {
/* Colon definition - compile
* the address of the word */
compile_word(ctx,
(stack_cell_t)
codeword_addr);
} else {
/* Primitive - compile the
* function pointer directly */
compile_word(
ctx,
(stack_cell_t)codeword);
}
}
} else {
ctx->plat.puts("Word not found: ");
ctx->plat.puts(token);
ctx->plat.puts("\n");
}
}
}
}
/**
* parsing decimal and hexadecimal is supported
*/
static int parse_number(char *token, int token_len, stack_cell_t *number_p)
{
long number;
if (token_len > 2 && token[0] == '0' &&
(token[1] == 'x' || token[1] == 'X')) {
for (int i = 2; i < token_len; i++) {
if (!isxdigit(token[i])) {
return -1;
}
}
number = strtol(token, NULL, 16);
} else {
for (int i = 0; i < token_len; i++) {
if (!isdigit(token[i])) {
return -1;
}
}
number = strtol(token, NULL, 10);
}
*number_p = number;
return 0;
}
void do_docol(struct forth_ctx *ctx)
{
/* Save return address on return stack */
if (ctx->rsp < RSTACK_SIZE_MAX) {
ctx->rstack[ctx->rsp++] = ctx->ip;
} else {
ctx->plat.puts("Return stack overflow\n");
return;
}
/* ctx->w should already point to the word being called */
/* Set IP to body of word (after the codeword) */
ctx->ip = ctx->w + 1;
}
void do_exit(struct forth_ctx *ctx)
{
/* Pop IP from return stack */
if (ctx->rsp > 0) {
ctx->ip = ctx->rstack[--ctx->rsp];
} else {
/* rstack underflow, we have finished execution */
ctx->ip = NULL;
}
}
void do_lit(struct forth_ctx *ctx)
{
stack_cell_t number = *(stack_cell_t *)(ctx->ip);
stack_push(ctx, number);
/* Skip over the literal value */
ctx->ip += 1;
}
/**
* @brief Initialize the outer interpreter
*/
void interpreter_init(struct forth_ctx *ctx)
{
ctx->intrp_data.mode = MODE_IMMEDIATE;
ctx->intrp_data.in_comment = false;
}
static int read_token(struct forth_ctx *ctx, char *token, size_t max_len)
{
int ch;
size_t pos = 0;
do {
ch = ctx->plat.getchar();
if (ch == EOF) {
return -1;
}
/* Handle backslash comments */
if (ch == '\\') {
ctx->intrp_data.in_comment = true;
} else if (ch == '\n' && ctx->intrp_data.in_comment) {
ctx->intrp_data.in_comment = false;
}
} while (isspace(ch) || ctx->intrp_data.in_comment);
while (!isspace(ch) && ch != EOF && pos < max_len) {
token[pos++] = ch;
ch = ctx->plat.getchar();
}
/* Null-terminate */
token[pos] = '\0';
return pos;
}
/**
* returns the pointer to the first word in the definition.
*/
dict_header_t *find_word_header(struct forth_ctx *ctx, const char *name,
size_t len)
{
dict_header_t *header = ctx->dict.latest;
while (header != DICT_NULL) {
/* we skip hidden words */
if (header->flags.f.hidden) {
header = header->link;
continue;
}
if (header->flags.f.length != len) {
header = header->link;
continue;
}
char *word_name = (char *)(header + 1);
if (memcmp(word_name, name, len) == 0) {
/* Found it - return pointer to header */
return header;
}
/* Move to previous word */
header = header->link;
}
return NULL;
}
/**
* 2cfa and 2dfa confusion, do we need both or just 2dfa?
* need to support strings
* else definition breaks if even if just compiled
*/