-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnbasic.cpp
More file actions
executable file
·1967 lines (1882 loc) · 48 KB
/
nbasic.cpp
File metadata and controls
executable file
·1967 lines (1882 loc) · 48 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
#include "nbasic.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIXTEENBIT 65536
//---------------------------------------
Token::Token(int num_, int type_, int line_, const char* val, int location_)
{
index = num_;
type = type_;
line = line_;
location = location_;
next = prev = NULL;
int len = strlen(val);
if (len == 0)
{
value = NULL;
return;
}
value = new char[len+1];
memset(value,0,len+1);
strcpy(value, val);
valuelen = len;
/*
if (type == INCBIN)
{
printf("incbin\n");
fflush(stdout);
}
*/
}
Token::~Token()
{
if (value != NULL)
delete value;
if (next != NULL)
delete next;
}
//---------------------------------------
TokenList::TokenList()
{
num_tokens = 0;
first = marker = last = NULL;
}
TokenList::~TokenList()
{
last = NULL;
marker = NULL;
if (first != NULL) delete first; //blows away whole list safely
}
bool TokenList::Match(int start, int type)
{
const Token *t = GetToken(start);
return (t != NULL && t->type == type);
}
bool TokenList::Match(int start, int type0, int type1)
{
return Match(start, type0) && Match(start+1, type1);
}
bool TokenList::Match(int start, int type0, int type1, int type2)
{
return Match(start, type0, type1) && Match(start+2, type2);
}
bool TokenList::Match(int start, int type0, int type1, int type2, int type3)
{
return Match(start, type0, type1, type2) && Match(start+3, type3);
}
void TokenList::AddToken(int type, const char *val, int len, int linenum, int loc)
{
char str[len+1];
memset(str,0,len+1);
strncpy(str,val,len);
Token *t = new Token(num_tokens, type, linenum, str, loc);
num_tokens++;
if(first == NULL)
{
first = t;
last = t;
}
else
{
last->next = t;
t->prev = last;
last = last->next;
}
}
void TokenList::RemoveToken(int n)
{
//set marker to the target token, and get handles on surrounding tokens
if (first == NULL || n < 0 || n >= num_tokens) return;
if (marker == NULL) marker = first;
while(marker->index < n) marker = marker->next;
while(marker->index > n) marker = marker->prev;
Token* tnext = marker->next;
Token* tprev = marker->prev;
//delete the token
if (first == marker) first = tnext; //so we don't lose the whole stream
marker->next = NULL;
marker->prev = NULL;
delete marker;
num_tokens--;
//re-point marker and hook up others
if (tprev != NULL) marker = tprev;
else marker = tnext;
if (tprev !=NULL) tprev->next = tnext;
if (tnext !=NULL) tnext->prev = tprev;
//decrement downstream index nums
Token* temp = tnext;
while (temp != NULL)
{
temp->index--;
temp = temp->next;
}
}
const Token* TokenList::GetToken(int n)
{
if (first == NULL || n < 0 || n >= num_tokens) return NULL;
if (marker == NULL) marker = first;
while(marker->index < n) marker = marker->next;
while(marker->index > n) marker = marker->prev;
return marker;
}
Token* TokenList::GetTokenPointer(int n)
{
if (GetToken(n) == NULL) return NULL;
return marker;
}
void TokenList::SetTokenLocation(int i, int loc)
{
if (GetToken(i) != NULL)
marker->location = loc;
}
bool TokenList::HasToken(const char *value)
{
const Token *t = first;
while(t!=NULL)
{
if (!strcmp(t->value, value)) return true;
t = t->next;
}
return false;
}
int TokenList::NumTokens() const
{
return num_tokens;
/*
int n = 0;
const Token *t = first;
while(t!=NULL)
{
n++;
t = t->next;
}
return n;
*/
}
//---------------------------------------
NBasic::NBasic()
{
verbose = true;
numfiles = 0;
numlines = 0;
num_regexes = 0;
next_autolabel = 1;
}
NBasic::~NBasic()
{
//empty
}
void NBasic::SetVerbosity(bool b)
{
verbose = b;
}
void NBasic::SetMaxFiles(int n)
{
filelines = new int[n];
filenames = new const char*[n];
}
int NBasic::TokenType(int i)
{
if (i<tokens.NumTokens())
return tokens.GetToken(i)->type;
return PAST_END_FILE;
}
const char* NBasic::TokenValue(int i)
{
return tokens.GetToken(i)->value;
}
void NBasic::AddRegEx(const char *exp, int result, bool require_whitespace)
{
if (num_regexes == MAX_REGEXES)
{
printf("ERROR: Max number of regular expressions. Change #define MAX_REGEXES\n");
exit(0);
}
regex_returns[num_regexes] = result;
regexes[num_regexes].Load(exp);
regex_requirewhite[num_regexes] = require_whitespace;
num_regexes++;
}
bool IsStartAsm(const char *s)
{
int i=0;
while(s[i]==' ' || s[i]=='\t') i++;
if (strncmp(&s[i],"asm",3)!=0) return false;
i+=3;
while(s[i]==' ' || s[i]=='\t' || s[i]=='\r' || s[i]=='\n') i++;
if (s[i]==0) return true;
return false;
}
bool IsEndAsm(const char *s)
{
int i=0;
while(s[i]==' ' || s[i]=='\t') i++;
if (strncmp(&s[i],"endasm",6)!=0) return false;
i+=6;
while(s[i]==' ' || s[i]=='\t' || s[i]=='\r' || s[i]=='\n') i++;
if (s[i]==0) return true;
return false;
}
bool IsAsmLine(const char *s, int &start)
{
int i=0;
while(s[i]==' ' || s[i]=='\t') i++;
if (strncmp(&s[i],"asmline",7)!=0) return false;
start = i+7;
return true;
}
bool NBasic::TokenizeLine(const char *line, int linenum)
{
static bool in_asm_block = false;
int len = strlen(line);
if (len == 0) return true; //sure, we can tokenize a blank line!
//special case, single ASM line
int asmstart = 0;
if (IsAsmLine(line,asmstart))
{
tokens.AddToken(ASM, NULL, 0, linenum);
tokens.AddToken(ASMTEXT, &line[asmstart], len-asmstart, linenum);
tokens.AddToken(ENDASM, NULL, 0, linenum);
return true;
}
//special case, entering ASM block
if (IsStartAsm(line))
{
tokens.AddToken(ASM, NULL, 0, linenum);
in_asm_block = true;
return true;
}
//special case, within ASM block
if (in_asm_block)
{
if (IsEndAsm(line))
{
tokens.AddToken(ENDASM, NULL, 0, linenum);
in_asm_block = false;
return true;
}
tokens.AddToken(ASMTEXT, line, len, linenum);
return true;
}
//general case, parse normally
for(int i = 0; i < num_regexes; i++) //try matching against each regex
{
int match = regexes[i].Matches(line);
if (match > -1)
{
//token match! check whitespace requirements
if ((!regex_requirewhite[i]) ||
(regex_requirewhite[i] &&
(
match == len ||
regexes[whitespace_regex].Matches(&line[match])>-1 ||
regexes[newline_regex].Matches(&line[match])>-1
)
))
{
//we're good. add the token, check the rest of the line
tokens.AddToken(regex_returns[i], line, match, linenum);
return TokenizeLine(&line[match], linenum);
}
}
}
return false;
}
void NBasic::TrimWhitespace()
{
//remove WHITESPACE, NEWLINE, and COMMENT tokens that aren't in an asm block
int i = 0;
bool asmblock = false;
while(i < tokens.NumTokens())
{
int type = TokenType(i);
if (asmblock)
{
if (type == ENDASM)
asmblock = false;
i++;
}
else
{
if (type == WHITESPACE || type == NEWLINE || type == COMMENT)
tokens.RemoveToken(i);
else i++;
if (type == ASM)
asmblock = true;
}
}
}
void NBasic::CompactMath()
{
bool done = false;
while(!done)
{
done = true; //set false when no longer true
for(int i = 0; i < tokens.NumTokens();)
{
if (IsMath(i) && tokens.Match(i+1,NUMBER,NUMBER))
{
int type = TokenType(i);
if (type==PLUS || type==MINUS || type==SHIFTLEFT || type==SHIFTRIGHT ||
type == BITAND || type == BITOR || type == BITEOR)
{
done = false;
Token *t = tokens.GetTokenPointer(i);
int one = NumberValue(TokenValue(i+1), t->line);
int two = NumberValue(TokenValue(i+2), t->line);
t->type = NUMBER;
if (t->value != NULL) delete t->value;
t->value = new char[8];
int result = (type==PLUS ? one+two : (type==MINUS ? one-two :
(type==SHIFTLEFT ? one<<two : (type==SHIFTRIGHT ? one>>two :
(type==BITAND?/*BITAND*/one&two:(type==BITOR?one | two:(one ^
two)))))));
sprintf(t->value,"%i\0", (result+SIXTEENBIT)%SIXTEENBIT);
tokens.RemoveToken(i+1);
tokens.RemoveToken(i+1);
i++;
}
else i+=3;
}
else
{
i++;
}
}
}
}
#define FBUFSIZE 1024
void NBasic::AddFile(const char *filename)
{
FILE *f = fopen(filename,"r");
if (!f)
{
fprintf(stderr,"Could not open file %s for reading. Aborting.\n",filename);
exit(0);
}
char buf[FBUFSIZE];
int line = 0;
while(fgets(buf,FBUFSIZE,f) != NULL)
{
line++;
TokenizeLine(buf,line+numlines);
}
fclose(f);
filenames[numfiles] = filename;
filelines[numfiles++] = line;
numlines += line;
if (verbose) printf("Read file %s (%i lines)\n",filename,line); fflush(stdout);
}
char* NBasic::MakeNumber(const char* n, int line)
{
int number = NumberValue(n, line);
char *s = new char[8]; //more than big enough
sprintf(s,"%i\0",number);
return s;
}
int NBasic::NumberValue(const char* n, int line)
{
int number = 0;
if (n[0] == '$')
{
for(int i = 1; i < strlen(n); i++)
number = number*16 +
(n[i]>='0'&&n[i]<='9' ? n[i]-'0' :
(n[i]>='a'&&n[i]<='f' ? n[i]-'a'+10 :
n[i]-'A'+10));
}
else if (n[0]=='%')
{
for(int i = 1; i < strlen(n); i++)
number = number*2+(n[i]=='0' ? 0 : 1);
}
else if (n[0]>='0' && n[0]<='9')
{
for(int i = 0; i < strlen(n); i++)
number = number*10+(n[i]-'0');
}
else CompileError("number",line);
number = number % SIXTEENBIT; //we'll never need anything bigger than that (CPU limitation)
return number;
}
void NBasic::CompileError(const char *s, int line)
{
int whichline = line;
int whichfile = 0;
while (whichline > filelines[whichfile])
{
whichline -= filelines[whichfile];
whichfile++;
}
fprintf(stderr,"\nERROR on line %i of file '%s' compiling %s statement.\n",
whichline,filenames[whichfile],s);
exit(0);
}
void NBasic::Compile()
{
TrimWhitespace();
CompactMath();
AddVariable("nbasic_stack", 0x100, 2, 0x100);
int numtokens = tokens.NumTokens();
int i = 0;
while (i < numtokens)
{
i += Compile(i);
}
}
int NBasic::Compile(int i)
{
const Token* t = tokens.GetToken(i);
int type = t->type;
int line = t->line;
//printf("line %i: %s\n", line, t->value);fflush(stdout);
switch(type)
{
case ENDPROG: //deprecated
if (verbose) printf("Encountered program end at line %i\n",line);
return tokens.NumTokens(); //past end of input. stop compile
case ASM:
return CompileAsm(i, type, line);
case LABEL:
case ENDASM:
case ARRAY:
return CompileIdentifiers(i, type, line);
case PUSH:
case POP:
return CompilePush(i, type, line);
case GOTO:
case GOSUB:
case RETURN:
case RESUME:
return CompileJump(i, type, line);
case COMMENT:
case WHITESPACE:
return 1;
case DATA:
return CompileData(i, type, line);
case SET:
return CompileSet(i, type, line);
case INC:
case DEC:
return CompileIncrement(i, type, line);
case IF:
return CompileConditional(i, type, line);
default:
break;
}
CompileError("",tokens.GetToken(i)->line);
return -1;
}
void NBasic::CreateAutoVariables()
{
int n = asmlist.NumTokens();
for(int i = 0; i < n; i++)
{
const Token *t = asmlist.GetToken(i);
if (t->type==STAp && (t->value[0]<'0' || t->value[0]>'9'))
AddVariable(t->value);
}
}
void NBasic::VarError(const char* name)
{
fprintf(stderr, "Redefinition of variable %s. Aborting.\n", name);
exit(0);
}
void NBasic::VarAllocError(const char* name)
{
fprintf(stderr, "Error allocating memory for variable %s. Aborting.\n", name);
exit(0);
}
void NBasic::OutputVariables(FILE *f)
{
TokenList tempvars;
int numtokens = variables.NumTokens();
//add absolute location vars
for(int i=0; i<numtokens; i++)
{
const Token *t = variables.GetToken(i);
if (t->type == 2)
{
if(tempvars.HasToken(t->value)) VarError(t->value);
tempvars.AddToken(t->type, t->value, t->valuelen, t->line, t->location);
}
}
//add zeropage vars
for(int i=0; i<numtokens; i++)
{
const Token *t = variables.GetToken(i);
if (t->type == 1)
{
if(tempvars.HasToken(t->value)) VarError(t->value);
tempvars.AddToken(t->type, t->value, t->valuelen, t->line, t->location);
}
}
//add auto-allocated pre-sized vars
for(int i=0; i<numtokens; i++)
{
const Token *t = variables.GetToken(i);
if (t->type == 0 && t->line != -1)
{
if(tempvars.HasToken(t->value)) VarError(t->value);
tempvars.AddToken(t->type, t->value, t->valuelen, t->line, t->location);
}
}
//add auto-allocated default vars
for(int i=0; i<numtokens; i++)
{
const Token *t = variables.GetToken(i);
if (t->type == 0 && t->line == -1)
if(!tempvars.HasToken(t->value))
tempvars.AddToken(t->type, t->value, t->valuelen, 1, t->location);
}
//now try to allocate memory for each variable
int numvars = tempvars.NumTokens();
bool memory[SIXTEENBIT]; //true means filled
for(int i=0; i<SIXTEENBIT; i++) memory[i] = (i < 2048 ? false : true);
for(int i=0; i<numvars; i++)
{
const Token *t = tempvars.GetToken(i);
int start = t->location;
int len = t->line;
if(t->type == 2 && len != 0) //absolute
{
for(int j = start; j < start+len; j++)
{
if(memory[j]) VarAllocError(t->value);
memory[j] = true;
}
}
else if (t->type == 1 && len != 0) //zeropage
{
start = 0;
bool found = false;
while(start < 0x100-len && !found)
{
if(!memory[start])
{
found = true;
for(int j = 0; j < len; j++)
if(memory[start+j]) found = false;
}
if (!found) start++;
}
if (!found) VarAllocError(t->value);
for(int j = 0; j < len; j++) memory[start+j] = true;
tempvars.SetTokenLocation(i, start);
}
else if(len != 0) //auto allocate
{
start = 0;
bool found = false;
while(start < 0xffff-len && !found)
{
if(!memory[start])
{
found = true;
for(int j = 0; j < len; j++)
if(memory[start+j]) found = false;
}
if (!found) start++;
}
if (!found) VarAllocError(t->value);
for(int j = 0; j < len; j++) memory[start+j] = true;
tempvars.SetTokenLocation(i, start);
}
}
//actually output them now
for(int i=0; i<numvars; i++)
{
const Token *t = tempvars.GetToken(i);
fprintf(f, "%s = %i\n", t->value, t->location);
}
}
bool NBasic::VariableExists(const char* name)
{
for(int i = 0; i < variables.NumTokens(); i++)
if (!strcmp(name, variables.GetToken(i)->value))
return true;
return false;
}
void NBasic::AddVariable(const char* name, int length, int vartype, int loc)
{
if(vartype==2 && strcmp(name,"nbasic_stack")) //warn user if it conflicts with the stack
{
for(int i=loc; i < loc+length; i++)
if (i>=256 && i<= 511)
{
fprintf(stderr,"Declaration of variable '%s' conflicts with NES stack ($100-$1FF)\n",name);
fprintf(stderr,"Compile Aborted\n");fflush(stdout);
exit(0);
}
}
variables.AddToken(vartype, name, strlen(name), length, loc);
}
int NBasic::CompileIdentifiers(int i, int type, int line)
{
const Token* t;
switch(type)
{
case LABEL:
t = tokens.GetToken(i);
asmlist.AddToken(ASMLABEL, t->value, t->valuelen, line);
return 1;
case ARRAY:
if(tokens.Match(i,ARRAY,VAR,NUMBER))
{
AddVariable(TokenValue(i+1),
NumberValue(TokenValue(i+2),line));
return 3;
}
else if(tokens.Match(i,ARRAY,ZEROPAGE,VAR,NUMBER))
{
AddVariable(TokenValue(i+2),
NumberValue(TokenValue(i+3),line), 1);
return 4;
}
else if(tokens.Match(i+1,/*ARRAY,*/ABSOLUTE,NUMBER,VAR,NUMBER))
{
AddVariable(TokenValue(i+3),
NumberValue(TokenValue(i+4),line),2,
NumberValue(TokenValue(i+2),line));
return 5;
}
CompileError("array declaration",line); //invalid array declaration syntax
case ENDASM:
CompileError("endasm",line); //should never see it here
}
}
int NBasic::CompileAsm(int i, int type, int line)
{
bool done = false;
int added = 1;
while (!done)
{
const Token* t = tokens.GetToken(i + added++);
if (t!=NULL && t->type!=ENDASM)
asmlist.AddToken(ASMTEXT, t->value, t->valuelen, t->line);
else
done = true;
}
return added;
}
int NBasic::CompileJump(int i, int type, int line)
{
const Token* t;
switch(type)
{
case RESUME:
asmlist.AddToken(RTI, "", 0, line);
return 1;
case RETURN:
asmlist.AddToken(RTS, "", 0, line);
return 1;
case GOTO:
case GOSUB:
if (tokens.Match(i+1, VAR))
{
t = tokens.GetToken(i+1);
asmlist.AddToken((type==GOTO ? JMP : JSR),
t->value, t->valuelen, line);
return 2;
}
break;
}
CompileError(type==GOTO ? "goto" : "gosub", line);
}
bool IsHex(char c)
{
if ('0'<=c && c<='9') return true;
if ('a'<=c && c<='f') return true;
if ('A'<=c && c<='F') return true;
return false;
}
int HexValue(char c)
{
if ('0'<=c && c<='9') return c-'0';
if ('a'<=c && c<='f') return c-'a'+10;
if ('A'<=c && c<='F') return c-'A'+10;
return 0;
}
int NBasic::CompileData(int toknum, int type, int line)
{
const Token *t = tokens.GetToken(toknum);
if (t==NULL || t->value==NULL) CompileError("data", line);
const char *s = t->value;
int i = 4, len = strlen(s), numints=0;
if (len > 80) CompileError("data (line too long)", line);
int ints[len];
while (s[i]==' ' || s[i]=='\t') i++; //skip initial whitespace
//value line includes data keyword until (including) \n
bool in_string = false;
bool between_values = false;
while (s[i]!='\n' && s[i]!=0)
{
if (between_values)
{
while (s[i]==' ' || s[i]=='\t') i++; //whitespace
if (s[i++]!=',') CompileError("data",line);
while (s[i]==' ' || s[i]=='\t') i++; //whitespace
between_values = false;
}
else if (!in_string)
{
if (s[i]=='\"')
{
in_string = true;
i++;
}
else if (s[i]=='$') //hexadecimal
{
int temp=0;
i++;
if (!IsHex(s[i])) CompileError("data (hexadecimal)", line);
while (IsHex(s[i]))
temp = temp*16+HexValue(s[i++]);
ints[numints++] = temp;
between_values = true;
}
else if (s[i]=='%') //binary
{
int temp=0;
i++;
if (s[i]!='0' && s[i]!='1') CompileError("data (binary)", line);
while (s[i]=='0' || s[i]=='1')
temp = temp*2 + (s[i]-'0');
ints[numints++] = temp;
between_values = true;
}
else if ('0'<=s[i] && s[i]<='9') //decimal
{
int temp = 0;
while ('0'<=s[i] && s[i]<='9')
temp = temp*10 + s[i++]-'0';
if (temp>255) CompileError("data (value too large)", line);
ints[numints++] = temp;
between_values = true;
}
else between_values = true;
}
else
{
if (s[i]=='\"') //quote
{
in_string = false;
between_values = true;
i++;
}
else if (s[i]=='\\') //backslash
{
ints[numints++] = s[i+1];
i+=2;
}
else ints[numints++] = s[i++];
}
}
//now create the ASM output
int startint = 0;
while (startint < numints)
{
int endint = startint+10;
if (endint > numints) endint = numints;
char outbuf[64];
sprintf(outbuf, " .db ");
for (int i = startint; i < endint; i++)
{
if (i!=startint) sprintf(&outbuf[strlen(outbuf)], ",%i", ints[i]);
else sprintf(&outbuf[strlen(outbuf)], "%i", ints[i]);
}
sprintf(&outbuf[strlen(outbuf)], "\n");
startint += 10;
asmlist.AddToken(ASMTEXT, outbuf, strlen(outbuf), line);
}
return 1;
}
bool NBasic::IsConditional(int token_index)
{
switch(TokenType(token_index))
{
case ISEQUAL:
case NOTEQUAL:
case ISLESS:
case ISGREATER:
case LESSEQ:
case GREATEREQ:
return true;
default:
break;
}
return false;
}
int NBasic::ConditionalOpposite(int cond)
{
switch(cond)
{
case ISEQUAL:
return ISEQUAL;
case NOTEQUAL:
return NOTEQUAL;
case ISLESS:
return ISGREATER;
case ISGREATER:
return ISLESS;
case LESSEQ:
return GREATEREQ;
case GREATEREQ:
return LESSEQ;
}
return -1;
}
int NBasic::CompileConditional(int i, int type, int line)
{
int j = -1; //indicates start of second half of the conditional
int cond = -1; //which conditional
// A cond VAR
// A cond NUM
// X cond math
// Y cond math
if(
(tokens.Match(i+1,X) || tokens.Match(i+1,Y) || tokens.Match(i+1,A)) &&
IsConditional(i+2) &&
IsMath(i+3))
{
int reg = TokenType(i+1); //which register
cond = TokenType(i+2); //which conditional
int type = TokenType(i+3); //which math type
int len = 1;
if (type == VAR)
{
asmlist.AddToken(reg==A?CMPp:(reg==X?CPXp:CPYp), TokenValue(i+3), strlen(TokenValue(i+3)), line);
}
else if (type == NUMBER)
{
char *num = MakeNumber(TokenValue(i+3), line);
asmlist.AddToken(reg==A?CMPi:(reg==X?CPXi:CPYi), num, strlen(num), line);
delete num;
}
else //other math
{
//if(reg==A) CompileError("conditional",line);
len = CompileMath(i+3, type, line);
asmlist.AddToken(STAp,"nbasic_temp",strlen("nbasic_temp"),line);
asmlist.AddToken( reg==X ? CPXp : CPYp, "nbasic_temp", strlen("nbasic_temp"), line);
}
j = len + 3;
}
// VAR cond math (compile math, opposite cond)
// NUM cond math (compile math, opposite cond)
else if((tokens.Match(i+1,VAR) || tokens.Match(i+1,NUMBER)) && IsConditional(i+2) && IsMath(i+3))
{
cond = ConditionalOpposite(TokenType(i+2));
j = CompileMath(i+3, TokenType(i+3), line) + 3;
if (TokenType(i+1) == VAR)
asmlist.AddToken(CMPp, TokenValue(i+1), strlen(TokenValue(i+1)), line);
else
{
char *num = MakeNumber(TokenValue(i+1), line);
asmlist.AddToken(CMPi, num, strlen(num), line);
delete num;
}
}
// math cond VAR (same cond)
// math cond NUM (same cond)
// math cond math
else if (IsConditional(i+1+MathLength(i+1,line)) && IsMath(i+2+MathLength(i+1,line)))
{
int len = CompileMath(i+1, TokenType(i+1), line);
cond = TokenType(i+1+len);
if (TokenType(i+2+len) == VAR)
{
asmlist.AddToken(CMPp, TokenValue(i+2+len), strlen(TokenValue(i+2+len)), line);
j = len + 3;
}
else if (TokenType(i+2+len) == NUMBER)
{
char *num = MakeNumber(TokenValue(i+2+len), line);
asmlist.AddToken(CMPi, num, strlen(num), line);
delete num;
j = len + 3;
}
else
{
asmlist.AddToken(STAp, "nbasic_temp", strlen("nbasic_temp"), line);
int two = CompileMath(i+2+len, TokenType(i+2+len), line);
cond = ConditionalOpposite(cond);
j = len + 2 + two;
}
}
if (j==-1) CompileError("conditional", line);
if (tokens.Match(i+j, BRANCHTO, VAR))
{
const char* label = TokenValue(i+j+1);
int len = strlen(label);
switch(cond) // branch on same case
{
case ISEQUAL: asmlist.AddToken(BEQ, label, len, line); break;
case NOTEQUAL: asmlist.AddToken(BNE, label, len, line); break;
case ISLESS: asmlist.AddToken(BMI, label, len, line); break;
case ISGREATER: asmlist.AddToken(BCS, label, len, line); break;
case LESSEQ: asmlist.AddToken(BCC, label, len, line); break;
case GREATEREQ: asmlist.AddToken(BPL, label, len, line); break;
default: CompileError("conditional branchto",line); break;
}
j+=2;
}
else