-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASIC.c
More file actions
1560 lines (1401 loc) · 55.8 KB
/
BASIC.c
File metadata and controls
1560 lines (1401 loc) · 55.8 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
/*
* MICRO-BASIC 2.1
*
* A small INTEGER BASIC interpreter originally written by Dave Dunfield,
* subsequently ported to MICRO-C, then modernized for GCC / ia16-elf-gcc
* / MinGW (2026).
*
* Variables:
* 260 Numeric variables : A0-A9 ... Z0-Z9
* 260 Character variables : A0$-A9$ ... Z0$-Z9$
* 260 Numeric arrays : A0()-A9() ... Z0()-Z9()
*
* The '0' suffix may be omitted: A == A0, Z$ == Z0$
*
* Statements:
* BEEP freq,ms Generate a tone on the PC speaker
* CLEAR Erase variables only
* CLOSE#n Close file (0-9) opened with OPEN
* DATA Inline data for READ
* DELAY ms Pause execution
* DIM var(size)[,...] Dimension an array
* DOS "command" Execute an OS shell command
* END Terminate program silently
* EXIT Quit MICRO-BASIC
* FOR v=init TO limit [STEP n] Counted loop
* GOSUB line Call subroutine
* GOTO line Unconditional jump
* IF test THEN line Conditional jump
* IF test THEN stmt Conditional single statement
* INPUT [prompt,] var Read a value
* INPUT#n, var Read from file
* LET (default) var = expression
* LIF test THEN stmts Long IF (rest of line)
* LIST [start[,end]] List source lines
* LIST#n ... List to file
* LOAD "name" Load program from disk
* NEW Clear program and variables
* NEXT [v] End FOR loop
* OPEN#n,"name","mode" Open file (fopen modes)
* ORDER line Set READ data pointer
* OUT port,expr Write I/O port
* PRINT [expr[,...]] Print to console
* PRINT#n,... Print to file
* READ var[,...] Read from DATA statements
* REM Comment
* RETURN Return from GOSUB
* RUN [line] Run program
* SAVE ["name"] Save program to disk
* STOP Halt with message
*
* Operators:
* + - * / % Arithmetic (+ also concatenates strings)
* & | ^ Bitwise AND, OR, XOR
* = <> Equal / not-equal (numeric or string)
* < <= > >= Comparisons (numeric only)
* ! Unary bitwise NOT
* Comparison operators evaluate to 1 (true) or 0 (false).
*
* Functions:
* ABS(n) Absolute value
* ASC(s) ASCII value of first character
* CHR$(n) Single character from ASCII value
* INP(port) Read I/O port
* KEY() Non-blocking keyboard test
* NUM(s) Convert string to number
* RND(n) Random number 0..n-1
* STR$(n) Convert number to string
*
* Copyright 1982-2003 Dave Dunfield - all rights reserved.
* Permission granted for personal (non-commercial) use only.
*
* Modernization notes (2026):
* - Explicit semantic typedefs (bint/ubint/bptr) replace raw int/unsigned.
* To retarget (Z80/SDCC, 6809/GCC6809, etc.) adjust the typedef block
* below only; no other changes are needed for the numeric types.
* - All functions have explicit return types and forward declarations.
* - Token bytes handled via tok_t type and TOKEN(x) macro throughout.
* - Platform HAL (#ifdef block) isolates BEEP/DELAY/KEY/INP/OUT.
* - File modes "rv"/"wv" -> "rb"/"wb" (Micro-C verbatim -> standard).
* - concat(), random() replaced with local/standard equivalents.
* - fgets() CR/LF stripping added (Micro-C I/O stripped these implicitly).
* - num_address()/str_address() replace the unsafe uintptr_t* trick.
*
* Build:
* GCC/Linux : gcc -std=c99 -Wall -O2 -o basic basic.c
* MinGW : gcc -std=c99 -Wall -O2 -o basic.exe basic.c
* ia16/DOS : ia16-elf-gcc -mcmodel=small -O2 -o basic.exe basic.c -li86
* ia16 small: ia16-elf-gcc -mcmodel=small -O2 -DSMALL_TARGET -o basic.exe basic.c -li86
* Micro-C : cc basic -fop
*
* Small-target tuning:
* -DSMALL_TARGET enables conservative defaults for 64 KB targets
* Individual overrides: -DNUM_VAR=52 -DCT_DEPTH=12 -DSA_SIZE=32 etc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <stdint.h>
/* =======================================================================
* Portable type aliases
*
* bint : the native BASIC integer - signed 16-bit on all current targets.
* On Z80 (SDCC) or 6809 (GCC6809) this stays int16_t; the
* compiler's own int is also 16-bit there, but being explicit is
* safer across compilers.
*
* ubint : unsigned form of bint - for bit ops, array indices, and anywhere
* the value is known non-negative (line numbers, dim sizes, etc.).
*
* bptr : must be wide enough to hold a data pointer on the target.
* The control stack stores both small bint values (step, limit,
* variable index) AND pointers (runptr, cmdptr), so it must use
* bptr throughout.
* - DOS/ia16 near model : uint16_t (same as ubint, flat 64 KB)
* - DOS/ia16 far model : uint32_t
* - 32-bit hosts : uint32_t (via uintptr_t)
* - 64-bit hosts : uint64_t (via uintptr_t)
* For a Z80 flat space : typedef uint16_t bptr;
* ======================================================================= */
typedef int16_t bint; /* BASIC numeric type - signed 16-bit */
typedef uint16_t ubint; /* BASIC unsigned type - unsigned 16-bit */
typedef uintptr_t bptr; /* pointer-width slot - ctl_stk entries */
/* =======================================================================
* tok_t - the type for a single byte read from a tokenised line.
* Using a named type removes all the scattered (signed char) casts.
*
* TOKEN(k) -> the byte stored in the token stream for keyword index k
* IS_TOK(c) -> true if byte c is a token (high bit set / value negative)
* ======================================================================= */
typedef signed char tok_t;
#define TOKEN(k) ((tok_t)((k) | 0x80))
#define IS_TOK(c) ((tok_t)(c) < 0)
/* =======================================================================
* Platform detection & Hardware Abstraction Layer
* Exports: do_beep(freq,ms) do_delay(ms) kbtst() do_in(p) do_out(p,v)
* ======================================================================= */
#if defined(__ia16__) || defined(__MSDOS__) || defined(_MSDOS)
/* -----------------------------------------------------------------------
* Real DOS: ia16-elf-gcc + libi86, DJGPP, Open Watcom, Turbo C, etc.
*
* Port I/O: libi86 and Open Watcom use outp()/inp() from <conio.h>.
* Borland Turbo C uses outportb()/inportb() from <dos.h>.
* We prefer the Watcom/libi86 names; <dos.h> is NOT included
* because its far-pointer typedefs break under -mcmodel=small
* with ia16-elf-gcc.
*
* delay(): lives in <conio.h> under libi86. Under DJGPP / Watcom it is
* in <dos.h> — if your toolchain puts it there, add <dos.h>
* and remove the inline-asm fallback below.
*
* Borland target: replace outp/inp with outportb/inportb and add
* #include <dos.h> (Borland's dos.h does not use far ptrs).
* ----------------------------------------------------------------------- */
# include <conio.h>
/* delay() is in <conio.h> under libi86. Provide a BIOS-tick fallback
* for toolchains that lack it (e.g. bare newlib without libi86). */
# if !defined(__LIBI86_COMPILING__) && !defined(delay)
static void delay(ubint ms)
{
/* INT 1Ah AH=00h: read BIOS tick counter (18.2 ticks/sec ~= 1/55ms) */
unsigned int ticks = ms / 55u + 1u;
unsigned int start_hi, start_lo, now_hi, now_lo;
__asm__ volatile (
"int $0x1a"
: "=c"(start_hi), "=d"(start_lo)
: "a"((unsigned int)0x0000)
: "cc"
);
for (;;) {
__asm__ volatile (
"int $0x1a"
: "=c"(now_hi), "=d"(now_lo)
: "a"((unsigned int)0x0000)
: "cc"
);
/* compare low word only - wraps ~every 24h, fine for short delays */
if ((unsigned int)(now_lo - start_lo) >= ticks) break;
}
}
# endif
static void do_beep(ubint freq, ubint ms)
{
ubint divisor = (ubint)(1193180UL / freq);
outp(0x43, 0xB6);
outp(0x42, (uint8_t)(divisor & 0xFF));
outp(0x42, (uint8_t)(divisor >> 8));
outp(0x61, (uint8_t)(inp(0x61) | 0x03));
delay(ms);
outp(0x61, (uint8_t)(inp(0x61) & ~0x03));
}
static void do_delay(ubint ms) { delay(ms); }
static bint kbtst(void) { return (bint)(kbhit() ? getch() : 0); }
static ubint do_in(ubint p) { return (ubint)inp(p); }
static void do_out(ubint p, ubint v) { outp(p, (uint8_t)v); }
#elif defined(__MINGW32__) || defined(__MINGW64__) || defined(_WIN32)
/* -----------------------------------------------------------------------
* Windows: MinGW 32/64, MSVC
* ----------------------------------------------------------------------- */
# include <windows.h>
# include <conio.h>
static void do_beep(ubint freq, ubint ms) { Beep(freq, ms); }
static void do_delay(ubint ms) { Sleep(ms); }
static bint kbtst(void) { return (bint)(_kbhit() ? _getch() : 0); }
static ubint do_in(ubint p) { (void)p; return 0; }
static void do_out(ubint p, ubint v) { (void)p; (void)v; }
#else
/* -----------------------------------------------------------------------
* POSIX: Linux / macOS
* BEEP -> terminal bell DELAY -> nanosleep (or clock() fallback)
* KEY -> non-blocking termios read
* INP/OUT -> no-ops (no user-mode port access on protected-mode OS)
* ----------------------------------------------------------------------- */
# include <time.h>
# include <alsa/asoundlib.h>
# include "tinybeep.h" // local public domain beeper
static void do_beep(ubint freq, ubint ms)
{
//(void)freq; (void)ms;
//fputc('\a', stdout); fflush(stdout);
snd_lib_error_set_handler(NULL); /* silence ALSA internal messages */
tinybeep(freq, ms);
}
static void do_delay(ubint ms)
{
# if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (long)(ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
# else
clock_t end = clock() + (clock_t)(ms * (CLOCKS_PER_SEC / 1000));
while (clock() < end) ;
# endif
}
# include <unistd.h>
# include <termios.h>
# include <fcntl.h>
static bint kbtst(void)
{
struct termios oldt, newt;
int ch, oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= (tcflag_t)~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
return (bint)((ch == EOF) ? 0 : ch);
}
static ubint do_in(ubint p) { (void)p; return 0; }
static void do_out(ubint p, ubint v) { (void)p; (void)v; }
#endif /* platform HAL */
/* =======================================================================
* RODATA / RD_BYTE / RD_PTR — ROM vs RAM address space abstraction
*
* On Von Neumann targets (ia16, Z80, 6809, x86) there is one address
* space; RODATA is just 'const' and the read macros are plain dereferences.
* The linker places const data in the ROM/flash segment automatically.
*
* On Harvard targets (AVR) flash and RAM are separate buses. String
* tables must be declared PROGMEM and read back via pgm_read_byte /
* pgm_read_word — normal pointer dereference will read RAM, not flash.
*
* To test the macro layer on a hosted build without real AVR hardware,
* compile with -DTEST_RODATA. All macros collapse to normal dereferences
* so behaviour is identical, but every access goes through the macro path.
*
* AVR / Arduino usage:
* Compile with -DAVR_PROGMEM (the Arduino toolchain defines __AVR__
* automatically; you can also key off that if preferred).
*
* RD_BYTE(p) : read one char/uint8 from a RODATA pointer
* RD_PTR(pp) : read one (const char *) from a RODATA pointer-to-pointer
* (used to walk reserved_words[] and error_messages[])
* ======================================================================= */
#if defined(AVR_PROGMEM) || defined(__AVR__)
# include <avr/pgmspace.h>
# define RODATA PROGMEM
# define RD_BYTE(p) pgm_read_byte(p)
# define RD_PTR(pp) ((const char *)pgm_read_word(pp))
#else
/* Von Neumann / hosted: plain dereference, const goes to .rodata */
# define RODATA /* nothing */
# define RD_BYTE(p) (*(const uint8_t *)(p))
# define RD_PTR(pp) (*(pp))
#endif
/* =======================================================================
* Interpreter constants
* ======================================================================= */
/* =======================================================================
* Build-time tuning
*
* Define SMALL_TARGET before including / compiling to get a configuration
* suited to a 64 KB address space (Z80, 6809, AVR, ia16 small model).
* Individual defines can also be overridden on the compiler command line:
* gcc -DBUFFER_SIZE=80 -DNUM_VAR=130 ...
*
* BUFFER_SIZE : input line buffer and scratch (bytes)
* SA_SIZE : string expression accumulator capacity (bytes)
* Must be >= the longest string your program uses.
* NUM_VAR : variable slots. Always (26 * digits_per_letter).
* Full set : 26*10 = 260 (A0..Z9)
* Half set : 26*5 = 130 (A0..Z4)
* Minimal : 26*2 = 52 (A0..Z1, i.e. A,B..Z + one extra)
* CTL_DEPTH : control stack depth (FOR + GOSUB frames combined).
* Each GOSUB frame = 3 slots, each FOR frame = 6 slots.
* MAX_FILES : number of user-accessible file handles (#0 .. #MAX_FILES-1)
* CP/M, FLEX, and most small DOSes support 4 open files fine.
*
* NOTE(stack): eval_sub() expression depth is capped at 8 levels via the
* nest counter; error(13) "Expression too deep" is raised cleanly on
* overflow. Sufficient for all practical integer BASIC programs.
* ======================================================================= */
#ifdef SMALL_TARGET
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 80 /* trim 20 bytes vs default */
# endif
# ifndef SA_SIZE
# define SA_SIZE 64 /* strings rarely exceed 64 chars on small targets */
# endif
# ifndef NUM_VAR
# define NUM_VAR 130 /* A0..Z4 : 26*5, halves variable table RAM */
# endif
# ifndef CTL_DEPTH
# define CTL_DEPTH 24 /* ~4 nested FOR loops or 8 GOSUBs */
# endif
# ifndef MAX_FILES
# define MAX_FILES 4 /* #0..#3 : enough for CP/M, FLEX, small DOS */
# endif
#else
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 100
# endif
# ifndef SA_SIZE
# define SA_SIZE 100
# endif
# ifndef NUM_VAR
# define NUM_VAR 260 /* A0..Z9 : full variable set */
# endif
# ifndef CTL_DEPTH
# define CTL_DEPTH 50
# endif
# ifndef MAX_FILES
# define MAX_FILES 10 /* #0..#9 */
# endif
#endif
/* Control stack frame tags - outside bint range so never confused with data */
#define _FOR 1000
#define _GOSUB (_FOR + 1)
/* Primary keyword tokens (1-based; 0 = not found) */
#define LET 1
#define EXIT 2
#define LIST 3
#define NEW 4
#define RUN 5
#define CLEAR 6
#define GOSUB 7
#define GOTO 8
#define RETURN 9
#define PRINT 10
#define FOR 11
#define NEXT 12
#define IF 13
#define LIF 14
#define REM 15
#define STOP 16
#define END 17
#define INPUT 18
#define OPEN 19
#define CLOSE 20
#define DIM 21
#define ORDER 22
#define READ 23
#define DATA 24
#define SAVE 25
#define LOAD 26
#define DELAY 27
#define BEEP 28
#define DOS 29
#define OUT 30
/* Secondary keyword tokens */
#define TO 31 /* lower bound of keyword range used in is_e_end() */
#define STEP 32
#define THEN 33
/* Operator / function tokens */
#define ADD 34 /* lower bound of operator range used in is_e_end() */
#define SUB 35
#define MUL 36
#define DIV 37
#define MOD 38
#define AND 39
#define OR 40
#define XOR 41
#define EQ 42
#define NE 43
#define LE 44
#define LT 45
#define GE 46
#define GT 47
#define CHR 48
#define STR 49
#define ASC 50
#define ABS 51
#define NUM 52
#define RND 53
#define KEY 54
#define INP 55
/* Pseudo-command: RUN without clearing variables (used by LOAD-in-program) */
#define RUN1 99
/* Operator priority table, indexed from 0 by (op_token - (ADD-1)) */
static const uint8_t RODATA priority[] = { 0, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1 };
/* Reserved word strings - order must match token #defines above */
static const char * const RODATA reserved_words[] = {
"LET", "EXIT", "LIST", "NEW", "RUN", "CLEAR", "GOSUB", "GOTO",
"RETURN","PRINT", "FOR", "NEXT", "IF", "LIF", "REM", "STOP",
"END", "INPUT", "OPEN", "CLOSE", "DIM", "ORDER", "READ", "DATA",
"SAVE", "LOAD", "DELAY", "BEEP", "DOS", "OUT",
"TO", "STEP", "THEN",
"+", "-", "*", "/", "%", "&", "|", "^",
"=", "<>", "<=", "<", ">=", ">",
"CHR$(", "STR$(", "ASC(", "ABS(", "NUM(", "RND(", "KEY(", "INP(",
NULL
};
/* Error messages, indexed by error number */
static const char * const RODATA error_messages[] = {
"Syntax", /* 0 */
"Illegal program", /* 1 */
"Illegal direct", /* 2 */
"Line number", /* 3 */
"Wrong type", /* 4 */
"Divide by zero", /* 5 */
"Nesting", /* 6 */
"File not open", /* 7 */
"File already open", /* 8 */
"Input", /* 9 */
"Dimension", /* 10 */
"Data", /* 11 */
"Out of memory", /* 12 */
"Expression too deep"/* 13 */
};
/* =======================================================================
* Program line storage
* Lines are kept in a singly-linked list sorted by line number.
* Ltext[] is a flexible member allocated with extra bytes for the
* tokenised line content.
* ======================================================================= */
struct line_rec {
ubint Lnumber;
struct line_rec *Llink;
char Ltext[1];
};
/* =======================================================================
* Global interpreter state
* ======================================================================= */
static char sa1[SA_SIZE], sa2[SA_SIZE]; /* string expression accumulators */
static struct line_rec *pgm_start; /* head of program line list */
static struct line_rec *runptr; /* line currently being executed */
static struct line_rec *readptr; /* current DATA line for READ */
static bint num_vars[NUM_VAR]; /* numeric (integer) variables */
static bint *dim_vars[NUM_VAR]; /* dimensioned (array) variable storage */
static char *char_vars[NUM_VAR]; /* string variable storage */
static ubint dim_check[NUM_VAR]; /* allocated sizes of dim arrays */
static FILE *files[MAX_FILES]; /* user-accessible file handles 0..MAX_FILES-1 */
static FILE *filein, *fileout; /* active I/O streams for current stmt */
static char buffer[BUFFER_SIZE]; /* raw input line / scratch */
static char *cmdptr; /* parse cursor (buffer or Ltext) */
static char *dataptr; /* parse cursor within a DATA line */
static char filename[256]; /* current LOAD / SAVE filename */
static char mode = 0; /* 0 = interactive, nonzero = running */
static char expr_type; /* 0 = numeric result, 1 = string */
static char nest; /* parenthesis / sub-expression depth */
static ubint line; /* current line number */
/* Control stack. Entries are either small bint values (step, limit,
* variable index, frame tag) or data pointers (runptr, cmdptr).
* bptr is the only type wide enough to hold both on all targets. */
static ubint ctl_ptr = 0;
static bptr ctl_stk[CTL_DEPTH];
static jmp_buf savjmp; /* error recovery / END / STOP / NEW */
/* =======================================================================
* Forward declarations
* ======================================================================= */
static int is_e_end(tok_t c);
static int is_l_end(tok_t c);
static int isterm(tok_t c);
static tok_t skip_blank(void);
static tok_t get_next(void);
static int test_next(tok_t token);
static void expect(tok_t token);
static ubint lookup(const char * const RODATA table[]);
static ubint get_num(void);
static char *allocate(ubint size);
static void delete_line(ubint lino);
static void insert_line(ubint lino);
static int edit_program(void);
static struct line_rec *find_line(ubint lno);
static bint *num_address(void);
static char **str_address(void);
static struct line_rec *execute(tok_t cmd);
static int chk_file(int flag);
static void disp_pgm(FILE *fp, ubint i, ubint j);
static void pgm_only(void);
static void direct_only(void);
static void skip_stmt(void);
static void error(ubint en);
static bint eval_num(void);
static void eval_char(void);
static bint eval(void);
static bint eval_sub(void);
static bint get_value(void);
static void get_char_value(char *ptr);
static bint do_arith(int opr, bint op1, bint op2);
static void num_string(bint value, char *ptr);
static void clear_pgm(void);
static void clear_vars(void);
static ubint get_var(void);
static void concat(char *dst, const char *a, const char *b);
/* =======================================================================
* concat() - replaces the Micro-C library built-in
* ======================================================================= */
static void concat(char *dst, const char *a, const char *b)
{
while (*a) *dst++ = *a++;
while (*b) *dst++ = *b++;
*dst = '\0';
}
/* =======================================================================
* Token / character classification helpers
* ======================================================================= */
/* True at the end of an expression token stream */
static int is_e_end(tok_t c)
{
if (c >= TOKEN(TO) && c < TOKEN(ADD)) return 1;
return (c == '\0') || (c == ':') || (c == ')') || (c == ',');
}
/* True at the end of a statement */
static int is_l_end(tok_t c)
{
return (c == '\0') || (c == ':');
}
/* True for horizontal whitespace */
static int isterm(tok_t c)
{
return (c == ' ') || (c == '\t');
}
/* Advance past whitespace; return next byte without consuming it */
static tok_t skip_blank(void)
{
while (isterm((tok_t)*cmdptr)) ++cmdptr;
return (tok_t)*cmdptr;
}
/* Advance past whitespace, consume and return the next byte */
static tok_t get_next(void)
{
tok_t c;
while (isterm((tok_t)(c = (tok_t)*cmdptr))) ++cmdptr;
if (c) ++cmdptr;
return c;
}
/* If the next non-blank byte equals token, consume it and return true */
static int test_next(tok_t token)
{
if (skip_blank() == token) { ++cmdptr; return 1; }
return 0;
}
/* Consume the next non-blank byte; syntax error if it != token */
static void expect(tok_t token)
{
if (get_next() != token) error(0);
}
/* =======================================================================
* lookup() - match next token in cmdptr against reserved_words[]
* Returns 1-based index on match (advancing cmdptr past it), 0 otherwise.
* RD_PTR/RD_BYTE used so the table can live in AVR flash (PROGMEM).
* ======================================================================= */
static ubint lookup(const char * const RODATA table[])
{
ubint i;
const char *cptr;
char *optr = cmdptr;
for (i = 0; (cptr = RD_PTR(&table[i])) != NULL; ++i) {
while (RD_BYTE(cptr) &&
(RD_BYTE(cptr) == toupper((unsigned char)*cmdptr))) {
++cptr; ++cmdptr; }
if (!RD_BYTE(cptr)) {
/* Avoid matching "FOR" inside "FORMAT": reject if both the
* last-matched char and the very next input char are alnum. */
if (!(isalnum((unsigned char)RD_BYTE(cptr-1)) &&
isalnum((unsigned char)*cmdptr))) {
skip_blank();
return (ubint)(i + 1); } }
cmdptr = optr; }
return 0;
}
/* =======================================================================
* get_num() - parse an unsigned decimal integer from cmdptr
* ======================================================================= */
static ubint get_num(void)
{
ubint value = 0;
char c;
while (isdigit((unsigned char)(c = *cmdptr))) {
++cmdptr;
value = (ubint)(value * 10 + (ubint)(c - '0')); }
return value;
}
/* =======================================================================
* allocate() - malloc + zero; calls error(12) on failure
* ======================================================================= */
static char *allocate(ubint size)
{
char *ptr = (char *)calloc(1, size);
if (!ptr) error(12);
return ptr;
}
/* =======================================================================
* Program line list management
* ======================================================================= */
static void delete_line(ubint lino)
{
struct line_rec *cur, *prev = NULL;
for (cur = pgm_start; cur; prev = cur, cur = cur->Llink) {
if (cur->Lnumber == lino) {
if (prev) prev->Llink = cur->Llink;
else pgm_start = cur->Llink;
free(cur);
return; } }
}
static void insert_line(ubint lino)
{
struct line_rec *node, *cur, *prev = NULL;
char *src = cmdptr;
ubint len;
for (len = (ubint)sizeof(struct line_rec); *src; ++len, ++src) ;
node = (struct line_rec *)allocate((ubint)(len + 1));
node->Lnumber = lino;
for (len = 0; *cmdptr; ++len) node->Ltext[len] = *cmdptr++;
node->Ltext[len] = '\0';
for (cur = pgm_start; cur && cur->Lnumber < lino;
prev = cur, cur = cur->Llink) ;
node->Llink = cur;
if (prev) prev->Llink = node;
else pgm_start = node;
}
/* =======================================================================
* edit_program()
* Tokenises buffer[], then inserts or removes the line.
* Returns non-zero when the input was a numbered source line.
* ======================================================================= */
static int edit_program(void)
{
ubint value;
char *ptr;
tok_t c;
/* Strip trailing CR/LF - fgets() keeps them; original Micro-C did not */
{ char *nl = buffer + strlen(buffer);
while (nl > buffer && (*(nl-1) == '\n' || *(nl-1) == '\r')) *--nl = '\0'; }
/* Tokenise: replace reserved words with (index | 0x80) bytes */
cmdptr = ptr = buffer;
while ((c = (tok_t)*cmdptr) != 0) {
if ((value = lookup(reserved_words)) != 0) {
*ptr++ = (char)(value | 0x80);
} else {
*ptr++ = (char)c; ++cmdptr;
if (c == '"') { /* pass string literals verbatim */
while ((c = (tok_t)*cmdptr) && c != '"') { ++cmdptr; *ptr++ = (char)c; }
*ptr++ = *cmdptr++; } } }
*ptr = '\0';
cmdptr = buffer;
if (isdigit((unsigned char)skip_blank())) {
value = get_num();
delete_line(value);
if (skip_blank()) insert_line(value);
return 1; }
return 0;
}
/* =======================================================================
* find_line() - locate line by number; error(3) if not found
* ======================================================================= */
static struct line_rec *find_line(ubint lno)
{
struct line_rec *p;
for (p = pgm_start; p; p = p->Llink)
if (p->Lnumber == lno) return p;
error(3);
return NULL; /* unreachable - error() longjmps */
}
/* =======================================================================
* Lvalue address helpers
*
* Two typed helpers replace the previous uintptr_t* trick, giving the
* compiler real type information and eliminating pointer-width casts.
*
* Both advance cmdptr past the variable name (and subscript).
* expr_type is set as a side effect of get_var() inside each helper.
* ======================================================================= */
/* Return pointer to the numeric lvalue named at cmdptr */
static bint *num_address(void)
{
ubint idx = get_var();
ubint sub;
if (expr_type) error(4); /* string var where numeric expected */
if (test_next('(')) { /* array element */
bint *arr = dim_vars[idx];
if (!arr) error(10);
nest = 0;
sub = (ubint)eval_sub();
if (sub >= dim_check[idx]) error(10);
return &arr[sub]; }
return &num_vars[idx];
}
/* Return pointer to the string lvalue named at cmdptr */
static char **str_address(void)
{
ubint idx = get_var();
if (!expr_type) error(4); /* numeric var where string expected */
return &char_vars[idx];
}
/* =======================================================================
* execute() - dispatch one BASIC statement
* Returns pointer to the next line_rec (for GOTO/GOSUB/IF),
* or NULL to continue with the next statement on the current line.
* ======================================================================= */
static struct line_rec *execute(tok_t cmd)
{
ubint i, j;
bint ii, jj, val;
struct line_rec *lp;
tok_t c;
switch ((int)(cmd & 0x7F)) {
/* ---- LET : variable = expression ---------------------------------- */
case LET : {
/* Peek at variable type before touching cmdptr permanently */
char *save = cmdptr;
ubint vtype;
get_var();
vtype = (ubint)expr_type;
cmdptr = save;
if (vtype) { /* string assignment */
char **dp = str_address();
expect(TOKEN(EQ));
eval_char(); /* result lands in sa1 */
if (*dp) free(*dp);
*dp = *sa1 ? strcpy(allocate((ubint)(strlen(sa1)+1)), sa1) : NULL;
} else { /* numeric assignment */
bint *dp = num_address();
expect(TOKEN(EQ));
*dp = eval();
}
break; }
/* ---- EXIT ---------------------------------------------------------- */
case EXIT :
exit(0);
/* ---- LIST [start[,end]] ------------------------------------------- */
case LIST :
chk_file(1);
if (!isdigit((unsigned char)skip_blank())) {
i = 0; j = (ubint)-1;
} else {
i = get_num();
if (get_next() == ',')
j = isdigit((unsigned char)skip_blank()) ? get_num() : (ubint)-1;
else
j = i; }
disp_pgm(fileout, i, j);
break;
/* ---- NEW ----------------------------------------------------------- */
case NEW :
clear_vars(); clear_pgm(); longjmp(savjmp, 1);
/* ---- RUN [line] ---------------------------------------------------- */
case RUN :
direct_only(); clear_vars();
/* fall through */
/* ---- RUN1 (no clear - used by LOAD mid-program) ------------------- */
case RUN1 :
runptr = is_e_end(skip_blank()) ? pgm_start
: find_line((ubint)eval_num());
--mode;
newline:
while (runptr) {
cmdptr = runptr->Ltext;
line = runptr->Lnumber;
do {
if ((c = skip_blank()) < 0) {
++cmdptr;
if ((lp = execute(c)) != NULL) { runptr = lp; goto newline; }
} else {
execute((tok_t)LET); }
} while ((c = get_next()) == ':');
if (c) error(0);
runptr = runptr->Llink; }
mode = 0;
break;
/* ---- CLEAR --------------------------------------------------------- */
case CLEAR :
clear_vars(); break;
/* ---- GOSUB line ---------------------------------------------------- */
case GOSUB :
ctl_stk[ctl_ptr++] = (bptr)runptr;
ctl_stk[ctl_ptr++] = (bptr)cmdptr;
ctl_stk[ctl_ptr++] = (bptr)_GOSUB;
/* fall through */
/* ---- GOTO line ----------------------------------------------------- */
case GOTO :
pgm_only();
return find_line((ubint)eval_num());
/* ---- RETURN -------------------------------------------------------- */
case RETURN :
pgm_only();
if ((int)ctl_stk[--ctl_ptr] != _GOSUB) error(6);
cmdptr = (char *)ctl_stk[--ctl_ptr];
runptr = (struct line_rec *)ctl_stk[--ctl_ptr];
line = runptr->Lnumber;
skip_stmt();
break;
/* ---- PRINT --------------------------------------------------------- */
case PRINT : {
int no_nl = 0;
chk_file(1);
do {
if (is_l_end(skip_blank())) { no_nl = 1; }
else {
val = eval();
if (!expr_type) { num_string(val, sa1); putc(' ', fileout); }
fputs(sa1, fileout); }
} while (test_next(','));
if (!no_nl) putc('\n', fileout);
break; }
/* ---- FOR v = init TO limit [STEP n] ------------------------------- */
case FOR :
pgm_only();
ii = 1;
i = get_var(); if (expr_type) error(0);
expect(TOKEN(EQ));
num_vars[i] = eval(); if (expr_type) error(0);
expect(TOKEN(TO));
jj = eval();
if (test_next(TOKEN(STEP))) ii = eval();
skip_stmt();
ctl_stk[ctl_ptr++] = (bptr)runptr; /* saved line ptr */
ctl_stk[ctl_ptr++] = (bptr)cmdptr; /* saved cmd ptr */
ctl_stk[ctl_ptr++] = (bptr)ii; /* step */
ctl_stk[ctl_ptr++] = (bptr)jj; /* limit */
ctl_stk[ctl_ptr++] = (bptr)i; /* variable index */
ctl_stk[ctl_ptr++] = (bptr)_FOR;
break;
/* ---- NEXT [v] ------------------------------------------------------ */
case NEXT :
pgm_only();
if ((int)ctl_stk[ctl_ptr-1] != _FOR) error(6);
i = (ubint) ctl_stk[ctl_ptr-2];
if (!is_l_end(skip_blank()))
if (get_var() != i) error(6);
jj = (bint)(intptr_t)ctl_stk[ctl_ptr-3];
ii = (bint)(intptr_t)ctl_stk[ctl_ptr-4];
num_vars[i] = (bint)(num_vars[i] + ii);
if ((ii < 0) ? (num_vars[i] >= jj) : (num_vars[i] <= jj)) {
cmdptr = (char *)ctl_stk[ctl_ptr-5];
runptr = (struct line_rec *)ctl_stk[ctl_ptr-6];
line = runptr->Lnumber;
} else { ctl_ptr -= 6; }
break;
/* ---- IF test THEN line | stmt ------------------------------------- */
case IF :
val = eval_num(); expect(TOKEN(THEN));
if (val) {
c = skip_blank();
if (isdigit((unsigned char)c)) return find_line((ubint)eval_num());
if (c < 0) { ++cmdptr; return execute(c); }
execute((tok_t)LET);
} else { skip_stmt(); }
break;
/* ---- LIF test THEN stmts ------------------------------------------ */
case LIF :
val = eval_num(); expect(TOKEN(THEN));
if (val) {
c = skip_blank();
if (c < 0) { ++cmdptr; return execute(c); }
execute((tok_t)LET);
break; }
/* condition false: fall through to DATA/REM skip behaviour */
/* fall through */
/* ---- DATA / REM : skip to next line in running mode --------------- */
case DATA :
pgm_only();
/* fall through */
case REM :
if (mode) {
if ((lp = runptr->Llink) != NULL) return lp;
longjmp(savjmp, 1); }
break;
/* ---- STOP ---------------------------------------------------------- */
case STOP :
pgm_only();
printf("STOP in line %u\n", (unsigned)line);
/* fall through */
/* ---- END ----------------------------------------------------------- */
case END :
pgm_only(); longjmp(savjmp, 1);
/* ---- INPUT ["prompt",] var ---------------------------------------- */
case INPUT : {
int from_file = chk_file(1);
char *save_cmd;
ubint vtype;