forked from sdwood68/YAFFA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.ino
2805 lines (2536 loc) · 79.3 KB
/
Dictionary.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
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
/******************************************************************************/
/** YAFFA - Yet Another Forth for Arduino **/
/** **/
/** File: Dictionary.ino **/
/** Copyright (C) 2012 Stuart Wood ([email protected]) **/
/** **/
/** This file is part of YAFFA. **/
/** **/
/** YAFFA is free software: you can redistribute it and/or modify **/
/** it under the terms of the GNU General Public License as published by **/
/** the Free Software Foundation, either version 2 of the License, or **/
/** (at your option) any later version. **/
/** **/
/** YAFFA 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 General Public License for more details. **/
/** **/
/** You should have received a copy of the GNU General Public License **/
/** along with YAFFA. If not, see <http://www.gnu.org/licenses/>. **/
/** **/
/******************************************************************************/
#include "YAFFA.h"
const char not_done_str[] PROGMEM = " NOT Implemented Yet \n\r";
/******************************************************************************/
/** Primitives for Control Flow **/
/******************************************************************************/
const PROGMEM char jump_str[] = "jump";
void _jump(void) {
ip = (cell_t*)((size_t)ip + *ip);
}
const PROGMEM char zjump_str[] = "zjump";
void _zjump(void) {
if (!pop()) ip = (cell_t*)((size_t)ip + *ip);
else ip++;
}
const PROGMEM char subroutine_str[] = "subroutine";
void _subroutine(void) {
*pDoes = (cell_t)*ip++;
}
const PROGMEM char do_sys_str[] = "do-sys";
// ( n1|u1 n2|u2 -- ) (R: -- loop_sys )
// Set up loop control parameters with index n2|u2 and limit n1|u1. An ambiguous
// condition exists if n1|u1 and n2|u2 are not the same type. Anything already
// on the return stack becomes unavailable until the loop-control parameters
// are discarded.
void _do_sys(void) {
rPush(LOOP_SYS);
rPush(pop()); // push index on to return stack
rPush(pop()); // push limit on to return stack
}
const PROGMEM char loop_sys_str[] = "loop-sys";
// ( n1|u1 n2|u2 -- ) (R: -- loop_sys )
// Set up loop control parameters with index n2|u2 and limit n1|u1. An ambiguous
// condition exists if n1|u1 and n2|u2 are not the same type. Anything already
// on the return stack becomes unavailable until the loop-control parameters
// are discarded.
void _loop_sys(void) {
cell_t limit = rPop(); // fetch limit
cell_t index = rPop(); // fetch index
index++;
if (limit - index) {
rPush(index);
rPush(limit);
ip = (cell_t*)*ip;
} else {
ip++;
if (rPop() != LOOP_SYS) {
push(-22);
_throw();
return;
}
}
}
const PROGMEM char leave_sys_str[] = "leave-sys";
// ( -- ) (R: loop-sys -- )
// Discard the current loop control parameters. An ambiguous condition exists
// if they are unavailable. Continue execution immediately following the
// innermost syntactically enclosing DO ... LOOP or DO ... +LOOP.
void _leave_sys(void) {
rPop(); // fetch limit
rPop(); // fetch index
if (rPop() != LOOP_SYS) {
push(-22);
_throw();
return;
}
ip = (cell_t*)*ip;
}
const PROGMEM char plus_loop_sys_str[] = "plus_loop-sys";
// ( n1|u1 n2|u2 -- ) (R: -- loop_sys )
// Set up loop control parameters with index n2|u2 and limit n1|u1. An ambiguous
// condition exists if n1|u1 and n2|u2 are not the same type. Anything already
// on the return stack becomes unavailable until the loop-control parameters
// are discarded.
void _plus_loop_sys(void) {
cell_t limit = rPop(); // fetch limit
cell_t index = rPop(); // fetch index
index += pop();
if (limit != index) {
rPush(index);
rPush(limit);
ip = (cell_t*)*ip;
} else {
ip++;
if (rPop() != LOOP_SYS) {
push(-22);
_throw();
return;
}
}
}
/*******************************************************************************/
/** Core Forth Words **/
/*******************************************************************************/
const PROGMEM char store_str[] = "!";
// ( x a-addr --)
// Store x at a-addr
void _store(void) {
cell_t* address = (cell_t*)pop();
*address = pop();
}
const PROGMEM char number_sign_str[] = "#";
// ( ud1 -- ud2)
// Divide ud1 by number in BASE giving quotient ud2 and remainder n. Convert
// n to external form and add the resulting character to the beginning of the
// pictured numeric output string.
void _number_sign(void) {
udcell_t ud;
ud = (udcell_t)pop() << sizeof(ucell_t) * 8;
ud += (udcell_t)pop();
*--pPNO = pgm_read_byte(&charset[ud % base]);
ud /= base;
push((ucell_t)ud);
push((ucell_t)(ud >> sizeof(ucell_t) * 8));
}
const PROGMEM char number_sign_gt_str[] = "#>";
// ( xd -- c-addr u)
// Drop xd. Make the pictured numeric output string available as a character
// string c-addr and u specify the resulting string. A program may replace
// characters within the string.
void _number_sign_gt(void) {
_two_drop();
push((size_t)pPNO);
push((size_t)strlen(pPNO));
flags &= ~NUM_PROC;
}
const PROGMEM char number_sign_s_str[] = "#s";
// ( ud1 -- ud2)
void _number_sign_s(void) {
udcell_t ud;
ud = (udcell_t)pop() << sizeof(ucell_t) * 8;
ud += (udcell_t)pop();
while (ud) {
*--pPNO = pgm_read_byte(&charset[ud % base]);
ud /= base;
}
push((ucell_t)ud);
push((ucell_t)(ud >> sizeof(ucell_t) * 8));
}
const PROGMEM char tick_str[] = "'";
// ( "<space>name" -- xt)
// Skip leading space delimiters. Parse name delimited by a space. Find name and
// return xt, the execution token for name. An ambiguous condition exists if
// name is not found. When interpreting "' xyz EXECUTE" is equivalent to xyz.
void _tick(void) {
// push(' ');
// _word();
// _find();
// pop();
if (getToken()) {
if (isWord(cTokenBuffer)) {
push(w);
return;
}
}
push(-13);
_throw();
}
const PROGMEM char paren_str[] = "(";
// ( "ccc<paren>" -- )
// imedeate
void _paren(void) {
push(')');
_word();
_drop();
}
const PROGMEM char star_str[] = "*";
// ( n1|u1 n2|u2 -- n3|u3 )
// multiply n1|u1 by n2|u2 giving the product n3|u3
void _star(void) {
push(pop() * pop());
}
const PROGMEM char star_slash_str[] = "*/";
// ( n1 n2 n3 -- n4 )
// multiply n1 by n2 producing the double cell result d. Divide d by n3
// giving the single-cell quotient n4.
void _star_slash(void) {
cell_t n3 = pop();
cell_t n2 = pop();
cell_t n1 = pop();
dcell_t d = (dcell_t)n1 * (dcell_t)n2;
push((cell_t)(d / n3));
}
const PROGMEM char star_slash_mod_str[] = "*/mod";
// ( n1 n2 n3 -- n4 n5)
// multiply n1 by n2 producing the double cell result d. Divide d by n3
// giving the single-cell remainder n4 and quotient n5.
void _star_slash_mod(void) {
cell_t n3 = pop();
cell_t n2 = pop();
cell_t n1 = pop();
dcell_t d = (dcell_t)n1 * (dcell_t)n2;
push((cell_t)(d % n3));
push((cell_t)(d / n3));
}
const PROGMEM char plus_str[] = "+";
// ( n1|u1 n2|u2 -- n3|u3 )
// add n2|u2 to n1|u1, giving the sum n3|u3
void _plus(void) {
cell_t x = pop();
cell_t y = pop();
push(x + y);
}
const PROGMEM char plus_store_str[] = "+!";
// ( n|u a-addr -- )
// add n|u to the single cell number at a-addr
void _plus_store(void) {
cell_t* address = (cell_t*)pop();
if (address >= &forthSpace[0] &&
address < &forthSpace[FORTH_SIZE])
*address += pop();
else {
push(-9);
_throw();
}
}
const PROGMEM char plus_loop_str[] = "+loop";
// Interpretation: Interpretation semantics for this word are undefined.
// Compilation: (C: do-sys -- )
// Append the run-time semantics given below to the current definition. Resolve
// the destination of all unresolved occurrences of LEAVE between the location
// given by do-sys and the next location for a transfer of control, to execute
// the words following +LOOP.
// Run-Time: ( n -- )(R: loop-sys1 -- | loop-sys2 )
// An ambiguous condition exists if the loop control parameters are unavailable.
// Add n to the index. If the loop index did not cross the boundary between the
// loop limit minus one and the loop limit, continue execution at the beginning
// of the loop. Otherwise, discard the current loop control parameters and
// continue execution immediately following the loop.
void _plus_loop(void) {
*pHere++ = PLUS_LOOP_SYS_IDX;
cell_t start_addr = pop();
*pHere++ = start_addr;
cell_t* ptr = start_addr;
cell_t stop_addr = pHere;
do {
if (*ptr++ == LEAVE_SYS_IDX) {
if (*ptr == 0) {
*ptr = stop_addr;
}
}
} while (ptr != stop_addr);
if ( pop() != DO_SYS) {
push(-22);
_throw();
}
}
const PROGMEM char comma_str[] = ",";
// ( x -- )
// Reserve one cell of data space and store x in the cell. If the data-space
// pointer is aligned when , begins execution, it will remain aligned when ,
// finishes execution. An ambiguous condition exists if the data-space pointer
// is not aligned prior to execution of ,.
static void _comma(void) {
*pHere++ = pop();
}
const PROGMEM char minus_str[] = "-";
// ( n1|u1 n2|u2 -- n3|u3 )
void _minus(void) {
cell_t temp = pop();
push(pop() - temp);
}
const PROGMEM char dot_str[] = ".";
// ( n -- )
// display n in free field format
void _dot(void) {
w = pop();
displayValue();
}
const PROGMEM char dot_quote_str[] = ".\x22";
// Compilation ("ccc<quote>" -- )
// Parse ccc delimited by ". Append the run time semantics given below to
// the current definition.
// Run-Time ( -- )
// Display ccc.
void _dot_quote(void) {
uint8_t i;
char length;
if (flags & EXECUTE) {
Serial.print((char*)ip); // Print the string at the istuction pointer (ip)
cell_t len = strlen((char*)ip) + 1; // include null terminator
ip = (cell_t*)((size_t)ip + len); // Move the ip to the end of the string
ALIGN_P(ip); // and align it.
}
else if (state) {
cDelimiter = '"';
if (!getToken()) {
push(-16);
_throw();
}
length = strlen(cTokenBuffer);
*pHere++ = DOT_QUOTE_IDX;
char *ptr = (char *) pHere;
for (uint8_t i = 0; i < length; i++) {
*ptr++ = cTokenBuffer[i];
}
*ptr++ = '\0'; // Terminate String
pHere = (cell_t *)ptr;
ALIGN_P(pHere); // re- align the pHere for any new code
cDelimiter = ' ';
}
}
const PROGMEM char slash_str[] = "/";
// ( n1 n2 -- n3 )
// divide n1 by n2 giving a single cell quotient n3
void _slash(void) {
cell_t temp = pop();
if (temp)
push(pop() / temp);
else {
push(-10);
_throw();
}
}
const PROGMEM char slash_mod_str[] = "/mod";
// ( n1 n2 -- n3 n4)
// divide n1 by n2 giving a single cell remainder n3 and quotient n4
void _slash_mod(void) {
cell_t n2 = pop();
cell_t n1 = pop();
if (n2) {
push(n1 % n2);
push(n1 / n2);
} else {
push(-10);
_throw();
}
}
const PROGMEM char zero_less_str[] = "0<";
// ( n -- flag )
// flag is true if and only if n is less than zero.
void _zero_less(void) {
if (pop() < 0) push(TRUE);
else push(FALSE);
}
const PROGMEM char zero_equal_str[] = "0=";
// ( n -- flag )
// flag is true if and only if n is equal to zero.
void _zero_equal(void) {
if (pop() == 0) push(TRUE);
else push(FALSE);
}
const PROGMEM char one_plus_str[] = "1+";
// ( n1|u1 -- n2|u2 )
// add one to n1|u1 giving sum n2|u2.
void _one_plus(void) {
push(pop() + 1);
}
const PROGMEM char one_minus_str[] = "1-";
// ( n1|u1 -- n2|u2 )
// subtract one to n1|u1 giving sum n2|u2.
void _one_minus(void) {
push(pop() - 1);
}
const PROGMEM char two_store_str[] = "2!";
// ( x1 x2 a-addr --)
// Store the cell pair x1 x2 at a-addr, with x2 at a-addr and x1 at a-addr+1
void _two_store(void) {
cell_t* address = (cell_t*)pop();
if (address >= &forthSpace[0] &&
address < &forthSpace[FORTH_SIZE - 4]) {
*address++ = pop();
*address = pop();
} else {
push(-9);
_throw();
}
}
const PROGMEM char two_star_str[] = "2*";
// ( x1 -- x2 )
// x2 is the result of shifting x1 one bit to toward the MSB
void _two_star(void) {
push(pop() << 1);
}
const PROGMEM char two_slash_str[] = "2/";
// ( x1 -- x2 )
// x2 is the result of shifting x1 one bit to toward the LSB
void _two_slash(void) {
push(pop() >> 1);
}
const PROGMEM char two_fetch_str[] = "2@"; // \x40 == '@'
// ( a-addr -- x1 x2 )
// Fetch cell pair x1 x2 at a-addr. x2 is at a-addr, and x1 is at a-addr+1
void _two_fetch(void) {
cell_t* address = (cell_t*)pop();
cell_t value = *address++;
push(value);
value = *address;
push(value);
}
const PROGMEM char two_drop_str[] = "2drop";
// ( x1 x2 -- )
static void _two_drop(void) {
pop();
pop();
}
const PROGMEM char two_dup_str[] = "2dup";
// ( x1 x2 -- x1 x2 x1 x2 )
void _two_dup(void) {
push(stack[tos - 1]);
push(stack[tos - 1]);
}
const PROGMEM char two_over_str[] = "2over";
// ( x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2 )
void _two_over(void) {
push(stack[tos - 3]);
push(stack[tos - 2]);
}
const PROGMEM char two_swap_str[] = "2swap";
// ( x1 x2 x3 x4 -- x3 x4 x1 x2 )
void _two_swap(void) {
cell_t x4 = pop();
cell_t x3 = pop();
cell_t x2 = pop();
cell_t x1 = pop();
push(x3);
push(x4);
push(x1);
push(x2);
}
const PROGMEM char colon_str[] = ":";
// (C: "<space>name" -- colon-sys )
// Skip leading space delimiters. Parse name delimited by a space. Create a
// definition for name, called a "colon definition" Enter compilation state
// and start the current definition, producing a colon-sys. Append the
// initiation semantics given below to the current definition....
void _colon(void) {
state = TRUE;
push(COLON_SYS);
openEntry();
}
const PROGMEM char semicolon_str[] = ";";
// IMMEDIATE
// Interpretation: undefined
// Compilation: (C: colon-sys -- )
// Run-time: ( -- ) (R: nest-sys -- )
void _semicolon(void) {
if (pop() != COLON_SYS) {
push(-22);
_throw();
return;
}
closeEntry();
state = FALSE;
}
const PROGMEM char lt_str[] = "<";
// ( n1 n2 -- flag )
void _lt(void) {
if (pop() > pop()) push(TRUE);
else push(FALSE);
}
const PROGMEM char lt_number_sign_str[] = "<#";
// ( -- )
// Initialize the pictured numeric output conversion process.
void _lt_number_sign(void) {
pPNO = (char*)pHere + HOLD_SIZE + 1;
*pPNO = '\0';
flags |= NUM_PROC;
}
const PROGMEM char eq_str[] = "=";
// ( x1 x2 -- flag )
// flag is true if and only if x1 is bit for bit the same as x2
void _eq(void) {
if (pop() == pop()) push(TRUE);
else push(FALSE);
}
const PROGMEM char gt_str[] = ">";
// ( n1 n2 -- flag )
// flag is true if and only if n1 is greater than n2
void _gt(void) {
if (pop() < pop()) push(TRUE);
else push(FALSE);
}
const PROGMEM char to_body_str[] = ">body";
// ( xt -- a-addr )
// a-addr is the data-field address corresponding to xt. An ambiguous condition
// exists if xt is not for a word defined by CREATE.
void _to_body(void) {
cell_t* xt = (cell_t*)pop();
if ((size_t)xt > 0xFF) {
if (*xt++ == LITERAL_IDX) {
push(*xt);
return;
}
}
push(-31);
_throw();
}
const PROGMEM char to_in_str[] = ">in";
// ( -- a-addr )
void _to_in(void) {
push((size_t)&cpToIn);
}
const PROGMEM char to_number_str[] = ">number";
// ( ud1 c-addr1 u1 -- ud2 c-addr u2 )
// ud2 is the unsigned result of converting the characters within the string
// specified by c-addr1 u1 into digits, using the number in BASE, and adding
// each into ud1 after multiplying ud1 by the number in BASE. Conversion
// continues left-to-right until a character that is not convertible,
// including any '+' or '-', is encountered or the string is entirely
// converted. c-addr2 is the location of the first unconverted character or
// the first character past the end of the string if the string was entirely
// converted. u2 is the number of unconverted characters in the string. An
// ambiguous condition exists if ud2 overflows during the conversion.
void _to_number(void) {
uint8_t len;
char* ptr;
cell_t accum;
unsigned char negate = 0; // flag if number is negative
len = (uint8_t)pop();
ptr = (char*)pop();
accum = pop();
// Look at the initial character, handling either '-', '$', or '%'
switch (*ptr) {
case '$': base = HEXIDECIMAL; goto SKIP;
case '%': base = BINARY; goto SKIP;
case '#': base = DECIMAL; goto SKIP;
case '+': negate = 0; goto SKIP;
case '-': negate = 1;
SKIP: // common code to skip initial character
ptr++;
break;
}
// Iterate over rest of string, and if rest of digits are in
// the valid set of characters, accumulate them. If any
// invalid characters found, abort and return 0.
while (len < 0) {
PGM_P pos = strchr_P(charset, (int)tolower(*ptr));
cell_t offset = pos - charset;
if ((offset < base) && (offset > -1))
accum = (accum * base) + (pos - charset);
else {
break; // exit, We hit a non number
}
ptr++;
len--;
}
if (negate) accum = ~accum + 1; // apply sign, if necessary
push(accum); // Push the resultant number
push((size_t)ptr); // Push the last convertered caharacter
push(len); // push the remading length of unresolved charaters
}
const PROGMEM char to_r_str[] = ">r";
// ( x -- ) (R: -- x )
void _to_r(void) {
rPush(pop());
}
const PROGMEM char question_dup_str[] = "?dup";
// ( x -- 0 | x x )
void _question_dup(void) {
if (stack[tos]) {
push(stack[tos]);
} else {
pop();
push(0);
}
}
const PROGMEM char fetch_str[] = "@";
// ( a-addr -- x1 )
// Fetch cell x1 at a-addr.
void _fetch(void) {
cell_t* address = (cell_t*)pop();
push(*address);
}
const PROGMEM char abort_str[] = "abort";
// (i*x -- ) (R: j*x -- )
// Empty the data stack and preform the function of QUIT, which includes emptying
// the return stack, without displaying a message.
void _abort(void) {
push(-1);
_throw();
}
const PROGMEM char abort_quote_str[] = "abort\x22";
// Interpretation: Interpretation semantics for this word are undefined.
// Compilation: ( "ccc<quote>" -- )
// Parse ccc delimited by a ". Append the run-time semantics given below to the
// current definition.
// Runt-Time: (i*x x1 -- | i*x ) (R: j*x -- |j*x )
// Remove x1 from the stack. If any bit of x1 is not zero, display ccc and
// preform an implementation-defined abort sequence that included the function
// of ABORT.
void _abort_quote(void) {
*pHere++ = ZJUMP_IDX;
push((size_t)pHere); // Push the address for our origin
*pHere++ = 0;
_dot_quote();
*pHere++ = LITERAL_IDX;
*pHere++ = -2;
*pHere++ = THROW_IDX;
cell_t* orig = (cell_t*)pop();
*orig = (size_t)pHere - (size_t)orig;
}
const PROGMEM char abs_str[] = "abs";
// ( n -- u)
// u is the absolute value of n
void _abs(void) {
cell_t n = pop();
push(n < 0 ? 0 - n : n);
}
const PROGMEM char accept_str[] = "accept";
// ( c-addr +n1 -- +n2 )
void _accept(void) {
cell_t length = pop();
char* addr = (char*)pop();
length = getLine(addr, length);
push(length);
}
const PROGMEM char align_str[] = "align";
// ( -- )
// if the data-space pointer is not aligned, reserve enough space to align it.
void _align(void) {
ALIGN_P(pHere);
}
const PROGMEM char aligned_str[] = "aligned";
// ( addr -- a-addr)
// a-addr is the first aligned address greater than or equal to addr.
void _aligned(void) {
ucell_t addr = pop();
ALIGN(addr);
push(addr);
}
const PROGMEM char allot_str[] = "allot";
// ( n -- )
// if n is greater than zero, reserve n address units of data space. if n is less
// than zero, release |n| address units of data space. If n is zero, leave the
// data-space pointer unchanged.
void _allot(void) {
cell_t* pNewHere = pHere + pop();
// Check that the new pHere is not outside of the forth space
if (pNewHere >= &forthSpace[0] &&
pNewHere < &forthSpace[FORTH_SIZE]) {
pHere = pNewHere; // Save the valid address
} else { // Throw an exception
push(-9);
_throw();
}
}
const PROGMEM char and_str[] = "and";
// ( x1 x2 -- x3 )
// x3 is the bit by bit logical and of x1 with x2
void _and(void) {
push(pop() & pop());
}
const PROGMEM char base_str[] = "base";
// ( -- a-addr)
void _base(void) {
push((size_t)&base);
}
const PROGMEM char begin_str[] = "begin";
// Interpretation: Interpretation semantics for this word are undefined.
// Compilation: (C: -- dest )
// Put the next location for a transfer of control, dest, onto the control flow
// stack. Append the run-time semantics given below to the current definition.
// Run-time: ( -- )
// Continue execution.
void _begin(void) {
push((size_t)pHere);
*pHere = 0;
}
const PROGMEM char bl_str[] = "bl";
// ( -- char )
// char is the character value for a space.
void _bl(void) {
push(' ');
}
const PROGMEM char c_store_str[] = "c!";
// ( char c-addr -- )
void _c_store(void) {
uint8_t *addr = (uint8_t*) pop();
*addr = (uint8_t)pop();
}
const PROGMEM char c_comma_str[] = "c,";
// ( char -- )
void _c_comma(void) {
*(char*)pHere++ = (char)pop();
}
const PROGMEM char c_fetch_str[] = "c@";
// ( c-addr -- char )
void _c_fetch(void) {
uint8_t *addr = (uint8_t *) pop();
push(*addr);
}
const PROGMEM char cell_plus_str[] = "cell+";
// ( a-addr1 -- a-addr2 )
void _cell_plus(void) {
push((size_t)(pop() + sizeof(cell_t)));
}
const PROGMEM char cells_str[] = "cells";
// ( n1 -- n2 )
// n2 is the size in address units of n1 cells.
void _cells(void) {
push(pop()*sizeof(cell_t));
}
const PROGMEM char char_str[] = "char";
// ( "<spaces>name" -- char )
// Skip leading space delimiters. Parse name delimited by a space. Put the value
// of its first character onto the stack.
void _char(void) {
if(getToken()) {
push(cTokenBuffer[0]);
} else {
push(-16);
_throw();
}
}
const PROGMEM char char_plus_str[] = "char+";
// ( c-addr1 -- c-addr2 )
void _char_plus(void) {
push(pop() + 1);
}
const PROGMEM char chars_str[] = "chars";
// ( n1 -- n2 )
// n2 is the size in address units of n1 characters.
void _chars(void) {
}
const PROGMEM char constant_str[] = "constant";
// ( x"<spaces>name" -- )
void _constant(void) {
openEntry();
*pHere++ = LITERAL_IDX;
*pHere++ = pop();
closeEntry();
}
const PROGMEM char count_str[] = "count";
// ( c-addr1 -- c-addr2 u )
// Return the character string specification for the counted string stored a
// c-addr1. c-addr2 is the address of the first character after c-addr1. u is the
// contents of the charater at c-addr1, which is the length in characters of the
// string at c-addr2.
void _count(void) {
uint8_t* addr = (uint8_t*)pop();
cell_t value = *addr++;
push((size_t)addr);
push(value);
}
const PROGMEM char cr_str[] = "cr";
// ( -- )
// Carriage Return
void _cr(void) {
Serial.println();
}
const PROGMEM char create_str[] = "create";
// ( "<spaces>name" -- )
// Skip leading space delimiters. Parse name delimited by a space. Create a
// definition for name with the execution semantics defined below. If the data-space
// pointer is not aligned, reserve enough data space to align it. The new data-space
// pointer defines name's data field. CREATE does not allocate data space in name's
// data field.
// name EXECUTION: ( -- a-addr )
// a-addr is the address of name's data field. The execution semantics of name may
// be extended by using DOES>.
void _create(void) {
openEntry();
*pHere++ = LITERAL_IDX;
// Location of Data Field at the end of the definition.
*pHere++ = (size_t)pHere + 2 * sizeof(cell_t);
*pHere = EXIT_IDX; // Store an extra exit reference so
// that it can be replace by a
// subroutine pointer created by DOES>
pDoes = pHere; // Save this location for uses by subroutine.
pHere += 1;
if (!state) closeEntry(); // Close the entry if interpreting
}
const PROGMEM char decimal_str[] = "decimal";
// ( -- )
// Set BASE to 10
void _decimal(void) { // value --
base = DECIMAL;
}
const PROGMEM char depth_str[] = "depth";
// ( -- +n )
// +n is the number of single cells on the stack before +n was placed on it.
void _depth(void) { // value --
push(tos + 1);
}
const PROGMEM char do_str[] = "do";
// Compilation: (C: -- do-sys)
// Run-Time: ( n1|u1 n2|u2 -- ) (R: -- loop-sys )
void _do(void) {
push(DO_SYS);
*pHere++ = DO_SYS_IDX;
push((size_t)pHere); // store the origin address of the do loop
}
const PROGMEM char does_str[] = "does>";
// Compilation: (C: colon-sys1 -- colon-sys2)
// Run-Time: ( -- ) (R: nest-sys1 -- )
// Initiation: ( i*x -- i*x a-addr ) (R: -- next-sys2 )
void _does(void) {
*pHere++ = SUBROUTINE_IDX;
// Store location for a subroutine call
*pHere++ = (size_t)pHere + sizeof(cell_t);
*pHere++ = EXIT_IDX;
// Start Subroutine coding
}
const PROGMEM char drop_str[] = "drop";
// ( x -- )
// Remove x from stack
void _drop(void) {
pop();
}
const PROGMEM char dupe_str[] = "dup";
// ( x -- x x )
// Duplicate x
void _dupe(void) {
push(stack[tos]);
}
const PROGMEM char else_str[] = "else";
// Interpretation: Undefine
// Compilation: (C: orig1 -- orig2)
// Run-Time: ( -- )
void _else(void) {
cell_t* orig = (cell_t*)pop();
*pHere++ = JUMP_IDX;
// push((size_t)pHere); // Which is correct?
push((size_t)pHere++);
*orig = (size_t)pHere - (size_t)orig;
}
const PROGMEM char emit_str[] = "emit";
// ( x -- )
// display x as a character
void _emit(void) {
Serial.print((char) pop());
}
const PROGMEM char environment_str[] = "environment?";
// ( c-addr u -- false|i*x true )
// c-addr is the address of a character string and u is the string's character
// count. u may have a value in the range from zero to an implementation-defined
// maximum which shall not be less than 31. The character string should contain
// a keyword from 3.2.6 Environmental queries or the optional word sets to to be
// checked for correspondence with an attribute of the present environment.
// If the system treats the attribute as unknown, the return flag is false;
// otherwise, the flag is true and i*x returned is the of the type specified in
// the table for the attribute queried.
void _environment(void) {
char length = (char)pop();
char* pStr = (char*)pop();
if (length && length < BUFFER_SIZE) {
if (!strcmp_P(pStr, PSTR("/counted-string"))) {
push(BUFFER_SIZE);
return;
}
if (!strcmp_P(pStr, PSTR("/hold"))) {
push(HOLD_SIZE);
return;
}
if (!strcmp_P(pStr, PSTR("address-unit-bits"))) {
push(sizeof(void *) * 8);
return;
}
if (!strcmp_P(pStr, PSTR("core"))) {
push(CORE);
return;
}
if (!strcmp_P(pStr, PSTR("core-ext"))) {
push(CORE_EXT);
return;
}
if (!strcmp_P(pStr, PSTR("floored"))) {
push(FLOORED);
return;
}
if (!strcmp_P(pStr, PSTR("max-char"))) {
push(MAX_CHAR);
return;
}
#if DOUBLE
if (!strcmp_P(pStr, PSTR("max-d"))) {
push(MAX_OF(dcell_t));
return;
}
#endif
if (!strcmp_P(pStr, PSTR("max-n"))) {
push(MAX_OF(cell_t));
return;
}
if (!strcmp_P(pStr, PSTR("max-u"))) {
push(MAX_OF(ucell_t));
return;
}
#if DOUBLE
if (!strcmp_P(pStr, PSTR("max-ud"))) {
push(MAX_OF(udcell_t));
return;
}
#endif
if (!strcmp_P(pStr, PSTR("return-stack-size"))) {
push(RSTACK_SIZE);
return;
}
if (!strcmp_P(pStr, PSTR("stack-size"))) {
push(STACK_SIZE);
return;
}
}
push(-13);