aboutsummaryrefslogtreecommitdiff
path: root/jindroush/chkbas/chkbas.cpp
blob: a1ad505e7bb5c676cc63cc53315a8f9d0cf2baf5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
//    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
//    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., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "jintypes.h"
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include "carray.h"
#include "cfile.h"

#include "pub.def"

#include "basic.h"
#include "basic_tb.h"
#include "basic_xl.h"
#include "basic_xe.h"

extern void strlwr(char *);

typedef enum
{
	BASIC = 0,			//standard basic
	BASIC_TB = 1,		//Turbo Basic
	BASIC_XL = 2,		//OSS Basic XL
	BASIC_XE = 3,		//OSS Basic XE
	BASIC_XEX = 4		//OSS Basic XE in extended mode
} BASIC_TYPE;

typedef struct
{
	char** ppCommands;	//array of commands
	char** ppOperands;	//array of operands

	int iCmdNum;			//number of commands
	int iOpNum;				//number of operands
	int iOpFirst;			//first operand
} LANGDESC;

LANGDESC langtbl[] =
{
	{ aCmdsBasic, aOpsBasic, BASIC_CMD_NUM, BASIC_OPS_NUM, 0x0E },
	{ aCmdsTBasic, aOpsTBasic, TBASIC_CMD_NUM, TBASIC_OPS_NUM, 0x0D },
	{ aCmdsBasicXl, aOpsBasicXl, BASICXL_CMD_NUM, BASICXL_OPS_NUM, 0x0D },
	{ aCmdsBasicXe, aOpsBasicXe, BASICXE_CMD_NUM, BASICXE_OPS_NUM, 0x0D },
	{ aCmdsBasicXe, aOpsBasicXe, BASICXE_CMD_NUM, BASICXE_OPS_NUM, 0x0D },
};

#define OP_NHCONST 		0x0D
#define OP_NCONST			0x0E
#define OP_SCONST			0x0F
#define OP_EOL				0x16

#define VART_SCALAR		0x00

#define VART_ARRAYU		0x40
#define VART_ARRAY		0x41

#define VART_STRINGU		0x80
#define VART_STRING		0x81

#define VART_STRINGAU	0x90
#define VART_STRINGA		0x91

#define VART_PROC			0xC1
#define VART_LABEL		0xC2

#define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"

#define HEADER SHEADER \
   PRG_DESC "\n" \
	"  Latest version can be found at " PRG_URL "\n" \
	"  Published under GPL. See GPL.TXT.\n" \
	"  Thanks to Russ Gilbert for his SALVAGE programs.\n\n"

#define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE

BOOL GenCode( CFile* pcf, DWORD dwStart, DWORD dwEnd );
BOOL GenLineCode( CFile* pcf, DWORD dwEnd );
BOOL GenCmdCode( CFile* pcf, DWORD dwLineStart, DWORD dwLineEnd );
BOOL GenOpsCode( CFile* pcf, DWORD dwTokEnd );
double ReadAtariBCD( CFile* pcf );

BOOL Vars_Load( CFile* pcf, WORD wVNT, WORD wVNTL, WORD wVVT, WORD NV );
void Var_Get( char* szRet, int iNum );
void Vars_Destroy();

FILE* g_fout = NULL;
CArray g_aVariables;			//array of variables

BOOL g_bVerbose = FALSE;	//verbose output
BOOL g_bAtariOut = FALSE;	//atari output
BOOL g_bExtOut = TRUE;		//extended output
BOOL g_bHasErrors = FALSE;	//decode error flag
BOOL g_bNoInverse = FALSE;	//don't emit inverse
BASIC_TYPE g_Type = BASIC;	//basic type. Default is Atari Basic

char** g_aBasicCmds;			//array of commands
char** g_aBasicOps;			//array of operands
int g_iCmdNum;					//number of commands
int g_iOpsNum;					//number of operands
int g_iOpFirst;				//first operand

#include "switches.cpp"

