-
Notifications
You must be signed in to change notification settings - Fork 4
/
ESP32forth705-serial2.ino
2030 lines (1965 loc) · 72.4 KB
/
ESP32forth705-serial2.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
/*
* ESP32forth v7.0.5 Brad Nelson
* Revision: 33cf8aaa6fe3e0bc4abf3e4cd5c496a3071b9171
+added serial2 words by PeterForth June2021
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef intptr_t cell_t;
typedef uintptr_t ucell_t;
#define Y(op, code) X(#op, id ## op, code)
#define NIP (--sp)
#define NIPn(n) (sp -= (n))
#define DROP (tos = *sp--)
#define DROPn(n) (NIPn(n-1), DROP)
#define DUP (*++sp = tos)
#define PUSH DUP; tos = (cell_t)
#define COMMA(n) *g_sys.heap++ = (n)
#define DOIMMEDIATE() (*g_sys.current)[-1] |= IMMEDIATE
#define UNSMUDGE() (*g_sys.current)[-1] &= ~SMUDGE
#define DOES(ip) **g_sys.current = (cell_t) ADDR_DODOES; (*g_sys.current)[1] = (cell_t) ip
#define PARK DUP; *++rp = (cell_t) sp; *++rp = (cell_t) ip
#ifndef SSMOD_FUNC
# if __SIZEOF_POINTER__ == 8
typedef __int128_t dcell_t;
# elif __SIZEOF_POINTER__ == 4 || defined(_M_IX86)
typedef int64_t dcell_t;
# else
# error "unsupported cell size"
# endif
# define SSMOD_FUNC dcell_t d = (dcell_t) *sp * (dcell_t) sp[-1]; \
--sp; cell_t a = (cell_t) (d < 0 ? ~(~d / tos) : d / tos); \
*sp = (cell_t) (d - ((dcell_t) a) * tos); tos = a
#endif
#define OPCODE_LIST \
X("0=", ZEQUAL, tos = !tos ? -1 : 0) \
X("0<", ZLESS, tos = (tos|0) < 0 ? -1 : 0) \
X("+", PLUS, tos += *sp--) \
X("U/MOD", USMOD, w = *sp; *sp = (ucell_t) w % (ucell_t) tos; \
tos = (ucell_t) w / (ucell_t) tos) \
X("*/MOD", SSMOD, SSMOD_FUNC) \
Y(LSHIFT, tos = (*sp-- << tos)) \
Y(RSHIFT, tos = (*sp-- >> tos)) \
Y(AND, tos &= *sp--) \
Y(OR, tos |= *sp--) \
Y(XOR, tos ^= *sp--) \
Y(DUP, DUP) \
Y(SWAP, w = tos; tos = *sp; *sp = w) \
Y(OVER, DUP; tos = sp[-1]) \
Y(DROP, DROP) \
X("@", AT, tos = *(cell_t *) tos) \
X("L@", LAT, tos = *(int32_t *) tos) \
X("C@", CAT, tos = *(uint8_t *) tos) \
X("!", STORE, *(cell_t *) tos = *sp--; DROP) \
X("L!", LSTORE, *(int32_t *) tos = *sp--; DROP) \
X("C!", CSTORE, *(uint8_t *) tos = *sp--; DROP) \
X("SP@", SPAT, DUP; tos = (cell_t) sp) \
X("SP!", SPSTORE, sp = (cell_t *) tos; DROP) \
X("RP@", RPAT, DUP; tos = (cell_t) rp) \
X("RP!", RPSTORE, rp = (cell_t *) tos; DROP) \
X(">R", TOR, *++rp = tos; DROP) \
X("R>", FROMR, DUP; tos = *rp; --rp) \
X("R@", RAT, DUP; tos = *rp) \
Y(EXECUTE, w = tos; DROP; JMPW) \
Y(BRANCH, ip = (cell_t *) *ip) \
Y(0BRANCH, if (!tos) ip = (cell_t *) *ip; else ++ip; DROP) \
Y(DONEXT, *rp = *rp - 1; if (~*rp) ip = (cell_t *) *ip; else (--rp, ++ip)) \
Y(DOLIT, DUP; tos = *ip++) \
Y(ALITERAL, COMMA(g_sys.DOLIT_XT); COMMA(tos); DROP) \
Y(CELL, DUP; tos = sizeof(cell_t)) \
Y(FIND, tos = find((const char *) *sp, tos); --sp) \
Y(PARSE, DUP; tos = parse(tos, sp)) \
X("S>NUMBER?", CONVERT, tos = convert((const char *) *sp, tos, sp); \
if (!tos) --sp) \
Y(CREATE, DUP; DUP; tos = parse(32, sp); \
create((const char *) *sp, tos, 0, ADDR_DOCREATE); \
COMMA(0); DROPn(2)) \
X("DOES>", DOES, DOES(ip); ip = (cell_t *) *rp; --rp) \
Y(IMMEDIATE, DOIMMEDIATE()) \
X("'SYS", SYS, DUP; tos = (cell_t) &g_sys) \
Y(YIELD, PARK; return rp) \
X(":", COLON, DUP; DUP; tos = parse(32, sp); \
create((const char *) *sp, tos, SMUDGE, ADDR_DOCOLON); \
g_sys.state = -1; --sp; DROP) \
Y(EVALUATE1, DUP; sp = evaluate1(sp); w = *sp--; DROP; if (w) JMPW) \
Y(EXIT, ip = (cell_t *) *rp--) \
X(";", SEMICOLON, UNSMUDGE(); COMMA(g_sys.DOEXIT_XT); g_sys.state = 0) \
#define SET tos = (cell_t)
#define n0 tos
#define n1 (*sp)
#define n2 sp[-1]
#define n3 sp[-2]
#define n4 sp[-3]
#define n5 sp[-4]
#define n6 sp[-5]
#define n7 sp[-6]
#define n8 sp[-7]
#define n9 sp[-8]
#define n10 sp[-9]
#define a0 ((void *) tos)
#define a1 (*(void **) &n1)
#define a2 (*(void **) &n2)
#define a3 (*(void **) &n3)
#define a4 (*(void **) &n4)
#define a5 (*(void **) &n5)
#define a6 (*(void **) &n6)
#define b0 ((uint8_t *) tos)
#define b1 (*(uint8_t **) &n1)
#define b2 (*(uint8_t **) &n2)
#define b3 (*(uint8_t **) &n3)
#define b4 (*(uint8_t **) &n4)
#define b5 (*(uint8_t **) &n5)
#define b6 (*(uint8_t **) &n6)
#define c0 ((char *) tos)
#define c1 (*(char **) &n1)
#define c2 (*(char **) &n2)
#define c3 (*(char **) &n3)
#define c4 (*(char **) &n4)
#define c5 (*(char **) &n5)
#define c6 (*(char **) &n6)
// For now, default on several options.
#define ENABLE_SPIFFS_SUPPORT
#define ENABLE_WIFI_SUPPORT
#define ENABLE_MDNS_SUPPORT
#define ENABLE_WEBSERVER_SUPPORT
#define ENABLE_SDCARD_SUPPORT
#define ENABLE_I2C_SUPPORT
#define ENABLE_SOCKETS_SUPPORT
#define ENABLE_FREERTOS_SUPPORT
#define ENABLE_INTERRUPTS_SUPPORT
// Uncomment this #define for OLED Support.
// You will need to install these libraries from the Library Manager:
// Adafruit SSD1306
// Adafruit GFX Library
// Adafruit BusIO
// #define ENABLE_OLED_SUPPORT
// For now assume only boards with PSRAM (ESP32-CAM)
// will want SerialBluetooth (very large) and camera support.
// Other boards can support these if they're set to a larger
// parition size. But it's unclear the best way to configure this.
#ifdef BOARD_HAS_PSRAM
# define ENABLE_CAMERA_SUPPORT
# define ENABLE_SERIAL_BLUETOOTH_SUPPORT
#endif
#ifdef ENABLE_WEBSERVER_SUPPORT
# include "WebServer.h"
#endif
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#if defined(ESP32)
# define HEAP_SIZE (100 * 1024)
# define STACK_SIZE 512
#elif defined(ESP8266)
# define HEAP_SIZE (40 * 1024)
# define STACK_SIZE 512
#else
# define HEAP_SIZE 2 * 1024
# define STACK_SIZE 32
#endif
#define INTERRUPT_STACK_CELLS 64
#define PLATFORM_OPCODE_LIST \
/* Memory Allocation */ \
Y(MALLOC, SET malloc(n0)) \
Y(SYSFREE, free(a0); DROP) \
Y(REALLOC, SET realloc(a1, n0); NIP) \
Y(heap_caps_malloc, SET heap_caps_malloc(n1, n0); NIP) \
Y(heap_caps_free, heap_caps_free(a0); DROP) \
Y(heap_caps_realloc, \
tos = (cell_t) heap_caps_realloc(a2, n1, n0); NIPn(2)) \
/* Serial */ \
X("Serial.begin", SERIAL_BEGIN, Serial.begin(tos); DROP) \
X("Serial.end", SERIAL_END, Serial.end()) \
X("Serial.available", SERIAL_AVAILABLE, PUSH Serial.available()) \
X("Serial.readBytes", SERIAL_READ_BYTES, n0 = Serial.readBytes(b1, n0); NIP) \
X("Serial.write", SERIAL_WRITE, n0 = Serial.write(b1, n0); NIP) \
X("Serial.flush", SERIAL_FLUSH, Serial.flush()) \
X("Serial2.begin", SERIAL2_BEGIN, Serial2.begin(tos); DROP) \
X("Serial2.end", SERIAL2_END, Serial2.end()) \
X("Serial2.available", SERIAL2_AVAILABLE, PUSH Serial2.available()) \
X("Serial2.readBytes", SERIAL2_READ_BYTES, n0 = Serial2.readBytes(b1, n0); NIP) \
X("Serial2.write", SERIAL2_WRITE, n0 = Serial2.write(b1, n0); NIP) \
X("Serial2.flush", SERIAL2_FLUSH, Serial2.flush()) \
/* Pins and PWM */ \
Y(pinMode, pinMode(n1, n0); DROPn(2)) \
Y(digitalWrite, digitalWrite(n1, n0); DROPn(2)) \
Y(digitalRead, n0 = digitalRead(n0)) \
Y(analogRead, n0 = analogRead(n0)) \
Y(pulseIn, n0 = pulseIn(n2, n1, n0); NIPn(2)) \
Y(dacWrite, dacWrite(n1, n0); DROPn(2)) \
Y(ledcSetup, \
n0 = (cell_t) (1000000 * ledcSetup(n2, n1 / 1000.0, n0)); NIPn(2)) \
Y(ledcAttachPin, ledcAttachPin(n1, n0); DROPn(2)) \
Y(ledcDetachPin, ledcDetachPin(n0); DROP) \
Y(ledcRead, n0 = ledcRead(n0)) \
Y(ledcReadFreq, n0 = (cell_t) (1000000 * ledcReadFreq(n0))) \
Y(ledcWrite, ledcWrite(n1, n0); DROPn(2)) \
Y(ledcWriteTone, \
n0 = (cell_t) (1000000 * ledcWriteTone(n1, n0 / 1000.0)); NIP) \
Y(ledcWriteNote, \
tos = (cell_t) (1000000 * ledcWriteNote(n2, (note_t) n1, n0)); NIPn(2)) \
/* General System */ \
X("MS-TICKS", MS_TICKS, PUSH millis()) \
X("RAW-YIELD", RAW_YIELD, yield()) \
Y(TERMINATE, exit(n0)) \
/* File words */ \
X("R/O", R_O, PUSH O_RDONLY) \
X("R/W", R_W, PUSH O_RDWR) \
X("W/O", W_O, PUSH O_WRONLY) \
Y(BIN, ) \
X("CLOSE-FILE", CLOSE_FILE, tos = close(tos); tos = tos ? errno : 0) \
X("FLUSH-FILE", FLUSH_FILE, fsync(tos); /* fsync has no impl and returns ENOSYS :-( */ tos = 0) \
X("OPEN-FILE", OPEN_FILE, cell_t mode = n0; DROP; cell_t len = n0; DROP; \
memcpy(filename, a0, len); filename[len] = 0; \
n0 = open(filename, mode, 0777); PUSH n0 < 0 ? errno : 0) \
X("CREATE-FILE", CREATE_FILE, cell_t mode = n0; DROP; cell_t len = n0; DROP; \
memcpy(filename, a0, len); filename[len] = 0; \
n0 = open(filename, mode | O_CREAT | O_TRUNC); PUSH n0 < 0 ? errno : 0) \
X("DELETE-FILE", DELETE_FILE, cell_t len = n0; DROP; \
memcpy(filename, a0, len); filename[len] = 0; \
n0 = unlink(filename); n0 = n0 ? errno : 0) \
X("WRITE-FILE", WRITE_FILE, cell_t fd = n0; DROP; cell_t len = n0; DROP; \
n0 = write(fd, a0, len); n0 = n0 != len ? errno : 0) \
X("READ-FILE", READ_FILE, cell_t fd = n0; DROP; cell_t len = n0; DROP; \
n0 = read(fd, a0, len); PUSH n0 < 0 ? errno : 0) \
X("FILE-POSITION", FILE_POSITION, \
n0 = (cell_t) lseek(n0, 0, SEEK_CUR); PUSH n0 < 0 ? errno : 0) \
X("REPOSITION-FILE", REPOSITION_FILE, cell_t fd = n0; DROP; \
n0 = (cell_t) lseek(fd, tos, SEEK_SET); n0 = n0 < 0 ? errno : 0) \
X("RESIZE-FILE", RESIZE_FILE, cell_t fd = n0; DROP; n0 = ResizeFile(fd, tos)) \
X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(n0, &st); \
n0 = (cell_t) st.st_size; PUSH w < 0 ? errno : 0) \
OPTIONAL_SPIFFS_SUPPORT \
OPTIONAL_WIFI_SUPPORT \
OPTIONAL_MDNS_SUPPORT \
OPTIONAL_WEBSERVER_SUPPORT \
OPTIONAL_SDCARD_SUPPORT \
OPTIONAL_I2C_SUPPORT \
OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \
OPTIONAL_CAMERA_SUPPORT \
OPTIONAL_SOCKETS_SUPPORT \
OPTIONAL_FREERTOS_SUPPORT \
OPTIONAL_INTERRUPTS_SUPPORT \
OPTIONAL_OLED_SUPPORT \
#ifndef ENABLE_SPIFFS_SUPPORT
// Provide a default failing SPIFFS.begin
# define OPTIONAL_SPIFFS_SUPPORT \
X("SPIFFS.begin", SPIFFS_BEGIN, NIPn(2); n0 = 0)
#else
# include "SPIFFS.h"
# define OPTIONAL_SPIFFS_SUPPORT \
X("SPIFFS.begin", SPIFFS_BEGIN, \
tos = SPIFFS.begin(n2, c1, n0); NIPn(2)) \
X("SPIFFS.end", SPIFFS_END, SPIFFS.end()) \
X("SPIFFS.format", SPIFFS_FORMAT, PUSH SPIFFS.format()) \
X("SPIFFS.totalBytes", SPIFFS_TOTAL_BYTES, PUSH SPIFFS.totalBytes()) \
X("SPIFFS.usedBytes", SPIFFS_USED_BYTES, PUSH SPIFFS.usedBytes())
#endif
#ifndef ENABLE_FREERTOS_SUPPORT
# define OPTIONAL_FREERTOS_SUPPORT
#else
# include "freertos/FreeRTOS.h"
# include "freertos/task.h"
# define OPTIONAL_FREERTOS_SUPPORT \
Y(vTaskDelete, vTaskDelete((TaskHandle_t) n0); DROP) \
Y(xTaskCreatePinnedToCore, n0 = xTaskCreatePinnedToCore((TaskFunction_t) a6, c5, n4, a3, (UBaseType_t) n2, (TaskHandle_t *) a1, (BaseType_t) n0); NIPn(6)) \
Y(xPortGetCoreID, PUSH xPortGetCoreID())
#endif
#ifndef ENABLE_INTERRUPTS_SUPPORT
# define OPTIONAL_INTERRUPTS_SUPPORT
#else
# include "esp_intr_alloc.h"
# include "driver/timer.h"
# include "driver/gpio.h"
# define OPTIONAL_INTERRUPTS_SUPPORT \
Y(gpio_config, n0 = gpio_config((const gpio_config_t *) a0)) \
Y(gpio_reset_pin, n0 = gpio_reset_pin((gpio_num_t) n0)) \
Y(gpio_set_intr_type, n0 = gpio_set_intr_type((gpio_num_t) n1, (gpio_int_type_t) n0); NIP) \
Y(gpio_intr_enable, n0 = gpio_intr_enable((gpio_num_t) n0)) \
Y(gpio_intr_disable, n0 = gpio_intr_disable((gpio_num_t) n0)) \
Y(gpio_set_level, n0 = gpio_set_level((gpio_num_t) n1, n0); NIP) \
Y(gpio_get_level, n0 = gpio_get_level((gpio_num_t) n0)) \
Y(gpio_set_direction, n0 = gpio_set_direction((gpio_num_t) n1, (gpio_mode_t) n0); NIP) \
Y(gpio_set_pull_mode, n0 = gpio_set_pull_mode((gpio_num_t) n1, (gpio_pull_mode_t) n0); NIP) \
Y(gpio_wakeup_enable, n0 = gpio_wakeup_enable((gpio_num_t) n1, (gpio_int_type_t) n0); NIP) \
Y(gpio_wakeup_disable, n0 = gpio_wakeup_disable((gpio_num_t) n0)) \
Y(gpio_pullup_en, n0 = gpio_pullup_en((gpio_num_t) n0)) \
Y(gpio_pullup_dis, n0 = gpio_pullup_dis((gpio_num_t) n0)) \
Y(gpio_pulldown_en, n0 = gpio_pulldown_en((gpio_num_t) n0)) \
Y(gpio_pulldown_dis, n0 = gpio_pulldown_dis((gpio_num_t) n0)) \
Y(gpio_hold_en, n0 = gpio_hold_en((gpio_num_t) n0)) \
Y(gpio_hold_dis, n0 = gpio_hold_dis((gpio_num_t) n0)) \
Y(gpio_deep_sleep_hold_en, gpio_deep_sleep_hold_en()) \
Y(gpio_deep_sleep_hold_dis, gpio_deep_sleep_hold_dis()) \
Y(gpio_install_isr_service, n0 = gpio_install_isr_service(n0)) \
Y(gpio_uninstall_isr_service, gpio_uninstall_isr_service()) \
Y(gpio_isr_handler_add, n0 = GpioIsrHandlerAdd(n2, n1, n0); NIPn(2)) \
Y(gpio_isr_handler_remove, n0 = gpio_isr_handler_remove((gpio_num_t) n0)) \
Y(gpio_set_drive_capability, n0 = gpio_set_drive_capability((gpio_num_t) n1, (gpio_drive_cap_t) n0); NIP) \
Y(gpio_get_drive_capability, n0 = gpio_get_drive_capability((gpio_num_t) n1, (gpio_drive_cap_t *) a0); NIP) \
Y(esp_intr_alloc, n0 = EspIntrAlloc(n4, n3, n2, n1, a0); NIPn(4)) \
Y(esp_intr_free, n0 = esp_intr_free((intr_handle_t) n0)) \
Y(timer_isr_register, n0 = TimerIsrRegister(n5, n4, n3, n2, n1, a0); NIPn(5))
#endif
#ifndef ENABLE_CAMERA_SUPPORT
# define OPTIONAL_CAMERA_SUPPORT
#else
# include "esp_camera.h"
# define OPTIONAL_CAMERA_SUPPORT \
Y(esp_camera_init, n0 = esp_camera_init((camera_config_t *) a0)) \
Y(esp_camera_deinit, PUSH esp_camera_deinit()) \
Y(esp_camera_fb_get, PUSH esp_camera_fb_get()) \
Y(esp_camera_fb_return, esp_camera_fb_return((camera_fb_t *) a0); DROP) \
Y(esp_camera_sensor_get, PUSH esp_camera_sensor_get())
#endif
#ifndef ENABLE_SOCKETS_SUPPORT
# define OPTIONAL_SOCKETS_SUPPORT
#else
# include <errno.h>
# include <sys/select.h>
# include <sys/socket.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/un.h>
# include <sys/poll.h>
# define OPTIONAL_SOCKETS_SUPPORT \
Y(socket, n0 = socket(n2, n1, n0); NIPn(2)) \
Y(bind, n0 = bind(n2, (struct sockaddr *) a1, n0); NIPn(2)) \
Y(listen, n0 = listen(n1, n0); NIP) \
Y(connect, n0 = connect(n2, (struct sockaddr *) a1, n0); NIPn(2)) \
Y(accept, n0 = accept(n2, (struct sockaddr *) a1, (socklen_t *) a0); NIPn(2)) \
Y(select, n0 = select(n4, (fd_set *) a3, (fd_set *) a2, (fd_set *) a1, (struct timeval *) a0); NIPn(4)) \
Y(poll, n0 = poll((struct pollfd *) a2, (nfds_t) n1, n0); NIPn(2)) \
Y(errno, PUSH errno)
#endif
#ifndef ENABLE_SDCARD_SUPPORT
# define OPTIONAL_SDCARD_SUPPORT
#else
# include "SD_MMC.h"
# define OPTIONAL_SDCARD_SUPPORT \
X("SD_MMC.begin", SD_MMC_BEGIN, tos = SD_MMC.begin(c1, n0); NIP) \
X("SD_MMC.end", SD_MMC_END, SD_MMC.end()) \
X("SD_MMC.cardType", SD_MMC_CARD_TYPE, PUSH SD_MMC.cardType()) \
X("SD_MMC.totalBytes", SD_MMC_TOTAL_BYTES, PUSH SD_MMC.totalBytes()) \
X("SD_MMC.usedBytes", SD_MMC_USED_BYTES, PUSH SD_MMC.usedBytes())
#endif
#ifndef ENABLE_I2C_SUPPORT
# define OPTIONAL_I2C_SUPPORT
#else
# include <Wire.h>
# define OPTIONAL_I2C_SUPPORT \
X("Wire.begin", WIRE_BEGIN, n0 = Wire.begin(n1, n0); NIP) \
X("Wire.setClock", WIRE_SET_CLOCK, Wire.setClock(n0); DROP) \
X("Wire.getClock", WIRE_GET_CLOCK, PUSH Wire.getClock()) \
X("Wire.setTimeout", WIRE_SET_TIMEOUT, Wire.setTimeout(n0); DROP) \
X("Wire.getTimeout", WIRE_GET_TIMEOUT, PUSH Wire.getTimeout()) \
X("Wire.lastError", WIRE_LAST_ERROR, PUSH Wire.lastError()) \
X("Wire.getErrorText", WIRE_GET_ERROR_TEXT, PUSH Wire.getErrorText(n0)) \
X("Wire.beginTransmission", WIRE_BEGIN_TRANSMISSION, Wire.beginTransmission(n0); DROP) \
X("Wire.endTransmission", WIRE_END_TRANSMISSION, SET Wire.endTransmission(n0)) \
X("Wire.requestFrom", WIRE_REQUEST_FROM, n0 = Wire.requestFrom(n2, n1, n0); NIPn(2)) \
X("Wire.writeTransmission", WIRE_WRITE_TRANSMISSION, \
n0 = Wire.writeTransmission(n3, b2, n1, n0); NIPn(3)) \
X("Wire.readTransmission", WIRE_READ_TRANSMISSION, \
n0 = Wire.readTransmission(n4, b3, n2, n1, (uint32_t *) a0); NIPn(4)) \
X("Wire.write", WIRE_WRITE, n0 = Wire.write(b1, n0); NIP) \
X("Wire.available", WIRE_AVAILABLE, PUSH Wire.available()) \
X("Wire.read", WIRE_READ, PUSH Wire.read()) \
X("Wire.peek", WIRE_PEEK, PUSH Wire.peek()) \
X("Wire.busy", WIRE_BUSY, PUSH Wire.busy()) \
X("Wire.flush", WIRE_FLUSH, Wire.flush())
#endif
#ifndef ENABLE_SERIAL_BLUETOOTH_SUPPORT
# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT
#else
# include "esp_bt_device.h"
# include "BluetoothSerial.h"
# define bt0 ((BluetoothSerial *) a0)
# define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \
X("SerialBT.new", SERIALBT_NEW, PUSH new BluetoothSerial()) \
X("SerialBT.delete", SERIALBT_DELETE, delete bt0; DROP) \
X("SerialBT.begin", SERIALBT_BEGIN, n0 = bt0->begin(c2, n1); NIPn(2)) \
X("SerialBT.end", SERIALBT_END, bt0->end(); DROP) \
X("SerialBT.available", SERIALBT_AVAILABLE, n0 = bt0->available()) \
X("SerialBT.readBytes", SERIALBT_READ_BYTES, n0 = bt0->readBytes(b2, n1); NIPn(2)) \
X("SerialBT.write", SERIALBT_WRITE, n0 = bt0->write(b2, n1); NIPn(2)) \
X("SerialBT.flush", SERIALBT_FLUSH, bt0->flush(); DROP) \
X("SerialBT.hasClient", SERIALBT_HAS_CLIENT, n0 = bt0->hasClient()) \
X("SerialBT.enableSSP", SERIALBT_ENABLE_SSP, bt0->enableSSP(); DROP) \
X("SerialBT.setPin", SERIALBT_SET_PIN, n0 = bt0->setPin(c1); NIP) \
X("SerialBT.unpairDevice", SERIALBT_UNPAIR_DEVICE, \
n0 = bt0->unpairDevice(b1); NIP) \
X("SerialBT.connect", SERIALBT_CONNECT, n0 = bt0->connect(c1); NIP) \
X("SerialBT.connectAddr", SERIALBT_CONNECT_ADDR, n0 = bt0->connect(b1); NIP) \
X("SerialBT.disconnect", SERIALBT_DISCONNECT, n0 = bt0->disconnect()) \
X("SerialBT.connected", SERIALBT_CONNECTED, n0 = bt0->connected(n1); NIP) \
X("SerialBT.isReady", SERIALBT_IS_READY, n0 = bt0->isReady(n2, n1); NIPn(2)) \
/* Bluetooth */ \
Y(esp_bt_dev_get_address, PUSH esp_bt_dev_get_address())
#endif
#ifndef ENABLE_WIFI_SUPPORT
# define OPTIONAL_WIFI_SUPPORT
#else
# include <WiFi.h>
# include <WiFiClient.h>
static IPAddress ToIP(cell_t ip) {
return IPAddress(ip & 0xff, ((ip >> 8) & 0xff), ((ip >> 16) & 0xff), ((ip >> 24) & 0xff));
}
static cell_t FromIP(IPAddress ip) {
cell_t ret = 0;
ret = (ret << 8) | ip[3];
ret = (ret << 8) | ip[2];
ret = (ret << 8) | ip[1];
ret = (ret << 8) | ip[0];
return ret;
}
# define OPTIONAL_WIFI_SUPPORT \
/* WiFi */ \
X("WiFi.config", WIFI_CONFIG, \
WiFi.config(ToIP(n3), ToIP(n2), ToIP(n1), ToIP(n0)); DROPn(4)) \
X("WiFi.begin", WIFI_BEGIN, WiFi.begin(c1, c0); DROPn(2)) \
X("WiFi.disconnect", WIFI_DISCONNECT, WiFi.disconnect()) \
X("WiFi.status", WIFI_STATUS, PUSH WiFi.status()) \
X("WiFi.macAddress", WIFI_MAC_ADDRESS, WiFi.macAddress(b0); DROP) \
X("WiFi.localIP", WIFI_LOCAL_IPS, PUSH FromIP(WiFi.localIP())) \
X("WiFi.mode", WIFI_MODE, WiFi.mode((wifi_mode_t) n0); DROP) \
X("WiFi.setTxPower", WIFI_SET_TX_POWER, WiFi.setTxPower((wifi_power_t) n0); DROP) \
X("WiFi.getTxPower", WIFI_GET_TX_POWER, PUSH WiFi.getTxPower())
#endif
#ifndef ENABLE_MDNS_SUPPORT
# define OPTIONAL_MDNS_SUPPORT
#else
# include <ESPmDNS.h>
# define OPTIONAL_MDNS_SUPPORT \
/* mDNS */ \
X("MDNS.begin", MDNS_BEGIN, n0 = MDNS.begin(c0))
#endif
#ifndef ENABLE_WEBSERVER_SUPPORT
# define OPTIONAL_WEBSERVER_SUPPORT
#else
# include <WebServer.h>
# define ws0 ((WebServer *) a0)
# define OPTIONAL_WEBSERVER_SUPPORT \
/* WebServer */ \
X("WebServer.new", WEBSERVER_NEW, PUSH new WebServer(tos)) \
X("WebServer.delete", WEBSERVER_DELETE, delete ws0; DROP) \
X("WebServer.begin", WEBSERVER_BEGIN, ws0->begin(n1); DROPn(2)) \
X("WebServer.stop", WEBSERVER_STOP, ws0->stop(); DROP) \
X("WebServer.on", WEBSERVER_ON, InvokeWebServerOn(ws0, c2, n1); DROPn(3)) \
X("WebServer.hasArg", WEBSERVER_HAS_ARG, n0 = ws0->hasArg(c1); DROP) \
X("WebServer.arg", WEBSERVER_ARG, \
string_value = ws0->arg(c1); \
c1 = &string_value[0]; n0 = string_value.length()) \
X("WebServer.argi", WEBSERVER_ARGI, \
string_value = ws0->arg(n1); \
c1 = &string_value[0]; n0 = string_value.length()) \
X("WebServer.argName", WEBSERVER_ARG_NAME, \
string_value = ws0->argName(n1); \
c1 = &string_value[0]; n0 = string_value.length()) \
X("WebServer.args", WEBSERVER_ARGS, n0 = ws0->args()) \
X("WebServer.setContentLength", WEBSERVER_SET_CONTENT_LENGTH, \
ws0->setContentLength(n1); DROPn(2)) \
X("WebServer.sendHeader", WEBSERVER_SEND_HEADER, \
ws0->sendHeader(c3, c2, n1); DROPn(4)) \
X("WebServer.send", WEBSERVER_SEND, ws0->send(n3, c2, c1); DROPn(4)) \
X("WebServer.sendContent", WEBSERVER_SEND_CONTENT, \
ws0->sendContent(c1); DROPn(2)) \
X("WebServer.method", WEBSERVER_METHOD, n0 = ws0->method()) \
X("WebServer.handleClient", WEBSERVER_HANDLE_CLIENT, ws0->handleClient(); DROP)
#endif
#ifndef ENABLE_OLED_SUPPORT
# define OPTIONAL_OLED_SUPPORT
#else
# include <Adafruit_GFX.h>
# include <Adafruit_SSD1306.h>
static Adafruit_SSD1306 *oled_display = 0;
# define OPTIONAL_OLED_SUPPORT \
Y(OledAddr, PUSH &oled_display) \
Y(OledNew, oled_display = new Adafruit_SSD1306(n2, n1, &Wire, n0); DROPn(3)) \
Y(OledDelete, delete oled_display) \
Y(OledBegin, n0 = oled_display->begin(n1, n0); NIP) \
Y(OledHOME, oled_display->setCursor(0,0); DROP) \
Y(OledCLS, oled_display->clearDisplay()) \
Y(OledTextc, oled_display->setTextColor(n0); DROP) \
Y(OledPrintln, oled_display->println(c0); DROP) \
Y(OledNumln, oled_display->println(n0); DROP) \
Y(OledNum, oled_display->print(n0); DROP) \
Y(OledDisplay, oled_display->display()) \
Y(OledPrint, oled_display->write(c0); DROP) \
Y(OledInvert, oled_display->invertDisplay(n0); DROP) \
Y(OledTextsize, oled_display->setTextSize(n0); DROP) \
Y(OledSetCursor, oled_display->setCursor(n1,n0); DROPn(2)) \
Y(OledPixel, oled_display->drawPixel(n2, n1, n0); DROPn(2)) \
Y(OledDrawL, oled_display->drawLine(n4, n3, n2, n1, n0); DROPn(4)) \
Y(OledCirc, oled_display->drawCircle(n3,n2, n1, n0); DROPn(3)) \
Y(OledCircF, oled_display->fillCircle(n3, n2, n1, n0); DROPn(3)) \
Y(OledRect, oled_display->drawRect(n4, n3, n2, n1, n0); DROPn(4)) \
Y(OledRectF, oled_display->fillRect(n4, n3, n2, n1, n0); DROPn(3)) \
Y(OledRectR, oled_display->drawRoundRect(n5, n4, n3, n2, n1, n0); DROPn(5)) \
Y(OledRectRF, oled_display->fillRoundRect(n5, n4, n3, n2, n1, n0 ); DROPn(5))
#endif
static char filename[PATH_MAX];
static String string_value;
static cell_t EspIntrAlloc(cell_t source, cell_t flags, cell_t xt, cell_t arg, cell_t *ret);
static cell_t GpioIsrHandlerAdd(cell_t pin, cell_t xt, cell_t arg);
static cell_t TimerIsrRegister(cell_t group, cell_t timer, cell_t xt, cell_t arg, void *ret);
#define PRINT_ERRORS 0
#define CELL_LEN(n) (((n) + sizeof(cell_t) - 1) / sizeof(cell_t))
#define FIND(name) find(name, sizeof(name) - 1)
#define LOWER(ch) ((ch) & 0x5F)
#define IMMEDIATE 1
#define SMUDGE 2
#define VOCABULARY_DEPTH 16
#if PRINT_ERRORS
#include <unistd.h>
#endif
static struct {
const char *tib;
cell_t ntib, tin, state, base;
cell_t *heap, **current, ***context, notfound;
int argc;
char **argv;
cell_t *(*runner)(cell_t *rp); // pointer to forth_run
cell_t *rp; // spot to park main thread
cell_t DOLIT_XT, DOEXIT_XT, YIELD_XT;
} g_sys;
static cell_t convert(const char *pos, cell_t n, cell_t *ret) {
*ret = 0;
cell_t negate = 0;
cell_t base = g_sys.base;
if (!n) { return 0; }
if (pos[0] == '-') { negate = -1; ++pos; --n; }
if (pos[0] == '$') { base = 16; ++pos; --n; }
for (; n; --n) {
uintptr_t d = pos[0] - '0';
if (d > 9) {
d = LOWER(d) - 7;
if (d < 10) { return 0; }
}
if (d >= base) { return 0; }
*ret = *ret * base + d;
++pos;
}
if (negate) { *ret = -*ret; }
return -1;
}
static cell_t same(const char *a, const char *b, cell_t len) {
for (;len && LOWER(*a) == LOWER(*b); --len, ++a, ++b);
return len;
}
static cell_t find(const char *name, cell_t len) {
for (cell_t ***voc = g_sys.context; *voc; ++voc) {
cell_t *pos = **voc;
cell_t clen = CELL_LEN(len);
while (pos) {
if (!(pos[-1] & SMUDGE) && len == pos[-3] &&
same(name, (const char *) &pos[-3 - clen], len) == 0) {
return (cell_t) pos;
}
pos = (cell_t *) pos[-2]; // Follow link
}
}
return 0;
}
static void create(const char *name, cell_t length, cell_t flags, void *op) {
char *pos = (char *) g_sys.heap;
for (cell_t n = length; n; --n) { *pos++ = *name++; } // name
g_sys.heap += CELL_LEN(length);
*g_sys.heap++ = length; // length
*g_sys.heap++ = (cell_t) *g_sys.current; // link
*g_sys.heap++ = flags; // flags
*g_sys.current = g_sys.heap;
*g_sys.heap++ = (cell_t) op; // code
}
static int match(char sep, char ch) {
return sep == ch || (sep == ' ' && (ch == '\t' || ch == '\n' || ch == '\r'));
}
static cell_t parse(cell_t sep, cell_t *ret) {
while (g_sys.tin < g_sys.ntib &&
match(sep, g_sys.tib[g_sys.tin])) { ++g_sys.tin; }
*ret = (cell_t) (g_sys.tib + g_sys.tin);
while (g_sys.tin < g_sys.ntib &&
!match(sep, g_sys.tib[g_sys.tin])) { ++g_sys.tin; }
cell_t len = g_sys.tin - (*ret - (cell_t) g_sys.tib);
if (g_sys.tin < g_sys.ntib) { ++g_sys.tin; }
return len;
}
static cell_t *evaluate1(cell_t *sp) {
cell_t call = 0;
cell_t name;
cell_t len = parse(' ', &name);
if (len == 0) { *++sp = 0; return sp; } // ignore empty
cell_t xt = find((const char *) name, len);
if (xt) {
if (g_sys.state && !(((cell_t *) xt)[-1] & IMMEDIATE)) {
*g_sys.heap++ = xt;
} else {
call = xt;
}
} else {
cell_t n;
cell_t ok = convert((const char *) name, len, &n);
if (ok) {
if (g_sys.state) {
*g_sys.heap++ = g_sys.DOLIT_XT;
*g_sys.heap++ = n;
} else {
*++sp = n;
}
} else {
#if PRINT_ERRORS
write(2, (void *) name, len);
write(2, "\n", 1);
#endif
*++sp = name;
*++sp = len;
*++sp = -1;
call = g_sys.notfound;
}
}
*++sp = call;
return sp;
}
static cell_t *forth_run(cell_t *initrp);
static void forth_init(int argc, char *argv[], void *heap,
const char *src, cell_t src_len) {
g_sys.heap = ((cell_t *) heap) + 4; // Leave a little room.
cell_t *sp = g_sys.heap + 1; g_sys.heap += STACK_SIZE;
cell_t *rp = g_sys.heap + 1; g_sys.heap += STACK_SIZE;
// FORTH vocabulary
*g_sys.heap++ = 0; cell_t *forth = g_sys.heap;
*g_sys.heap++ = 0; *g_sys.heap++ = 0; *g_sys.heap++ = 0;
// Vocabulary stack
g_sys.current = (cell_t **) forth;
g_sys.context = (cell_t ***) g_sys.heap;
*g_sys.heap++ = (cell_t) forth;
for (int i = 0; i < VOCABULARY_DEPTH; ++i) { *g_sys.heap++ = 0; }
forth_run(0);
(*g_sys.current)[-1] = IMMEDIATE; // Make last word ; IMMEDIATE
g_sys.DOLIT_XT = FIND("DOLIT");
g_sys.DOEXIT_XT = FIND("EXIT");
g_sys.YIELD_XT = FIND("YIELD");
g_sys.notfound = FIND("DROP");
cell_t *start = g_sys.heap;
*g_sys.heap++ = FIND("EVALUATE1");
*g_sys.heap++ = FIND("BRANCH");
*g_sys.heap++ = (cell_t) start;
g_sys.argc = argc;
g_sys.argv = argv;
g_sys.base = 10;
g_sys.tib = src;
g_sys.ntib = src_len;
*++rp = (cell_t) sp;
*++rp = (cell_t) start;
g_sys.rp = rp;
g_sys.runner = forth_run;
}
#define JMPW goto **(void **) w
#define NEXT w = *ip++; JMPW
#define ADDR_DOCOLON && OP_DOCOLON
#define ADDR_DOCREATE && OP_DOCREATE
#define ADDR_DODOES && OP_DODOES
static cell_t *forth_run(cell_t *init_rp) {
if (!init_rp) {
#define X(name, op, code) create(name, sizeof(name) - 1, name[0] == ';', && OP_ ## op);
PLATFORM_OPCODE_LIST
OPCODE_LIST
#undef X
return 0;
}
register cell_t *ip, *rp, *sp, tos, w;
rp = init_rp; ip = (cell_t *) *rp--; sp = (cell_t *) *rp--;
DROP; NEXT;
#define X(name, op, code) OP_ ## op: { code; } NEXT;
PLATFORM_OPCODE_LIST
OPCODE_LIST
#undef X
OP_DOCOLON: ++rp; *rp = (cell_t) ip; ip = (cell_t *) (w + sizeof(cell_t)); NEXT;
OP_DOCREATE: DUP; tos = w + sizeof(cell_t) * 2; NEXT;
OP_DODOES: DUP; tos = w + sizeof(cell_t) * 2;
++rp; *rp = (cell_t) ip; ip = (cell_t *) *(cell_t *) (w + sizeof(cell_t)); NEXT;
}
const char boot[] =
": ( 41 parse drop drop ; immediate\n"
"\n"
"( Useful Basic Compound Words )\n"
": nip ( a b -- b ) swap drop ;\n"
": rdrop ( r: n n -- ) r> r> drop >r ;\n"
": */ ( n n n -- n ) */mod nip ;\n"
": * ( n n -- n ) 1 */ ;\n"
": /mod ( n n -- n n ) 1 swap */mod ;\n"
": / ( n n -- n ) /mod nip ;\n"
": mod ( n n -- n ) /mod drop ;\n"
": invert ( n -- ~n ) -1 xor ;\n"
": negate ( n -- -n ) invert 1 + ;\n"
": - ( n n -- n ) negate + ;\n"
": rot ( a b c -- c a b ) >r swap r> swap ;\n"
": -rot ( a b c -- b c a ) swap >r swap r> ;\n"
": < ( a b -- a<b ) - 0< ;\n"
": > ( a b -- a>b ) swap - 0< ;\n"
": <= ( a b -- a>b ) swap - 0< 0= ;\n"
": >= ( a b -- a<b ) - 0< 0= ;\n"
": = ( a b -- a!=b ) - 0= ;\n"
": <> ( a b -- a!=b ) = 0= ;\n"
": 0<> ( n -- n) 0= 0= ;\n"
": bl 32 ; : nl 10 ;\n"
": 1+ 1 + ; : 1- 1 - ;\n"
": 2* 2 * ; : 2/ 2 / ;\n"
": 4* 4 * ; : 4/ 4 / ;\n"
": +! ( n a -- ) swap over @ + swap ! ;\n"
"\n"
"( Line Comments )\n"
": \\ nl parse drop drop ; immediate\n"
"\n"
"( Cells )\n"
": cell+ ( n -- n ) cell + ;\n"
": cells ( n -- n ) cell * ;\n"
": cell/ ( n -- n ) cell / ;\n"
"\n"
"( Double Words )\n"
": 2drop ( n n -- ) drop drop ;\n"
": 2dup ( a b -- a b a b ) over over ;\n"
": 2@ ( a -- lo hi ) dup @ swap cell+ @ ;\n"
": 2! ( lo hi a -- ) dup >r cell+ ! r> ! ;\n"
"\n"
"( System Variables )\n"
": 'tib ( -- a ) 'sys 0 cells + ;\n"
": #tib ( -- a ) 'sys 1 cells + ;\n"
": >in ( -- a ) 'sys 2 cells + ;\n"
": state ( -- a ) 'sys 3 cells + ;\n"
": base ( -- a ) 'sys 4 cells + ;\n"
": 'heap ( -- a ) 'sys 5 cells + ;\n"
": current ( -- a ) 'sys 6 cells + ;\n"
": 'context ( -- a ) 'sys 7 cells + ; : context 'context @ cell+ ;\n"
": 'notfound ( -- a ) 'sys 8 cells + ;\n"
"\n"
"( Dictionary )\n"
": here ( -- a ) 'heap @ ;\n"
": allot ( n -- ) 'heap +! ;\n"
": aligned ( a -- a ) cell 1 - dup >r + r> invert and ;\n"
": align here aligned here - allot ;\n"
": , ( n -- ) here ! cell allot ;\n"
": c, ( ch -- ) here c! 1 allot ;\n"
"\n"
"( Compilation State )\n"
": [ 0 state ! ; immediate\n"
": ] -1 state ! ; immediate\n"
"\n"
"( Quoting Words )\n"
": ' bl parse 2dup find dup >r -rot r> 0= 'notfound @ execute 2drop ;\n"
": ['] ' aliteral ; immediate\n"
": char bl parse drop c@ ;\n"
": [char] char aliteral ; immediate\n"
": literal aliteral ; immediate\n"
"\n"
"( Core Control Flow )\n"
": begin here ; immediate\n"
": again ['] branch , , ; immediate\n"
": until ['] 0branch , , ; immediate\n"
": ahead ['] branch , here 0 , ; immediate\n"
": then here swap ! ; immediate\n"
": if ['] 0branch , here 0 , ; immediate\n"
": else ['] branch , here 0 , swap here swap ! ; immediate\n"
": while ['] 0branch , here 0 , swap ; immediate\n"
": repeat ['] branch , , here swap ! ; immediate\n"
": aft drop ['] branch , here 0 , here swap ; immediate\n"
"\n"
"( Compound words requiring conditionals )\n"
": min 2dup < if drop else nip then ;\n"
": max 2dup < if nip else drop then ;\n"
": abs ( n -- +n ) dup 0< if negate then ;\n"
"\n"
"( Dictionary Format )\n"
": >name ( xt -- a n ) 3 cells - dup @ swap over aligned - swap ;\n"
": >link& ( xt -- a ) 2 cells - ; : >link ( xt -- a ) >link& @ ;\n"
": >flags ( xt -- flags ) cell - ;\n"
": >body ( xt -- a ) dup @ [ ' >flags @ ] literal = 2 + cells + ;\n"
"\n"
"( Postpone - done here so we have ['] and IF )\n"
": immediate? ( xt -- f ) >flags @ 1 and 0= 0= ;\n"
": postpone ' dup immediate? if , else aliteral ['] , , then ; immediate\n"
"\n"
"( Constants and Variables )\n"
": constant ( n \"name\" -- ) create , does> @ ;\n"
": variable ( \"name\" -- ) create 0 , ;\n"
"\n"
"( Stack Convience )\n"
"sp@ constant sp0\n"
"rp@ constant rp0\n"
": depth ( -- n ) sp@ sp0 - cell/ ;\n"
"\n"
"( FOR..NEXT )\n"
": for postpone >r postpone begin ; immediate\n"
": next postpone donext , ; immediate\n"
"\n"
"( DO..LOOP )\n"
"variable leaving\n"
": leaving, here leaving @ , leaving ! ;\n"
": leaving( leaving @ 0 leaving ! ;\n"
": )leaving leaving @ swap leaving !\n"
" begin dup while dup @ swap here swap ! repeat drop ;\n"
": (do) ( n n -- .. ) swap r> -rot >r >r >r ;\n"
": do ( lim s -- ) leaving( postpone (do) here ; immediate\n"
": (?do) ( n n -- n n f .. ) 2dup = if 2drop 0 else -1 then ;\n"
": ?do ( lim s -- ) leaving( postpone (?do) postpone 0branch leaving,\n"
" postpone (do) here ; immediate\n"
": unloop postpone rdrop postpone rdrop ; immediate\n"
": leave postpone unloop postpone branch leaving, ; immediate\n"
": (+loop) ( n -- f .. ) dup 0< swap r> r> rot + dup r@ < -rot >r >r xor 0= ;\n"
": +loop ( n -- ) postpone (+loop) postpone until\n"
" postpone unloop )leaving ; immediate\n"
": loop 1 aliteral postpone +loop ; immediate\n"
": i ( -- n ) postpone r@ ; immediate\n"
": j ( -- n ) rp@ 3 cells - @ ;\n"
"\n"
"( Exceptions )\n"
"variable handler\n"
": catch ( xt -- n )\n"
" sp@ >r handler @ >r rp@ handler ! execute r> handler ! r> drop 0 ;\n"
": throw ( n -- )\n"
" dup if handler @ rp! r> handler ! r> swap >r sp! drop r> else drop then ;\n"
"' throw 'notfound !\n"
"\n"
"( Values )\n"
": value ( n -- ) create , does> @ ;\n"
": to ( n -- )\n"
" ' >body state @ if aliteral postpone ! else ! then ; immediate\n"
": +to ( n -- )\n"
" ' >body state @ if aliteral postpone +! else +! then ; immediate\n"
"\n"
"( Deferred Words )\n"
": defer ( \"name\" -- ) create 0 , does> @ dup 0= throw execute ;\n"
": is ( xt \"name -- ) postpone to ; immediate\n"
"\n"
"( Defer I/O to platform specific )\n"
"defer type\n"
"defer key\n"
"defer key?\n"
"defer bye\n"
": emit ( n -- ) >r rp@ 1 type rdrop ;\n"
": space bl emit ; : cr nl emit ;\n"
"\n"
"( Numeric Output )\n"
"variable hld\n"
": pad ( -- a ) here 80 + ;\n"
": digit ( u -- c ) 9 over < 7 and + 48 + ;\n"
": extract ( n base -- n c ) u/mod swap digit ;\n"
": <# ( -- ) pad hld ! ;\n"
": hold ( c -- ) hld @ 1 - dup hld ! c! ;\n"
": # ( u -- u ) base @ extract hold ;\n"
": #s ( u -- 0 ) begin # dup while repeat ;\n"
": sign ( n -- ) 0< if 45 hold then ;\n"
": #> ( w -- b u ) drop hld @ pad over - ;\n"
": str ( n -- b u ) dup >r abs <# #s r> sign #> ;\n"
": hex ( -- ) 16 base ! ; : octal ( -- ) 8 base ! ;\n"
": decimal ( -- ) 10 base ! ; : binary ( -- ) 2 base ! ;\n"
": u. ( u -- ) <# #s #> type space ;\n"
": . ( w -- ) base @ 10 xor if u. exit then str type space ;\n"
": ? ( a -- ) @ . ;\n"
": n. ( n -- ) base @ swap decimal <# #s #> type base ! ;\n"
"\n"
"( Strings )\n"
": parse-quote ( -- a n ) [char] \" parse ;\n"
": $place ( a n -- ) for aft dup c@ c, 1+ then next drop 0 c, align ;\n"
": $@ r@ dup cell+ swap @ r> dup @ 1+ aligned + cell+ >r ;\n"
": s\" parse-quote state @ if postpone $@ dup , $place\n"
" else dup here swap >r >r $place r> r> then ; immediate\n"
": .\" postpone s\" state @ if postpone type else type then ; immediate\n"
": z\" postpone s\" state @ if postpone drop else drop then ; immediate\n"
": r\" parse-quote state @ if swap aliteral aliteral then ; immediate\n"
": r| [char] | parse state @ if swap aliteral aliteral then ; immediate\n"
": s>z ( a n -- z ) here >r $place r> ;\n"
": z>s ( z -- a n ) 0 over begin dup c@ while 1+ swap 1+ swap repeat drop ;\n"
"\n"
"( Fill, Move )\n"
": cmove ( a a n -- ) for aft >r dup c@ r@ c! 1+ r> 1+ then next 2drop ;\n"
": cmove> ( a a n -- ) for aft 2dup swap r@ + c@ swap r@ + c! then next 2drop ;\n"
": fill ( a a n -- ) swap for swap aft 2dup c! 1 + then next 2drop ;\n"
"\n"
"( Better Errors )\n"
": notfound ( a n n -- )\n"
" if cr .\" ERROR: \" type .\" NOT FOUND!\" cr -1 throw then ;\n"
"' notfound 'notfound !\n"
"\n"
"( Input )\n"
": raw.s depth 0 max for aft sp@ r@ cells - @ . then next ;\n"
"variable echo -1 echo ! variable arrow -1 arrow !\n"
": ?echo ( n -- ) echo @ if emit else drop then ;\n"
": ?arrow. arrow @ if >r >r raw.s r> r> .\" --> \" then ;\n"
": accept ( a n -- n ) ?arrow. 0 swap begin 2dup < while\n"
" key\n"
" dup nl = over 13 = or if ?echo drop nip exit then\n"
" dup 8 = over 127 = or if\n"
" drop over if rot 1- rot 1- rot 8 ?echo bl ?echo 8 ?echo then\n"
" else\n"
" dup ?echo\n"
" >r rot r> over c! 1+ -rot swap 1+ swap\n"
" then\n"
" repeat drop nip ;\n"
"200 constant input-limit\n"
": tib ( -- a ) 'tib @ ;\n"
"create input-buffer input-limit allot\n"
": tib-setup input-buffer 'tib ! ;\n"
": refill tib-setup tib input-limit accept #tib ! 0 >in ! -1 ;\n"
"\n"
"( REPL )\n"
": prompt .\" ok\" cr ;\n"
": evaluate-buffer begin >in @ #tib @ < while evaluate1 repeat ;\n"
": evaluate ( a n -- ) 'tib @ >r #tib @ >r >in @ >r\n"
" #tib ! 'tib ! 0 >in ! evaluate-buffer\n"
" r> >in ! r> #tib ! r> 'tib ! ;\n"
": quit begin ['] evaluate-buffer catch\n"
" if 0 state ! sp0 sp! rp0 rp! .\" ERROR\" cr then\n"
" prompt refill drop again ;\n"
": ok .\" uEForth\" cr prompt refill drop quit ;\n"
"( Interpret time conditionals )\n"
"\n"
": DEFINED? ( \"name\" -- xt|0 )\n"
" bl parse find state @ if aliteral then ; immediate\n"
"defer [SKIP]\n"
": [THEN] ; : [ELSE] [SKIP] ; : [IF] 0= if [SKIP] then ;\n"
": [SKIP]' 0 begin postpone defined? dup if\n"
" dup ['] [IF] = if swap 1+ swap then\n"
" dup ['] [ELSE] = if swap dup 0 <= if 2drop exit then swap then\n"
" dup ['] [THEN] = if swap 1- dup 0< if 2drop exit then swap then\n"
" then drop again ;\n"
"' [SKIP]' is [SKIP]\n"
"( Implement Vocabularies )\n"
"variable last-vocabulary\n"
"current @ constant forth-wordlist\n"
": forth forth-wordlist context ! ;\n"
": vocabulary ( \"name\" ) create 0 , current @ 2 cells + , current @ @ last-vocabulary !\n"
" does> cell+ context ! ;\n"
": definitions context @ current ! ;\n"
"\n"
"( Make it easy to transfer words between vocabularies )\n"
": xt-find& ( xt -- xt& ) context @ begin 2dup @ <> while @ >link& repeat nip ;\n"
": xt-hide ( xt -- ) xt-find& dup @ >link swap ! ;\n"
": xt-transfer ( xt -- ) dup xt-hide current @ @ over >link& ! current @ ! ;\n"
": transfer ( \"name\" ) ' xt-transfer ;\n"
": }transfer ;\n"