/
TinyBasicPlus.ino
2277 lines (1980 loc) · 49.9 KB
/
TinyBasicPlus.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
////////////////////////////////////////////////////////////////////////////////
// TinyBasic Plus
////////////////////////////////////////////////////////////////////////////////
//
// Authors:
// Gordon Brandly (Tiny Basic for 68000)
// Mike Field <hamster@snap.net.nz> (Arduino Basic) (port to Arduino)
// Scott Lawrence <yorgle@gmail.com> (TinyBasic Plus) (features, etc)
//
// Contributors:
// Brian O'Dell <megamemnon@megamemnon.com> (INPUT)
// (full list tbd)
// For full history of Tiny Basic, please see the wikipedia entry here:
// https://en.wikipedia.org/wiki/Tiny_BASIC
// LICENSING NOTES:
// Mike Field based his C port of Tiny Basic on the 68000
// Tiny BASIC which carried the following license:
/*
******************************************************************
* *
* Tiny BASIC for the Motorola MC68000 *
* *
* Derived from Palo Alto Tiny BASIC as published in the May 1976 *
* issue of Dr. Dobb's Journal. Adapted to the 68000 by: *
* Gordon Brandly *
* 12147 - 51 Street *
* Edmonton AB T5W 3G8 *
* Canada *
* (updated mailing address for 1996) *
* *
* This version is for MEX68KECB Educational Computer Board I/O. *
* *
******************************************************************
* Copyright (C) 1984 by Gordon Brandly. This program may be *
* freely distributed for personal use only. All commercial *
* rights are reserved. *
******************************************************************
*/
// ref: http://members.shaw.ca:80/gbrandly/68ktinyb.html
//
// However, Mike did not include a license of his own for his
// version of this.
// ref: http://hamsterworks.co.nz/mediawiki/index.php/Arduino_Basic
//
// From discussions with him, I felt that the MIT license is
// the most applicable to his intent.
//
// I am in the process of further determining what should be
// done wrt licensing further. This entire header will likely
// change with the next version 0.16, which will hopefully nail
// down the whole thing so we can get back to implementing
// features instead of licenses. Thank you for your time.
#define kVersion "v0.15"
// v0.15: 2018-06-23
// Integrating some contributions
// Corrected some of the #ifdef nesting atop this page
// Licensing issues beginning to be addressed
// v0.14: 2013-11-07
// Modified Input command to accept an expression using getn()
// Syntax is "input x" where x is any variable
// NOTE: This only works for numbers, expressions. not strings.
//
// v0.13: 2013-03-04
// Support for Arduino 1.5 (SPI.h included, additional changes for DUE support)
//
// v0.12: 2013-03-01
// EEPROM load and save routines added: EFORMAT, ELIST, ELOAD, ESAVE, ECHAIN
// added EAUTORUN option (chains to EEProm saved program on startup)
// Bugfixes to build properly on non-arduino systems (PROGMEM #define workaround)
// cleaned up a bit of the #define options wrt TONE
//
// v0.11: 2013-02-20
// all display strings and tables moved to PROGMEM to save space
// removed second serial
// removed pinMode completely, autoconf is explicit
// beginnings of EEPROM related functionality (new,load,save,list)
//
// v0.10: 2012-10-15
// added kAutoConf, which eliminates the "PINMODE" statement.
// now, DWRITE,DREAD,AWRITE,AREAD automatically set the PINMODE appropriately themselves.
// should save a few bytes in your programs.
//
// v0.09: 2012-10-12
// Fixed directory listings. FILES now always works. (bug in the SD library)
// ref: http://arduino.cc/forum/index.php/topic,124739.0.html
// fixed filesize printouts (added printUnum for unsigned numbers)
// #defineable baud rate for slow connection throttling
//e
// v0.08: 2012-10-02
// Tone generation through piezo added (TONE, TONEW, NOTONE)
//
// v0.07: 2012-09-30
// Autorun buildtime configuration feature
//
// v0.06: 2012-09-27
// Added optional second serial input, used for an external keyboard
//
// v0.05: 2012-09-21
// CHAIN to load and run a second file
// RND,RSEED for random stuff
// Added "!=" for "<>" synonym
// Added "END" for "STOP" synonym (proper name for the functionality anyway)
//
// v0.04: 2012-09-20
// DELAY ms - for delaying
// PINMODE <pin>, INPUT|IN|I|OUTPUT|OUT|O
// DWRITE <pin>, HIGH|HI|1|LOW|LO|0
// AWRITE <pin>, [0..255]
// fixed "save" appending to existing files instead of overwriting
// Updated for building desktop command line app (incomplete)
//
// v0.03: 2012-09-19
// Integrated Jurg Wullschleger whitespace,unary fix
// Now available through github
// Project renamed from "Tiny Basic in C" to "TinyBasic Plus"
//
// v0.02b: 2012-09-17 Scott Lawrence <yorgle@gmail.com>
// Better FILES listings
//
// v0.02a: 2012-09-17 Scott Lawrence <yorgle@gmail.com>
// Support for SD Library
// Added: SAVE, FILES (mostly works), LOAD (mostly works) (redirects IO)
// Added: MEM, ? (PRINT)
// Quirk: "10 LET A=B+C" is ok "10 LET A = B + C" is not.
// Quirk: INPUT seems broken?
// IF testing with Visual C, this needs to be the first thing in the file.
//#include "stdafx.h"
char eliminateCompileErrors = 1; // fix to suppress arduino build errors
// hack to let makefiles work with this file unchanged
#ifdef FORCE_DESKTOP
#undef ARDUINO
#include "desktop.h"
#else
#define ARDUINO 1
#endif
////////////////////////////////////////////////////////////////////////////////
// Feature option configuration...
// This enables LOAD, SAVE, FILES commands through the Arduino SD Library
// it adds 9k of usage as well.
//#define ENABLE_FILEIO 1
#undef ENABLE_FILEIO
// this turns on "autorun". if there's FileIO, and a file "autorun.bas",
// then it will load it and run it when starting up
//#define ENABLE_AUTORUN 1
#undef ENABLE_AUTORUN
// and this is the file that gets run
#define kAutorunFilename "autorun.bas"
// this is the alternate autorun. Autorun the program in the eeprom.
// it will load whatever is in the EEProm and run it
#define ENABLE_EAUTORUN 1
//#undef ENABLE_EAUTORUN
// this will enable the "TONE", "NOTONE" command using a piezo
// element on the specified pin. Wire the red/positive/piezo to the kPiezoPin,
// and the black/negative/metal disc to ground.
// it adds 1.5k of usage as well.
//#define ENABLE_TONES 1
#undef ENABLE_TONES
#define kPiezoPin 5
// we can use the EEProm to store a program during powerdown. This is
// 1kbyte on the '328, and 512 bytes on the '168. Enabling this here will
// allow for this funcitonality to work. Note that this only works on AVR
// arduino. Disable it for DUE/other devices.
#define ENABLE_EEPROM 1
//#undef ENABLE_EEPROM
// Sometimes, we connect with a slower device as the console.
// Set your console D0/D1 baud rate here (9600 baud default)
#define kConsoleBaud 9600
////////////////////////////////////////////////////////////////////////////////
// fixes for RAMEND on some platforms
#ifndef RAMEND
// RAMEND is defined for Uno type Arduinos
#ifdef ARDUINO
// probably DUE or 8266?
#ifdef ESP8266
#define RAMEND (8192-1)
#else
// probably DUE - ARM rather than AVR
#define RAMEND (4096-1)
#endif
#endif
#endif
// Enable memory alignment for certain processers (e.g. some ESP8266-based devices)
#ifdef ESP8266
// Uses up to one extra byte per program line of memory
#define ALIGN_MEMORY 1
#else
#undef ALIGN_MEMORY
#endif
#ifndef ARDUINO
// not an arduino, so we can disable these features.
// turn off EEProm
#undef ENABLE_EEPROM
#undef ENABLE_TONES
#endif
// includes, and settings for Arduino-specific features
#ifdef ARDUINO
// EEPROM
#ifdef ENABLE_EEPROM
#include <EEPROM.h> /* NOTE: case sensitive */
int eepos = 0;
#endif
// SD card File io
#ifdef ENABLE_FILEIO
#include <SD.h>
#include <SPI.h> /* needed as of 1.5 beta */
// set this to the card select for your Arduino SD shield
#define kSD_CS 10
#define kSD_Fail 0
#define kSD_OK 1
File fp;
#endif
// set up our RAM buffer size for program and user input
// NOTE: This number will have to change if you include other libraries.
// It is also an estimation. Might require adjustments...
#ifdef ENABLE_FILEIO
#define kRamFileIO (1030) /* approximate */
#else
#define kRamFileIO (0)
#endif
#ifdef ENABLE_TONES
#define kRamTones (40)
#else
#define kRamTones (0)
#endif
#define kRamSize (RAMEND - 1160 - kRamFileIO - kRamTones)
#endif /* ARDUINO Specifics */
// set up file includes for things we need, or desktop specific stuff.
#ifdef ARDUINO
// Use pgmspace/PROGMEM directive to store strings in progmem to save RAM
#include <avr/pgmspace.h>
#else
#include <stdio.h>
#include <stdlib.h>
#undef ENABLE_TONES
// size of our program ram
#define kRamSize 64*1024 /* arbitrary - not dependant on libraries */
#ifdef ENABLE_FILEIO
FILE * fp;
#endif
#endif
////////////////////
// memory alignment
// necessary for some esp8266-based devices
#ifdef ALIGN_MEMORY
// Align memory addess x to an even page
#define ALIGN_UP(x) ((unsigned char*)(((unsigned int)(x + 1) >> 1) << 1))
#define ALIGN_DOWN(x) ((unsigned char*)(((unsigned int)x >> 1) << 1))
#else
#define ALIGN_UP(x) x
#define ALIGN_DOWN(x) x
#endif
////////////////////
// various other desktop-tweaks and such.
#ifndef boolean
#define boolean int
#define true 1
#define false 0
#endif
#ifndef byte
typedef unsigned char byte;
#endif
// some catches for AVR based text string stuff...
#ifndef PROGMEM
#define PROGMEM
#endif
#ifndef pgm_read_byte
#define pgm_read_byte( A ) *(A)
#endif
////////////////////
#ifdef ENABLE_FILEIO
// functions defined elsehwere
void cmd_Files( void );
unsigned char * filenameWord(void);
static boolean sd_is_initialized = false;
#endif
// some settings based things
boolean inhibitOutput = false;
static boolean runAfterLoad = false;
static boolean triggerRun = false;
// these will select, at runtime, where IO happens through for load/save
enum {
kStreamSerial = 0,
kStreamEEProm,
kStreamFile
};
static unsigned char inStream = kStreamSerial;
static unsigned char outStream = kStreamSerial;
////////////////////////////////////////////////////////////////////////////////
// ASCII Characters
#define CR '\r'
#define NL '\n'
#define LF 0x0a
#define TAB '\t'
#define BELL '\b'
#define SPACE ' '
#define SQUOTE '\''
#define DQUOTE '\"'
#define CTRLC 0x03
#define CTRLH 0x08
#define CTRLS 0x13
#define CTRLX 0x18
typedef short unsigned LINENUM;
#ifdef ARDUINO
#define ECHO_CHARS 1
#else
#define ECHO_CHARS 0
#endif
static unsigned char program[kRamSize];
static const char * sentinel = "HELLO";
static unsigned char *txtpos,*list_line, *tmptxtpos;
static unsigned char expression_error;
static unsigned char *tempsp;
/***********************************************************/
// Keyword table and constants - the last character has 0x80 added to it
const static unsigned char keywords[] PROGMEM = {
'L','I','S','T'+0x80,
'L','O','A','D'+0x80,
'N','E','W'+0x80,
'R','U','N'+0x80,
'S','A','V','E'+0x80,
'N','E','X','T'+0x80,
'L','E','T'+0x80,
'I','F'+0x80,
'G','O','T','O'+0x80,
'G','O','S','U','B'+0x80,
'R','E','T','U','R','N'+0x80,
'R','E','M'+0x80,
'F','O','R'+0x80,
'I','N','P','U','T'+0x80,
'P','R','I','N','T'+0x80,
'P','O','K','E'+0x80,
'S','T','O','P'+0x80,
'B','Y','E'+0x80,
'F','I','L','E','S'+0x80,
'M','E','M'+0x80,
'?'+ 0x80,
'\''+ 0x80,
'A','W','R','I','T','E'+0x80,
'D','W','R','I','T','E'+0x80,
'D','E','L','A','Y'+0x80,
'E','N','D'+0x80,
'R','S','E','E','D'+0x80,
'C','H','A','I','N'+0x80,
#ifdef ENABLE_TONES
'T','O','N','E','W'+0x80,
'T','O','N','E'+0x80,
'N','O','T','O','N','E'+0x80,
#endif
#ifdef ARDUINO
#ifdef ENABLE_EEPROM
'E','C','H','A','I','N'+0x80,
'E','L','I','S','T'+0x80,
'E','L','O','A','D'+0x80,
'E','F','O','R','M','A','T'+0x80,
'E','S','A','V','E'+0x80,
#endif
#endif
0
};
// by moving the command list to an enum, we can easily remove sections
// above and below simultaneously to selectively obliterate functionality.
enum {
KW_LIST = 0,
KW_LOAD, KW_NEW, KW_RUN, KW_SAVE,
KW_NEXT, KW_LET, KW_IF,
KW_GOTO, KW_GOSUB, KW_RETURN,
KW_REM,
KW_FOR,
KW_INPUT, KW_PRINT,
KW_POKE,
KW_STOP, KW_BYE,
KW_FILES,
KW_MEM,
KW_QMARK, KW_QUOTE,
KW_AWRITE, KW_DWRITE,
KW_DELAY,
KW_END,
KW_RSEED,
KW_CHAIN,
#ifdef ENABLE_TONES
KW_TONEW, KW_TONE, KW_NOTONE,
#endif
#ifdef ARDUINO
#ifdef ENABLE_EEPROM
KW_ECHAIN, KW_ELIST, KW_ELOAD, KW_EFORMAT, KW_ESAVE,
#endif
#endif
KW_DEFAULT /* always the final one*/
};
struct stack_for_frame {
char frame_type;
char for_var;
short int terminal;
short int step;
unsigned char *current_line;
unsigned char *txtpos;
};
struct stack_gosub_frame {
char frame_type;
unsigned char *current_line;
unsigned char *txtpos;
};
const static unsigned char func_tab[] PROGMEM = {
'P','E','E','K'+0x80,
'A','B','S'+0x80,
'A','R','E','A','D'+0x80,
'D','R','E','A','D'+0x80,
'R','N','D'+0x80,
0
};
#define FUNC_PEEK 0
#define FUNC_ABS 1
#define FUNC_AREAD 2
#define FUNC_DREAD 3
#define FUNC_RND 4
#define FUNC_UNKNOWN 5
const static unsigned char to_tab[] PROGMEM = {
'T','O'+0x80,
0
};
const static unsigned char step_tab[] PROGMEM = {
'S','T','E','P'+0x80,
0
};
const static unsigned char relop_tab[] PROGMEM = {
'>','='+0x80,
'<','>'+0x80,
'>'+0x80,
'='+0x80,
'<','='+0x80,
'<'+0x80,
'!','='+0x80,
0
};
#define RELOP_GE 0
#define RELOP_NE 1
#define RELOP_GT 2
#define RELOP_EQ 3
#define RELOP_LE 4
#define RELOP_LT 5
#define RELOP_NE_BANG 6
#define RELOP_UNKNOWN 7
const static unsigned char highlow_tab[] PROGMEM = {
'H','I','G','H'+0x80,
'H','I'+0x80,
'L','O','W'+0x80,
'L','O'+0x80,
0
};
#define HIGHLOW_HIGH 1
#define HIGHLOW_UNKNOWN 4
#define STACK_SIZE (sizeof(struct stack_for_frame)*5)
#define VAR_SIZE sizeof(short int) // Size of variables in bytes
static unsigned char *stack_limit;
static unsigned char *program_start;
static unsigned char *program_end;
static unsigned char *stack; // Software stack for things that should go on the CPU stack
static unsigned char *variables_begin;
static unsigned char *current_line;
static unsigned char *sp;
#define STACK_GOSUB_FLAG 'G'
#define STACK_FOR_FLAG 'F'
static unsigned char table_index;
static LINENUM linenum;
static const unsigned char okmsg[] PROGMEM = "OK";
static const unsigned char whatmsg[] PROGMEM = "What? ";
static const unsigned char howmsg[] PROGMEM = "How?";
static const unsigned char sorrymsg[] PROGMEM = "Sorry!";
static const unsigned char initmsg[] PROGMEM = "TinyBasic Plus " kVersion;
static const unsigned char memorymsg[] PROGMEM = " bytes free.";
#ifdef ARDUINO
#ifdef ENABLE_EEPROM
static const unsigned char eeprommsg[] PROGMEM = " EEProm bytes total.";
static const unsigned char eepromamsg[] PROGMEM = " EEProm bytes available.";
#endif
#endif
static const unsigned char breakmsg[] PROGMEM = "break!";
static const unsigned char unimplimentedmsg[] PROGMEM = "Unimplemented";
static const unsigned char backspacemsg[] PROGMEM = "\b \b";
static const unsigned char indentmsg[] PROGMEM = " ";
static const unsigned char sderrormsg[] PROGMEM = "SD card error.";
static const unsigned char sdfilemsg[] PROGMEM = "SD file error.";
static const unsigned char dirextmsg[] PROGMEM = "(dir)";
static const unsigned char slashmsg[] PROGMEM = "/";
static const unsigned char spacemsg[] PROGMEM = " ";
static int inchar(void);
static void outchar(unsigned char c);
static void line_terminator(void);
static short int expression(void);
static unsigned char breakcheck(void);
/***************************************************************************/
static void ignore_blanks(void)
{
while(*txtpos == SPACE || *txtpos == TAB)
txtpos++;
}
/***************************************************************************/
static void scantable(const unsigned char *table)
{
int i = 0;
table_index = 0;
while(1)
{
// Run out of table entries?
if(pgm_read_byte( table ) == 0)
return;
// Do we match this character?
if(txtpos[i] == pgm_read_byte( table ))
{
i++;
table++;
}
else
{
// do we match the last character of keywork (with 0x80 added)? If so, return
if(txtpos[i]+0x80 == pgm_read_byte( table ))
{
txtpos += i+1; // Advance the pointer to following the keyword
ignore_blanks();
return;
}
// Forward to the end of this keyword
while((pgm_read_byte( table ) & 0x80) == 0)
table++;
// Now move on to the first character of the next word, and reset the position index
table++;
table_index++;
ignore_blanks();
i = 0;
}
}
}
/***************************************************************************/
static void pushb(unsigned char b)
{
sp--;
*sp = b;
}
/***************************************************************************/
static unsigned char popb()
{
unsigned char b;
b = *sp;
sp++;
return b;
}
/***************************************************************************/
void printnum(int num)
{
int digits = 0;
if(num < 0)
{
num = -num;
outchar('-');
}
do {
pushb(num%10+'0');
num = num/10;
digits++;
}
while (num > 0);
while(digits > 0)
{
outchar(popb());
digits--;
}
}
void printUnum(unsigned int num)
{
int digits = 0;
do {
pushb(num%10+'0');
num = num/10;
digits++;
}
while (num > 0);
while(digits > 0)
{
outchar(popb());
digits--;
}
}
/***************************************************************************/
static unsigned short testnum(void)
{
unsigned short num = 0;
ignore_blanks();
while(*txtpos>= '0' && *txtpos <= '9' )
{
// Trap overflows
if(num >= 0xFFFF/10)
{
num = 0xFFFF;
break;
}
num = num *10 + *txtpos - '0';
txtpos++;
}
return num;
}
/***************************************************************************/
static unsigned char print_quoted_string(void)
{
int i=0;
unsigned char delim = *txtpos;
if(delim != '"' && delim != '\'')
return 0;
txtpos++;
// Check we have a closing delimiter
while(txtpos[i] != delim)
{
if(txtpos[i] == NL)
return 0;
i++;
}
// Print the characters
while(*txtpos != delim)
{
outchar(*txtpos);
txtpos++;
}
txtpos++; // Skip over the last delimiter
return 1;
}
/***************************************************************************/
void printmsgNoNL(const unsigned char *msg)
{
while( pgm_read_byte( msg ) != 0 ) {
outchar( pgm_read_byte( msg++ ) );
};
}
/***************************************************************************/
void printmsg(const unsigned char *msg)
{
printmsgNoNL(msg);
line_terminator();
}
/***************************************************************************/
static void getln(char prompt)
{
outchar(prompt);
txtpos = program_end+sizeof(LINENUM);
while(1)
{
char c = inchar();
switch(c)
{
case NL:
//break;
case CR:
line_terminator();
// Terminate all strings with a NL
txtpos[0] = NL;
return;
case CTRLH:
if(txtpos == program_end)
break;
txtpos--;
printmsg(backspacemsg);
break;
default:
// We need to leave at least one space to allow us to shuffle the line into order
if(txtpos == variables_begin-2)
outchar(BELL);
else
{
txtpos[0] = c;
txtpos++;
outchar(c);
}
}
}
}
/***************************************************************************/
static unsigned char *findline(void)
{
unsigned char *line = program_start;
while(1)
{
if(line == program_end)
return line;
if(((LINENUM *)line)[0] >= linenum)
return line;
// Add the line length onto the current address, to get to the next line;
line += line[sizeof(LINENUM)];
}
}
/***************************************************************************/
static void toUppercaseBuffer(void)
{
unsigned char *c = program_end+sizeof(LINENUM);
unsigned char quote = 0;
while(*c != NL)
{
// Are we in a quoted string?
if(*c == quote)
quote = 0;
else if(*c == '"' || *c == '\'')
quote = *c;
else if(quote == 0 && *c >= 'a' && *c <= 'z')
*c = *c + 'A' - 'a';
c++;
}
}
/***************************************************************************/
void printline()
{
LINENUM line_num;
line_num = *((LINENUM *)(list_line));
list_line += sizeof(LINENUM) + sizeof(char);
// Output the line */
printnum(line_num);
outchar(' ');
while(*list_line != NL)
{
outchar(*list_line);
list_line++;
}
list_line++;
#ifdef ALIGN_MEMORY
// Start looking for next line on even page
if (ALIGN_UP(list_line) != list_line)
list_line++;
#endif
line_terminator();
}
/***************************************************************************/
static short int expr4(void)
{
// fix provided by Jurg Wullschleger wullschleger@gmail.com
// fixes whitespace and unary operations
ignore_blanks();
if( *txtpos == '-' ) {
txtpos++;
return -expr4();
}
// end fix
if(*txtpos == '0')
{
txtpos++;
return 0;
}
if(*txtpos >= '1' && *txtpos <= '9')
{
short int a = 0;
do {
a = a*10 + *txtpos - '0';
txtpos++;
}
while(*txtpos >= '0' && *txtpos <= '9');
return a;
}
// Is it a function or variable reference?
if(txtpos[0] >= 'A' && txtpos[0] <= 'Z')
{
short int a;
// Is it a variable reference (single alpha)
if(txtpos[1] < 'A' || txtpos[1] > 'Z')
{
a = ((short int *)variables_begin)[*txtpos - 'A'];
txtpos++;
return a;
}
// Is it a function with a single parameter
scantable(func_tab);
if(table_index == FUNC_UNKNOWN)
goto expr4_error;
unsigned char f = table_index;
if(*txtpos != '(')
goto expr4_error;
txtpos++;
a = expression();
if(*txtpos != ')')
goto expr4_error;
txtpos++;
switch(f)
{
case FUNC_PEEK:
return program[a];
case FUNC_ABS:
if(a < 0)
return -a;
return a;
#ifdef ARDUINO
case FUNC_AREAD:
pinMode( a, INPUT );
return analogRead( a );
case FUNC_DREAD:
pinMode( a, INPUT );
return digitalRead( a );
#endif
case FUNC_RND:
#ifdef ARDUINO
return( random( a ));
#else
return( rand() % a );
#endif
}
}
if(*txtpos == '(')
{
short int a;
txtpos++;
a = expression();
if(*txtpos != ')')
goto expr4_error;
txtpos++;
return a;
}
expr4_error:
expression_error = 1;
return 0;
}
/***************************************************************************/
static short int expr3(void)
{
short int a,b;
a = expr4();
ignore_blanks(); // fix for eg: 100 a = a + 1
while(1)
{
if(*txtpos == '*')
{
txtpos++;
b = expr4();
a *= b;
}
else if(*txtpos == '/')
{
txtpos++;
b = expr4();
if(b != 0)
a /= b;
else
expression_error = 1;
}
else
return a;
}
}
/***************************************************************************/
static short int expr2(void)
{
short int a,b;
if(*txtpos == '-' || *txtpos == '+')
a = 0;
else
a = expr3();
while(1)
{
if(*txtpos == '-')
{
txtpos++;
b = expr3();
a -= b;
}
else if(*txtpos == '+')
{
txtpos++;
b = expr3();
a += b;
}
else
return a;
}
}
/***************************************************************************/
static short int expression(void)
{
short int a,b;
a = expr2();
// Check if we have an error
if(expression_error) return a;