int main( int argc, char* argv[] )
{
	setbuf( stdout, NULL );
	setbuf( stderr, NULL );

	if ( !SWITCHES_Init( &argc, argv ) )
		return 1;

	if(strstr(argv[0], "listbas"))
		g_bExtOut = FALSE;

	if ( argc < 2 )
	{
		SWFN_HELP( USAGE );
		return 1;
	}

	if ( g_bExtOut )
		fprintf( stderr, SHEADER "\n" );
	else
		fprintf( stderr, HEADER );

	char* szInfile = argv[ 1 ];
	char* szOutfile = NULL;

	if ( argc > 2 )
		szOutfile = argv[ 2 ];

	CFile cf;

	if ( !cf.Open( szInfile ) )
	{
		fprintf( stderr, "Can't open input file '%s'\n", szInfile );
		return 1;
	}

	if ( szOutfile )
	{
		if( g_bAtariOut )
			g_fout = fopen( szOutfile, "wb" );	//opening as binary because of
															//atascii
		else
			g_fout = fopen( szOutfile, "wt" );
	}

	if ( !g_fout )
		g_fout = stdout;

	//Don't emit additional info if you are in Atari mode
	if ( g_bAtariOut )
		g_bExtOut = FALSE;

	if ( g_bExtOut )
	{
		fprintf( g_fout, HEADER );
		fprintf( g_fout, "Input file: %s\n\n", szInfile );
	}

	//initialize globals depending on language set
	g_aBasicCmds = langtbl[ g_Type ].ppCommands;
	g_aBasicOps = langtbl[ g_Type ].ppOperands;
	g_iCmdNum = langtbl[ g_Type ].iCmdNum;
	g_iOpsNum = langtbl[ g_Type ].iOpNum;
	g_iOpFirst = langtbl[ g_Type ].iOpFirst;

	DWORD dwFileLen = cf.GetLength();

	//read head
	WORD wLOMEM = cf.readLEw();
	WORD wVNT = cf.readLEw();
	WORD wVNTE = cf.readLEw();
	WORD wVVT = cf.readLEw();
	WORD wSTMTAB = cf.readLEw();
	WORD wSTMCUR = cf.readLEw();
	WORD wSTARP = cf.readLEw();

	if( ( wLOMEM == 0x00DD ) && ( g_Type == BASIC_XE ) )
	{
		//extended mode of Basic XE
		wLOMEM = 0;
		g_Type = BASIC_XEX;
	}

	WORD wCOR = wVNT - wLOMEM - 0x0E;

	wVNT -= wCOR;
	wVNTE -= wCOR;
	wVVT -= wCOR;
	wSTMTAB -= wCOR;
	wSTMCUR -= wCOR;
	wSTARP -= wCOR;

	WORD wVNTL = wVNTE - wVNT + 1;
	WORD wVVTE = wSTMTAB - 1;
	WORD wVVTL = wVVTE - wVVT + 1;
	WORD NV = wVVTL / 8;

	WORD wCodeLen = wSTMCUR - wSTMTAB;
	WORD wCodeLenCur = wSTARP - wSTMCUR;

	long lLenDiff = (long)dwFileLen - (long)wSTARP;

	if ( g_bExtOut )
	{
		fprintf( g_fout, "Constants & pointers:\n" );
		fprintf( g_fout, "Start of Name Table      (VNT)   : %04X\n", wVNT );
		fprintf( g_fout, "End of Name Table        (VNTE)  : %04X\n", wVNTE );
		fprintf( g_fout, "Lenght of Name Table     (VNTL)  : %04X\n", wVNTL );

		fprintf( g_fout, "Start of Variable Table  (VVT)   : %04X\n", wVVT );
		fprintf( g_fout, "End of Variable Table    (VVTE)  : %04X\n", wVVTE );
		fprintf( g_fout, "Length of Variable Table (VVTL)  : %04X\n", wVVTL );

		fprintf( g_fout, "Number of Variables      (NV)    : %04X\n", NV );

		fprintf( g_fout, "Start of Code            (STMTAB): %04X\n", wSTMTAB );
		fprintf( g_fout, "Length of Code                   : %04X\n", wCodeLen );
		fprintf( g_fout, "Current command          (STMCUR): %04X\n", wSTMCUR );
		fprintf( g_fout, "Length of current command        : %04X\n", wCodeLenCur );
		fprintf( g_fout, "First byte after program (STARP) : %04X\n", wSTARP );

		if( g_Type != BASIC_XEX )
		{
			fprintf( g_fout, "Length of file                   : %04lX\n", dwFileLen );
			fprintf( g_fout, "File len difference              : %08lX\n", (DWORD)lLenDiff );
		}
		fputc( '\n', g_fout );
	}

	//records must be 8 bytes long
	if ( NV * 8 != wVVTL )
	{
		fprintf( stderr, "Variable Table Length Mismatch!\n" );
		return 1;
	}

	if ( lLenDiff <0 )
	{
		fprintf( stderr, "File Length Incorrect!\n" );
		return 1;
	}

	if ( !Vars_Load( &cf, wVNT, wVNTL, wVVT, NV ) )
		return 1;

	if ( g_bExtOut )
	{
		fprintf( g_fout, "Main code starts here:\n" );
	}

	GenCode( &cf, wSTMTAB, wSTMCUR );

	if ( g_bExtOut )
	{
		fputc( '\n', g_fout );

		fprintf( g_fout, "Immediate code starts here:\n" );
		GenCode( &cf, wSTMCUR, wSTARP );
		fputc( '\n', g_fout );
	}
	else
	{
		cf.Seek( wSTARP, SEEK_SET );
	}

	//if extended mode of Basic XE
	if( g_Type == BASIC_XEX )
	{
		//code is stored in 4 extended memory banks
		for( int iBank = 0; iBank < 4; iBank++ )
		{
			//read the end of code in bank
			WORD wLen = cf.readLEw();

			//because bank starts at 0x4000, the value must be GE
			if( wLen < 0x4000 )
			{
				fprintf( stderr, "Bank end too low! %04X\n", wLen );
				g_bHasErrors = TRUE;
				break;
			}

			wLen -= 0x4000;

			if ( g_bExtOut )
			{
				fprintf( g_fout, "\nBank %d code:\n", iBank );
			}

			if ( wLen )
			{
				//if we have anything to process, we do so
				long lNow = cf.Tell();
				GenCode( &cf, lNow, lNow + wLen );
			}
			else
			{
				//else we skip one byte (error in BXE?) in empty bank
				cf.skip( 1 );
			}

			if ( g_bExtOut )
			{
				fputc( '\n', g_fout );
			}
		}
	}

	cf.Close();

	if ( g_bHasErrors )
		fprintf( stderr, "File may contain some errors!\n" );

	if ( g_fout != stdout )
		fclose( g_fout );

	fprintf( stderr, "Done!\n" );

	Vars_Destroy();

	return 0;
}

