forked from slviajero/tinybasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.c
More file actions
6927 lines (6203 loc) · 139 KB
/
basic.c
File metadata and controls
6927 lines (6203 loc) · 139 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
/*
*
* $Id: basic.c,v 1.139 2022/11/19 16:43:24 stefan Exp stefan $
*
* Stefan's IoT BASIC interpreter
*
* See the licence file on
* https://github.com/slviajero/tinybasic for copyright/left.
* (GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007)
*
* Author: Stefan Lenz, sl001@serverfabrik.de
*
* The first set of definions define the target.
* - MINGW switches on Windows calls.
* - MSDOS for MSDOS file access.
* - MAC doesn't need more settings here
* - RASPPI activates wiring code
* - Review hardware-*.h for settings specific Arduino hardware settings
* - HAS* activates or deactives features of the interpreter
* - the extension flags control features and code size
*
* MEMSIZE sets the BASIC main memory to a fixed value,
* if MEMSIZE=0 a heuristic is used based on free heap
* size and architecture parameters
*
* USEMEMINTERFACE controls the way memory is accessed. Don't change
* this here. It is a parameter set by hardware-arduino.h.
* This feature is experimental.
*
*/
#undef MINGW
#undef MSDOS
#undef RASPPI
/*
interpreter feature sets, choose one of the predefines
or undefine all predefines and set the features in custom settings
*/
/*
* BASICFUL: full language set
* BASICINTEGER: integer BASIC with full language
* BASICTINYWITHFLOAT: a floating point tinybasic
* BASICMINIMAL: minimal language
*/
#define BASICFULL
#undef BASICINTEGER
#undef BASICSIMPLE
#undef BASICMINIMAL
#undef BASICTINYWITHFLOAT
/*
* custom settings undef all the the language sets
* when you def here
*/
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#define HASFLOAT
#define HASGRAPH
#define HASDARTMOUTH
#define HASDARKARTS
#define HASIOT
#define HASMULTIDIM
#define HASSTRINGARRAYS
/* Palo Alto plus Arduino functions */
#ifdef BASICMINIMAL
#undef HASAPPLE1
#define HASARDUINOIO
#undef HASFILEIO
#undef HASTONE
#undef HASPULSE
#undef HASSTEFANSEXT
#undef HASERRORMSG
#undef HASVT52
#undef HASFLOAT
#undef HASGRAPH
#undef HASDARTMOUTH
#undef HASDARKARTS
#undef HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#endif
/* all features minus float and tone */
#ifdef BASICINTEGER
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#undef HASFLOAT
#define HASGRAPH
#define HASDARTMOUTH
#define HASDARKARTS
#define HASIOT
#define HASMULTIDIM
#define HASSTRINGARRAYS
#endif
/* a simple integer basic for small systems (UNO etc) */
#ifdef BASICSIMPLE
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#undef HASFLOAT
#undef HASGRAPH
#define HASDARTMOUTH
#undef HASDARKARTS
#define HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#endif
/* all features activated */
#ifdef BASICFULL
#define HASAPPLE1
#define HASARDUINOIO
#define HASFILEIO
#define HASTONE
#define HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#define HASVT52
#define HASFLOAT
#define HASGRAPH
#define HASDARTMOUTH
#define HASDARKARTS
#define HASIOT
#define HASMULTIDIM
#define HASSTRINGARRAYS
#endif
/* a Tinybasic with float support */
#ifdef BASICTINYWITHFLOAT
#undef HASAPPLE1
#define HASARDUINOIO
#undef HASFILEIO
#undef HASTONE
#undef HASPULSE
#define HASSTEFANSEXT
#define HASERRORMSG
#undef HASVT52
#define HASFLOAT
#undef HASGRAPH
#undef HASDARTMOUTH
#undef HASDARKARTS
#undef HASIOT
#undef HASMULTIDIM
#undef HASSTRINGARRAYS
#endif
/*
* Experimental features, to be tested and not sure that the add
* real value
*
* HASMSTAB: make tab more like MS TAB then Apple 1 TAB
* HASARRAYLIMIT: make the lower limit of an variable
*/
#define HASMSTAB
#define HASARRAYLIMIT
/*
* Language feature dependencies
*
* Dartmouth and darkarts needs the heap which is in Apple 1
* IoT needs strings and the heap, also Apple 1
*/
#if defined(HASDARTMOUTH) || defined(HASDARKARTS) || defined(HASIOT)
#define HASAPPLE1
#endif
#if defined(HASSTRINGARRAYS)
#define HASMULTIDIM
#endif
/* hardcoded memory size, set 0 for automatic malloc, don't redefine this beyond this point
* on an 168 with only 1 k memory and 16 kB flash we try to reduce the overhead and always
* set the memsize
*/
#define MEMSIZE 0
#ifdef ARDUINO_AVR_DUEMILANOVE
#define MEMSIZE 512
#endif
/* debug mode switch */
#define DEBUG 0
/*
* the core basic language headers including some Arduino device stuff
*/
#include "basic.h"
/*
* Hardware dependend definitions code are isolated in hardware-*.h
* Currently there are two versions
* hardware-arduino.h contains all platforms compiled in the Arduino IDE
* (ESP8266, ESP32, AVR, MEGAAVR, SAM*, RP2040)
* hardware-posix.h contains all platforms compiled in gcc with a POSIX OS
* (Mac, Raspberry, Windows/MINGW) plus rudimentary MSDOS with tc2.0. The
* latter will be removed soon.
*/
#ifdef ARDUINO
#include "hardware-arduino.h"
#else
#include "hardware-posix.h"
#endif
/*
* Determine the possible basic memory size.
* using malloc causes some overhead which can be relevant on the smaller
* boards. set MEMSIZE instead to a static value. In this case ballocmem
* just returns the static MEMSIZE.
*
* if SPIRAMINTERFACE is defined, we use the memory from a serial RAM and dont
* allocate it here at all.
*
*/
#if MEMSIZE == 0 && !defined(SPIRAMINTERFACE)
address_t ballocmem() {
mem_t i = 0;
/* on most platforms we know the free memory for BASIC */
long m=freememorysize();
if (m>maxaddr) m=maxaddr;
if (m>0) {
mem=(signed char*)malloc(m);
if (mem != 0) return m-1;
}
/*
* fallback if freememmorysize didn't work
*/
mem=(mem_t*)malloc(128);
if (mem != 0) return 128; else return 0;
}
#else
address_t ballocmem(){ return MEMSIZE-1; };
#endif
/*
* Layer 0 function - variable handling.
*
* These function access variables and data
*/
/*
* eeprom load / save / autorun functions
* needed for SAVE and LOAD to an EEPROM
* autorun is generic
*/
/* save a file to EEPROM, disabled if we use the EEPROM directly */
void esave() {
#ifndef EEPROMMEMINTERFACE
address_t a=0;
if (top+eheadersize < elength()) {
a=0;
/* EEPROM per default is 255, 0 indicates that there is a program */
eupdate(a++, 0);
/* store the size of the program in byte 1,2 of the EEPROM*/
z.a=top;
esetnumber(a, addrsize);
a+=addrsize;
while (a < top+eheadersize){
/* eupdate(a, mem[a-eheadersize]); */
eupdate(a, memread2(a-eheadersize));
a++;
}
eupdate(a++,0);
} else {
error(EOUTOFMEMORY);
er=0;
}
/* needed on I2C EEPROM and other platforms where we buffer */
eflush();
#endif
}
/* load a file from EEPROM, disabled if the use the EEPROM directly */
void eload() {
#ifndef EEPROMMEMINTERFACE
address_t a=0;
if (elength()>0 && (eread(a) == 0 || eread(a) == 1)) { // have we stored a program
a++;
/* how long is it? */
egetnumber(a, addrsize);
top=z.a;
a+=addrsize;
while (a < top+eheadersize){
/* mem[a-eheadersize]=eread(a); */
memwrite2(a-eheadersize, eread(a));
a++;
}
} else {
/* no valid program data is stored */
error(EEEPROM);
}
#endif
}
/* autorun something from EEPROM or a filesystem */
char autorun() {
#if defined(ARDUINOEEPROM) || defined(ARDUINOI2CEEPROM) || ! defined(ARDUINO)
if (eread(0) == 1){ /* autorun from the EEPROM */
egetnumber(1, addrsize);
top=z.a;
st=SERUN;
return 1; /* EEPROM autorun overrules filesystem autorun */
}
#endif
#if defined(FILESYSTEMDRIVER) || ! defined(ARDUINO)
/* on a POSIX or DOS platform, we check the command line first and the autoexec */
#ifndef ARDUINO
if (bargc > 0) {
if (ifileopen(bargv[1])) {
xload(bargv[1]);
st=SRUN;
ifileclose();
bnointafterrun=1;
return 1;
}
}
#endif
if (ifileopen("autoexec.bas")) {
xload("autoexec.bas");
st=SRUN;
ifileclose();
return 1;
}
#endif
return 0;
}
#ifdef HASAPPLE1
/*
* bmalloc() allocates a junk of memory for a variable on the
* heap, every objects is identified by name (c,d) and type t
* 3 bytes are used here.
*/
address_t bmalloc(mem_t t, mem_t c, mem_t d, address_t l) {
address_t vsize; /* the length of the header on the heap*/
address_t b;
if (DEBUG) { outsc("** bmalloc with token "); outnumber(t); outcr(); }
/* check if the object already exists */
if (bfind(t, c, d) != 0 ) { error(EVARIABLE); return 0; };
/*
* how much space is needed
* 3 bytes for the token and the 2 name characters
* numsize for every number including array length
* one byte for every string character
*/
switch(t) {
case VARIABLE:
vsize=numsize+3;
break;
#ifndef HASMULTIDIM
case ARRAYVAR:
vsize=numsize*l+addrsize+3;
break;
#else
/* multidim implementation for n=2 */
case ARRAYVAR:
vsize=numsize*l+addrsize*2+3;
break;
#endif
case TFN:
vsize=addrsize+2+3;
break;
default:
vsize=l+addrsize+3;
}
/* enough memory ?, on an EEPROM system we limit the heap to the RAM */
#ifndef EEPROMMEMINTERFACE
if ((himem-top) < vsize) { error(EOUTOFMEMORY); return 0;}
#else
if (himem-(elength()-eheadersize) < vsize) { error(EOUTOFMEMORY); return 0;}
#endif
/* here we could create a hash, currently simplified
the hash is the first digit of the variable plus the token */
b=himem;
memwrite2(b--, c);
memwrite2(b--, d);
memwrite2(b--, t);
/* for strings, arrays and buffers write the (maximum) length
directly after the header */
if (t == ARRAYVAR || t == STRINGVAR || t == TBUFFER) {
b=b-addrsize+1;
z.a=vsize-(addrsize+3);
setnumber(b, addrsize);
b--;
}
/* remark on multidim, the byte length is after the header and the following
address_t bytes are then reserved for the first dimension */
/* reserve space for the payload */
himem-=vsize;
nvars++;
return himem+1;
}
/*
* bfind() passes back the location of the object as result
* the length of the object is in z.a as a side effect
* rememers the last search
*
*/
address_t bfind(mem_t t, mem_t c, mem_t d) {
address_t b = memsize;
mem_t t1;
mem_t c1, d1;
address_t i=0;
/* the bfind cache, did we ask for that object before? -
needed to make the string code efficient */
if (t == bfindt && c == bfindc && d == bfindd) {
z.a=bfindz;
return bfinda;
}
while (i < nvars) {
c1=memread2(b--);
d1=memread2(b--);
t1=memread2(b--);
switch(t1) {
case VARIABLE:
z.a=numsize;
break;
case TFN:
z.a=addrsize+2;
break;
default:
b=b-addrsize+1;
getnumber(b, addrsize);
b--;
}
b-=z.a;
if (c1 == c && d1 == d && t1 == t) {
bfindc=c;
bfindd=d;
bfindt=t;
bfindz=z.a;
bfinda=b+1;
return b+1;
}
i++;
}
return 0;
}
/* finds an object and deletes the heap from this object on
including the object itself */
address_t bfree(mem_t t, mem_t c, mem_t d) {
address_t b = memsize;
mem_t t1;
mem_t c1, d1;
address_t i=0;
if (DEBUG) { outsc("*** bfree called for "); outch(c); outch(d); outsc(" on heap with token "); outnumber(t); outcr(); }
while (i < nvars) {
c1=memread2(b--);
d1=memread2(b--);
t1=memread2(b--);
if (t == t1 && c == c1 && d == d1) {
/* set the number of variables to the new value */
nvars=i;
if (DEBUG) { outsc("*** bfree setting nvars to "); outnumber(nvars); outcr(); }
/* clean up - this is somehow optional, one could drop this */
if (DEBUG) { outsc("*** bfree clearing "); outnumber(himem); outspc(); outnumber(b+3); outcr(); }
for (i=himem; i<=b+3; i++) memwrite2(i, 0);
/* now set the memory to the right address */
himem=b+3;
/* forget the chache !! */
bfindc=0;
bfindd=0;
bfindt=0;
bfindz=0;
bfinda=0;
return himem;
}
switch(t1) {
case VARIABLE:
z.a=numsize;
break;
case TFN:
z.a=addrsize+2;
break;
default:
b=b-addrsize+1;
getnumber(b, addrsize);
b--;
}
b-=z.a;
i++;
}
return 0;
}
/* the length of an object */
address_t blength (mem_t t, mem_t c, mem_t d) {
if (bfind(t, c, d)) return z.a; else return 0;
}
#endif
/* ununsed so far, simple variables are created on the fly */
void createvar(mem_t c, mem_t d){
return;
}
/* get and create a variable */
number_t getvar(mem_t c, mem_t d){
address_t a;
if (DEBUG) { outsc("* getvar "); outch(c); outch(d); outspc(); outcr(); }
/* the static variable array */
if (c >= 65 && c <= 91 && d == 0) return vars[c-65];
/* the special variables */
if ( c == '@' )
switch (d) {
case 'A':
return availch();
case 'S':
return ert;
case 'I':
return id;
case 'O':
return od;
case 'C':
if (availch()) return inch(); else return 0;
case 'E':
return elength()/numsize;
case 0:
return (himem-top)/numsize;
case 'R':
return rd;
#ifdef DISPLAYDRIVER
case 'X':
return dspgetcursorx();
case 'Y':
return dspgetcursory();
#endif
}
#ifdef HASAPPLE1
/* dynamically allocated vars, create them on the fly if needed */
if (!(a=bfind(VARIABLE, c, d))) a=bmalloc(VARIABLE, c, d, 0);
if (er != 0) return 0;
/* retrieve the value */
getnumber(a, numsize);
return z.i;
#else
error(EVARIABLE);
return 0;
#endif
}
/* set and create a variable */
void setvar(mem_t c, mem_t d, number_t v){
address_t a;
if (DEBUG) { outsc("* setvar "); outch(c); outch(d); outspc(); outnumber(v); outcr(); }
/* the static variable array */
if (c >= 65 && c <= 91 && d == 0) {
vars[c-65]=v;
return;
}
/* the special variables */
if ( c == '@' )
switch (d) {
case 'S':
ert=v;
return;
case 'I':
id=v;
return;
case 'O':
od=v;
return;
case 'C':
outch(v);
return;
case 'R':
rd=v;
return;
#ifdef DISPLAYDRIVER
case 'X':
dspsetcursorx((int)v);
return;
case 'Y':
dspsetcursory((int)v);
return;
#endif
}
#ifdef HASAPPLE1
/* dynamically allocated vars */
if (!(a=bfind(VARIABLE, c, d))) a=bmalloc(VARIABLE, c, d, 0);
if (er != 0) return;
/* set the valus */
z.i=v;
setnumber(a, numsize);
#else
error(EVARIABLE);
#endif
}
/* clr all variables */
void clrvars() {
address_t i;
for (i=0; i<VARSIZE; i++) vars[i]=0;
#ifdef HASAPPLE1
nvars=0;
/* for (i=himem; i<memsize; i++) mem[i]=0; */
for (i=himem; i<memsize; i++) memwrite2(i, 0);
himem=memsize;
bfindc=bfindd=bfindt=0;
bfinda=bfindz=0;
#endif
}
/* the BASIC memory access function */
void getnumber(address_t m, mem_t n){
mem_t i;
z.i=0;
switch (n) {
case 1:
z.i=memread2(m);
break;
case 2:
z.b.l=memread2(m++);
z.b.h=memread2(m);
break;
default:
for (i=0; i<n; i++) z.c[i]=memread2(m++);
}
}
/* test code only for the SPI RAM code, this function goes through the
ro buffer to avoit rw buffer page faults */
void pgetnumber(address_t m, mem_t n){
mem_t i;
z.i=0;
switch (n) {
case 1:
z.i=memread(m);
break;
case 2:
z.b.l=memread(m++);
z.b.h=memread(m);
break;
default:
for (i=0; i<n; i++) z.c[i]=memread(m++);
}
}
/* the eeprom memory access */
void egetnumber(address_t m, mem_t n){
mem_t i;
z.i=0;
switch (n) {
case 1:
z.i=eread(m);
break;
case 2:
z.b.l=eread(m++);
z.b.h=eread(m);
break;
default:
for (i=0; i<n; i++) z.c[i]=eread(m++);
}
}
/* set a number at a memory location */
void setnumber(address_t m, mem_t n){
mem_t i;
switch (n) {
case 1:
memwrite2(m, z.i);
break;
case 2:
memwrite2(m++, z.b.l);
memwrite2(m++, z.b.h);
break;
default:
for (i=0; i<n; i++) memwrite2(m++, z.c[i]);
}
}
/* set a number at a eepromlocation */
void esetnumber(address_t m, mem_t n){
mem_t i;
switch (n) {
case 1:
eupdate(m, z.i);
break;
case 2:
eupdate(m++, z.b.l);
eupdate(m, z.b.h);
break;
default:
for (i=0; i<n; i++) eupdate(m++, z.c[i]);
}
}
/* create an array */
address_t createarray(mem_t c, mem_t d, address_t i, address_t j) {
address_t a, zat, at;
#ifdef HASAPPLE1
if (DEBUG) { outsc("* create array "); outch(c); outch(d); outspc(); outnumber(i); outcr(); }
#ifndef HASMULTIDIM
if (bfind(ARRAYVAR, c, d)) error(EVARIABLE); else return bmalloc(ARRAYVAR, c, d, i);
#else
if (bfind(ARRAYVAR, c, d)) error(EVARIABLE); else {
a=bmalloc(ARRAYVAR, c, d, i*j);
zat=z.a; /* preserve z.a because it is needed on autocreate later */
z.a=j;
at=a+i*j*numsize;
memwrite2(at++, z.b.l);
memwrite2(at, z.b.h);
z.a=zat;
return a;
}
#endif
#endif
return 0;
}
/* finds if an array is twodimensional and what the second dimension structure is
test code, should be rewritten to properly use getnumber*/
address_t getarrayseconddim(address_t a, address_t za) {
#ifdef HASMULTIDIM
address_t zat1, zat2;
zat1=z.a;
z.b.l=memread2(a+za-2); /* test code, assuming 16 bit address_t here, should be ported to setnumber */
z.b.h=memread2(a+za-1);
zat2=z.a;
z.a=zat1;
return zat2;
#else
return 1;
#endif
}
/* generic array access function */
void array(mem_t m, mem_t c, mem_t d, address_t i, address_t j, number_t* v) {
address_t a;
address_t h;
mem_t e = 0;
#ifdef HASMULTIDIM
address_t dim=1, zat;
#endif
if (DEBUG) { outsc("* get/set array "); outch(c); outspc(); outnumber(i); outcr(); }
/* special arrays EEPROM, Display and Wang, dimension j is ignored here and not handled */
if (c == '@') {
switch(d) {
case 'E':
h=elength()/numsize;
a=elength()-numsize*i;
e=1;
break;
#if defined(DISPLAYDRIVER) && defined(DISPLAYCANSCROLL)
case 'D':
if (m == 'g') *v=dspget(i-1);
else if (m == 's') dspset(i-1, *v);
return;
#endif
#if !defined(ARDUINO) || defined(ARDUINORTC)
case 'T':
if (m == 'g') *v=rtcget(i);
else if (m == 's') rtcset(i, *v);
return;
#endif
#if defined(ARDUINO) && defined(ARDUINOSENSORS)
case 'S':
if (m == 'g') *v=sensorread(i, 0);
return;
#endif
case 0:
default:
h=(himem-top)/numsize;
a=himem-numsize*(i)+1;
break;
}
} else {
#ifdef HASAPPLE1
/* dynamically allocated arrays autocreated if needed */
if ( !(a=bfind(ARRAYVAR, c, d)) ) a=createarray(c, d, ARRAYSIZEDEF, 1);
if (er != 0) return;
#ifndef HASMULTIDIM
h=z.a/numsize;
#else
h=(z.a - addrsize)/numsize;
#endif
if (DEBUG) { outsc("** in array base address"); outnumber(a); outcr(); }
/* redudant code to getarrayseconddim */
#ifdef HASMULTIDIM
dim=getarrayseconddim(a, z.a);
a=a+((i-arraylimit)*dim+(j-arraylimit))*numsize;
#else
a=a+(i-arraylimit)*numsize;
#endif
#else
error(EVARIABLE);
return;
#endif
}
/* is the index in range */
#ifdef HASMULTIDIM
if (DEBUG) {
outsc("** in array ");
outnumber(i); outspc();
outnumber(j); outspc();
outnumber(dim); outspc();
outnumber(h); outspc();
outnumber(a); outcr();
}
if ( (j < arraylimit) || (j >= dim + arraylimit) || (i < arraylimit) || (i >= h/dim + arraylimit)) { error(EORANGE); return; }
#else
if (DEBUG) { outsc("** in array "); outnumber(i); outspc(); outnumber(a); outcr(); }
if ( (i < arraylimit) || (i >= h + arraylimit) ) { error(EORANGE); return; }
#endif
/* set or get the array */
if (m == 'g') {
if (! e) { getnumber(a, numsize); } else { egetnumber(a, numsize); }
*v=z.i;
} else if ( m == 's') {
z.i=*v;
if (! e) { setnumber(a, numsize); } else { esetnumber(a, numsize); }
}
}
/* create a string on the heap, i is the length of the string, j the dimension of the array */
address_t createstring(char c, char d, address_t i, address_t j) {
#ifdef HASAPPLE1
address_t a, zt;
if (DEBUG) { outsc("Create string "); outch(c); outch(d); outspc(); outnumber(nvars); outcr(); }
#ifndef HASSTRINGARRAYS
/* if no string arrays are in the code, we reserve the number of bytes i and space for the index */
if (bfind(STRINGVAR, c, d)) error(EVARIABLE); else a=bmalloc(STRINGVAR, c, d, i+strindexsize);
if (er != 0) return 0;
return a;
#else
/* string arrays need the number of array elements which address_ hence addresize bytes and then
the space for j strings */
if (bfind(STRINGVAR, c, d)) error(EVARIABLE); else a=bmalloc(STRINGVAR, c, d, addrsize+j*(i+strindexsize));
if (er != 0) return 0;
/* preserve z.a, this is a side effect of bmalloc and bfind */
zt=z.a;
/* set the dimension of the array */
z.a=j;
setnumber(a+j*(i+strindexsize), addrsize);
z.a=zt;
return a;
#endif
if (er != 0) return 0;
return a;
#else
return 0;
#endif
}
/* this is an experimental helper for @X$, generates a string
this is a method to insert a user defined string, e.g. from an I/O device
makemyxstring() can be called multiple times in the code for the same string
operation in BASIC, it cannot be used as a trigger for an I/O operation*/
void makemyxstring() {
mem_t i;
const char text[] = "hello world";
for(i=0; i<SBUFSIZE-1 && text[i]!=0 ; i++) sbuffer[i+1]=text[i];
sbuffer[0]=i;
}
/* get a string at position b, the -1+stringdexsize is needed because a string index starts with 1
* in addition to the memory pointer, return the address in memory.
* We use a pointer to memory here instead of going through the mem interface with an integer variable
* This makes string code lean to compile but is awkward for systems with serial memory
*
* The code may look like doing multiple heap scans for just one string as stringdim and lenstring are
* called here which causes bfind to called multiple time. This is harmless as bfind caches the last
* heap address.
*
* Getstring returns a pointer to the first string element in question.
*
* There are two side effects of getstring
* - ax has the address in memory of the string, if the string is on heap. 0 otherwise.
* - z.a has the number of bytes in the string payload area inculding the string length counter
*/
char* getstring(char c, char d, address_t b, address_t j) {
address_t k, zt, dim, maxlen;
ax=0;
if (DEBUG) { outsc("* get string var "); outch(c); outch(d); outspc(); outnumber(b); outcr(); }
/* direct access to the input buffer - deprectated but still there */
if ( c == '@' && d == 0) {
return ibuffer+b;
}
#ifdef HASAPPLE1
/* special strings */
/* the time string */
#if !defined(ARDUINO) || defined(ARDUINORTC)
if ( c == '@' && d == 'T') {
rtcmkstr();
return rtcstring+1+b;
}
#endif
/* a user definable special string in sbuffer, makemyxtring is a
user definable function */
if ( c == '@' && d == 'X' ) {
makemyxstring();
return sbuffer+b;
}
/* the arguments string on POSIX systems */
#ifndef ARDUINO
if ( c == '@' && d == 'A' ) {
k=0;
if (bargc > 1) while(k < SBUFSIZE && bargv[2][k] !=0) { sbuffer[k]=bargv[2][k]; k++; }
return sbuffer+b;