-
Notifications
You must be signed in to change notification settings - Fork 2
/
bas2tap.c
3375 lines (3235 loc) · 185 KB
/
bas2tap.c
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
/**********************************************************************************************************************************/
/* Module : BAS2TAP.C */
/* Executable : BAS2TAP.EXE */
/* Doc file : BAS2TAP.DOC */
/* Version type : Single file */
/* Last changed : 20-01-2013 16:00 */
/* Update count : 18 */
/* OS type : Generic */
/* Watcom C = wcl386 -mf -fp3 -fpi -3r -oxnt -w4 -we bas2tap.c */
/* MS C = cl /Ox /G2 /AS bas2tap.c /F 1000 */
/* gcc = gcc -Wall -O2 bas2tap.c -o bas2tap -lm ; strip bas2tap */
/* SAS/C = sc link math=ieee bas2tap.c */
/* Libs needed : math */
/* Description : Convert ASCII BASIC file to TAP tape image emulator file */
/* */
/* Notes : There's a check for a define "__DEBUG__", which generates tons of output if defined. */
/* */
/* Copyleft (C) 1998-2013 ThunderWare Research Center, written by Martijn van der Heide. */
/* */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation; either version 2 */
/* of the License, or (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* */
/**********************************************************************************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
/**********************************************************************************************************************************/
/* Some compilers don't define the following things, so I define them here... */
/**********************************************************************************************************************************/
#ifdef __WATCOMC__
#define x_strnicmp(_S1,_S2,_Len) strnicmp (_S1, _S2, _Len)
#define x_log2(_X) log2 (_X)
#else
int x_strnicmp (char *_S1, char *_S2, int _Len) /* Case independant partial string compare */
{
for ( ; _Len && *_S1 && *_S2 && toupper (*_S1) == toupper (*_S2) ; _S1 ++, _S2 ++, _Len --)
;
return (_Len ? (int)toupper (*_S1) - (int)toupper (*_S2) : 0);
}
#define x_log2(_X) (log (_X) / log (2.0)) /* If your compiler doesn't know the 'log2' function */
#endif
//#define __DEBUG__
typedef unsigned char byte;
#ifndef FALSE
typedef unsigned char bool; /* If your compiler doesn't know this variable type yet */
#define TRUE 1
#define FALSE 0
#endif
/**********************************************************************************************************************************/
/* Define the global variables */
/**********************************************************************************************************************************/
struct TokenMap_s
{
char *Token;
byte TokenType;
/* Type 0 = No special meaning */
/* Type 1 = Always keyword */
/* Type 2 = Can be both keyword and non-keyword (colour parameters) */
/* Type 3 = Numeric expression token */
/* Type 4 = String expression token */
/* Type 5 = May only appear in (L)PRINT statements (AT and TAB) */
/* Type 6 = Type-less (normal ASCII or expression token) */
byte KeywordClass[8]; /* The class this keyword belongs to, as defined in the Spectrum ROM */
/* This table is used by expression tokens as well. Class 12 was added for this purpose */
/* Class 0 = No further operands */
/* Class 1 = Used in LET. A variable is required */
/* Class 2 = Used in LET. An expression, numeric or string, must follow */
/* Class 3 = A numeric expression may follow. Zero to be used in case of default */
/* Class 4 = A single character variable must follow */
/* Class 5 = A set of items may be given */
/* Class 6 = A numeric expression must follow */
/* Class 7 = Handles colour items */
/* Class 8 = Two numeric expressions, separated by a comma, must follow */
/* Class 9 = As for class 8 but colour items may precede the expression */
/* Class 10 = A string expression must follow */
/* Class 11 = Handles cassette routines */
/* The following classes are not available in the ROM but were needed */
/* Class 12 = One or more string expressions, separated by commas, must follow */
/* Class 13 = One or more expressions, separated by commas, must follow */
/* Class 14 = One or more variables, separated by commas, must follow (READ) */
/* Class 15 = DEF FN */
} TokenMap[256] = {
/* Everything below ASCII 32 */
{NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }},
{NULL, 6, { 0 }}, /* Print ' */
{NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }},
{"(eoln)", 6, { 0 }}, /* CR */
{NULL, 6, { 0 }}, /* Number */
{NULL, 6, { 0 }},
{NULL, 6, { 0 }}, /* INK */
{NULL, 6, { 0 }}, /* PAPER */
{NULL, 6, { 0 }}, /* FLASH */
{NULL, 6, { 0 }}, /* BRIGHT */
{NULL, 6, { 0 }}, /* INVERSE */
{NULL, 6, { 0 }}, /* OVER */
{NULL, 6, { 0 }}, /* AT */
{NULL, 6, { 0 }}, /* TAB */
{NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }}, {NULL, 6, { 0 }},
{NULL, 6, { 0 }},
/* Normal ASCII set */
{"\x20", 6, { 0 }}, {"\x21", 6, { 0 }}, {"\x22", 6, { 0 }}, {"\x23", 6, { 0 }}, {"\x24", 6, { 0 }}, {"\x25", 6, { 0 }},
{"\x26", 6, { 0 }}, {"\x27", 6, { 0 }}, {"\x28", 6, { 0 }}, {"\x29", 6, { 0 }}, {"\x2A", 6, { 0 }}, {"\x2B", 6, { 0 }},
{"\x2C", 6, { 0 }}, {"\x2D", 6, { 0 }}, {"\x2E", 6, { 0 }}, {"\x2F", 6, { 0 }}, {"\x30", 6, { 0 }}, {"\x31", 6, { 0 }},
{"\x32", 6, { 0 }}, {"\x33", 6, { 0 }}, {"\x34", 6, { 0 }}, {"\x35", 6, { 0 }}, {"\x36", 6, { 0 }}, {"\x37", 6, { 0 }},
{"\x38", 6, { 0 }}, {"\x39", 6, { 0 }}, {"\x3A", 2, { 0 }}, {"\x3B", 6, { 0 }}, {"\x3C", 6, { 0 }}, {"\x3D", 6, { 0 }},
{"\x3E", 6, { 0 }}, {"\x3F", 6, { 0 }}, {"\x40", 6, { 0 }}, {"\x41", 6, { 0 }}, {"\x42", 6, { 0 }}, {"\x43", 6, { 0 }},
{"\x44", 6, { 0 }}, {"\x45", 6, { 0 }}, {"\x46", 6, { 0 }}, {"\x47", 6, { 0 }}, {"\x48", 6, { 0 }}, {"\x49", 6, { 0 }},
{"\x4A", 6, { 0 }}, {"\x4B", 6, { 0 }}, {"\x4C", 6, { 0 }}, {"\x4D", 6, { 0 }}, {"\x4E", 6, { 0 }}, {"\x4F", 6, { 0 }},
{"\x50", 6, { 0 }}, {"\x51", 6, { 0 }}, {"\x52", 6, { 0 }}, {"\x53", 6, { 0 }}, {"\x54", 6, { 0 }}, {"\x55", 6, { 0 }},
{"\x56", 6, { 0 }}, {"\x57", 6, { 0 }}, {"\x58", 6, { 0 }}, {"\x59", 6, { 0 }}, {"\x5A", 6, { 0 }}, {"\x5B", 6, { 0 }},
{"\x5C", 6, { 0 }}, {"\x5D", 6, { 0 }}, {"\x5E", 6, { 0 }}, {"\x5F", 6, { 0 }}, {"\x60", 6, { 0 }}, {"\x61", 6, { 0 }},
{"\x62", 6, { 0 }}, {"\x63", 6, { 0 }}, {"\x64", 6, { 0 }}, {"\x65", 6, { 0 }}, {"\x66", 6, { 0 }}, {"\x67", 6, { 0 }},
{"\x68", 6, { 0 }}, {"\x69", 6, { 0 }}, {"\x6A", 6, { 0 }}, {"\x6B", 6, { 0 }}, {"\x6C", 6, { 0 }}, {"\x6D", 6, { 0 }},
{"\x6E", 6, { 0 }}, {"\x6F", 6, { 0 }}, {"\x70", 6, { 0 }}, {"\x71", 6, { 0 }}, {"\x72", 6, { 0 }}, {"\x73", 6, { 0 }},
{"\x74", 6, { 0 }}, {"\x75", 6, { 0 }}, {"\x76", 6, { 0 }}, {"\x77", 6, { 0 }}, {"\x78", 6, { 0 }}, {"\x79", 6, { 0 }},
{"\x7A", 6, { 0 }}, {"\x7B", 6, { 0 }}, {"\x7C", 6, { 0 }}, {"\x7D", 6, { 0 }}, {"\x7E", 6, { 0 }}, {"\x7F", 6, { 0 }},
/* Block graphics without shift */
{"\x80", 6, { 0 }}, {"\x81", 6, { 0 }}, {"\x82", 6, { 0 }}, {"\x83", 6, { 0 }}, {"\x84", 6, { 0 }}, {"\x85", 6, { 0 }},
{"\x86", 6, { 0 }}, {"\x87", 6, { 0 }},
/* Block graphics with shift */
{"\x88", 6, { 0 }}, {"\x89", 6, { 0 }}, {"\x8A", 6, { 0 }}, {"\x8B", 6, { 0 }}, {"\x8C", 6, { 0 }}, {"\x8D", 6, { 0 }},
{"\x8E", 6, { 0 }}, {"\x8F", 6, { 0 }},
/* UDGs */
{"\x90", 6, { 0 }}, {"\x91", 6, { 0 }}, {"\x92", 6, { 0 }}, {"\x93", 6, { 0 }}, {"\x94", 6, { 0 }}, {"\x95", 6, { 0 }},
{"\x96", 6, { 0 }}, {"\x97", 6, { 0 }}, {"\x98", 6, { 0 }}, {"\x99", 6, { 0 }}, {"\x9A", 6, { 0 }}, {"\x9B", 6, { 0 }},
{"\x9C", 6, { 0 }}, {"\x9D", 6, { 0 }}, {"\x9E", 6, { 0 }}, {"\x9F", 6, { 0 }}, {"\xA0", 6, { 0 }}, {"\xA1", 6, { 0 }},
{"\xA2", 6, { 0 }},
{"SPECTRUM", 1, { 0 }}, /* For Spectrum 128 */
{"PLAY", 1, { 12 }},
/* BASIC tokens - expression */
{"RND", 3, { 0 }},
{"INKEY$", 4, { 0 }},
{"PI", 3, { 0 }},
{"FN", 3, { 1, '(', 13, ')', 0 }},
{"POINT", 3, { '(', 8, ')', 0 }},
{"SCREEN$", 4, { '(', 8, ')', 0 }},
{"ATTR", 3, { '(', 8, ')', 0 }},
{"AT", 5, { 8, 0 }},
{"TAB", 5, { 6, 0 }},
{"VAL$", 4, { 10, 0 }},
{"CODE", 3, { 10, 0 }},
{"VAL", 3, { 10, 0 }},
{"LEN", 3, { 10, 0 }},
{"SIN", 3, { 6, 0 }},
{"COS", 3, { 6, 0 }},
{"TAN", 3, { 6, 0 }},
{"ASN", 3, { 6, 0 }},
{"ACS", 3, { 6, 0 }},
{"ATN", 3, { 6, 0 }},
{"LN", 3, { 6, 0 }},
{"EXP", 3, { 6, 0 }},
{"INT", 3, { 6, 0 }},
{"SQR", 3, { 6, 0 }},
{"SGN", 3, { 6, 0 }},
{"ABS", 3, { 6, 0 }},
{"PEEK", 3, { 6, 0 }},
{"IN", 3, { 6, 0 }},
{"USR", 3, { 6, 0 }},
{"STR$", 4, { 6, 0 }},
{"CHR$", 4, { 6, 0 }},
{"NOT", 3, { 6, 0 }},
{"BIN", 6, { 0 }},
{"OR", 6, { 5, 0 }}, /* -\ */
{"AND", 6, { 5, 0 }}, /* | */
{"<=", 6, { 5, 0 }}, /* | These are handled directly within ScanExpression */
{">=", 6, { 5, 0 }}, /* | */
{"<>", 6, { 5, 0 }}, /* -/ */
{"LINE", 6, { 0 }},
{"THEN", 6, { 0 }},
{"TO", 6, { 0 }},
{"STEP", 6, { 0 }},
/* BASIC tokens - keywords */
{"DEF FN", 1, { 15, 0 }}, /* Special treatment - insertion of call-by-value room required for the evaluator */
{"CAT", 1, { 11, 0 }},
{"FORMAT", 1, { 11, 0 }},
{"MOVE", 1, { 11, 0 }},
{"ERASE", 1, { 11, 0 }},
{"OPEN #", 1, { 11, 0 }},
{"CLOSE #", 1, { 11, 0 }},
{"MERGE", 1, { 11, 0 }},
{"VERIFY", 1, { 11, 0 }},
{"BEEP", 1, { 8, 0 }},
{"CIRCLE", 1, { 9, ',', 6, 0 }},
{"INK", 2, { 7, 0 }},
{"PAPER", 2, { 7, 0 }},
{"FLASH", 2, { 7, 0 }},
{"BRIGHT", 2, { 7, 0 }},
{"INVERSE", 2, { 7, 0 }},
{"OVER", 2, { 7, 0 }},
{"OUT", 1, { 8, 0 }},
{"LPRINT", 1, { 5, 0 }},
{"LLIST", 1, { 3, 0 }},
{"STOP", 1, { 0 }},
{"READ", 1, { 14, 0 }},
{"DATA", 2, { 13, 0 }},
{"RESTORE", 1, { 3, 0 }},
{"NEW", 1, { 0 }},
{"BORDER", 1, { 6, 0 }},
{"CONTINUE", 1, { 0 }},
{"DIM", 1, { 1, '(', 13, ')', 0 }},
{"REM", 1, { 5, 0 }}, /* (Special: taken out separately) */
{"FOR", 1, { 4, '=', 6, 0xCC, 6, 0xCD, 6, 0 }}, /* (Special: STEP (0xCD) is not required) */
{"GO TO", 1, { 6, 0 }},
{"GO SUB", 1, { 6, 0 }},
{"INPUT", 1, { 5, 0 }},
{"LOAD", 1, { 11, 0 }},
{"LIST", 1, { 3, 0 }},
{"LET", 1, { 1, '=', 2, 0 }},
{"PAUSE", 1, { 6, 0 }},
{"NEXT", 1, { 4, 0 }},
{"POKE", 1, { 8, 0 }},
{"PRINT", 1, { 5, 0 }},
{"PLOT", 1, { 9, 0 }},
{"RUN", 1, { 3, 0 }},
{"SAVE", 1, { 11, 0 }},
{"RANDOMIZE", 1, { 3, 0 }},
{"IF", 1, { 6, 0xCB, 0 }},
{"CLS", 1, { 0 }},
{"DRAW", 1, { 9, ',', 6, 0 }},
{"CLEAR", 1, { 3, 0 }},
{"RETURN", 1, { 0 }},
{"COPY", 1, { 0 }}};
#define MAXLINELENGTH 1024
char ConvertedSpectrumLine[MAXLINELENGTH + 1];
byte ResultingLine[MAXLINELENGTH + 1];
struct TapeHeader_s
{
byte LenLo1;
byte LenHi1;
byte Flag1;
byte HType;
char HName[10];
byte HLenLo;
byte HLenHi;
byte HStartLo;
byte HStartHi;
byte HBasLenLo;
byte HBasLenHi;
byte Parity1;
byte LenLo2;
byte LenHi2;
byte Flag2;
} TapeHeader = {19, 0, /* Len header */
0, /* Flag header */
0, {32, 32, 32, 32, 32, 32, 32, 32, 32, 32}, 0, 0, 0, 128, 0, 0, /* The header itself */
0, /* Parity header */
0, 0, /* Len converted BASIC */
255}; /* Flag converted BASIC */
int Is48KProgram = -1; /* -1 = unknown */
/* 1 = 48K */
/* 0 = 128K */
int UsesInterface1 = -1; /* -1 = unknown */
/* 0 = either Interface1 or Opus Discovery */
/* 1 = Interface1 */
/* 2 = Opus Discovery */
bool CaseIndependant = FALSE;
bool Quiet = FALSE; /* Suppress banner and progress indication if TRUE */
bool NoWarnings = FALSE; /* Suppress warnings if TRUE */
bool DoCheckSyntax = TRUE;
bool TokenBracket = FALSE;
bool HandlingDEFFN = FALSE; /* Exceptional instruction */
bool InsideDEFFN = FALSE;
#define DEFFN 0xCE
FILE *ErrStream;
/**********************************************************************************************************************************/
/* Let's be lazy and define a very commonly used error message.... */
/**********************************************************************************************************************************/
#define BADTOKEN(_Exp,_Got) fprintf (ErrStream, "ERROR in line %d, statement %d - Expected %s, but got \"%s\"\n", \
BasicLineNo, StatementNo, _Exp, _Got)
/**********************************************************************************************************************************/
/* And let's generate tons of debugging info too.... */
/**********************************************************************************************************************************/
#ifdef __DEBUG__
char ListSpaces[20];
int RecurseLevel;
#endif
/**********************************************************************************************************************************/
/* Prototype all functions */
/**********************************************************************************************************************************/
int GetLineNumber (char **FirstAfter);
int MatchToken (int BasicLineNo, bool WantKeyword, char **LineIndex, byte *Token);
int HandleNumbers (int BasicLineNo, char **BasicLine, byte **SpectrumLine);
int HandleBIN (int BasicLineNo, char **BasicLine, byte **SpectrumLine);
int ExpandSequences (int BasicLineNo, char **BasicLine, byte **SpectrumLine, bool StripSpaces);
int PrepareLine (char *LineIn, int FileLineNo, char **FirstToken);
bool ScanVariable (int BasicLineNo, int StatementNo, int Keyword, byte **Index, bool *Type, int *NameLen, int AllowSlicing);
bool SliceDirectString (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool ScanStream (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool ScanChannel (int BasicLineNo, int StatementNo, int Keyword, byte **Index, byte *WhichChannel);
bool SignalInterface1 (int BasicLineNo, int StatementNo, int NewMode);
bool CheckEnd (int BasicLineNo, int StatementNo, byte **Index);
bool ScanExpression (int BasicLineNo, int StatementNo, int Keyword, byte **Index, bool *Type, int Level);
bool HandleClass01 (int BasicLineNo, int StatementNo, int Keyword, byte **Index, bool *Type);
bool HandleClass02 (int BasicLineNo, int StatementNo, int Keyword, byte **Index, bool Type);
bool HandleClass03 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass04 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass05 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass06 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass07 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass08 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass09 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass10 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass11 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass12 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass13 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass14 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool HandleClass15 (int BasicLineNo, int StatementNo, int Keyword, byte **Index);
bool CheckSyntax (int BasicLineNo, byte *Line);
/**********************************************************************************************************************************/
/* Start of the program */
/**********************************************************************************************************************************/
int GetLineNumber (char **FirstAfter)
/**********************************************************************************************************************************/
/* Pre : The line must have been prepared into (global) `ConvertedSpectrumLine'. */
/* Post : The BASIC line number has been returned, or -1 if there was none. */
/* Import: None. */
/**********************************************************************************************************************************/
{
int LineNo = 0;
char *LineIndex;
bool SkipSpaces = TRUE;
bool Continue = TRUE;
LineIndex = ConvertedSpectrumLine;
while (*LineIndex && Continue)
if (*LineIndex == ' ') /* Skip leading spaces */
{
if (SkipSpaces)
LineIndex ++;
else
Continue = FALSE;
}
else if (isdigit (*LineIndex)) /* Process number */
{
LineNo = LineNo * 10 + *(LineIndex ++) - '0';
SkipSpaces = FALSE;
}
else
Continue = FALSE;
*FirstAfter = LineIndex;
if (SkipSpaces) /* Nothing found yet ? */
return (-1);
else
while ((**FirstAfter) == ' ') /* Skip trailing spaces */
(*FirstAfter) ++;
return (LineNo);
}
int MatchToken (int BasicLineNo, bool WantKeyword, char **LineIndex, byte *Token)
/**********************************************************************************************************************************/
/* Pre : `WantKeyword' is TRUE if we need in keyword match, `LineIndex' holds the position to match. */
/* Post : If there was a match, the token value is returned in `Token' and `LineIndex' is pointing after the string plus any */
/* any trailing space. */
/* The return value is 0 for no match, -2 for an error, -1 for a match of the wrong type, 1 for a good match. */
/* Import: None. */
/**********************************************************************************************************************************/
{
int Cnt;
size_t Length;
size_t LongestMatch = 0;
bool Match = FALSE;
bool Match2;
if ((**LineIndex) == ':') /* Special exception */
{
LongestMatch = 1;
Match = TRUE;
*Token = ':';
}
else for (Cnt = 0xA3 ; Cnt <= 0xFF ; Cnt ++) /* (Keywords start after the UDGs) */
{
Length = strlen (TokenMap[Cnt].Token);
if (CaseIndependant)
Match2 = !x_strnicmp (*LineIndex, TokenMap[Cnt].Token, Length);
else
Match2 = !strncmp (*LineIndex, TokenMap[Cnt].Token, Length);
if (Match2)
if (Length > LongestMatch)
{
LongestMatch = Length;
Match = TRUE;
*Token = Cnt;
}
}
if (!Match)
return (0); /* Signal: no match */
if (isalpha (*(*LineIndex + LongestMatch - 1)) && isalpha (*(*LineIndex + LongestMatch))) /* Continueing alpha string ? */
return (0); /* Then there's no match after all! (eg. 'INT' must not match 'INTER') */
*LineIndex += LongestMatch; /* Go past the token */
while ((**LineIndex) == ' ') /* Skip trailing spaces */
(*LineIndex) ++;
if (*Token == 0xA3 || *Token == 0xA4) /* 'SPECTRUM' or 'PLAY' ? */
switch (Is48KProgram) /* Then the program must be 128K */
{
case -1 : Is48KProgram = 0; break; /* Set the flag */
case 1 : fprintf (ErrStream, "ERROR - Line %d contains a 128K keyword, but the program\n"
"also uses UDGs \'T\' and/or \'U\'\n", BasicLineNo);
return (-2);
case 0 : break;
}
if ((WantKeyword && TokenMap[*Token].TokenType == 0) || /* Wanted keyword but got something else */
(!WantKeyword && TokenMap[*Token].TokenType == 1)) /* Did not want a keyword but got one nonetheless */
return (-1); /* Signal: match, but of wrong type */
else
return (1); /* Signal: match! */
}
int HandleNumbers (int BasicLineNo, char **BasicLine, byte **SpectrumLine)
/**********************************************************************************************************************************/
/* Pre : `BasicLineNo' holds the current BASIC line number, `BasicLine' points into the line, `SpectrumLine' points to the */
/* TAPped Spectrum line. */
/* Post : If there was a (floating point) number at this position, it has been processed into `SpectrumLine' and `LineIndex' is */
/* pointing after the number. */
/* The return value is: 0 = no number, 1 = number done, -1 = number error (already reported). */
/* Import: None. */
/**********************************************************************************************************************************/
{
#define SHIFT31BITS (double)2147483648.0 /* (= 2^31) */
char *StartOfNumber;
double Value = 0.0;
double Divider = 1.0;
double Exp = 0.0;
int IntValue;
byte Sign = 0x00;
unsigned long Mantissa;
if (!isdigit (**BasicLine) && /* Current character is not a digit ? */
(**BasicLine) != '.') /* And not a decimal point (eg. '.5') ? */
return (0); /* Then it can hardly be a number */
StartOfNumber = *BasicLine;
while (isdigit (**BasicLine)) /* First read the integer part */
Value = Value * 10 + *((*BasicLine) ++) - '0';
if ((**BasicLine) == '.') /* Decimal point ? */
{ /* Read the decimal part */
(*BasicLine) ++;
while (isdigit (**BasicLine))
Value = Value + (Divider /= 10) * (*((*BasicLine) ++) - '0');
}
if ((**BasicLine) == 'e' || (**BasicLine) == 'E') /* Exponent ? */
{
(*BasicLine) ++;
if ((**BasicLine) == '+') /* Both "Ex" and "E+x" do the same thing */
(*BasicLine) ++;
else if ((**BasicLine) == '-') /* Negative exponent */
{
Sign = 0xFF;
(*BasicLine) ++;
}
while (isdigit (**BasicLine)) /* Read the exponent value */
Exp = Exp * 10 + *((*BasicLine) ++) - '0';
if (Sign == 0x00) /* Raise the resulting value to the read exponent */
Value = Value * pow (10.0, Exp);
else
Value = Value / pow (10.0, Exp);
}
strncpy ((char *)*SpectrumLine, StartOfNumber, *BasicLine - StartOfNumber); /* Insert the ASCII value first */
(*SpectrumLine) += (*BasicLine - StartOfNumber);
IntValue = (int)floor (Value);
if (Value == IntValue && Value >= -65536 && Value < 65536) /* Small integer ? */
{
*((*SpectrumLine) ++) = 0x0E; /* Insert number marker */
*((*SpectrumLine) ++) = 0x00;
if (IntValue >= 0) /* Insert sign */
*((*SpectrumLine) ++) = 0x00;
else
{
*((*SpectrumLine) ++) = 0xFF;
IntValue += 65536; /* Maintain bug in Spectrum ROM - INT(-65536) will result in -1 */
}
*((*SpectrumLine) ++) = (byte)(IntValue & 0xFF);
*((*SpectrumLine) ++) = (byte)(IntValue >> 8);
*((*SpectrumLine) ++) = 0x00;
}
else /* Need to store in full floating point format */
{
if (Value < 0)
{
Sign = 0x80; /* Sign bit is high bit of byte 2 */
Value = -Value;
}
else
Sign = 0x00;
Exp = floor (x_log2 (Value));
if (Exp < -129 || Exp > 126)
{
fprintf (ErrStream, "ERROR - Number too big in line %d\n", BasicLineNo);
return (-1);
}
Mantissa = (unsigned long)floor ((Value / pow (2.0, Exp) - 1.0) * SHIFT31BITS + 0.5); /* Calculate mantissa */
*((*SpectrumLine) ++) = 0x0E; /* Insert number marker */
*((*SpectrumLine) ++) = (byte)Exp + 0x81; /* Insert exponent */
*((*SpectrumLine) ++) = (byte)((Mantissa >> 24) & 0x7F) | Sign; /* Insert mantissa */
*((*SpectrumLine) ++) = (byte)((Mantissa >> 16) & 0xFF); /* (Big endian!) */
*((*SpectrumLine) ++) = (byte)((Mantissa >> 8) & 0xFF);
*((*SpectrumLine) ++) = (byte)(Mantissa & 0xFF);
}
return (1);
}
int HandleBIN (int BasicLineNo, char **BasicLine, byte **SpectrumLine)
/**********************************************************************************************************************************/
/* Pre : `BasicLineNo' holds the current BASIC line number, `BasicLine' points into the line just past the BIN token, */
/* `SpectrumLine' points to the TAPped Spectrum line. */
/* Post : If there was a BINary number at this position, it has been processed into `SpectrumLine' and `LineIndex' is pointing */
/* after the number. */
/* The return value is: 1 = number done, -1 = number error (already reported). */
/* Import: None. */
/**********************************************************************************************************************************/
{
int Value = 0;
while ((**BasicLine) == '0' || (**BasicLine) == '1') /* Read only binary digits */
{
Value = Value * 2 + **BasicLine - '0';
if (Value > 65535)
{
fprintf (ErrStream, "ERROR - Number too big in line %d\n", BasicLineNo);
return (-1);
}
*((*SpectrumLine) ++) = *((*BasicLine) ++); /* (Copy digit across) */
}
*((*SpectrumLine) ++) = 0x0E; /* Insert number marker */
*((*SpectrumLine) ++) = 0x00; /* (A small integer by definition) */
*((*SpectrumLine) ++) = 0x00;
*((*SpectrumLine) ++) = (byte)(Value & 0xFF);
*((*SpectrumLine) ++) = (byte)(Value >> 8);
*((*SpectrumLine) ++) = 0x00;
return (1);
}
int ExpandSequences (int BasicLineNo, char **BasicLine, byte **SpectrumLine, bool StripSpaces)
/**********************************************************************************************************************************/
/* Pre : `BasicLineNo' holds the current BASIC line number, `BasicLine' points into the line, `SpectrumLine' points to the */
/* TAPped Spectrum line. */
/* Post : If there was an expandable '{...}' sequence at this position, it has been processed into `SpectrumLine', `LineIndex' */
/* is pointing after the sequence. Returned is -1 for error, 0 for no expansion, 1 for expansion. */
/* Import: None. */
/**********************************************************************************************************************************/
{
char *StartOfSequence;
byte Attribute = 0;
byte AttributeLength = 0;
byte AttributeVal1 = 0;
byte AttributeVal2 = 0;
byte OldCharacter;
int Cnt;
if (**BasicLine != '{')
return (0);
StartOfSequence = (*BasicLine) + 1;
/* 'CODE' and 'CAT' were added for the sole purpuse of allowing them to be OPEN #'ed as channels! */
if (!x_strnicmp (StartOfSequence, "CODE}", 5)) /* Special: 'CODE' */
{
*((*SpectrumLine) ++) = 0xAF;
(*BasicLine) += 6;
return (1);
}
if (!x_strnicmp (StartOfSequence, "CAT}", 4)) /* Special: 'CAT' */
{
*((*SpectrumLine) ++) = 0xCF;
(*BasicLine) += 5;
return (1);
}
if (!x_strnicmp (StartOfSequence, "(C)}", 4))
{ /* Form "{(C)}" -> copyright sign */
*((*SpectrumLine) ++) = 0x7F;
(*BasicLine) += 5;
if (StripSpaces)
while ((**BasicLine) == ' ') /* Skip trailing spaces */
(*BasicLine) ++;
return (1);
}
if (*StartOfSequence == '+' && *(StartOfSequence + 1) >= '1' && *(StartOfSequence + 1) <= '8' && *(StartOfSequence + 2) == '}')
{ /* Form "{+X}" -> block graphics with shift */
*((*SpectrumLine) ++) = 0x88 + (((*(StartOfSequence + 1) - '0') % 8) ^ 7);
(*BasicLine) += 4;
if (StripSpaces)
while ((**BasicLine) == ' ')
(*BasicLine) ++;
return (1);
}
if (*StartOfSequence == '-' && *(StartOfSequence + 1) >= '1' && *(StartOfSequence + 1) <= '8' && *(StartOfSequence + 2) == '}')
{ /* Form "{-X}" -> block graphics without shift */
*((*SpectrumLine) ++) = 0x80 + (*(StartOfSequence + 1) - '0') % 8;
(*BasicLine) += 4;
if (StripSpaces)
while ((**BasicLine) == ' ')
(*BasicLine) ++;
return (1);
}
if (toupper (*StartOfSequence) >= 'A' && toupper (*StartOfSequence) <= 'U' && *(StartOfSequence + 1) == '}')
{ /* Form "{X}" -> UDG */
if (toupper (*StartOfSequence) == 'T' || toupper (*StartOfSequence) == 'U') /* 'T' or 'U' ? */
switch (Is48KProgram) /* Then the program must be 48K */
{
case -1 : Is48KProgram = 1; break; /* Set the flag */
case 0 : fprintf (ErrStream, "ERROR - Line %d contains UDGs \'T\' and/or \'U\'\n"
"but the program was already marked 128K\n", BasicLineNo);
return (-1);
case 1 : break;
}
*((*SpectrumLine) ++) = 0x90 + toupper (*StartOfSequence) - 'A';
(*BasicLine) += 3;
if (StripSpaces)
while ((**BasicLine) == ' ')
(*BasicLine) ++;
return (1);
}
if (isxdigit (*StartOfSequence) && isxdigit (*(StartOfSequence + 1)) && *(StartOfSequence + 2) == '}')
{ /* Form "{XX}" -> below 32 */
if (*StartOfSequence <= '9')
(**SpectrumLine) = *StartOfSequence - '0';
else
(**SpectrumLine) = toupper (*StartOfSequence) - 'A' + 10;
if (*(StartOfSequence + 1) <= '9')
(**SpectrumLine) = (**SpectrumLine) * 16 + *(StartOfSequence + 1) - '0';
else
(**SpectrumLine) = (**SpectrumLine) * 16 + toupper (*(StartOfSequence + 1)) - 'A' + 10;
(*SpectrumLine) ++;
(*BasicLine) += 4;
if (StripSpaces)
while ((**BasicLine) == ' ')
(*BasicLine) ++;
return (1);
}
if (!x_strnicmp (StartOfSequence, "INK", 3))
{
Attribute = 0x10;
AttributeLength = 3;
}
else if (!x_strnicmp (StartOfSequence, "PAPER", 5))
{
Attribute = 0x11;
AttributeLength = 5;
}
else if (!x_strnicmp (StartOfSequence, "FLASH", 5))
{
Attribute = 0x12;
AttributeLength = 5;
}
else if (!x_strnicmp (StartOfSequence, "BRIGHT", 6))
{
Attribute = 0x13;
AttributeLength = 6;
}
else if (!x_strnicmp (StartOfSequence, "INVERSE", 7))
{
Attribute = 0x14;
AttributeLength = 7;
}
else if (!x_strnicmp (StartOfSequence, "OVER", 4))
{
Attribute = 0x15;
AttributeLength = 4;
}
else if (!x_strnicmp (StartOfSequence, "AT", 2))
{
Attribute = 0x16;
AttributeLength = 2;
}
else if (!x_strnicmp (StartOfSequence, "TAB", 3))
{
Attribute = 0x17;
AttributeLength = 3;
}
if (Attribute > 0)
{
StartOfSequence += AttributeLength;
while (*StartOfSequence == ' ')
StartOfSequence ++;
while (isdigit (*StartOfSequence))
AttributeVal1 = AttributeVal1 * 10 + *(StartOfSequence ++) - '0';
if (Attribute == 0x16 || Attribute == 0x17)
{
if (*StartOfSequence != ',')
Attribute = 0;
else
{
StartOfSequence ++; /* (Step past the comma) */
while (*StartOfSequence == ' ')
StartOfSequence ++;
while (isdigit (*StartOfSequence))
AttributeVal2 = AttributeVal2 * 10 + *(StartOfSequence ++) - '0';
}
}
if (*StartOfSequence != '}') /* Need closing bracket */
Attribute = 0;
if (Attribute > 0)
{
*((*SpectrumLine) ++) = Attribute;
*((*SpectrumLine) ++) = AttributeVal1;
if (Attribute == 0x16 || Attribute == 0x17)
*((*SpectrumLine) ++) = AttributeVal2;
(*BasicLine) = StartOfSequence + 1;
if (StripSpaces)
while ((**BasicLine) == ' ')
(*BasicLine) ++;
return (1);
}
}
if (!NoWarnings)
{
for (Cnt = 0 ; *((*BasicLine) + Cnt) && *((*BasicLine) + Cnt) != '}' ; Cnt ++)
;
if (*((*BasicLine) + Cnt) == '}')
{
OldCharacter = *((*BasicLine) + Cnt + 1);
*((*BasicLine) + Cnt + 1) = '\0';
printf ("WARNING - Unexpandable sequence \"%s\" in line %d\n", (*BasicLine), BasicLineNo);
*((*BasicLine) + Cnt + 1) = OldCharacter;
return (0);
}
}
return (0);
}
int PrepareLine (char *LineIn, int FileLineNo, char **FirstToken)
/**********************************************************************************************************************************/
/* Pre : `LineIn' points to the read line, `FileLineNo' holds the real line number. */
/* Post : Multiple spaces have been removed (unless within a string), the BASIC line number has been found and `FirstToken' is */
/* pointing at the first non-whitespace character after the line number. */
/* Bad characters are reported, as well as any other error. The return value is the BASIC line number, -1 if error, or */
/* -2 if the (empty!) line should be skipped. */
/* Import: GetLineNumber. */
/**********************************************************************************************************************************/
{
char *IndexIn;
char *IndexOut;
bool InString = FALSE;
bool SingleSeparator = FALSE;
bool StillOk = TRUE;
bool DoingREM = FALSE;
int BasicLineNo = -1;
static int PreviousBasicLineNo = -1;
IndexIn = LineIn;
IndexOut = ConvertedSpectrumLine;
while (*IndexIn && StillOk)
{
if (*IndexIn == '\t') /* EXCEPTION: Print ' */
{
*(IndexOut ++) = 0x06;
IndexIn ++;
}
else if (*IndexIn < 32 || *IndexIn >= 127) /* (Exclude copyright sign as well) */
StillOk = FALSE;
else
{
if (!DoingREM)
if (!x_strnicmp (IndexIn, " REM ", 5) || /* Going through REM statement ? */
!x_strnicmp (IndexIn, ":REM ", 5))
DoingREM = TRUE; /* Signal: copy anything and everything ASCII */
if (InString || DoingREM)
*(IndexOut ++) = *IndexIn;
else
{
if (*IndexIn == ' ')
{
if (!SingleSeparator) /* Remove multiple spaces */
{
SingleSeparator = TRUE;
*(IndexOut ++) = *IndexIn;
}
}
else
{
SingleSeparator = FALSE;
*(IndexOut ++) = *IndexIn;
}
}
if (*IndexIn == '\"' && !DoingREM)
InString = !InString;
IndexIn ++;
}
}
*IndexOut = '\0';
if (!StillOk)
if (*IndexIn == 0x0D || *IndexIn == 0x0A) /* 'Correct' for end-of-line */
StillOk = TRUE; /* (Accept CR and/or LF as end-of-line) */
BasicLineNo = GetLineNumber (FirstToken);
if (InString)
fprintf (ErrStream, "ERROR - %s line %d misses terminating quote\n",
BasicLineNo < 0 ? "ASCII" : "BASIC", BasicLineNo < 0 ? FileLineNo : BasicLineNo);
else if (!StillOk)
fprintf (ErrStream, "ERROR - %s line %d contains a bad character (code %02Xh)\n",
BasicLineNo < 0 ? "ASCII" : "BASIC", BasicLineNo < 0 ? FileLineNo : BasicLineNo, *IndexIn);
else if (BasicLineNo < 0) /* Could not read line number */
{
if (!(**FirstToken)) /* Line is completely empty ? */
{
if (!NoWarnings)
printf ("WARNING - Skipping empty ASCII line %d\n", FileLineNo);
return (-2); /* Signal: skip entire line */
}
else
{
fprintf (ErrStream, "ERROR - Missing line number in ASCII line %d\n", FileLineNo);
StillOk = FALSE;
}
}
else if (PreviousBasicLineNo >= 0) /* Not the first line ? */
{
if (BasicLineNo < PreviousBasicLineNo) /* This line number smaller than previous ? */
{
fprintf (ErrStream, "ERROR - Line number %d is smaller than previous line number %d\n", BasicLineNo, PreviousBasicLineNo);
StillOk = FALSE;
}
else if (BasicLineNo == PreviousBasicLineNo && !NoWarnings) /* Same line number as previous ? */
printf ("WARNING - Duplicate use of line number %d\n", BasicLineNo); /* (BASIC can handle it after all...) */
}
else if (!(**FirstToken)) /* Line contains only a line number ? */
{
fprintf (ErrStream, "ERROR - Line %d contains no statements!\n", BasicLineNo);
StillOk = FALSE;
}
PreviousBasicLineNo = BasicLineNo; /* Remember this line number */
if (!InString && StillOk)
return (BasicLineNo);
else
return (-1);
}
bool CheckEnd (int BasicLineNo, int StatementNo, byte **Index)
/**********************************************************************************************************************************/
/* Pre : `BasicLineNo' holds the line number, `StatementNo' the statement number, `Index' the current position in the line. */
/* Post : A check is made whether the end of the current statement has been reached. */
/* If so, an error is reported and TRUE is returned (so FALSE indicates that everything is still fine and dandy). */
/* Import: none. */
/**********************************************************************************************************************************/
{
if (**Index == ':' || **Index == 0x0D) /* End of statement or end of line ? */
{
fprintf (ErrStream, "ERROR in line %d, statement %d - Unexpected end of statement\n", BasicLineNo, StatementNo);
return (TRUE);
}
return (FALSE);
}
bool ScanVariable (int BasicLineNo, int StatementNo, int Keyword, byte **Index, bool *Type, int *NameLen, int AllowSlicing)
/**********************************************************************************************************************************/
/* Pre : `BasicLineNo' holds the line number, `StatementNo' the statement number, `Keyword' the keyword to which this operand */
/* belongs, `Index' the current position in the line. */
/* `AllocSlicing' is one of the following values: */
/* -1 = Don't check for slicing/indexing (used by DEF FN) */
/* 0 = No slicing/indexing allowed */
/* 1 = Either slicing or indexing may follow (indices being numeric) */
/* 2 = Only numeric indexing may follow (used by LET and READ) */
/* Post : A check has been made whether there's a variable at the current position. If so, it has been skipped. */
/* Slicing is handled here as well, but notice that this is not necessarily correct! */
/* Single letter string variables can be either flat or array and both possibilities are considered here. */
/* Both "a$(1 TO 10)" and "a$(1, 2)" are correct to BASIC, but depend on whether a "DIM" statement was used. */
/* The length of the found string (without any '$') is returned in `NameLen', its type is returned in `Type' (TRUE for */
/* numeric and FALSE for string variables). The return value is TRUE is all went well. Errors have already been reported. */
/* The return value is FALSE either when no variable is at this point or an error was found. */
/* `NameLen' is returned 0 if no variable was detected here, or > 0 if in error. */
/* Import: ScanExpression. */
/**********************************************************************************************************************************/
{
bool SubType;
bool IsArray = FALSE;
bool SetTokenBracket = FALSE;
Keyword = Keyword; /* (Keep compilers happy) */
*Type = TRUE; /* Assume it will be numeric */
*NameLen = 0;
if (!isalpha (**Index)) /* The first character must be alphabetic for a variable */
return (FALSE);
*NameLen = 1;
while (isalnum (*(++ (*Index)))) /* Read on, until end of the word */
(*NameLen) ++;
if (**Index == '$') /* It's a string variable ? */
{
if (*NameLen > 1) /* String variables can only have a single character name */
{
fprintf (ErrStream, "ERROR in line %d, statement %d - String variables can only have single character names\n",
BasicLineNo, StatementNo);
return (FALSE);
}
(*Index) ++;
*Type = FALSE;
}
#ifdef __DEBUG__
printf ("DEBUG - %sScanVariable, Type is %s\n", ListSpaces, *Type ? "NUM" : "ALPHA");
#endif
if (AllowSlicing >= 0 && **Index == '(') /* Slice the string ? */
{
#ifdef __DEBUG__
printf ("DEBUG - %sScanVariable, reading index\n", ListSpaces);
#endif
if (*NameLen > 1) /* Arrays can only have a single character name */
{
fprintf (ErrStream, "ERROR in line %d, statement %d - Arrays can only have single character names\n",
BasicLineNo, StatementNo);
return (FALSE);
}
if (AllowSlicing == 0) /* Slicing/Indexing not allowed ? */
{
fprintf (ErrStream, "ERROR in line %d, statement %d - Slicing/Indexing not allowed\n", BasicLineNo, StatementNo);
return (FALSE);
}
(*Index) ++; /* (Skip the bracket) */
if (**Index == ')') /* Empty slice "a$()" is not ok */
{
fprintf (ErrStream, "ERROR in line %d, statement %d - Empty array index not allowed\n", BasicLineNo, StatementNo);
return (FALSE);
}
if (**Index == 0xCC) /* "a$( TO num)" or "a$( TO )" */
{
if (AllowSlicing == 2)
{
fprintf (ErrStream, "ERROR in line %d, statement %d - Slicing token \"TO\" inappropriate for arrays\n",
BasicLineNo, StatementNo);
return (FALSE);
}
}
else /* Not "a$( TO num)" nor "a$( TO )" */
{
if (!TokenBracket)
{
TokenBracket = TRUE; /* Allow complex expression */
SetTokenBracket = TRUE;
}
if (!ScanExpression (BasicLineNo, StatementNo, '(', Index, &SubType, 0)) /* First parameter */
return (FALSE);
if (SetTokenBracket)
TokenBracket = FALSE;
if (!SubType) /* Must be numeric */
{
fprintf (ErrStream, "ERROR in line %d, statement %d - Variables indices must be numeric\n", BasicLineNo, StatementNo);
return (FALSE);
}
if (**Index == ')') /* "a$(num)" is ok */
{
(*Index) ++;
#ifdef __DEBUG__
printf ("DEBUG - %sScanVariable, index ending, next char is \"%s\"\n", ListSpaces, TokenMap[**Index].Token);
#endif
return (TRUE);
}
}
if (**Index != 0xCC && **Index != ',') /* Either an array or a slice */
{