BOOL GenCode( CFile* pcf, DWORD dwStart, DWORD dwEnd )
{
	pcf->Seek( dwStart, SEEK_SET );

	//going thru the bank
	while( pcf->Tell() < (long)dwEnd )
	{
		//generating line
		if ( !GenLineCode( pcf, dwEnd ) )
		{
			g_bHasErrors = TRUE;
			return FALSE;
		}

		//output the correct line terminator
		if ( g_bAtariOut )
			fputc( 0x9B, g_fout );
		else
			fputc( '\n', g_fout );
	}

	return TRUE;
}

BOOL GenLineCode( CFile* pcf, DWORD dwEnd )
{
	DWORD dwLineStart = pcf->Tell();

	//some debug info
	if ( g_bVerbose )
		fprintf( g_fout, "[O:%08lX]", dwLineStart );

	//line number
	WORD wLineNum = pcf->readLEw();

	//end of line
	BYTE btLEnd = pcf->readb();
	DWORD dwLineEnd = dwLineStart + btLEnd;

	//don't print immediate line in BASIC XE extended (already filtered in
	//  other versions

	if( g_Type == BASIC_XEX )
	{
		if( wLineNum == 0x8000 )
		{
			pcf->Seek( dwLineEnd, SEEK_SET );
			return TRUE;
		}
	}

	fprintf( g_fout, "%d ", wLineNum );

	if ( g_bVerbose )
	{
		fprintf( g_fout, "[S:%04lX E:%04lX]", dwLineStart, dwLineEnd );
	}

	//if the line goes the end of buffer, croak
	if ( ( dwLineStart > dwEnd ) || ( dwLineEnd > dwEnd ) )
	{
		fprintf( g_fout, "***Line size mismatch. (%04lX-%04lX)\n", dwLineEnd, dwEnd );
		return FALSE;
	}

	//and generate the output for commands
	while( pcf->Tell() < (long)dwLineEnd )
	{
		if ( !GenCmdCode( pcf, dwLineStart, dwLineEnd ) )
			return FALSE;
	}

	return TRUE;
}

