-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbasic.c
More file actions
9636 lines (8397 loc) · 219 KB
/
Copy pathbasic.c
File metadata and controls
9636 lines (8397 loc) · 219 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
/*----------------------------------------------------------------
* Please read this before compiling:
* - Review hardware.h for settings specific hardware settings.
* Super important on Arduino and Raspberry PI.
* - language.h controls the language features.
* For Arduino Integer BASIC is default and a limited language
* set. For the larger boards this can be extended a lot.
*-----------------------------------------------------------------
*
* $Id: basic.c,v 1.5 2024/03/02 15:38:20 stefan Exp stefan $
*
* Stefan's IoT BASIC interpreter - BASIC for everywhere.
*
* See the licence file on
* https:
* (GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007)
*
* Author: Stefan Lenz, sl001@serverfabrik.de
*
* Currently there are two versions of the runtime environment.
* One contains all platforms compiled in the Arduino IDE
* (ESP8266, ESP32, AVR, MEGAAVR, SAM*, RP2040)
*
* Anothers 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.
*
* The interface to BASIC is identical.
*/
/* the runtime environment */
#include "hardware.h"
#include "runtime.h"
/*
* the core basic language headers
*/
#include "language.h"
#include "basic.h"
/* use long jump for error handling */
#if USELONGJUMP == 1
#include "setjmp.h"
#endif
/* Global BASIC definitions */
/*
* All BASIC keywords for the tokens, PROGMEM on Arduino
* Normal memory elsewhere.
*/
const char sge[] PROGMEM = "=>";
const char sle[] PROGMEM = "<=";
const char sne[] PROGMEM = "<>";
/* Palo Alto language set */
const char sprint[] PROGMEM = "PRINT";
const char slet[] PROGMEM = "LET";
const char sinput[] PROGMEM = "INPUT";
const char sgoto[] PROGMEM = "GOTO";
const char sgosub[] PROGMEM = "GOSUB";
const char sreturn[] PROGMEM = "RETURN";
const char sif[] PROGMEM = "IF";
const char sfor[] PROGMEM = "FOR";
const char sto[] PROGMEM = "TO";
const char sstep[] PROGMEM = "STEP";
const char snext[] PROGMEM = "NEXT";
const char sstop[] PROGMEM = "STOP";
const char slist[] PROGMEM = "LIST";
const char snew[] PROGMEM = "NEW";
const char srun[] PROGMEM = "RUN";
const char sabs[] PROGMEM = "ABS";
const char srnd[] PROGMEM = "RND";
const char ssize[] PROGMEM = "SIZE";
const char srem[] PROGMEM = "REM";
/* Apple 1 language set */
#ifdef HASAPPLE1
const char snot[] PROGMEM = "NOT";
const char sand[] PROGMEM = "AND";
const char sor[] PROGMEM = "OR";
const char slen[] PROGMEM = "LEN";
const char ssgn[] PROGMEM = "SGN";
const char speek[] PROGMEM = "PEEK";
const char sdim[] PROGMEM = "DIM";
const char sclr[] PROGMEM = "CLR";
const char shimem[] PROGMEM = "HIMEM";
const char stab[] PROGMEM = "TAB";
const char sthen[] PROGMEM = "THEN";
const char sbend[] PROGMEM = "END";
const char spoke[] PROGMEM = "POKE";
#endif
/* Stefan's basic additions */
#ifdef HASSTEFANSEXT
const char scont[] PROGMEM = "CONT";
const char ssqr[] PROGMEM = "SQR";
const char spow[] PROGMEM = "POW";
const char smap[] PROGMEM = "MAP";
const char sdump[] PROGMEM = "DUMP";
const char sbreak[] PROGMEM = "BREAK";
#endif
/* LOAD and SAVE is always there */
const char ssave[] PROGMEM = "SAVE";
const char sload[] PROGMEM = "LOAD";
#ifdef HASSTEFANSEXT
const char sget[] PROGMEM = "GET";
const char sput[] PROGMEM = "PUT";
const char sset[] PROGMEM = "SET";
const char scls[] PROGMEM = "CLS";
const char slocate[] PROGMEM = "LOCATE";
const char selse[] PROGMEM = "ELSE";
#endif
/* Arduino functions */
#ifdef HASARDUINOIO
const char spinm[] PROGMEM = "PINM";
const char sdwrite[] PROGMEM = "DWRITE";
const char sdread[] PROGMEM = "DREAD";
const char sawrite[] PROGMEM = "AWRITE";
const char saread[] PROGMEM = "AREAD";
const char sdelay[] PROGMEM = "DELAY";
const char smillis[] PROGMEM = "MILLIS";
const char sazero[] PROGMEM = "AZERO";
const char sled[] PROGMEM = "LED";
#endif
#ifdef HASTONE
const char stone[] PROGMEM = "PLAY";
#endif
#ifdef HASPULSE
const char spulse[] PROGMEM = "PULSE";
#endif
/* DOS functions */
#ifdef HASFILEIO
const char scatalog[] PROGMEM = "CATALOG";
const char sdelete[] PROGMEM = "DELETE";
const char sfopen[] PROGMEM = "OPEN";
const char sfclose[] PROGMEM = "CLOSE";
const char sfdisk[] PROGMEM = "FDISK";
#endif
/* low level access functions */
#ifdef HASSTEFANSEXT
const char susr[] PROGMEM = "USR";
const char scall[] PROGMEM = "CALL";
#endif
/* mathematics */
#ifdef HASFLOAT
const char ssin[] PROGMEM = "SIN";
const char scos[] PROGMEM = "COS";
const char stan[] PROGMEM = "TAN";
const char satan[] PROGMEM = "ATAN";
const char slog[] PROGMEM = "LOG";
const char sexp[] PROGMEM = "EXP";
#endif
/* INT is always needed to make float/int programs compatible */
const char sint[] PROGMEM = "INT";
/* elemetars graphics */
#ifdef HASGRAPH
const char scolor[] PROGMEM = "COLOR";
const char splot[] PROGMEM = "PLOT";
const char sline[] PROGMEM = "LINE";
const char scircle[] PROGMEM = "CIRCLE";
const char srect[] PROGMEM = "RECT";
const char sfcircle[] PROGMEM = "FCIRCLE";
const char sfrect[] PROGMEM = "FRECT";
#endif
/* Dartmouth BASIC extensions */
#ifdef HASDARTMOUTH
const char sdata[] PROGMEM = "DATA";
const char sread[] PROGMEM = "READ";
const char srestore[] PROGMEM = "RESTORE";
const char sdef[] PROGMEM = "DEF";
const char sfn[] PROGMEM = "FN";
const char son[] PROGMEM = "ON";
#endif
/* The Darkarts commands unthinkable in Dartmouth */
#ifdef HASDARKARTS
const char smalloc[] PROGMEM = "MALLOC";
const char sfind[] PROGMEM = "FIND";
const char seval[] PROGMEM = "EVAL";
#endif
/* complex error handling */
#ifdef HASERRORHANDLING
const char serror[] PROGMEM = "ERROR";
#endif
/* iot extensions */
#ifdef HASIOT
const char savail[] PROGMEM = "AVAIL";
const char sstr[] PROGMEM = "STR";
const char sinstr[] PROGMEM = "INSTR";
const char sval[] PROGMEM = "VAL";
const char snetstat[] PROGMEM = "NETSTAT";
const char ssensor[] PROGMEM = "SENSOR";
const char swire[] PROGMEM = "WIRE";
const char ssleep[] PROGMEM = "SLEEP";
#endif
/* events and interrupts */
#ifdef HASTIMER
const char safter[] PROGMEM = "AFTER";
const char severy[] PROGMEM = "EVERY";
#endif
#ifdef HASEVENTS
const char sevent[] PROGMEM = "EVENT";
#endif
#ifdef HASSTRUCT
const char swhile[] PROGMEM = "WHILE";
const char swend[] PROGMEM = "WEND";
const char srepeat[] PROGMEM = "REPEAT";
const char suntil[] PROGMEM = "UNTIL";
const char sswitch[] PROGMEM = "SWITCH";
const char scase[] PROGMEM = "CASE";
const char sswend[] PROGMEM = "SWEND";
const char sdo[] PROGMEM = "DO";
const char sdend[] PROGMEM = "DEND";
#endif
#ifdef HASDARTMOUTH
#ifdef HASMULTILINEFUNCTIONS
const char sfend[] PROGMEM = "FEND";
#endif
#endif
#ifdef HASMSSTRINGS
const char sasc[] PROGMEM = "ASC";
const char schr[] PROGMEM = "CHR";
const char sright[] PROGMEM = "RIGHT";
const char sleft[] PROGMEM = "LEFT";
const char smid[] PROGMEM = "MID";
const char sspc[] PROGMEM = "SPC";
#endif
#ifdef HASEDITOR
const char sedit[] PROGMEM = "EDIT";
#endif
/* zero terminated keyword storage */
const char* const keyword[] PROGMEM = {
sge, sle, sne, sprint, slet, sinput,
sgoto, sgosub, sreturn, sif, sfor, sto,
sstep, snext, sstop, slist, snew, srun,
sabs, srnd, ssize, srem,
#ifdef HASAPPLE1
snot, sand, sor, slen, ssgn, speek, sdim,
sclr, shimem, stab, sthen,
sbend, spoke,
#endif
#ifdef HASSTEFANSEXT
scont, ssqr, spow, smap, sdump, sbreak,
#endif
ssave, sload,
#ifdef HASSTEFANSEXT
sget, sput, sset, scls, slocate, selse,
#endif
#ifdef HASARDUINOIO
spinm, sdwrite, sdread, sawrite, saread,
sdelay, smillis, sazero, sled,
#endif
#ifdef HASTONE
stone,
#endif
#ifdef HASPULSE
spulse,
#endif
#ifdef HASFILEIO
scatalog, sdelete, sfopen, sfclose, sfdisk,
#endif
#ifdef HASSTEFANSEXT
susr, scall,
#endif
#ifdef HASFLOAT
ssin, scos, stan, satan, slog, sexp,
#endif
sint,
#ifdef HASGRAPH
scolor, splot, sline, scircle, srect,
sfcircle, sfrect,
#endif
#ifdef HASDARTMOUTH
sdata, sread, srestore, sdef, sfn, son,
#endif
#ifdef HASDARKARTS
smalloc, sfind, seval,
#endif
/* complex error handling */
#ifdef HASERRORHANDLING
serror,
#endif
#ifdef HASIOT
savail, sstr, sinstr, sval,
snetstat, ssensor, swire, ssleep,
#endif
#ifdef HASTIMER
safter, severy,
#endif
#ifdef HASEVENTS
sevent,
#endif
#ifdef HASSTRUCT
swhile, swend, srepeat, suntil, sswitch, scase, sswend,
sdo, sdend,
#endif
#ifdef HASDARTMOUTH
#ifdef HASMULTILINEFUNCTIONS
sfend,
#endif
#endif
#ifdef HASMSSTRINGS
sasc, schr, sright, sleft, smid, sspc,
#endif
#ifdef HASEDITOR
sedit,
#endif
0
};
/* the zero terminated token dictonary needed for scalability */
const token_t tokens[] PROGMEM = {
GREATEREQUAL, LESSEREQUAL, NOTEQUAL, TPRINT, TLET,
TINPUT, TGOTO, TGOSUB, TRETURN, TIF, TFOR, TTO, TSTEP,
TNEXT, TSTOP, TLIST, TNEW, TRUN, TABS, TRND, TSIZE, TREM,
#ifdef HASAPPLE1
TNOT, TAND, TOR, TLEN, TSGN, TPEEK, TDIM, TCLR,
THIMEM, TTAB, TTHEN, TEND, TPOKE,
#endif
#ifdef HASSTEFANSEXT
TCONT, TSQR, TPOW, TMAP, TDUMP, TBREAK,
#endif
TSAVE, TLOAD,
#ifdef HASSTEFANSEXT
TGET, TPUT, TSET, TCLS, TLOCATE, TELSE,
#endif
#ifdef HASARDUINOIO
TPINM, TDWRITE, TDREAD, TAWRITE, TAREAD, TDELAY, TMILLIS,
TAZERO, TLED,
#endif
#ifdef HASTONE
TTONE,
#endif
#ifdef HASPULSE
TPULSE,
#endif
#ifdef HASFILEIO
TCATALOG, TDELETE, TOPEN, TCLOSE, TFDISK,
#endif
#ifdef HASSTEFANSEXT
TUSR, TCALL,
#endif
#ifdef HASFLOAT
TSIN, TCOS, TTAN, TATAN, TLOG, TEXP,
#endif
TINT,
#ifdef HASGRAPH
TCOLOR, TPLOT, TLINE, TCIRCLE, TRECT,
TFCIRCLE, TFRECT,
#endif
#ifdef HASDARTMOUTH
TDATA, TREAD, TRESTORE, TDEF, TFN, TON,
#endif
#ifdef HASDARKARTS
TMALLOC, TFIND, TEVAL,
#endif
#ifdef HASERRORHANDLING
TERROR,
#endif
#ifdef HASIOT
TAVAIL, TSTR, TINSTR, TVAL, TNETSTAT,
TSENSOR, TWIRE, TSLEEP,
#endif
#ifdef HASTIMER
TAFTER, TEVERY,
#endif
#ifdef HASEVENTS
TEVENT,
#endif
#ifdef HASSTRUCT
TWHILE, TWEND, TREPEAT, TUNTIL, TSWITCH, TCASE, TSWEND,
TDO, TDEND,
#endif
#ifdef HASDARTMOUTH
#ifdef HASMULTILINEFUNCTIONS
TFEND,
#endif
#endif
#ifdef HASMSSTRINGS
TASC, TCHR, TRIGHT, TLEFT, TMID, TSPC,
#endif
#ifdef HASEDITOR
TEDIT,
#endif
0
};
/* experimental, do not use right now */
const bworkfunction_t workfunctions[] PROGMEM = {
0, 0, 0, xprint, 0
};
/* errors and messages */
const char mfile[] PROGMEM = "file.bas";
const char mprompt[] PROGMEM = "> ";
const char mgreet[] PROGMEM = "Stefan's Basic 2.0alpha";
const char mline[] PROGMEM = "LINE";
const char mnumber[] PROGMEM = "NUMBER";
const char mvariable[] PROGMEM = "VARIABLE";
const char marray[] PROGMEM = "ARRAY";
const char mstring[] PROGMEM = "STRING";
const char mstringv[] PROGMEM = "STRINGVAR";
const char egeneral[] PROGMEM = "Error";
#ifdef HASERRORMSG
const char eunknown[] PROGMEM = "Syntax";
const char enumber[] PROGMEM = "Number";
const char edivide[] PROGMEM = "Div by 0";
const char eline[] PROGMEM = "Unknown Line";
const char emem[] PROGMEM = "Memory";
const char estack[] PROGMEM = "Stack";
const char erange[] PROGMEM = "Range";
const char estring[] PROGMEM = "String";
const char evariable[] PROGMEM = "Variable";
const char eloop[] PROGMEM = "Loop";
const char efile[] PROGMEM = "File";
const char efun[] PROGMEM = "Function";
const char eargs[] PROGMEM = "Args";
const char eeeprom[] PROGMEM = "EEPROM";
const char esdcard[] PROGMEM = "SD card";
#endif
const char* const message[] PROGMEM = {
mfile, mprompt, mgreet,
mline, mnumber, mvariable, marray,
mstring, mstringv,
egeneral
#ifdef HASERRORMSG
, eunknown, enumber, edivide, eline,
emem, estack, erange,
estring, evariable, eloop, efile, efun, eargs,
eeeprom, esdcard
#endif
};
/*
* maxnum: the maximum accurate(!) integer of a
* 32 bit float
* strindexsize: in the new code this is simply the size of the
* stringlength type. Currently only 1 byte and 2 bytes are tested.
*/
#ifdef HASFLOAT
const number_t maxnum=16777216;
#else
const number_t maxnum=(number_t)~((number_t)1<<(sizeof(number_t)*8-1));
#endif
const int numsize=sizeof(number_t);
const int addrsize=sizeof(address_t);
const int eheadersize=sizeof(address_t)+1;
const int strindexsize=sizeof(stringlength_t); /* default in the meantime, strings up to unsigned 16 bit length */
const address_t maxaddr=(address_t)(~0);
/*
* The basic interpreter is implemented as a stack machine
* with global variable for the interpreter state, the memory
* and the arithmetic during run time.
*/
/* the stack, all BASIC arithmetic is done here */
accu_t stack[STACKSIZE];
address_t sp=0;
/* a small buffer to process string arguments, mostly used for Arduino PROGMEM and string functions */
/* use with care as it is used in some string functions */
char sbuffer[SBUFSIZE];
/* the input buffer, the lexer can tokenize this and run from it, bi is an index to this.
bi must be global as it is the program cursor in interactive mode */
char ibuffer[BUFSIZE] = "\0";
char *bi;
/* a static array of variables A-Z for the small systems that have no heap */
#ifndef HASAPPLE1
number_t vars[VARSIZE];
#endif
/* the BASIC working memory, either malloced or allocated as a global array */
#if MEMSIZE != 0
mem_t mem[MEMSIZE];
#else
mem_t* mem;
#endif
address_t himem, memsize;
/* reimplementation of the loops, will replace the forstack */
bloop_t loopstack[FORDEPTH];
index_t loopsp = 0;
/* the GOSUB stack remembers an address to jump to */
address_t gosubstack[GOSUBDEPTH];
index_t gosubsp = 0;
/* arithmetic accumulators */
number_t x, y;
/* the name of on object, replaced xc and xy in BASIC 1 */
name_t name;
/* an address accumulator, used a lot in string operations */
address_t ax;
/* a string index registers, new style identifying a string either in C memory or BASIC memory */
string_t sr;
/* the active token */
token_t token;
/* the curent error, can be a token, hence token type */
token_t er;
/* the jmp buffer for the error handling */
#if USELONGJUMP == 1
jmp_buf sthook;
#endif
/* a trapable error */
mem_t ert;
/* the interpreter state, interactive, run or run from EEPROM */
mem_t st;
/* the current program location */
address_t here;
/* the topmost byte of a program in memory, beginning of free BASIC RAM */
address_t top;
/* used to format output with # */
mem_t form = 0;
/* do we use the Microsoft convention of an array starting at 0 or 1 like Apple 1
two seperate variables because arraylimit can be changed at runtime for existing arrays
msarraylimit says if an array should be created with n or n+1 elements */
#ifdef MSARRAYLIMITS
mem_t msarraylimits = 1;
address_t arraylimit = 0;
#else
mem_t msarraylimits = 0;
address_t arraylimit = 1;
#endif
/* behaviour around boolean, needed to change the interpreters personality at runtime */
/* -1 is microsoft true while 1 is Apple 1 and C style true. */
mem_t booleanmode = BOOLEANMODE;
/* setting the interpreter to integer at runtime */
mem_t forceint = 0;
/* the default size of a string now as a variable */
stringlength_t defaultstrdim = STRSIZEDEF;
/* the base of the random number generator
* 0 is Apple 1 style RND from 0 to n-epsilon
* 1 is Palo Alto style from 1 to n
*/
mem_t randombase = 0;
/* is substring logic used or not */
#ifdef SUPPRESSSUBSTRINGS
mem_t substringmode = 0;
#else
mem_t substringmode = 1;
#endif
/* the flag for true MS tabs */
mem_t reltab = 0;
/* the flag for lower case names */
mem_t lowercasenames = 0;
/* the number of arguments parsed from a command */
mem_t args;
/* the random number seed, this is unsigned */
#ifndef HASFLOAT
address_t rd;
#else
unsigned long rd;
#endif
/* the RUN debuglevel */
mem_t debuglevel = 0;
/* DATA pointer, where is the current READ statement */
#ifdef HASDARTMOUTH
address_t data = 0;
address_t datarc = 1;
#endif
/*
* process command line arguments in the POSIX world
* bnointafterrun is a flag to remember if called as command
* line argument, in this case we don't return to interactive
*/
#ifdef HASARGS
int bargc;
char** bargv;
mem_t bnointafterrun = 0;
#endif
/* formaters lastouttoken and spaceafterkeyword to make a nice LIST */
mem_t lastouttoken;
mem_t spaceafterkeyword;
mem_t outliteral = 0;
mem_t lexliteral = 0;
/*
* The cache for the heap search - helps the string code.
* The last found object on the heap is remembered. This is needed
* because the string code sometime searches the heap twice during the
* same operation. Also, bfind is used to remember the length of the
* last found object.
*/
#ifdef HASAPPLE1
heap_t bfind_object;
#endif
/*
* a variable for string to numerical conversion,
* telling you were the number ended.
*/
address_t vlength;
/* the timer code - very simple needs to be converted to to a struct */
/* timer type */
#ifdef HASTIMER
btimer_t after_timer = {0, 0, 0, 0, 0};
btimer_t every_timer = {0, 0, 0, 0, 0};
#endif
/* the event code */
#ifdef HASEVENTS
#define EVENTLISTSIZE 4
/* the event list */
int nevents = 0;
int ievent = 0;
mem_t events_enabled = 1;
volatile bevent_t eventlist[EVENTLISTSIZE];
/* the extension of the GOSUB stack */
mem_t gosubarg[GOSUBDEPTH];
#endif
#ifdef HASERRORHANDLING
/* the error handler type, very simple for now */
typedef struct {
mem_t type;
address_t linenumber;
} berrorh_t;
berrorh_t berrorh = {0 , 0};
mem_t erh = 0;
#endif
/* the string for real time clocks */
char rtcstring[20] = { 0 };
/* the units pulse operates on, in microseconds*/
address_t bpulseunit = 10;
/* only needed for POSIXNONBLOCKING */
mem_t breakcondition = 0;
/* the FN context, how deep are we in a nested function call, negative values reserved */
int fncontext = 0;
/*
* BASIC timer stuff, this is a core interpreter function now
*/
/* the millis function for BASIC */
void bmillis() {
number_t m;
/* millis is processed as integer and is cyclic mod maxnumber and not cast to float!! */
m=(number_t) (millis()/(unsigned long)pop() % (unsigned long)maxnum);
push(m);
}
/*
* 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() {
/* on most platforms we know the free memory for BASIC, this comes from runtime */
long m=freememorysize();
/* we subtract some language feature depended things, this is only needed on
small Arduino boards with memories below 16kb */
if (m < 16000) {
#ifdef HASAPPLE1
m-=64; /* strings cost memory */
#endif
#ifdef USELONGJUMP
m-=160; /* odd but true on Ardunio UNO and the like */
#endif
#ifdef HASFLOAT
m-=96;
#endif
#ifdef HASGRAPH
m-=256;
#endif
}
/* and keep fingers crossed here */
if (m<0) m=128;
/* we allocate as much as address_t can handle */
if (m>maxaddr) m=maxaddr;
/* try to allocate the memory */
mem=(mem_t*)malloc(m);
if (mem != 0) return m-1;
/* fallback if allocation failed, 128 bytes */
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.
*
* The eeprom is accessed through the runtime functions
* elength(), eupdate(), ewrite() and eflush();
*
*/
/* save a file to EEPROM, disabled if we use the EEPROM directly */
void esave() {
#ifndef EEPROMMEMINTERFACE
address_t a=0;
/* does the program fit into the eeprom */
if (top+eheadersize < elength()) {
/* 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*/
setaddress(a, eupdate, top);
a+=addrsize;
/* store the program */
while (a < top+eheadersize){
eupdate(a, memread2(a-eheadersize));
a++;
}
eupdate(a++,0);
/* needed on I2C EEPROM and other platforms where we buffer */
eflush();
} else {
error(EOUTOFMEMORY);
er=0;
}
#endif
}
/* load a file from EEPROM, disabled if the use the EEPROM directly */
void eload() {
#ifndef EEPROMMEMINTERFACE
address_t a=0;
/* have we stored a program? */
if (elength()>0 && (eread(a) == 0 || eread(a) == 1)) {
/* how long is it? */
a++;
top=getaddress(a, eread);
a+=addrsize;
/* load it to memory, memwrite2 is direct mem access */
while (a < top+eheadersize){
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() {
/* autorun from EEPROM if there is an EEPROM flagged for autorun */
if (elength()>0 && eread(0) == 1) { /* autorun from the EEPROM */
top=getaddress(1, eread);
st=SERUN;
return 1; /* EEPROM autorun overrules filesystem autorun */
}
/* autorun from a given command line argument, if we have one */
#ifdef HASARGS
if (bargc > 0 && ifileopen(bargv[1])) {
xload(bargv[1]);
st=SRUN;
ifileclose();
bnointafterrun=TERMINATEAFTERRUN;
return 1;
}
#endif
/* on a platform with a file system, autoexec from a file */
#if defined(FILESYSTEMDRIVER)
if (ifileopen("autoexec.bas")) {
xload("autoexec.bas");
st=SRUN;
ifileclose();
return 1;
}
#endif
/* nothing to autorun */
return 0;
}
#ifdef HASAPPLE1
/*
* The new malloc code. Heap structure is now
* payload
* (payload size)
* name
* type
*
* In the new heap implementation himem points to the first free byte on the heap.
* The payload is stored first and then the header.
*
* The new heap code uses name_t for the name of the object.
*
*/
address_t bmalloc(name_t* name, address_t l) {
address_t payloadsize; /* the payload size */
address_t heapheadersize = sizeof(name_t) + addrsize; /* this is only used to estimate the free space, it is the maximum */
address_t b=himem; /* the current position on the heap, we store it in case of errors */
/* Initial DEBUG message. */
if (DEBUG) {
outsc("** bmalloc with token ");
outnumber(name->token); outspc();
outname(name); outspc();
outnumber(l); outcr();
}
/*
* How much space does the payload of the object need?
*/
switch(name->token) {
case VARIABLE: /* a variable needs numsize bytes*/
payloadsize=numsize;
break;
#ifndef HASMULTIDIM
case ARRAYVAR: /* a one dimensional array needs numsize*l bytes */
payloadsize=numsize*l;
break;
#else
case ARRAYVAR: /* a two dimensional array needs numsize*l bytes plus one word for the additional dimension*/
payloadsize=numsize*l+addrsize;
break;
#endif
#ifdef HASDARTMOUTH
case TFN: /* the jump address, the type of function/type of return value, the number of vars
and all variables are stored*/
payloadsize=addrsize+2+sizeof(name_t)*l;
break;
#endif
/* these are plain buffers allocated by the MALLOC call in BASIC */
default:
payloadsize=l;
}
/* enough memory ?, on an EEPROM system we limit the heap to the RAM */
#ifndef EEPROMMEMINTERFACE
if ((himem-top) < payloadsize+heapheadersize) { error(EOUTOFMEMORY); return 0;}
#else
if (himem-(elength()-eheadersize) < payloadsize+heapheadersize) { error(EOUTOFMEMORY); return 0;}
#endif
/* first we reserve space for the payload, address points to the first byte of the payload */
/* b points to the first free byte after the payload*/
b-=payloadsize;
bfind_object.address=b+1;
/* for ARRAYS, STRINGS and BUFFERS, store the object length now - these are variable size objects*/
if (name->token != VARIABLE) {
b-=(addrsize-1);
setaddress(b, memwrite2, payloadsize);
if (DEBUG) {
outsc("** bmalloc writes payloadsize "); outnumber(payloadsize);
outsc(" at "); outnumber(b); outcr();
}
b--;
}
/* store the name of the objects including the type as identifier */
b=setname_heap(b, name);
/* store the type of the object */
memwrite2(b--, name->token);
/* if anything went wrong we exit here without changing himem */
if (b < top || er) { error(EOUTOFMEMORY); return 0; }
/* we fill the cache here as well, both right now for compatibility */
bfind_object.name=*name;
bfind_object.size=payloadsize;
/* himem is the next free byte now again */
himem=b;
if (DEBUG) {
outsc("** bmalloc returns "); outnumber(bfind_object.address);
outsc(" himem is "); outnumber(himem); outcr();
}
/* return the address of the payload */
return bfind_object.address;
}
address_t bfind(name_t* name) {
address_t b, b0;
address_t i=0;
/* Initial DEBUG message. */
if (DEBUG) {
outsc("*** bfind called for "); outname(name);
outsc(" on heap with token "); outnumber(name->token);
outsc(" himem is "); outnumber(himem); outcr();
}
/* do we have anything on the heap? */
if (himem == memsize) return 0; else b=himem+1;
/* we have the object already in cache and return */
if (name->token == bfind_object.name.token && cmpname(name, &bfind_object.name)) {
if (DEBUG) { outsc("*** bfind found in cache "); outname(name); outsc(" at "); outnumber(bfind_object.address); outcr(); }
return bfind_object.address;
}
/* walk through the heap from the last object added to the first */
while (b <= memsize) {
/* get the name and the type */
bfind_object.name.token=memread2(b++);
b=getname(b, &bfind_object.name, memread2);
/* determine the size of the object and advance */
if (bfind_object.name.token != VARIABLE) {
bfind_object.size=getaddress(b, memread2);
b+=addrsize;
} else {
bfind_object.size=numsize;
}
/* this is the location of the payload */
bfind_object.address=b;
/* have we found the object */
if (name->token == bfind_object.name.token && cmpname(name, &bfind_object.name)) {
if (DEBUG) { outsc("*** bfind found "); outname(name); outsc(" at "); outnumber(bfind_object.address); outcr(); }
return bfind_object.address;
}
/* advance on the heap */
b0=b;
b+=bfind_object.size;
/* safety net */
if (b0 > b) {
error(EVARIABLE);
return 0;
}
}