-
Notifications
You must be signed in to change notification settings - Fork 4
/
Interpreter.ino
403 lines (346 loc) · 7.66 KB
/
Interpreter.ino
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
#include <avr/pgmspace.h>
/* Tiny interpreter,
similar to myforth's Standalone Interpreter
This example code is in the public domain */
/* Structure of a dictionary entry */
typedef struct {
const char *name;
void (*function)();
} entry;
/* Data stack for parameter passing
This "stack" is circular,
like a Green Arrays F18A data stack,
so overflow and underflow are not possible
Number of items must be a power of 2 */
const int STKSIZE = 8;
const int STKMASK = 7;
int stack[STKSIZE];
int p = 0;
/* TOS is Top Of Stack */
#define TOS stack[p]
/* NAMED creates a string in flash */
#define NAMED(x, y) PROGMEM const char x[]=y
/* Terminal Input Buffer for interpreter */
const byte maxtib = 16;
char tib[maxtib];
/* buffer required for strings read from flash */
char namebuf[maxtib];
byte pos;
/* push n to top of data stack */
void push(int n) {
p=(p+1)&STKMASK;
TOS=n;
}
/* return top of stack */
int pop() {
int n=TOS;
p=(p-1)&STKMASK;
return n;
}
/* Global delay timer */
int spd = 15;
/* top of stack becomes current spd */
NAMED(_speed, "speed");
void speed() { spd=pop();}
/* discard top of stack */
NAMED(_drop, "drop");
void drop() { pop();}
/* recover dropped stack item */
NAMED(_back, "back");
void back() { for(int i=1; i<STKSIZE; i++) drop();}
/* copy top of stack */
NAMED(_dup, "dup");
void dup() { push(TOS);}
/* exchange top two stack items */
NAMED(_swap, "swap");
void swap() {
int a;
int b;
a = pop();
b = pop();
push(a);
push(b);
}
/* copy second on stack to top */
NAMED(_over, "over");
void over() {
int a;
int b;
a = pop();
b = pop();
push(b);
push(a);
push(b);
}
/* add top two items */
NAMED(_add, "+");
void add() {
int a = pop();
TOS=a+TOS;
}
/* bitwise and top two items */
NAMED(_and, "and");
void and_() {
int a = pop();
TOS=a&TOS;
}
/* inclusive or top two items */
NAMED(_or, "or");
void or_() {
int a = pop();
TOS=a|TOS;
}
/* exclusive or top two items */
NAMED(_xor, "xor");
void xor_() {
int a = pop();
TOS=a^TOS;
}
/* invert all bits in top of stack */
NAMED(_invert, "invert");
void invert() { TOS=~(TOS);}
/* negate top of stack */
NAMED(_negate, "negate");
void negate() { TOS=-(TOS);}
/* destructively display top of stack, decimal */
NAMED(_dot, ".");
void dot() {
Serial.print(pop());
Serial.print(" ");
}
/* destructively display top of stack, hex */
NAMED(_dotHEX, ".h");
void dotHEX() {
Serial.print(0xffff & pop(), HEX);
Serial.print(" ");
}
/* display whole stack, hex */
NAMED(_dotShex, ".sh");
void dotShex() {
for(int i=0; i<STKSIZE; i++) dotHEX();
}
/* display whole stack, decimal */
NAMED(_dotS, ".s");
void dotS() { for(int i=0; i<STKSIZE; i++) dot();}
/* delay TOS # of milliseconds */
NAMED(_delay, "delay");
void del() { delay(pop());}
/* Toggle pin at TOS and delay(spd), repeat... */
NAMED(_wiggle, "wiggle");
void wiggle() {
int a=pop();
pinMode(a, OUTPUT);
for(int i=0; i<20; i++) {
digitalWrite(a, HIGH);
delay(100);
digitalWrite(a, LOW);
delay(100);
}
}
/* TOS is pin number, set it HIGH */
NAMED(_high, "high");
void high() { digitalWrite(pop(), HIGH);}
/* set TOS pin LOW */
NAMED(_low, "low");
void low() { digitalWrite(pop(), LOW);}
/* read TOS pin */
NAMED(_in, "in");
void in() { TOS=digitalRead(TOS);}
/* make TOS pin an input */
NAMED(_input, "input");
void input() { pinMode(pop(), INPUT);}
/* make TOS pin an output */
NAMED(_output, "output");
void output() { pinMode(pop(), OUTPUT);}
/* make TOS pin an input with weak pullup */
NAMED(_input_pullup, "input_pullup");
void input_pullup() { pinMode(pop(), INPUT_PULLUP);}
/* read flash byte at TOS address */
NAMED(_fetchp, "@p");
void fetchp() { TOS=pgm_read_byte_near(TOS);}
/* dump 16 bytes of flash in hex with ascii on the side */
void dumpFLASH() {
char buffer[5]="";
int p=pop();
sprintf(buffer, "%4x", p);
Serial.print(buffer);
Serial.print(" ");
for(int i=0; i<16; i++) {
sprintf(buffer, " %2x", pgm_read_byte_near(p++));
Serial.print(buffer);
}
p=p-16;
Serial.print(" ");
for(int i=0; i<16; i++) {
buffer[0]=pgm_read_byte_near(p++);
if(buffer[0]>0x7f || buffer[0]<' ') buffer[0]='.';
buffer[1]='\0';
Serial.print(buffer);
}
push(p);
}
/* dump 256 bytes of flash */
NAMED(_dumps, "d");
void dumps() {
for(int i=0; i<16; i++) {
Serial.println();
dumpFLASH();
}
}
/* dump 16 bytes of RAM in hex with ascii on the side */
void dumpRAM() {
char buffer[5]="";
char *ram;
int p=pop();
ram = (char*)p;
sprintf(buffer, "%4x", p);
Serial.print(buffer);
Serial.print(" ");
for(int i=0; i<16; i++) {
char c=*ram++;
sprintf(buffer, " %2x", (c&0xff));
Serial.print(buffer);
}
ram = (char*)p;
Serial.print(" ");
for(int i=0; i<16; i++) {
buffer[0]=*ram++;
if(buffer[0]>0x7f || buffer[0]<' ') buffer[0]='.';
buffer[1]='\0';
Serial.print(buffer);
}
push(p+16);
}
/* dump 256 bytes of RAM */
NAMED(_dumpr, "r");
void rdumps() {
for(int i=0; i<16; i++) {
Serial.println();
dumpRAM();
}
}
/* End of Forth interpreter words */
/* ******************************************** */
/* Beginning of application words */
/* End of application words */
/* ******************************************** */
/* Now build the dictionary */
/* empty words don't cause an error */
NAMED(_nop, " ");
void nop() { }
/* Forward declaration required here */
NAMED(_words, "words");
void words();
/* table of names and function addresses in flash */
const entry dictionary[] = {
{_nop, nop},
{_words, words},
{_dup, dup},
{_drop, drop},
{_back, back},
{_swap, swap},
{_over, over},
{_add, add},
{_and, and_},
{_or, or_},
{_xor, xor_},
{_invert, invert},
{_negate, negate},
{_dotS, dotS},
{_dotShex, dotShex},
{_dot, dot},
{_dotHEX, dotHEX},
{_delay, del},
{_high, high},
{_low, low},
{_in, in},
{_input, input},
{_output, output},
{_input_pullup, input_pullup},
{_wiggle, wiggle},
{_fetchp, fetchp},
{_dumps, dumps},
{_dumpr, rdumps},
{_speed, speed}
};
/* Number of words in the dictionary */
const int entries = sizeof dictionary / sizeof dictionary[0];
/* Display all words in dictionary */
void words() {
for(int i=entries-1; i>=0; i--) {
strcpy_P(namebuf, dictionary[i].name);
Serial.print(namebuf);
Serial.print(" ");
}
}
/* Find a word in the dictionary, returning its position */
int locate() {
for(int i=entries; i>=0; i--) {
strcpy_P(namebuf, dictionary[i].name);
if(!strcmp(tib, namebuf)) return i;
}
return 0;
}
/* Is the word in tib a number? */
int isNumber() {
char *endptr;
strtol(tib, &endptr, 0);
if(endptr == tib) return 0;
if(*endptr != '\0') return 0;
return 1;
}
/* Convert number in tib */
int number() {
char *endptr;
return (int) strtol(tib, &endptr, 0);
}
char ch;
void ok() { if(ch == '\r') Serial.println("ok");}
/* Incrementally read command line from serial port */
byte reading() {
if (!Serial.available()) return 1;
ch = Serial.read();
if (ch == '\n') return 1;
if (ch == '\r') return 0;
if (ch == ' ') return 0;
if (pos < maxtib) {
tib[pos++] = ch;
tib[pos] = 0;
}
return 1;
}
/* Block on reading the command line from serial port */
/* then echo each word */
void readword() {
pos = 0;
tib[0]=0;
while(reading());
Serial.print(tib);
Serial.print(" ");
}
/* Run a word via its name */
void runword() {
int place = locate();
if(place != 0) {
dictionary[place].function();
ok();
return;
}
if(isNumber()) {
push(number());
ok();
return;
}
Serial.println("?");
}
/* Arduino main loop */
void setup() {
Serial.begin(115200);
Serial.println("Tiny Interpreter:");
words();
Serial.println();
}
void loop() {
readword();
runword();
}