BOOL GenCmdCode( CFile* pcf, DWORD dwLineStart, DWORD dwLineEnd )
{
	//end of token
	BYTE btTEnd = pcf->readb();

	DWORD dwTokStart = pcf->Tell();
	DWORD dwTokEnd = dwLineStart + btTEnd;

	if ( g_bVerbose )
	{
		fprintf( g_fout, "[C:%04lX E:%04lX]", dwTokStart, dwTokEnd );
	}

	//could this ever happen?
	if ( dwTokStart >= dwTokEnd )
	{
		fprintf( g_fout, "***Command size problem!\n" );
		return FALSE;
	}

	//if token goes behind the eol
	if ( dwTokEnd > dwLineEnd )
	{
		fprintf( g_fout, "***Command size mismatch. (%04lX-%04lX)\n", dwTokEnd, dwLineEnd );
		return FALSE;
	}

	//which token
	BYTE btTok = pcf->readb();

	//token buffer
	char szTok[ 50 ];

	//if known token
	if ( btTok < g_iCmdNum ) 
	{
		//sub table for BXL extended commands
		if( ( btTok == BXL_EXTEND ) && ( g_Type == BASIC_XL ) )
		{
			BYTE btTok = pcf->readb();

			switch( btTok )
			{
				case 0x10:
					strcpy( szTok, "LOCAL" );
					break;

				case 0x11:
					strcpy( szTok, "EXIT" );
					break;

				case 0x12:
					strcpy( szTok, "PROCEDURE" );
					break;

				case 0x13:
					strcpy( szTok, "CALL" );
					break;

				case 0x14:
					strcpy( szTok, "SORTUP" );
					break;

				case 0x15:
					strcpy( szTok, "SORTDOWN" );
					break;

				default:
					sprintf( szTok, "EXTEND?%02X", btTok );
					g_bHasErrors = TRUE;
					break;

			}
		}
		else
			strcpy( szTok, g_aBasicCmds[ btTok ] );
	}
	else
	{
		sprintf( szTok, "COM%02X", btTok );
		fprintf( stderr, "Unknown command %02X!!!\n", btTok );
		g_bHasErrors = TRUE;
	}

	if ( *szTok )
	{
		//lower case for some dialects
		if( ( g_Type == BASIC_XL ) || ( g_Type == BASIC_XE ) || ( g_Type == BASIC_XEX ) )
		{
			strlwr( szTok + 1 );
		}

		fprintf( g_fout, "%s ", szTok );
	}

	//special handling for REM & DATA (first two statements)
	if ( btTok < 2 )
	{
		BYTE c;

		int iSecure = 0x100;

		for( ;; )
		{
			c = pcf->readb();

			if ( c == 0x9B )
				break;

			if ( g_bNoInverse )
				c &= 0x7F;

			fputc( c, g_fout );

			iSecure--;

			if( !iSecure )
			{
				fprintf( stderr, "Runaway in Rem/Data code!\n" );
				return FALSE;
			}
		} 

		return TRUE;

	}

	//generate operands for remain of command
	while( pcf->Tell() < (long)dwTokEnd )
	{
		if ( !GenOpsCode( pcf, dwTokEnd ) )
			return FALSE;
	}

	return TRUE;
}

