Repository navigation
Expand file tree
/
Copy pathShell.h
More file actions
1287 lines (1209 loc) · 29.8 KB
/
Copy pathShell.h
File metadata and controls
1287 lines (1209 loc) · 29.8 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Shell.h
* @version 2.0
*
* @section License
* Copyright (C) 2016, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef SHELL_H
#define SHELL_H
/**
* Application script dictionary entry (in program memory).
*/
struct script_t {
const char* name; //!< Script name (in program memory).
const char* code; //!< Script code (in program memory).
};
/**
* Create script name and script strings in program memory.
* @param[in] name identifier.
* @param[in] script string.
*/
#define SCRIPT(name,code) \
const char name ## _name[] PROGMEM = #name; \
const char name ## _code[] PROGMEM = code \
/**
* Create script entry in script table in program memory.
* @param[in] name identifier.
*/
#define SCRIPT_ENTRY(name) \
{ name ## _name, name ## _code }
/**
* Create last entry in script table in program memory.
*/
#define SCRIPT_NULL() { NULL, NULL }
/**
* Script Shell with stack machine instruction set. Instructions are
* printable characters so that command lines and scripts can be
* written directly.
* @param[in] STACK_MAX max stack depth (default 16).
* @param[in] VAR_MAX max number of variables (default 32).
* @param[in] FULL_OP_NAMES trace with operation name (default true).
*/
template<int STACK_MAX = 16, int VAR_MAX = 32, bool FULL_OP_NAMES = true>
class Shell {
public:
/**
* Construct a shell with given stream.
* @param[in] ios input output stream.
* @param[in] scripts null terminated vector of application scripts.
*/
Shell(Stream& ios, const script_t* scripts = NULL) :
m_scripts(scripts),
m_dp((char*) sizeof(m_dp) + sizeof(m_entries) + (sizeof(dict_t) * VAR_MAX)),
m_dict((dict_t*) sizeof(m_dp) + sizeof(m_entries)),
m_entries(0),
m_fp(m_stack + STACK_MAX),
m_sp(m_stack + STACK_MAX),
m_tos(0),
m_marker(-1),
m_trace(false),
m_cycle(0),
m_base(10),
m_ios(ios)
{
// Restore state from eeprom
uint8_t entries = eeprom_read_byte((const uint8_t*) sizeof(char*));
char* dp = (char*) eeprom_read_word(0);
// Check valid state before restore
if (dp != (char*) 0xffff && entries < VAR_MAX) {
m_entries = entries;
m_dp = dp;
for (uint8_t i = 0; i < entries; i++)
m_var[i] = eeprom_read_word((uint16_t*) &m_dict[i].value);
}
}
/**
* Set trace to given flag.
* @param[in] flag trace true/false.
*/
void trace(bool flag)
{
m_trace = flag;
}
/**
* Get trace mode.
* @return trace mode.
*/
bool trace() const
{
return (m_trace);
}
/**
* Get depth of parameter stack.
* @return number of elements on stack.
*/
int depth() const
{
return (m_stack + STACK_MAX - m_sp);
}
/**
* Get top of stack.
* @return top of stack.
*/
int tos() const
{
return (m_tos);
}
/**
* Set top of stack to given value.
* @param[in] value to set.
*/
void tos(int value)
{
m_tos = value;
}
/**
* Set top of stack to given value.
* @param[in] s to set.
*/
void tos(const char* s)
{
m_tos = (int) s;
}
/**
* Drop top element from parameter stack.
*/
void drop()
{
m_tos = (depth() > 0) ? *m_sp++ : 0;
}
/**
* Pop top element from parameter stack.
* @return top of stack.
*/
int pop()
{
int res = m_tos;
m_tos = (depth() > 0) ? *m_sp++ : 0;
return (res);
}
/**
* Push given value onto the parameter stack.
* @param[in] value to push.
*/
void push(int value)
{
if (depth() != STACK_MAX) *--m_sp = m_tos;
m_tos = value;
}
/**
* Push given value onto the parameter stack.
* @param[in] s string to push.
*/
void push(const char* s)
{
push((int) s);
}
/**
* Clear parameter stack.
*/
void clear()
{
m_sp = m_stack + STACK_MAX;
}
/**
* Print parameter stack contents in format: n: xn..x1
*/
void print() const
{
int n = depth();
m_ios.print(n);
m_ios.print(':');
if (n > 0) {
const int* tp = m_stack + STACK_MAX - 1;
while (--n) {
m_ios.print(' ');
m_ios.print(*--tp);
}
m_ios.print(' ');
m_ios.print(m_tos);
}
m_ios.println();
}
/**
* Read variable.
* @param[in] addr address.
* @return value.
*/
int read(int addr) const
{
if (addr >= 0 && addr < (VAR_MAX + STACK_MAX)) return (m_var[addr]);
return (-pgm_read_word(&m_scripts[addr - 0x4000].code));
}
/**
* Write variable.
* @param[in] addr address.
* @param[in] value to assign.
*/
void write(int addr, int value)
{
if (addr < 0 || addr >= (VAR_MAX + STACK_MAX)) return;
m_var[addr] = value;
}
/**
* Write variable.
* @param[in] addr address.
* @param[in] s script.
*/
void write(int addr, const char* s)
{
write(addr, (int) s);
}
/**
* Define a dictionary entry with given name and value.
* @param[in] var name string (in program memory).
* @param[in] value.
* @return index or negative error code.
*/
int set(const __FlashStringHelper* var, int value = 0)
{
char name[NAME_MAX];
strcpy_P(name, (const char*) var);
int i = lookup(name, strlen(name), true);
if (i < 0) return (i);
m_var[i] = value;
return (i);
}
/**
* Define a function with given name and script in data memory.
* @param[in] var name string.
* @param[in] script string in data memory.
* @return index or negative error code.
*/
int set(const __FlashStringHelper* var, const char* script)
{
return (set(var, (int) script));
}
/**
* Define a function with given name and script in program memory.
* @param[in] var name string.
* @param[in] script in program memory.
* @return index or negative error code.
*/
int set(const __FlashStringHelper* var, const __FlashStringHelper* script)
{
return (set(var, (int) m_progmem.as_addr((const char*) script)));
}
/**
* List dictionaries; eeprom and progmem.
*/
void words()
{
const char* np;
char c;
uint8_t i = 0;
// List scripts in eeprom dicionary (dynamic)
m_ios.print(m_eeprom.prefix());
m_ios.print(F(": "));
for (; i < m_entries; i++) {
np = (const char*) eeprom_read_word((const uint16_t*) &m_dict[i].name);
while ((c = (char) eeprom_read_byte((const uint8_t*) np++)) != 0)
m_ios.print(c);
m_ios.print(' ');
}
m_ios.println();
// List scripts in program memory dicionary (static)
m_ios.print(m_progmem.prefix());
m_ios.print(F(": "));
i = 0;
while ((np = (const char*) pgm_read_word(&m_scripts[i].name)) != NULL) {
m_ios.print((const __FlashStringHelper*) np);
m_ios.print(' ');
i += 1;
}
m_ios.println();
}
/**
* Non-blocking read next character from shell stream. If available
* add to buffer. If newline was read the buffer is null-terminated
* and true is returned, otherwise false.
* @param[in,out] bp buffer pointer.
* @return true if line was read otherwise false.
*/
bool read(char* &bp)
{
int c = m_ios.read();
if (c < 0) return (false);
*bp++ = c;
if (c != '\n') return (false);
*bp = 0;
m_cycle = 0;
return (true);
}
/**
* Execute given script (null terminated sequence of operation
* codes). Return NULL if successful otherwise script reference that
* failed. Prints error position in trace mode.
* @param[in] script.
* @return script reference or NULL.
*/
const char* execute(const char* script)
{
Memory* mem = access(script);
next_fn next = mem->get_next_fn();
const char* ip = mem->as_local(script);
bool neg = false;
int base = 10;
int* fp = m_fp;
size_t len = 0;
int w, n, addr, pin;
const char* sp;
char op;
// Execute operation code in script
while ((op = next(ip++)) != 0) {
// Check for negative numbers
if (op == '-') {
op = next(ip);
if (op < '0' || op > '9') {
op = '-';
}
else {
neg = true;
ip += 1;
}
}
// Check for base prefix
else if (op == '0') {
op = next(ip++);
if (op == 'x') base = 16;
else if (op == 'b') base = 2;
else ip -= 2;
op = next(ip++);
}
// Check for literal numbers
if (is_digit(op, base)) {
int w = 0;
do {
if (base == 16 && op >= 'a')
w = (w * base) + (op - 'a') + 10;
else
w = (w * base) + (op - '0');
op = next(ip++);
} while (is_digit(op, base));
if (neg) {
w = -w;
neg = false;
}
push(w);
base = 10;
if (op == 0) break;
}
// Translate newline (for trace mode)
if (op == '\n') op = 'N';
// Check for trace mode
if (m_trace) {
m_ios.print(++m_cycle);
m_ios.print(':');
m_ios.print(mem->prefix());
m_ios.print('/');
m_ios.print((int) ip - 1);
m_ios.print(':');
const class __FlashStringHelper* str = as_fstr(op);
if (str == NULL)
m_ios.print(op);
else
m_ios.print(str);
if (op != '`' && op != ':') {
m_ios.print(':');
print();
}
}
// Execute operation or parse special form
char left = 0, right;
switch (op) {
/*
* No operations.
*/
case ' ': // -- | no operation
case ',':
continue;
/*
* Arithmetic operators.
*/
case 'n': // x -- -x | negate
tos(-tos());
continue;
case '+': // x y -- x+y | addition
w = pop();
tos(tos() + w);
continue;
case '-': // x y -- x-y | subtraction
w = pop();
tos(tos() - w);
continue;
case '*': // x y -- x*y | multiplication
w = pop();
tos(tos() * w);
continue;
case '/': // x y -- x/y | division
w = pop();
tos(tos() / w);
break;
case '%': // x y -- x%y | modulo
w = pop();
tos(tos() % w);
break;
case 'h': // x y z -- x*y/z | scale
w = pop();
n = pop();
tos(tos() * ((long) n) / w);
break;
/*
* Comparison/relational operators.
*/
case 'F': // -- false | false
push(0);
continue;
case 'T': // -- true | true
push(-1);
continue;
case '=': // x y -- x==y | equal
w = pop();
tos(as_bool(tos() == w));
continue;
case '#': // x y -- x!=y | not equal
w = pop();
tos(as_bool(tos() != w));
continue;
case '<': // x y -- x<y | less than
w = pop();
tos(as_bool(tos() < w));
continue;
case '>': // x y -- x>y | greater than
w = pop();
tos(as_bool(tos() > w));
continue;
/*
* Bitwise/logical operators.
*/
case '~': // x -- ~x | bitwise not
tos(~tos());
continue;
case '&': // x y -- x&y | bitwise and
w = pop();
tos(tos() & w);
continue;
case '|': // x y -- x|y | bitwise or
w = pop();
tos(tos() | w);
continue;
case '^': // x y -- x^y | bitwise xor
w = pop();
tos(tos() ^ w);
continue;
/*
* Stack operations.
*/
case 'c': // xn..x1 n -- | drop n stack elements
n = tos();
if (n > 0 && n < depth()) {
m_sp += n;
}
case 'd': // x -- | drop
drop();
continue;
case 'g': // xn..x1 n -- xn-1..x1 xn | rotate n-elements
n = tos();
if (n > 0 && n < depth()) {
tos(m_sp[--n]);
for (; n > 0; n--)
m_sp[n] = m_sp[n - 1];
m_sp += 1;
}
else drop();
continue;
case 'j': // xn..x1 -- xn..x1 n | stack depth
push(depth());
continue;
case 'o': // x y -- x y x | over
push(*m_sp);
continue;
case 'p': // xn..x1 n -- xn..x1 xn | pick
tos(*(m_sp + tos() - 1));
continue;
case 'r': // x y z --- y z x | rotate
w = tos();
tos(*(m_sp + 1));
*(m_sp + 1) = *m_sp;
*m_sp = w;
continue;
case 's': // x y -- y x | swap
w = tos();
tos(*m_sp);
*m_sp = w;
continue;
case 'q': // x -- [x x] or 0 | duplicate if not zero
if (tos() == 0) continue;
case 'u': // x -- x x | duplicate
push(tos());
continue;
/*
* Memory access operations.
*/
case '@': // addr -- value | read variable
tos(read(tos()));
continue;
case '!': // value addr -- | write variable
addr = pop();
w = pop();
write(addr, w);
continue;
case 'z': // addr -- | write variable to eeprom
w = pop();
if (w >= 0 && w < m_entries)
eeprom_write_word((uint16_t*) &m_dict[w].value, (uint16_t) m_var[w]);
continue;
case 'a': // -- bytes entries | allocated eeprom
push((int) m_dp);
push(m_entries);
continue;
/*
* Stack frame operations.
*/
case '\\':
n = pop();
// x1..xn n -- x1..xn | mark n-element stack frame
if (n > 0) {
m_fp = m_sp + n - 1;
}
// x1..xn y1..ym n -- y1..ym | resolve n-element stack frame
else {
n = m_fp - m_sp + n;
if (n >= 0) {
while (n--) *--m_fp = m_sp[n];
m_sp = m_fp;
}
else {
m_sp = m_fp;
drop();
}
}
continue;
case '$': // n -- addr | address of n-element in frame
n = tos();
tos((m_fp - n) - m_var);
continue;
/*
* Input/output operations.
*/
case 'k': // -- char | blocking read from input stream
while ((w = m_ios.read()) < 0) yield();
push(w);
continue;
case 'b': // base -- | number print base
m_base = pop();
continue;
case '?': // addr -- | print variable
tos(read(tos()));
case '.': // x -- | print number followed by one space
w = pop();
if (m_base == 2) m_ios.print(F("0b"));
else if (m_base == 8) m_ios.print(F("0"));
else if (m_base == 16) m_ios.print(F("0x"));
m_ios.print(w, m_base > 0 ? m_base : -m_base);
m_ios.print(' ');
continue;
case 'm': // -- | write new line to output stream
m_ios.println();
continue;
case 't': // addr -- | write variable name output stream
addr = tos();
if (addr >= 0 && addr < m_entries) {
const uint8_t* np =
(const uint8_t*) eeprom_read_word((const uint16_t*) &m_dict[addr].name);
char c;
while ((c = eeprom_read_byte(np++)) != 0)
m_ios.print(c);
m_ios.print(' ');
tos(-1);
}
else tos(0);
continue;
case 'v': // char -- | write character to output stream
w = pop();
m_ios.write(w);
continue;
/*
* Control structure operations.
*/
case 'e': // flag if-block else-block -- | execute block on flag
w = *(m_sp + 1);
if (w != 0) {
drop();
sp = (const char*) pop();
}
else {
sp = (const char*) pop();
drop();
}
drop();
if (execute(sp) != NULL) goto error;
continue;
case 'f': // addr -- | forget variable
w = pop();
if (w >= 0 && w < m_entries) {
m_entries = w;
m_dp = (char*) eeprom_read_word((const uint16_t*) &m_dict[w].name);
eeprom_update_block(&m_dp, 0, sizeof(m_dp));
eeprom_update_byte((uint8_t*) sizeof(m_dp), m_entries);
}
continue;
case 'i': // flag block -- | execute block if flag is true
sp = (const char*) pop();
if (pop() && execute(sp) != NULL) goto error;
continue;
case 'l': // low high block( i -- ) -- | execute block from low to high
{
sp = (const char*) pop();
int high = pop();
int low = pop();
for (int i = low; i <= high; i++) {
push(i);
if (execute(sp) != NULL) goto error;
}
}
continue;
case 'w': // block( -- flag) -- | execute block while
sp = (const char*) pop();
do {
if (execute(sp) != NULL) goto error;
} while (pop());
continue;
case 'x': // script -- | execute script
sp = (const char*) pop();
if (execute(sp) != NULL) goto error;
continue;
case 'y': // -- | yield
yield();
continue;
/*
* Script operations.
*/
case ':': // -- addr | lookup and append variable/function
case '`': // -- | lookup and execute function
{
char name[NAME_MAX];
size_t len = 0;
bool flag = (op == ':');
// Scan variable/function name
op = next(ip);
if (!isalpha(op)) goto error;
name[len++] = op;
while (((op = next(++ip)) != 0) && isalnum(op))
name[len++] = op;
name[len] = 0;
if (m_trace) {
m_ios.print(name);
m_ios.print(':');
print();
}
if (len == 0) goto error;
// Lookup in dictionaries with append flag
int i = lookup(name, len, flag);
if (i < 0) goto error;
// Push address on colon and execute on quote
if (flag) {
if (i < VAR_MAX)
push(i);
else push((i - VAR_MAX) + 0x4000);
}
else if (i < VAR_MAX) {
sp = (const char*) read(i);
if (sp == NULL) goto error;
if (execute(sp) != NULL) goto error;
}
else {
i = i - VAR_MAX;
sp = (const char*) pgm_read_word(&m_scripts[i].code);
if (execute(m_progmem.as_addr(sp)) != NULL) goto error;
}
}
continue;
case ';': // addr block -- | copy block to variable
{
const char* dest = m_eeprom.as_addr(m_dp);
const char* src = (const char*) pop();
int addr = pop();
if (addr >= 0 && addr < m_entries) {
eeprom_update_block(src, m_dp, len);
m_dp += len;
eeprom_update_byte((uint8_t*) m_dp, 0);
m_dp += 1;
eeprom_update_block(&m_dp, 0, sizeof(m_dp));
write(addr, dest);
eeprom_write_word((uint16_t*) &m_dict[addr].value, (uint16_t) dest);
}
}
continue;
case '{': // -- block | start code block
left = '{';
right = '}';
push(mem->as_addr(ip));
break;
case '}': // -- | end of block
return (NULL);
case '(': // -- | start output string
left = '(';
right = ')';
break;
case '[': // -- | start stack marker
if (m_marker == -1) {
m_marker = depth();
continue;
}
break;
case ']': // xn..x1 -- n | end stack marker
if (m_marker != -1) {
push(depth() - m_marker);
m_marker = -1;
continue;
}
case '\'': // -- char | push character
op = next(ip);
if (op != 0) {
push(op);
ip += 1;
}
continue;
/*
* Arduino operations.
*/
case 'A': // pin -- sample | analogRead(pin)
pin = tos();
tos(analogRead(pin));
continue;
case 'C': // xn..x1 -- | clear
clear();
continue;
case 'D': // ms -- | delay()
w = pop();
delay((unsigned) w);
continue;
case 'E': // period addr -- bool | time-out
addr = pop();
w = read(addr);
n = tos();
if ((((unsigned) millis() & 0xffff) - ((unsigned) w)) >= ((unsigned) n)) {
tos(-1);
write(addr, millis());
}
else tos(0);
continue;
case 'H': // pin -- | digitalWrite(pin, HIGH)
pin = pop();
digitalWrite(pin, HIGH);
continue;
case 'I': // pin -- | pinMode(pin, INPUT)
pin = pop();
pinMode(pin, INPUT);
continue;
case 'K': // -- [char true] or false | non-blocking read from input stream
w = m_ios.read();
if (w < 0) {
push(0);
} else {
push(w);
push(-1);
}
continue;
case 'L': // pin -- | digitalWrite(pin, LOW)
pin = pop();
digitalWrite(pin, LOW);
continue;
case 'M': // -- ms | millis()
push(millis());
continue;
case 'N': // -- | no operation
continue;
case 'O': // pin -- | pinMode(pin, OUTPUT)
pin = pop();
pinMode(pin, OUTPUT);
continue;
case 'P': // value pin -- | analogWrite(pin, value)
pin = pop();
w = pop();
analogWrite(pin, w);
continue;
case 'R': // pin -- bool | digitalRead(pin)
pin = tos();
tos(as_bool(digitalRead(pin)));
continue;
case 'S': // -- | print stack contents
print();
continue;
case 'U': // pin -- | pinMode(pin, INPUT_PULLUP)
pin = pop();
pinMode(pin, INPUT_PULLUP);
continue;
case 'W': // value pin -- | digitalWrite(pin, value)
pin = pop();
w = pop();
digitalWrite(pin, w);
continue;
case 'X': // pin -- | digitalToggle(pin)
pin = pop();
digitalWrite(pin, !digitalRead(pin));
continue;
case 'Y': // -- | words
words();
continue;
case 'Z': // -- | toggle trace mode
m_trace = !m_trace;
continue;
case TRAP_OP_CODE:
sp = trap(ip);
if (sp == NULL) goto error;
ip = sp;
continue;
default:
goto error;
}
// Parse special forms (allow nesting)
if (left) {
int n = 1;
while ((n != 0) && ((op = next(ip++)) != 0)) {
if (op == left) n++;
else if (op == right) n--;
if (left == '(' && n > 0) m_ios.print(op);
}
if (op == '}') {
const char* src = (const char*) tos();
len = ip - src - 1;
}
if (op == 0) {
ip = ip - 1;
op = left;
break;
}
}
}
// Restore frame pointer
m_fp = fp;
// Check for no errors
if (op == 0 || op == '}') return (NULL);
// Check for trace mode and error print
error:
ip = ip - 1;
if (m_trace && mem == &m_memory) {
m_ios.print(script);
if (script[strlen(script) - 1] != '\n') m_ios.println();
for (int i = 0, n = ip - script; i < n; i++)
m_ios.print(' ');
m_ios.println(F("^--?"));
}
// Return error position
return (ip);
}
/**
* Execute given script in program memory (null terminated sequence
* of operation codes). Return NULL if successful otherwise script
* reference that failed. Prints error position in trace mode.
* @param[in] script program memory based script.
* @return script reference or NULL.
*/
const char* execute(const __FlashStringHelper* script)
{
return (execute(m_progmem.as_addr((const char*) script)));
}
/**
* Execute script with extended operation code (character). Return
* next script reference if successful otherwise NULL.
* @param[in] ip script.
* @return script reference or NULL.
*/
virtual const char* trap(const char*)
{
return (NULL);
}
protected:
/** Max length of name. */
static const size_t NAME_MAX = 16;
/** Trap operation code prefix. */
static const char TRAP_OP_CODE = '_';
/**
* Return next token from given source.
* @param[in] src source pointer.
* @return next token.
*/
typedef char (*next_fn)(const char* src);
/** Dictionary entry (in eeprom). */
struct dict_t {
const char* name; //!< Name string (in eeprom).
int value; //!< Value persistent.
};
const script_t* m_scripts; //!< Application scripts (in progmem).
char* m_dp; //!< Dictionary pointer (in eeprom).
dict_t* m_dict; //!< Dictionary (in eeprom).
uint8_t m_entries; //!< Dictionary entries.
int* m_fp; //!< Frame pointer.
int* m_sp; //!< Stack pointer.
int m_tos; //!< Top of stack register.
int m_marker; //!< Stack marker.
bool m_trace; //!< Trace mode.
unsigned m_cycle; //!< Cycle counter.
int m_base; //!< Number print base.
Stream& m_ios; //!< Input/output Stream.
int m_var[VAR_MAX]; //!< Variable table.
int m_stack[STACK_MAX]; //!< Parameter stack.
/**
* Map given integer value to boolean (true(-1) and false(0)).
* @param[in] val value.
* @return bool.
*/
int as_bool(int value)
{
return (value ? -1 : 0);
}
/**
* Check that the given character is a digit in the given base.
* @param[in] c character.
* @param[in] base must be bin, dec or hex.
* @return bool.
*/
bool is_digit(char c, int base)
{
if (base == 2)
return (c >= '0' && c <= '1');
if (base == 16 && c >= 'a')
return (c >= 'a' && c <= 'f');
return (c >= '0' && c <= '9');
}
/**
* Return full operation code name.
* @param[in] op operation code (character).
* @return program memory string or NULL.