forked from ref-xx/basinc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASSupport.pas
More file actions
1953 lines (1793 loc) · 73.4 KB
/
BASSupport.pas
File metadata and controls
1953 lines (1793 loc) · 73.4 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
unit BASSupport;
interface
Uses Windows, Math, SysUtils, Classes;
Procedure DecodeBAS;
Function ProcessBASLine(CurLine: String): String;
Procedure ParseBASLabels;
Procedure ProcessDEFFN(var CurBASICLine: String);
Function FindDEFFN(Line: String): Integer;
Procedure GetNextBASLine(var CurPos: Integer; var ResultLine: String);
Function GetVarDimensions(Var CurLine: String; Var LinePos, NumElements: Integer; ElementSize: Byte): String;
Function GetElementsNum(Var CurLine: String; Var LinePos, NumElements: Integer): String;
Function GetElementsStr(Var CurLine: String; Var LinePos: Integer): String;
Function GetString(var CurLine: String; Var LinePos: Integer): String;
Function GetSign(CurLine: String; var LinePos: Integer; var CurBASICLine: String): Boolean;
Function GetNumber(CurLine: String; var LinePos: Integer; var CurBASICLine: String; IsBinary: Boolean): Extended;
Function FloatTo5Byte(Value: Extended): String;
Function Byte5ToFloat(Exponent: Byte; Mantissa: DWord): Extended;
Procedure ProcessEscapeChars(Var Line: String; var LPos: Integer; var BASICLine: String);
Function InsertEscapes(Line: String): String;
Function GetColourCode(Var Line: String; Var LinePos: Integer): String;
Function GetToken(Keyword: String): Byte;
Function FormatEscapes(CurLine: String): String;
Function Strip5Bytes(Text: String): String;
Function Insert5Bytes(Text: String): String;
Function StripWhiteSpace(Text: String): String;
Function GetDelimitedString(Text: String; Var Index: Integer; Delimiter: Char): String;
Procedure SaveBAS(SaveEdit, DoSave: Boolean);
Const
CRCtable: ARRAY[0..255] OF DWORD =
($00000000, $77073096, $EE0E612C, $990951BA, $076DC419, $706AF48F, $E963A535, $9E6495A3,
$0EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988, $09B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
$1DB71064, $6AB020F2, $F3B97148, $84BE41DE, $1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
$136C9856, $646BA8C0, $FD62F97A, $8A65C9EC, $14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
$3B6E20C8, $4C69105E, $D56041E4, $A2677172, $3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
$35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940, $32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
$26D930AC, $51DE003A, $C8D75180, $BFD06116, $21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
$2802B89E, $5F058808, $C60CD9B2, $B10BE924, $2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,
$76DC4190, $01DB7106, $98D220BC, $EFD5102A, $71B18589, $06B6B51F, $9FBFE4A5, $E8B8D433,
$7807C9A2, $0F00F934, $9609A88E, $E10E9818, $7F6A0DBB, $086D3D2D, $91646C97, $E6635C01,
$6B6B51F4, $1C6C6162, $856530D8, $F262004E, $6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
$65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C, $62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
$4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2, $4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
$4369E96A, $346ED9FC, $AD678846, $DA60B8D0, $44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
$5005713C, $270241AA, $BE0B1010, $C90C2086, $5768B525, $206F85B3, $B966D409, $CE61E49F,
$5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4, $59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,
$EDB88320, $9ABFB3B6, $03B6E20C, $74B1D29A, $EAD54739, $9DD277AF, $04DB2615, $73DC1683,
$E3630B12, $94643B84, $0D6D6A3E, $7A6A5AA8, $E40ECF0B, $9309FF9D, $0A00AE27, $7D079EB1,
$F00F9344, $8708A3D2, $1E01F268, $6906C2FE, $F762575D, $806567CB, $196C3671, $6E6B06E7,
$FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC, $F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
$D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252, $D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
$D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60, $DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
$CB61B38C, $BC66831A, $256FD2A0, $5268E236, $CC0C7795, $BB0B4703, $220216B9, $5505262F,
$C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04, $C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,
$9B64C2B0, $EC63F226, $756AA39C, $026D930A, $9C0906A9, $EB0E363F, $72076785, $05005713,
$95BF4A82, $E2B87A14, $7BB12BAE, $0CB61B38, $92D28E9B, $E5D5BE0D, $7CDCEFB7, $0BDBDF21,
$86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E, $81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
$88085AE6, $FF0F6A70, $66063BCA, $11010B5C, $8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
$A00AE278, $D70DD2EE, $4E048354, $3903B3C2, $A7672661, $D06016F7, $4969474D, $3E6E77DB,
$AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0, $A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
$BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6, $BAD03605, $CDD70693, $54DE5729, $23D967BF,
$B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94, $B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);
Var
BASLineReadNum: Integer;
GetNumError: boolean;
implementation
Uses FastCore, ROMUtils, Filing, Utility, LogWind, notes;
Function GetCRC32FromString(Str: String): String;
Var
i, CRCValue: DWORD;
q: ^BYTE;
Begin
CRCValue := $FFFFFFFF;
q := @Str[1];
For i := 0 TO Length(Str) -1 Do Begin
CRCvalue := (CRCvalue Shr 8) Xor
CRCTable[q^ Xor (CRCvalue And $000000FF)];
Inc(q)
End;
Result := UpperCase(IntToHex(CRCValue, 8));
End;
Function CheckCRC: String;
Var
Str: String;
CurPos: Integer;
CurLine, CRC32String, NewCRCString: String;
Begin
Result := '';
CRC32String := '';
CurLine := '';
CurPos := 0;
Str := '';
While CurLine <> '$$END$$' Do Begin
GetNextBASLine(CurPos, CurLine);
If Lowercase(Copy(CurLine, 1, 5)) = 'check' Then
CRC32String := Uppercase(Copy(CurLine, 7, Length(CurLine)))
Else
Str := Str + CurLine + #13#10;
End;
NewCRCString := GetCRC32FromString(Str);
If CRC32String <> '' Then Begin
If NewCRCString <> CRC32String Then
Log('Warning - BAS file may be corrupt');
End Else
Result := NewCRCString;
End;
Procedure DecodeBAS;
Var
TempValue: Extended;
AutoLine: Word;
NotesPresent, InString, Done: Boolean;
LinePos, CurPos, LineNum, Idx, Idx2, NumElements: Integer;
TempStr, CurBASICLine, VarSpace, CurLine, VarString: String;
Label
AbortLine;
Begin
// This rather lengthy procedure Decodes a .bas file
// currently held in FileArray.
// Avoids the use of Delphi's TStringlist.
// It supports all of zmakebas's escape codes and labeling systems.
Log('Loading BAS file - '+Filename);
NotesWindow.Memo1.Lines.Clear; //rather punch in the face type entry by arda
CheckCRC;
BASLineReadNum := 0;
ParseBASLabels;
CurBASICLine := '';
VarSpace := '';
FileBody := '';
CurPos := 0;
Done := False;
AutoLine := $8000;
Repeat
AbortLine:
Inc(BASLineReadNum);
GetNextBASLine(CurPos, CurLine);
CurLine := CurLine + ' ';
If CurLine = '$$END$$ ' Then Begin
Done := True;
End Else Begin
// Process a line of BASIC
If Curline <> ' ' Then Begin
// Comments begin with a '#' in .bas files.
If CurLine[1] <> '#' Then Begin
// Process the special commands first, so they don't get
// assigned a linenumber.
// Currently, these are "Auto", "Check" and "Var".
If Lowercase(Copy(CurLine, 1, 5)) = 'check' Then
// Do Nothing
Else If LowerCase(Copy(CurLine, 1, 4)) = 'auto' Then Begin
// Found a line which specifies the AutoStart line number.
// Grab the number for later poking into the header. First,
// skip non-numerals.
LinePos := 5;
TempStr := '';
While (not (CurLine[LinePos] in ['0'..'9'])) and (linePos < Length(CurLine)+1) Do
Inc(LinePos);
// Grab the number now.
If LinePos < Length(CurLine)+1 Then Begin
While CurLine[LinePos] in ['0'..'9'] Do Begin
TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
If Opt_LoadAutoStart Then
If Integer(GetAsyncKeyState(VK_SHIFT)) >= 0 Then
AutoLine := StrToIntDef(TempStr, $8000);
End;
End Else If Lowercase(Copy(CurLine, 1, 3)) = 'var' Then Begin
// Variables declared at runtime and saved within the program
// This allows for example, The user to type:
// 10 PRINT A
// LET A=20
// SAVE "PROG" LINE 10
// and the program will autorun with A set to 20.
// Step 1 - remove all whitespace *not* inside quotes.
CurLine := Copy(CurLine, 4, 999999);
InString := False;
TempStr := '';
LinePos := 1;
While LinePos < Length(CurLine)+1 Do Begin
If CurLine[LinePos] = '"' Then InString := Not InString;
If (InString and (CurLine[LinePos] <= ' ')) or
(CurLine[LinePos] > ' ') Then TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
CurLine := TempStr;
// Step 2 - Get the variable name, terminated by ":"
LinePos := 1;
TempStr := '';
While Not Done and (LinePos < Length(CurLine)+1) Do Begin
If CurLine[LinePos] = ':' Then
Done := True
Else
TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
If Not Done Then Begin
// Var is invalid - not terminated by a ":"
Log('Loading BAS file - Invalid Variable');
Goto AbortLine;
End;
Done := False;
CurLine := Copy(CurLine, LinePos, 999999);
// if the user specified a "$" after the name, then
// trim it now. We only determine type on the next field.
If TempStr[Length(TempStr)] = '$' Then
TempStr := Copy(TempStr, 1, Length(TempStr)-1);
VarString := LowerCase(TempStr);
// Step 4 - Get the type of variable. Num, Str, NumArray, StrArray, NumFOR.
// Terminated by "=" or "(" for dimensions.
LinePos := 1;
TempStr := '';
While Not Done and (LinePos < Length(CurLine)+1) Do Begin
If (CurLine[LinePos] = '=') or (CurLine[LinePos] = '(') Then
Done := True
Else
TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
If Not Done Then Begin
// Var is invalid - argument is not qualified with "=" or "("
Log('Loading BAS file - Invalid Variable');
Goto AbortLine;
End;
Done := False;
CurLine := Copy(CurLine, LinePos, 999999);
TempStr := lowercase(TempStr);
LinePos := 1;
// Step 5 - Determine subtype, and get the actual var.
// This now branches into seperate routines for each type.
If TempStr = 'num' Then Begin
// Numeric Var.
TempValue := GetNumber(Curline, LinePos, TempStr, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid Numvar Value');
Goto AbortLine;
End;
TempStr := FloatTo5Byte(TempValue);
If Length(VarString) = 1 Then Begin
// Simple Numeric
VarString := Chr(96+(Ord(VarString[1])-96))+TempStr;
End Else Begin
// Complex Numeric
LinePos := 2;
VarString[1] := Chr(160+(Ord(VarString[1])-96));
For LinePos := 2 To Length(VarString) Do
VarString[LinePos] := Chr(Ord(VarString[LinePos]) and $7F);
VarString[Length(VarString)] := Chr(Ord(VarString[Length(VarString)])+$80);
VarString := VarString + TempStr;
End;
End Else If TempStr = 'str' Then Begin
// Simple String
TempStr := Copy(CurLine, 1, Length(CurLine) -1);
CurLine := '';
If TempStr = '' Then Begin
Log('Loading BAS file - Invalid String Var Argument');
Goto AbortLine;
End;
VarString := Chr(64+(Ord(VarString[1])-96))+' '+Copy(TempStr, 2, 999999);
PutWord(@VarString[2], Word(Length(TempStr)-1));
End Else If TempStr = 'numarray' Then Begin
// A Numeric Array. We're currently pointing at the char after "("
// So go and get the array dimensions.
TempStr := GetVarDimensions(CurLine, LinePos, NumElements, 5);
If TempStr = '' Then Begin
Log('Loading BAS file - Invalid NumArray Parameters');
Goto AbortLine;
End;
VarString := Chr(128+(Ord(VarString[1])-96))+TempStr;
LinePos := 1;
TempStr := GetElementsNum(CurLine, LinePos, NumElements);
If TempStr = '' Then Begin
Log('Loading BAS file - Invalid NumArray Element');
Goto AbortLine;
End;
VarString := VarString + TempStr;
End Else If TempStr = 'strarray' Then Begin
// A string array - process as a numeric, but elements are
// one byte in size, as opposed to 5.
TempStr := GetVarDimensions(CurLine, LinePos, NumElements, 1);
If TempStr = '' Then Begin
Log('Loading BAS file - Invalid StrArray Parameters');
Goto AbortLine;
End;
VarString := Chr(192+(Ord(VarString[1])-96))+TempStr;
LinePos := 1;
TempStr := GetElementsStr(CurLine, LinePos);
If TempStr = '' Then Begin
Log('Loading BAS file - Invalid StrArray Element');
Goto AbortLine;
End;
VarString := VarString + TempStr;
End Else If TempStr = 'numfor' Then Begin
// a Looping FOR control variable. Unlikely to be of use to the
// .bas writer, but needs to be saved even so.
// First, set the name:
VarString := Chr(224+(Ord(VarString[1])-96));
// Get the current value
LinePos := 1;
TempValue := GetNumber(CurLine, LinePos, TempStr, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid FOR Var Value');
Goto AbortLine;
End;
TempStr := FloatTo5Byte(TempValue);
VarString := VarString + TempStr;
// Get Limit
Inc(LinePos);
TempValue := GetNumber(CurLine, LinePos, TempStr, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid FOR Var Limit');
Goto AbortLine;
End;
TempStr := FloatTo5Byte(TempValue);
VarString := VarString + TempStr;
// Get Step
Inc(LinePos);
TempValue := GetNumber(CurLine, LinePos, TempStr, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid FOR Var STEP');
Goto AbortLine;
End;
TempStr := FloatTo5Byte(TempValue);
VarString := VarString + TempStr;
// Get Line - a WORD value
Inc(LinePos);
TempValue := GetNumber(CurLine, LinePos, TempStr, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid FOR Var line number');
Goto AbortLine;
End;
VarString := VarString + ' ';
PutWord(@VarString[Length(VarString)-1], Word(Round(TempValue)));
// And finally, the statement.
Inc(LinePos);
TempValue := GetNumber(CurLine, LinePos, TempStr, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid FOR Var Statement');
Goto AbortLine;
End;
VarString := VarString + Chr(Byte(Round(TempValue)));
End Else Begin
// Var is invalid - unknown type
Log('Loading BAS file - Unknown Variable "'+TempStr+'"');
Goto AbortLine;
End;
// Having got the Variable in VarString, now add to the VarSpace.
VarSpace := VarSpace + VarString;
Dec(LinePos);
End Else Begin
// Process the line, as it must (hopefully) be BASIC text.
// Note that lines are inserted in line order.
CurBASICLine := ProcessBASLine(Copy(CurLine, 1, Length(CurLine)-1));
If CurBASICLine = '' Then Goto AbortLine;
// Find the line number
LineNum := (Ord(CurBASICLine[1]) Shl 8) + Ord(CurBASICLine[2]);
// Got the line number, find its place in the text. The current file
// is in tokenised Speccy format, so it's a case of grabbing words. Which are backwards :(
Idx := 1;
While True Do Begin
If Idx >= Length(FileBody) Then
Break
Else
If (Ord(FileBody[Idx]) Shl 8) + Ord(FileBody[Idx +1]) >= LineNum Then
Break
Else
Inc(Idx, Ord(FileBody[Idx +2])+(Ord(FileBody[Idx + 3]) Shl 8) + 4);
End;
If Idx >= Length(FileBody) Then
FileBody := Copy(FileBody, 1, Idx -1) + CurBASICLine + Copy(FileBody, Idx, 999999)
Else
If (Ord(FileBody[Idx]) Shl 8) + Ord(FileBody[Idx +1]) <> LineNum Then
FileBody := Copy(FileBody, 1, Idx -1) + CurBASICLine + Copy(FileBody, Idx, 999999)
Else Begin
Idx2 := Idx;
While (Idx2 < Length(FileBody)) and (FileBody[Idx2] <> #13) Do
Inc(Idx2);
FileBody := Copy(FileBody, 1, Idx -1) + CurBASICLine + Copy(FileBody, Idx2 +1, 999999);
End;
CurBASICLine := '';
End;
End Else Begin
// Special Memo added by Arda
If Lowercase(Copy(CurLine, 1, 5)) = '#note' Then Begin
LinePos := 5;
TempStr := '';
// Grab the memo now.
NotesWindow.Memo1.Lines.Add(Copy(CurLine,16,Length(CurLine)-16));
NotesPresent:=True;
End;
End;
End;
End;
Until Done;
// Now the file has been processed, and so we can create a header.
// The blank header was already created above.
// The AutoStart
PutWord(@FileHeader[$D], AutoLine);
// Now Just add the length of the BASIC (Without the Variables)
PutWord(@FileHeader[$F], Word(Length(FileBody)));
FileBody := FileBody + VarSpace;
// Total Block Length
PutWord(@FileHeader[$B], Word(Length(FileBody)));
Log('Loading BAS file - Success');
if Opt_ShowNotes and NotesPresent Then ShowWindow(NotesWindow, False);
End;
Function ProcessBASLine(CurLine: String): String;
Var
CurChar, LastChar: Char;
TempValue: Extended;
TempWord: Word;
InString, REMCommand: Boolean;
LinePos, LineNum, LineLen: Integer;
TempStr, CurBASICLine: String;
Begin
CurBASICLine := '';
Result := '';
REMCommand := False;
InString := False;
// Lines in a .bas must start with a number.
// so get the line number now.
LinePos := 1;
TempStr := '';
While CurLine[LinePos] in ['0'..'9'] Do Begin
TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
// If it's an empty string, then no numbers were found, so
// Set PC to R Tape Loading Error.
LineNum := StrToIntDef(TempStr, -1);
If (LineNum < 0) or (LineNum > 999999) Then Begin
Log('Loading BAS file - Invalid Line Number');
DoError($1A, 'Invalid line number');
Exit;
End Else Begin
// Store the line number in the FileBody.
// with two pad bytes for the line length when it's been calculated.
TempWord := Word(LineNum);
CurBASICLine := CurBASICLine + Chr(TempWord shr 8) + Chr(TempWord and 255)+#0#0;
End;
// Having got the line number, go get the rest of the line.
// it should be ready for tokenising, so...
CurLine := Copy(CurLine, LinePos, 999999);
CurLine := TokeniseLine(CurLine, False);
LinePos := 1;
LineLen := Length(CurLine)+1;
CurChar := #0;
// and it should now be filled with lovely tokens for the keywords.
// now to do the numbers into speccy #14 + 5 Byte form, and process specials
// such as the zmakebas's \ escape char.
While LinePos < LineLen Do Begin
LastChar := CurChar;
CurChar := CurLine[LinePos];
If Not (Instring or REMCommand) and (CurChar = '$') and not (Curline[LinePos-1] in ['a'..'z', 'A'..'Z']) Then Begin
// A Hex number, of the $xx variety.
// go get the number, if one exists.
TempValue := GetNumber(CurLine, LinePos, CurBASICLine, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid number in line '+IntToStr(LineNum));
Exit;
End;
TempStr := FloatTo5Byte(TempValue);
CurBASICLine := CurBASICLine + #$E + TempStr;
Dec(LinePos);
End Else If Not (Instring or REMCommand) and (CurChar in ['0'..'9', '.', '%']) and Not (LastChar in ['0'..'9', 'a'..'z', 'A'..'Z']) then Begin
// A number. Needs to be dumped as-is, then converted
// to Spectrum 5-Byte floating point.
// We don't need to know the sign of the number,
// the ROM will do that at Runtime.
GetSign(CurLine, LinePos, CurBASICLine);
// go get the number, if one exists.
TempValue := GetNumber(CurLine, LinePos, CurBASICLine, False);
If GetNumError Then Begin
Log('Loading BAS file - Invalid number in line '+IntToStr(LineNum));
Exit;
End;
// Now just update the number with the correct sign,
// and process to 5 bytes float/smallint format.
TempStr := FloatTo5Byte(TempValue);
CurBASICLine := CurBASICLine + #$E + TempStr;
Dec(LinePos);
End Else If CurChar = '"' Then Begin
// This is necessary - the backslash escape code is
// only legal inside strings.
InString := Not InString;
CurBASICLine := CurBASICLine + '"';
End Else If CurChar = '\' Then Begin
// This is an escape character, for UDGs and such.
ProcessEscapeChars(CurLine, LinePos, CurBASICLine);
End Else If CurChar = Chr(196) Then Begin
// BIN is a pain, it stores the 5byte fp after the 101010...
// Number.
CurBASICLine := CurBASICLine + CurChar;
Inc(LinePos);
While (CurLine[LinePos] = ' ') and (LinePos < LineLen) Do Begin
CurBASICLine := CurBASICLine + CurLine[LinePos];
Inc(LinePos);
End;
If LinePos >= LineLen Then Begin
Log('Loading BAS file - BIN without Argument at line '+IntToStr(LineNum));
Exit;
End;
// Possible whitespace skipped, on with the number
TempValue := GetNumber(CurLine, LinePos, CurBASICLine, True);
If GetNumError Then Begin
Log('Loading BAS file - Invalid number in line '+IntToStr(LineNum));
Exit;
End;
If Not REMCommand Then
CurBASICLine := CurBASICLine + #$E + FloatTo5Byte(TempValue);
Dec(LinePos);
End Else Begin
// A keyword token, or probably an Alpha character,
// as chars < Space are stripped earlier on by the call
// to GetNextBASLine.
If CurChar = #234 Then
REMCommand := True;
If (CurChar <> ' ') or InString or REMCommand Then Begin
If CurChar = '£' Then CurChar := #96;
CurBASICLine := CurBASICLine + CurChar;
End;
End;
Inc(LinePos);
End;
// *Finally* we have a tokenised line. The last job is to test for DEF FN, and
// insert the dummy bytes after each parameter.
ProcessDEFFN(CurBASICLine);
// Got a line, now add the $13 terminator, and add to the filebody.
// Also update the length of the text in the 3/4th bytes.
PutWord(@CurBASICLine[3], Word(Length(CurBASICLine)-3));
CurBASICLine := CurBASICLine + #13;
Result := CurBASICLine;
End;
Procedure ProcessDEFFN(var CurBASICLine: String);
Var
LinePos: Integer;
TempStr: String;
TempWord: Word;
DEFFNDone, DEFFNHasParams: Boolean;
Begin
LinePos := FindDEFFN(CurBASICLine);
If LinePos <> 0 Then Begin
Inc(LinePos);
DEFFNDone := False;
DEFFNHasParams := False;
While Not DEFFNDone Do Begin
// Find either the ")" or the next "," - whichever comes first.
// If we find an Alpha or a "," then we have parameters.
If CurBASICLine[LinePos] in ['a'..'z', 'A'..'Z'] Then
DEFFNHasParams := True;
If CurBASICLine[LinePos] = ')' Then Begin
DEFFNDone := True;
If DEFFNHasParams Then Begin
If CurBASICLine[LinePos-1] in ['a'..'z', 'A'..'Z', '$'] Then
CurBASICLine := Copy(CurBASICLine, 1, LinePos-1)+#$0E#0#0#0#0#0+Copy(CurBASICLine, LinePos, 999999);
End;
End;
If CurBASICLine[LinePos] = ',' Then Begin
DEFFNHasParams := True;
If CurBASICLine[LinePos-1] in ['a'..'z', 'A'..'Z', '$'] Then
CurBASICLine := Copy(CurBASICLine, 1, LinePos-1)+#$0E#0#0#0#0#0+Copy(CurBASICLine, LinePos, 999999);
Inc(LinePos, 6);
End;
Inc(LinePos);
If LinePos >= Length(CurBASICLine) Then DEFFNDone := True;
// Test to see if there's any more DEF FNs on the line
If DEFFNDone Then Begin
TempStr := Copy(CurBASICLine, LinePos+1, 999999);
TempWord := FindDEFFN(TempStr);
If TempWord <> 0 Then Begin
Inc(LinePos, TempWord+1);
DEFFNDone := False;
End;
End;
End;
End;
End;
Function FindDEFFN(Line: String): Integer;
Var
Found: Boolean;
Begin
// TO-DO: This needs a complete rewrite.
Result := 0;
Found := False;
If Length(Line) >= 1 Then Begin
Result := 1; //R16 fix by Arda --original value was 5. But it misses the def fn if line number is shorter than 4 digits. fix (result=1) causes false detection of def fn caused by number 206 in line number bytes.
While Not Found and (Result < Length(Line)) Do Begin
If Line[Result] = #$CE Then Found := True;
Inc(Result);
End;
End;
If Not Found Then
Result := 0;
End;
Function GetString(var CurLine: String; Var LinePos: Integer): String;
Var
TempStr: String;
LPos: Integer;
Begin
// Gets, and removes, a string delimited by quote marks from
// Curline.
TempStr := '';
Result := '';
// step past the initial quote mark, if available.
If CurLine[LinePos] <> '"' Then Begin
Log('Invalid String - No opening quotes');
Exit;
End;
Inc(LinePos);
While CurLine[LinePos] <> '"' Do Begin
TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
// And step past the closing '"' mark.
If CurLine[LinePos] <> '"' Then Begin
Log('Invalid String - No closing quotes');
Exit;
End;
Inc(LinePos);
CurLine := Copy(CurLine, LinePos, 999999);
// Now process the string for escape codes
LPos := 1;
Result := '"';
While LPos < Length(TempStr)+1 Do Begin
If TempStr[LPos] = '\' Then
ProcessEscapeChars(TempStr, LPos, Result)
Else
Result := Result + TempStr[LPos];
Inc(LPos);
End;
End;
Function GetVarDimensions(Var CurLine: String; Var LinePos, NumElements: Integer; ElementSize: Byte): String;
Var
Done: Boolean;
Count: Integer;
TempStr: String;
TempValue: Extended;
TempWord: Word;
Begin
// Grabs the dimensions of an array (in "()"s) from a string.
// And returns the Variable as it is constructed in memory.
// Does not get the elements though - that is a job for the calling procedure,
// Which will also have to fill in the variable name.
Result := ' ';
Done := False;
Count := 0;
NumElements := 1;
// Add in the first "(" as it's been trimmed.
CurLine := '('+CurLine;
// now repeat, getting numbers until EOL reached.
While Not Done Do begin
If LinePos >= Length(CurLine) Then Begin
Result := '';
Exit;
End;
If CurLine[LinePos] = ')' Then
Done := True
Else Begin
Inc(LinePos);
TempValue := GetNumber(CurLine, LinePos, TempStr, False);
Result := Result + ' ';
PutWord(@Result[Length(Result)-1], Word(Round(TempValue)));
NumElements := NumElements * Round(TempValue);
Inc(Count);
End;
End;
If Count = 0 Then Begin
Result := '';
Exit;
End;
Result[3] := Chr(Count);
TempWord := (Count *2)+(NumElements*ElementSize)+1;
PutWord(@Result[1], TempWord);
// Now discard the array dimensions part, ready for the calling proc
// to process the elements.
CurLine := Copy(CurLine, LinePos+1, 999999);
Inc(LinePos);
End;
Function GetElementsNum(Var CurLine: String; Var LinePos, NumElements: Integer): String;
Var
TempStr, NumStr: String;
Count: Integer;
Begin
Result := '';
Count := NumElements;
// repeat, getting numbers until count reached.
While Count <> 0 Do begin
Inc(LinePos);
NumStr := FloatTo5Byte(GetNumber(CurLine, LinePos, TempStr, False));
If NumStr = '' Then Begin
Result := '';
Exit;
End;
Result := Result + NumStr;
CurLine := Copy(CurLine, LinePos, 999999);
LinePos := 1;
Dec(Count);
End;
End;
Function GetElementsStr(Var CurLine: String; Var LinePos: Integer): String;
Var
TempStr: String;
Begin
TempStr := '';
// repeat, getting Quote delimited strings, until EOL reached.
While CurLine <> '' Do begin
If (CurLine[LinePos] <> ',') And (CurLine[LinePos+1] <> '"') Then Begin
Result := '';
Exit;
End;
// Step over the , and the "
Inc(LinePos, 2);
While (LinePos < Length(CurLine)) And (CurLine[LinePos] <> '"') Do Begin
TempStr := TempStr + CurLine[LinePos];
Inc(LinePos);
End;
CurLine := Copy(CurLine, LinePos+1, 999999);
LinePos := 1;
End;
// Now process the string for escape codes
Result := '';
While LinePos < Length(TempStr)+1 Do Begin
If TempStr[LinePos] = '\' Then
ProcessEscapeChars(TempStr, LinePos, Result)
Else
Result := Result + TempStr[LinePos];
Inc(LinePos);
End;
linePos := 1;
End;
Function GetSign(CurLine: String; var LinePos: Integer; var CurBASICLine: String): Boolean;
Begin
// Grabs the sign of a number from text, +/- chars.
// You can add the chars found to the BASIC text by specifying
// a valid string as CurBASICLine. Specifying nil will get the sign
// without copying chars to the BASIC.
Result := False;
While ((CurLine[LinePos] = '+') or (Curline[LinePos] = '-')) and
(LinePos < Length(CurLine)+1) Do Begin
// Any number of +,- can precede a number.
If CurLine[LinePos] = '-' Then Result := Not Result;
If @CurBASICLine <> nil Then
CurBASICLine := CurBASICLine + CurLine[LinePos];
Inc(LinePos);
End;
End;
Function GetNumber(CurLine: String; var LinePos: Integer; var CurBASICLine: String; IsBinary: Boolean): Extended;
Var
ExpSign: Boolean;
Divider: Extended;
Exponent: Integer;
CurChar: Char;
TempDWord: DWord;
IsNegative: Boolean;
TempStr: String;
Begin
// Like GetSign, but returns a number rather than a sign.
// The calling proc must ensure that the char pointed at by linepos, in
// curline, is a valid digit. Any sign should be processed before this
// function is called.
// Handles both Binary (BIN 10101/%10101) and Hex (0x0000/$0000) formats.
Result := 0.0;
If Length(CurLine) < LinePos Then Exit;
// First, test for +/- modifiers
IsNegative := False;
While CurLine[LinePos] in ['-', '+'] Do Begin
If CurLine[LinePos] = '-' Then IsNegative := Not IsNegative;
Inc(LinePos);
End;
// Next, test for Hex.
GetNumError := False;
If ((CurLine[LinePos+1] in ['x', 'X']) and
(Curline[LinePos] = '0') and Not
(CurLine[LinePos-1] in ['a'..'z', 'A'..'Z', '0'..'9'])) or
((CurLine[LinePos] = '$') and (CurLine[LinePos+1] in ['0'..'9'])) Then Begin
// This is a hex number.
CurBASICLine := CurBASICLine + '0x';
TempDWord := 0;
Inc(LinePos);
If CurLine[LinePos] in ['x', 'X'] Then Inc(linePos);
While CurLine[LinePos] in ['0'..'9', 'A'..'F', 'a'..'f'] Do Begin
CurChar := CurLine[LinePos];
If CurChar in ['a'..'f'] Then CurChar := Chr(Ord(CurChar)-32);
If CurChar in ['A'..'F'] Then
TempDWord := (TempDWord Shl 4) + Ord(CurChar)-55
Else
TempDWord := (TempDWord shl 4) + Ord(CurChar)-48;
CurBASICLine := CurBASICLine + CurLine[LinePos];
Inc(LinePos);
End;
Result := TempDWord;
End Else If IsBinary or (CurLine[LinePos] = '%') Then Begin
// It's not hex, the calling proc expects binary, which may not exist
TempDWord := 0;
If CurLine[LinePos] = '%' Then Inc(LinePos);
If Not IsBinary Then TempStr := '%';
While CurLine[LinePos] in ['0', '1'] Do Begin
TempStr := TempStr + CurLine[LinePos];
TempDWord := (TempDWord Shl 1) + (Ord(CurLine[LinePos])-48);
Inc(LinePos);
End;
If (TempStr <> '%') and (TempStr <> '') Then
CurBASICLine := CurBASICLine + TempStr;
Result := TempDWord;
End Else Begin
// Not expecting Binary, not found any hex, so...
// Must be a number.
If Not (CurLine[LinePos] in ['0'..'9', '.', 'e', 'E']) Then Begin
GetNumError := True;
Exit;
End;
Result := 0;
While (CurLine[LinePos] in ['0'..'9']) And (LinePos < Length(CurLine)+1) Do Begin
Result := (Result *10)+(Ord(CurLine[LinePos])-48);
CurBASICLine := CurBASICLine + CurLine[LinePos];
Inc(LinePos);
End;
// Now test for 'E' and '.'
If CurLine[LinePos] = '.' Then Begin
// the point *must* be followed by numbers.
CurBASICLine := CurBASICLine + '.';
Inc(LinePos);
Divider := 1;
While CurLine[LinePos] in ['0'..'9'] Do Begin
CurBASICLine := CurBASICLine + CurLine[LinePos];
Divider := Divider /10;
Result := Result + (Divider*(Ord(CurLine[LinePos])-48));
Inc(LinePos);
End;
End;
If (Curline[Linepos] = 'e') or (Curline[LinePos] = 'E') Then Begin
CurBASICLine := CurBASICLine + CurLine[LinePos];
Inc(LinePos);
// 'E' must be followed by an optional single +/- sign,
// and then an integer.
Exponent := 0;
EXPSign := True;
If CurLine[LinePos] = '+' Then Begin
CurBASICLine := CurBASICLine + '+';
Inc(LinePos);
End Else If CurLine[linePos] = '-' Then Begin
CurBASICLine := CurBASICLine + '-';
Inc(LinePos);
EXPSign := False;
End;
While CurLine[LinePos] in ['0'..'9'] Do Begin
Exponent := (Exponent *10)+(Ord(CurLine[LinePos])-48);
CurBASICLine := CurBASICLine + CurLine[LinePos];
Inc(LinePos);
End;
// We have an exponent, now calculate the new value
If EXPSign Then
Result := Result * Power(10, Exponent)
Else
Result := Result / Power(10, Exponent);
End;
If IsNegative Then Result := -Result;
End;
End;
Function FloatTo5Byte(Value: Extended): String;
Var
Sign: Byte;
Mantissa: DWord;
Exp: Integer;
NewVal: Integer;
Begin
Result := '';
// If it's a small integer, then store as such, else
// it's the 5 byte float.
If ((Value >= -65535) and (Value < 65536)) and
(Value = Round(Value)) Then Begin
Result := #0;
If Value >= 0 Then
Result := Result + #0
Else Begin
Result := Result + #$FF;
Value := Value + 65536;
End;
NewVal := Round(Value);
Result := Result + Chr(Byte(NewVal and $FF)) + Chr(Byte(NewVal Shr 8)) + #0;
End Else Begin
// 5 byte floating point.
// Determine the sign
If Value < 0 Then Begin
Sign := $80;
Value := -Value;
End Else
Sign := 0;
// Find the exponent.
Exp := Floor(log2(Value));
If (Exp < -129) or (Exp > 126) Then Exit;
// And the mantissa. Multiply by a big number for later shifting
Mantissa := Round(((Value/Power(2.0, Exp) -1.0) * 2147483648) + 0.5);
// Now store the bytes. Shift the by now huge mantissa
// and grab each byte as it falls off the end.
// First the Exponent.
Result := Chr(Byte( Exp + $81));
// Next the Mantissa - note that we ignore the high bit of the first byte.
Result := Result + Chr(Byte(((Mantissa Shr 24) and $7F) or Sign));
Result := Result + Chr(Byte(( Mantissa Shr 16) and $FF));
Result := Result + Chr(Byte(( Mantissa Shr 8) and $FF));
Result := Result + Chr(Byte( Mantissa and $FF));
End;
End;
Function Byte5ToFloat(Exponent: Byte; Mantissa: DWord): Extended;
Var
ResStr: String;
Begin
// Converts from Spectrum 5Byte format to a floating point format.
// This is primarily for the Variables converter, so that they can
// be saved in Human-Readable form.
Set8087CW($133f);
If Exponent = 0 Then Begin
// the SmallINT form.
Result := (Mantissa Shr 8) and 65535;
If (Mantissa And 255) = $FF Then Result := Result - 65536;
End Else Begin
// the more complex floating point form
// The mantissa as it arrives in DWord form is backwards -
// Sort that out now :)
Mantissa := (Mantissa Shr 24)+(((Mantissa Shr 16) And $FF) Shl 8)+(((Mantissa Shr 8) and $FF) Shl 16)+((Mantissa And $FF) Shl 24);