BOOL GenOpsCode( CFile* pcf, DWORD dwTokEnd )
{
	//get operand
	BYTE btTok = pcf->readb();

	if ( btTok == OP_EOL )
	{
		//new line
		return TRUE;
	}

	if ( btTok == OP_NCONST )
	{
		//bcd num

		fprintf( g_fout, "%.10g", ReadAtariBCD( pcf ) );

		return TRUE;
	}

	if ( btTok == OP_NHCONST )
	{
		if ( g_Type == BASIC_TB )
		{
			//hex num TBASIC
			fprintf( g_fout, "$%X", (unsigned short)ReadAtariBCD( pcf ) );
			return TRUE;
		}

		if( ( g_Type == BASIC_XL ) || ( g_Type == BASIC_XE ) || ( g_Type == BASIC_XEX ) )
		{
			//hex num BASIC XL
			WORD wNum = (unsigned short)ReadAtariBCD( pcf );

			if( wNum > 0xFF )
				fprintf( g_fout, "$%04X", wNum );
			else
				fprintf( g_fout, "$%02X", wNum );

			return TRUE;
		}
	}

	if ( btTok == OP_SCONST )
	{
		//string
		BYTE c;

		BYTE btLen = pcf->readb();

		fprintf( g_fout, "\"" );
		while( btLen-- )
		{
			c = pcf->readb();

			if ( c == 0x9B )
			{
				break;
			}

			//is this for TBASIC only?
			if ( c == '\"' )
			{
				fprintf( g_fout, "\"\"" );
			}
			else
				fputc( c, g_fout );

		} 
		fprintf( g_fout, "\"" );
		return TRUE;
	}

	//and now for variables
	if ( btTok & 0x80 )
	{
		char szPom[ 255 ];

		Var_Get( szPom, btTok & 0x7F );

		if( ( g_Type == BASIC_XL ) || ( g_Type == BASIC_XE ) || ( g_Type == BASIC_XEX ) )
		{
			strlwr( szPom + 1 );
		}

		fprintf( g_fout, szPom );

		return TRUE;
	}

	//func or op
	if ( ( btTok < g_iOpFirst ) || ( btTok - g_iOpFirst + 1 > (BYTE)g_iOpsNum ) )
	{
		fprintf( g_fout, "UNKOP%02X", btTok );
		fprintf( stderr, "Unknown operand %02X!!!\n", btTok );
		g_bHasErrors = TRUE;
	}
	else
	{
		char szPom[ 100 ];
		strcpy( szPom, g_aBasicOps[ btTok - g_iOpFirst ] );

		if( ( g_Type == BASIC_XL ) || ( g_Type == BASIC_XE ) || ( g_Type == BASIC_XEX ) )
		{
			strlwr( szPom + 1 );
		}

		fprintf( g_fout, "%s", szPom );
	}

	return TRUE;

}

//code for reading Atari BCD format
double ReadAtariBCD( CFile* pcf )
{
	double dRes = 0;

	//reads exponent
	BYTE btExp = pcf->readb();

	int iExp;

	if ( !btExp )
	{
		//if Exp==0, we're skipping it! (silently!)
		iExp = 0;
		pcf->skip( 5 );
	}
	else
	{
		//compute exponent
		iExp = ( btExp - 68 ) * 2;

		int i = 5;

		//read 5 pairs of numbers and combine 'em
		while ( i-- )
		{
			BYTE btPom = pcf->readb();

			BYTE btNum = ( btPom >> 4 ) * 10 + ( btPom &0xf );

			dRes *= 100;
			dRes += btNum;
		}

	}

	dRes *= pow( 10, iExp );

	return dRes;
}

BOOL Vars_Load( CFile* pcf, WORD wVNT, WORD wVNTL, WORD wVVT, WORD NV )
{
	char szVarTemp[ 256 ];
	char szVarGen[ 256 ];

	char* pStr = NULL;

	BOOL bTog = FALSE;

	pcf->Seek( wVNT, SEEK_SET );

	for( int i = 0; i < wVNTL; i++ )
	{
		if ( !pStr )
			pStr = szVarTemp;

		char c = pcf->readb();

		if ( c & 0x80 )
		{
			c &= 0x7F;
			bTog = TRUE;
		}

		*( pStr++ ) = c;
		*pStr = '\0';

		//this is for correcting the runaway
		if ( pStr - szVarTemp > 254 )
		{
			*szVarTemp = '\0';
			bTog = TRUE;
		}

		if ( bTog )
		{
			int iVar = g_aVariables.GetSize();

			sprintf( szVarGen, "VAR%d", iVar );

			pStr = szVarTemp;

			int iLength = strlen( szVarTemp );

			if ( iLength )
			{
				for( int i = 0; i < iLength; i++ )
				{
					if ( !isprint( *( pStr++ ) ) )
					{
						strcpy( szVarTemp, szVarGen );
						break;
					}
				}

				if ( iVar )
				{
					for( int i = 0; i < iVar; i++ )
					{
						if( !strcmp( (char*)g_aVariables.GetAt( i ), szVarTemp ) )
						{
							fprintf( g_fout, "Dupped varname: %s\n", szVarTemp );
							strcpy( szVarTemp, szVarGen );
							break;
						}
					}
				}

			}
			else
			{
				strcpy( szVarTemp, szVarGen );
			}

			pStr = new char [ strlen( szVarTemp ) + 1 ];

			if ( pStr )
			{
				strcpy( pStr, szVarTemp );
				g_aVariables.Add( pStr );
			}
			else
			{
				printf( "Not enough memory!\n" );
				return FALSE;
			}

			bTog = FALSE;
			pStr = NULL;
		}
	}

	if ( g_bExtOut )
	{
		fprintf( g_fout, "Variable table:\n" );
	}

	pcf->Seek( wVVT, SEEK_SET );

	for( int i = 0 ; i < NV; i++ )
	{
		BYTE btType = pcf->readb();
		BYTE btNumber = pcf->readb();

		char* szType;

		char szText[ 50 ];

		switch( btType )
		{
			case VART_SCALAR:
			{
				szType = "SCALAR  ";
				sprintf( szText, "%.10g", ReadAtariBCD( pcf ) );
				break;
			}

			case VART_ARRAYU:
			{
				szType = "ARRAYu  ";
			  	char* szP = szText;
				szP += sprintf( szP, "SPoff: %04X ", pcf->readLEw() );
				szP += sprintf( szP, "Dim1: %d ", pcf->readLEw() );
				szP += sprintf( szP, "Dim2: %d ", pcf->readLEw() );
				break;
			}

			case VART_ARRAY:
			{
				szType = "ARRAY   ";
			  	char* szP = szText;
				szP += sprintf( szP, "SPoff: %04X ", pcf->readLEw() );
				szP += sprintf( szP, "Dim1: %d ", pcf->readLEw() );
				szP += sprintf( szP, "Dim2: %d ", pcf->readLEw() );
				break;
			}

			case VART_STRINGAU:
			{
				//BASIC_XL / XE
				szType = "STRINGAu";
			  	char* szP = szText;
				szP += sprintf( szP, "SPoff: %04X ", pcf->readLEw() );
				szP += sprintf( szP, "Dim1: %d ", pcf->readLEw() );
				szP += sprintf( szP, "Dim2: %d ", pcf->readLEw() );
				break;
			}

			case VART_STRINGA:
			{
				//BASIC_XL / XE
				szType = "STRINGA ";
			  	char* szP = szText;
				szP += sprintf( szP, "SPoff: %04X ", pcf->readLEw() );
				szP += sprintf( szP, "Dim1: %d ", pcf->readLEw() );
				szP += sprintf( szP, "Dim2: %d ", pcf->readLEw() );
				break;
			}

			case VART_STRINGU:
			{
				szType = "STRINGu ";
			  	char* szP = szText;
				szP += sprintf( szP, "SPoff: %04X ", pcf->readLEw() );
				szP += sprintf( szP, "Len: %d ", pcf->readLEw() );
				szP += sprintf( szP, "Dim: %d ", pcf->readLEw() );
				break;
			}

			case VART_STRING:
			{
				szType = "STRING  ";
			  	char* szP = szText;
				szP += sprintf( szP, "SPoff: %04X ", pcf->readLEw() );
				szP += sprintf( szP, "Len: %d ", pcf->readLEw() );
				szP += sprintf( szP, "Dim: %d ", pcf->readLEw() );
				break;
			}

			case VART_PROC:
			{
				//TBASIC
				szType = "PROC    ";
			  	char* szP = szText;
				for( int i = 0 ; i < 6; i++ )
				{
					szP += sprintf( szP, "%02X ", pcf->readb() );
				}
				break;
			}

			case VART_LABEL:
			{
				//TBASIC
				szType = "LABEL   ";
			  	char* szP = szText;
				for( int i = 0 ; i < 6; i++ )
				{
					szP += sprintf( szP, "%02X ", pcf->readb() );
				}
				break;
			}

			default:
			{
				szType = "UNKNOWN ";
			  	char* szP = szText;
				for( int i = 0 ; i < 6; i++ )
				{
					szP += sprintf( szP, "%02X ", pcf->readb() );
				}
				g_bHasErrors = TRUE;
			}
		}

		char szName[ 255 ];
		Var_Get( szName, btNumber );

		char cLastChar = szName[ strlen( szName ) - 1 ];

		switch( btType )
		{
			case VART_STRING:
			case VART_STRINGU:
			{
				if ( cLastChar != '$' )
				{
					strcat( szName, "$" );
					char* szStr = new char [ strlen( szName ) + 1 ];
					strcpy( szStr, szName );
					char* szOld = (char*) g_aVariables.GetAt( btNumber );

					if ( szOld )
						delete [] szOld;

					g_aVariables.SetAt( btNumber, szStr );

				}

				break;
			}

			case VART_ARRAY:
			case VART_ARRAYU:
			{
				if ( cLastChar == '(' )
				{
					szName[ strlen( szName ) - 1 ] = '\0';
					char* szStr = new char [ strlen( szName ) + 1 ];
					strcpy( szStr, szName );
					char* szOld = (char*) g_aVariables.GetAt( btNumber );

					if ( szOld )
						delete [] szOld;

					g_aVariables.SetAt( btNumber, szStr );

				}

				break;
			}

		}

		if ( g_bExtOut )
		{
			fprintf( g_fout, "%04X %s (%02X) %02X: %s %s\n",
					i+1, 
					szType, 
					btType, 
					btNumber, 
					szText,
					szName );
		}

	}

	if ( g_bExtOut )
		fputc( '\n', g_fout );

	return TRUE;
}

void Var_Get( char* szRet, int iNum )
{
	char* szVar = (char*) g_aVariables.GetAt( iNum );

	if ( szVar )
		strcpy( szRet, szVar );
	else
		sprintf( szRet, "VAR%d", iNum );
}

void Vars_Destroy()
{
	for( int i = 0; i < g_aVariables.GetSize(); i++ )
	{
		if ( g_aVariables.GetAt( i ) )
			delete [] ( char* ) g_aVariables.GetAt( i );
	}
}