-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBC.cpp
3124 lines (2833 loc) · 69.4 KB
/
DBC.cpp
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
// DBC.cpp: DBC/MF DragonBASIC compiler
//
// Copyright (C) 2015 Ulrich Hecht
// Source code reconstructed with permission from DBC/MF executable version
// 1.4.5, Copyright (C) Jeff Massung
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Ulrich Hecht
#ifdef __WIN32__
#include <windows.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "DBC.h"
#include "os.h"
char dir_stack[200][256];
char err_buf[256];
int dir_stack_idx;
int my_errno;
bool option_multiboot;
bool option_debug;
bool option_optimize;
bool option_mod;
bool option_sym;
void GLB_error(enum error_t error_code, ...);
void GLB_failWithMbox(const char *msg, const char *func, int line);
void GLB_pushDir(const char *lpPathName);
void GLB_popDir();
Subroutine::Subroutine(const char *ident, bool is_function,
enum vartype_t rtype)
{
strcpy(this->ident, ident);
this->is_function = is_function;
ret_vtype = rtype;
num_args = 0;
num_locals = 0;
next = 0;
can_be_naked = true;
tin_start = NULL;
is_inline = false;
has_regvar = false;
}
Subroutine::~Subroutine()
{
Subroutine *sub;
if (next) {
sub = next;
if (sub)
delete sub;
}
}
Subroutine *Subroutine::findByIdent(const char *ident)
{
Subroutine *result;
if (strcasecmp(ident, this->ident)) {
if (next)
result = next->findByIdent(ident);
else
result = NULL;
} else
result = this;
return result;
}
void Subroutine::addArgument(const char *name, enum vartype_t vtype)
{
int idx;
idx = num_args;
++num_args;
if (num_args > 15)
GLB_error(ERR_TOO_MANY_PROTO_ARGS, name);
arg_vtype[idx] = vtype;
if (name)
strcpy(arg_name[idx], name);
else
arg_name[idx][0] = 0;
}
// This is a copy of Subroutine::addArgument(), with the difference that it
// acts on locals and does not allow anonymous variables.
void Subroutine::addLocal(const char *name, enum vartype_t vtype)
{
int idx;
idx = num_locals;
++num_locals;
if (num_locals > 31)
GLB_error(ERR_TOO_MANY_PROTO_ARGS, name);
local_vtype[idx] = vtype;
if (name)
strcpy(local_name[idx], name);
else
GLB_error(ERR_SYNTAX);
}
TIN *Subroutine::declareTinLocals(TIN *tin)
{
char buf[384];
int argc;
// MF assigns addresses incrementally from 0. The arguments are
// already on the stack and must therefore have higher addresses
// than the BASIC local variables, so the latter must be declared
// first.
argc = num_locals;
while (true) {
--argc;
if (argc < 0)
break;
sprintf(buf, "LOCAL: %s::%s\n", ident, local_name[argc]);
tin = tin->addCode(buf);
}
argc = num_args;
while (true) {
--argc;
if (argc < 0)
break;
sprintf(buf, "LOCAL: %s::%s\n", ident, arg_name[argc]);
tin = tin->addCode(buf);
}
return tin;
}
static const char *error_strings[] = {
[ERR_NONE] = "",
[ERR_WOT] = "%s?",
[ERR_UNKNOWN_CFLAG] = "Unknown compiler flag? %s",
[ERR_NO_SOURCE] = "No source file to compile?",
[ERR_OPEN_FAIL] = "Failed to open file? %s",
[ERR_WRITE_OBJ] = "Failed to write object file? %s",
[ERR_UNK_DIR] = "Unknown directive? '%s'",
[ERR_UNK_TOK] = "Unknown token? %c",
[ERR_SYNTAX] = "Syntax error?",
[ERR_NOTOKEN] = "Expected token? %s",
[ERR_TOO_MANY_PROTO_ARGS] = "Too many arguments in prototype? %s",
[ERR_DUP_IDENT] = "Duplicate identifier? %s",
[ERR_UNK_IDENT] = "Unknown identifier? %s",
[ERR_NEED_LVAL] = "Expected L-value? %s",
[ERR_NEED_RVAL] = "Expected R-value? %s",
[ERR_TOO_FEW_ARGS] = "Too few arguments? %s",
[ERR_TOO_MANY_ARGS] = "Too many arguments? %s",
[ERR_STACK_OVER] = "Stack overflow? %s",
[ERR_STACK_UNDER] = "Stack underflow? %s",
[ERR_UNTERM_CONTROL] = "Unterminated control on line %d?",
[ERR_TERM_MISMATCH] = "Terminal does not match control on line %d? %s",
[ERR_EXPR] = "Error in expression?",
[ERR_SEGMENT] = "Wrong segment? %s",
[ERR_NOT_ARRAY] = "Variable is not an array? %s",
[ERR_ARRAY] = "Variable is an array? %s",
[ERR_TYPE_MISMATCH] = "Type mis-match?",
[ERR_TYPED_SUB] = "Subroutines cannot have types? %s",
[ERR_ILLEGAL_OP] = "Operation not supported for this type?",
[ERR_RETVAL] = "Return value expected",
[ERR_UNREACHABLE] = "Unreachable code?",
[ERR_WITHOUT] = "%s without %s?",
[ERR_NOT_FOUND] = "%s not found?",
[ERR_SET_FOLDER] = "Failed to set folder? %s",
[ERR_LAUNCH] = "%s failed to launch?",
};
static Compiler *gCompiler = 0;
void GLB_error(enum error_t error_code, ...)
{
va_list va;
va_start(va, error_code);
my_errno = error_code;
vsprintf(err_buf, error_strings[error_code], va);
GLB_failWithMbox(err_buf, gCompiler->parser->filename,
gCompiler->line_no);
}
void GLB_error(const char *msg, ...)
{
va_list va;
va_start(va, msg);
my_errno = -1;
vsprintf(err_buf, msg, va);
GLB_failWithMbox(err_buf, gCompiler->parser->filename,
gCompiler->line_no);
}
void GLB_failWithMbox(const char *msg, const char *func, int line)
{
char Text[256];
char line_str[168];
memset(line_str, 0, 168);
if (line > 0)
sprintf(line_str, " line %d", line);
if (func)
sprintf(Text, "%s%s: %s", func, line_str, msg);
else
sprintf(Text, "%s", msg);
#ifdef __WIN32__
MessageBoxA(0, Text, "Dragon BASIC", 0x10u);
#else
fprintf(stderr, "Error: %s\n", Text);
#endif
exit(-my_errno);
}
void GLB_exitWithMbox(const char *msg, ...)
{
char Text[256];
va_list va;
va_start(va, msg);
vsprintf(Text, msg, va);
#ifdef __WIN32__
MessageBoxA(0, Text, "Dragon BASIC", 0x40u);
#else
fprintf(stderr, "%s\n", Text);
#endif
exit(0);
}
void GLB_getAppDir(char *lpFilename)
{
OS_getAppDir(lpFilename);
}
void GLB_checkFileInAppdir(const char *filename)
{
char appdir[256];
char buf[512];
FILE *fp;
GLB_getAppDir(appdir);
sprintf(buf, "%s" PATHSEP "%s", appdir, filename);
fp = fopen(buf, "r");
if (!fp)
GLB_error(ERR_NOT_FOUND, filename);
fclose(fp);
}
void GLB_runProgram(const char *lpApplicationName, const char *args,
bool show_window)
{
char Dirname[256];
char CommandLine[1024];
#ifdef __WIN32__
struct _STARTUPINFOA StartupInfo;
struct _PROCESS_INFORMATION ProcessInformation;
#endif
GLB_checkFileInAppdir(lpApplicationName);
GLB_getAppDir(Dirname);
sprintf(CommandLine, "\"%s" PATHSEP "%s\" %s", Dirname,
lpApplicationName, args);
#ifdef __WIN32__
/* I do not know why Win32 needs this pushDir(); the binary and all file
* arguments are given as absolute paths, but CreateProcessA() still fails
* unless we change into the directory containing mf.exe first... */
GLB_pushDir(Dirname);
memset(&StartupInfo, 0, 68u);
StartupInfo.cb = 68;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = show_window ? SW_SHOW : 0;
if (!CreateProcessA(
lpApplicationName,
CommandLine,
0,
0,
false,
NORMAL_PRIORITY_CLASS,
0,
dir_stack[dir_stack_idx - 1],
&StartupInfo,
&ProcessInformation))
GLB_error(ERR_LAUNCH, lpApplicationName);
WaitForSingleObject(ProcessInformation.hProcess, 0xFFFFFFFFu);
CloseHandle(ProcessInformation.hProcess);
CloseHandle(ProcessInformation.hThread);
GLB_popDir();
#else
if (system(CommandLine))
GLB_error(ERR_LAUNCH, lpApplicationName);
#endif
}
void GLB_runProgramWithArgs(const char *lpApplicationName, const char *options,
const char *lpFileName, const char *outfile,
bool del_output)
{
char buf[256];
char args[780];
getcwd(buf, 256);
sprintf(args, "%s\"%s" PATHSEP "%s\" \"%s" PATHSEP "%s\"", options, buf,
lpFileName, buf, outfile);
GLB_runProgram(lpApplicationName, args, !del_output);
if (del_output)
unlink(lpFileName);
}
void GLB_pushDir(const char *lpPathName)
{
char *dir_buf;
dir_buf = dir_stack[dir_stack_idx++];
getcwd(dir_buf, 256);
if (chdir(lpPathName))
GLB_error(ERR_SET_FOLDER, lpPathName);
}
void GLB_popDir()
{
--dir_stack_idx;
if (dir_stack_idx < 0)
GLB_error(ERR_STACK_UNDER, "#INCLUDE");
if (chdir(dir_stack[dir_stack_idx]))
GLB_error(ERR_SET_FOLDER, dir_stack[dir_stack_idx]);
}
Compiler::Compiler()
{
TIN *tin;
tin = new TIN(NULL);
tin_head = tin;
tin_tail = tin_head;
sub_head = NULL;
var_head = NULL;
sub_args_head = NULL;
sub_locals_head = NULL;
parser = NULL;
cur_seg = SEG_TOP;
}
Compiler::~Compiler()
{
if (sub_head)
delete sub_head;
if (var_head)
delete var_head;
if (tin_head)
delete tin_head;
if (parser)
delete parser;
}
void Compiler::parseFile(const char *filename)
{
line_no = 1;
if (parser)
parser = new Parser(filename, parser->symbol_head);
else
parser = new Parser(filename, NULL);
emitTin("#FILE\" %s\" ", filename);
parser->parseAll();
}
int Compiler::writeOutput(const char *filename)
{
FILE *fp;
line_no = 1;
fp = fopen(filename, "w+t");
if (!fp)
GLB_error(ERR_WRITE_OBJ, filename);
tin_head->writeCodeToFile(fp);
return fclose(fp);
}
void Compiler::emitTin(const char *fmt, ...)
{
TIN *tail;
char buf[256];
va_list va;
va_start(va, fmt);
vsprintf(buf, fmt, va);
tail = tin_tail->addCode(buf);
tin_tail = tail;
}
void Compiler::doSubroutine(BasicObject *bobj, bool is_function, bool emit_code)
{
bool iwram = false;
if (bobj->otype == OBJ_CMD && bobj->val.numeric == CMD_IWRAM) {
iwram = true;
bobj = parser->consumeNextBasicObj();
}
if (bobj->otype && bobj->otype != OBJ_SUB)
GLB_error(ERR_NOTOKEN, "Identifier");
if (!is_function && bobj->vtype)
GLB_error(ERR_TYPED_SUB, bobj->val.symbolic);
addNewSub(bobj->val.symbolic, is_function, bobj->vtype);
if (bobj->otype == OBJ_SUB)
doArgs(emit_code);
// Manually check if there are locals declared at the start of the
// function. We need to do this here because the local declarations
// have to be emitted before the actual function body.
while (parser->checkNextBasicObjType(OBJ_EOL)) {
// Consume any additional EOLs. This is necessary so the
// compiler won't stumble over empty lines or comments before
// the local declarations.
while (parser->retrieveNextBasicObject()->next->otype == OBJ_EOL) {
parser->consumeNextBasicObj();
}
BasicObject *loc = parser->retrieveNextBasicObject()->next;
assert(loc);
if (loc->otype == OBJ_CMD && loc->val.numeric == CMD_LOCAL) {
parser->consumeNextBasicObj(); // EOL
parser->consumeNextBasicObj(); // LOCAL
compileBasicObject(loc);
} else
break;
}
if (emit_code) {
tin_tail = sub_head->declareTinLocals(tin_tail);
emitTin(": %s%s ", iwram ? "IWRAM " : "", bobj->val.symbolic);
sub_head->tin_start = tin_tail;
if (sub_head->num_args > 0 || sub_head->num_locals > 0) {
// Code the local variable prolog. Arguments
// are assumed to have been placed on the stack
// already, so we only have to reserve space for
// local variables.
emitTin("%d LPROLOG ", 4 * sub_head->num_locals);
}
}
}
void Compiler::doArgs(bool emit_code)
{
BasicObject *bobj;
if (!parser->requireRop(ROP_CPAREN)) {
do {
bobj =
parser->getObjectWithType(OBJ_IDENT,
"Identifier");
sub_head->addArgument(bobj->val.symbolic, bobj->vtype);
if (emit_code)
addSubArgument(bobj->val.symbolic, bobj->vtype);
} while (parser->requireRop(ROP_COMMA));
if (!parser->requireRop(ROP_CPAREN))
GLB_error(ERR_NOTOKEN, ")");
}
}
void Compiler::addSubArgument(const char *ident, enum vartype_t vtype)
{
if (sub_locals_head->findByIdent(ident) ||
sub_args_head->findByIdent(ident))
GLB_error(ERR_DUP_IDENT, ident);
sub_args_head = sub_args_head->prependNew(ident, vtype);
}
void Compiler::addSubLocal(const char *ident, enum vartype_t vtype)
{
if (sub_locals_head->findByIdent(ident) ||
sub_args_head->findByIdent(ident))
GLB_error(ERR_DUP_IDENT, ident);
sub_locals_head = sub_locals_head->prependNew(ident, vtype);
}
void Compiler::addVariable(const char *ident, enum vartype_t vtype)
{
if (var_head->findByIdent(ident))
GLB_error(ERR_DUP_IDENT, ident);
var_head = var_head->prependNew(ident, vtype);
}
void Compiler::addNewSub(const char *ident, bool is_function,
enum vartype_t vtype)
{
Subroutine *sub;
sub = new Subroutine(ident, is_function, vtype);
if (sub_head) {
if (sub_head->findByIdent(ident))
GLB_error(ERR_DUP_IDENT, ident);
sub->next = sub_head;
}
sub_head = sub;
if (sub_args_head) {
delete sub_args_head;
sub_args_head = 0;
}
if (sub_locals_head) {
delete sub_locals_head;
sub_locals_head = 0;
}
}
void Compiler::checkNotSegment(int seg_mask, const char *human_seg)
{
if ((seg_mask & cur_seg) > 0)
GLB_error(ERR_SEGMENT, human_seg);
}
void Compiler::compile()
{
BasicObject *bobj;
restart:
while (true) {
bobj = parser->consumeNextBasicObj();
if (!bobj)
break;
line_no = bobj->line_no;
if (bobj->otype != OBJ_EOL) {
if (bobj->otype != OBJ_EOT) {
while (true) {
emitTin("\n#LINE %4d ", bobj->line_no);
compileBasicObject(bobj);
if (parser->checkNextBasicObjType(
OBJ_EOT))
break;
if (!parser->requireRop(ROP_COLON)) {
parser->getObjectWithType(
OBJ_EOL, "EOL");
goto restart;
}
bobj = parser->consumeNextBasicObj();
}
}
break;
}
}
loop_stack.checkEmpty();
}
void Compiler::compileBasicObject(BasicObject *bobj)
{
switch (bobj->otype) {
case OBJ_EOL:
return;
case OBJ_DIR:
doDirective(bobj);
break;
case OBJ_CMD:
doCommand(bobj);
break;
case OBJ_IDENT:
doLval(bobj);
break;
case OBJ_ARR:
doLvalNotSub(bobj, true);
break;
default:
GLB_error(ERR_SYNTAX);
return;
}
}
void Compiler::doDirective(BasicObject *bobj)
{
checkNotSegment(SEG_INTR | SEG_SUB, "# Directive");
switch (bobj->val.numeric) {
case DIR_REGISTER:
doDirRegister();
break;
case DIR_TITLE:
doDirTitle();
break;
case DIR_REQUIRES:
doDirRequires();
break;
case DIR_INCLUDE:
doDirInclude();
break;
case DIR_IMPORT:
doDirImport();
break;
case DIR_BITMAP:
doDirBitmap();
break;
case DIR_MAP:
doDirMap();
break;
case DIR_PALETTE:
doDirPalette();
break;
case DIR_SOUND:
doDirSound();
break;
case DIR_MUSIC:
doDirMusic();
break;
case DIR_CONSTANT:
doDirConstant();
break;
default:
GLB_error(ERR_SYNTAX);
return;
}
}
void Compiler::doDirRegister()
{
parser->getObjectWithType(OBJ_STR, "Registration code");
}
void Compiler::doDirTitle()
{
BasicObject *bobj;
bobj = parser->getObjectWithType(OBJ_STR, "\"title\"");
emitTin("TITLE\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirRequires()
{
BasicObject *bobj;
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
emitTin("REQUIRES\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirInclude()
{
char *lpPathName;
BasicObject *bobj;
Parser *parser_saved;
char *basename;
const char *filename;
int line_no_saved;
bool change_dir;
char appdir_path[256];
if (parser->checkNextBasicObjType(OBJ_PATH))
bobj = parser->getObjectWithType(OBJ_PATH, "<include file name>");
else
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
line_no_saved = line_no;
parser_saved = parser;
lpPathName = bobj->val.symbolic;
for (basename = bobj->val.symbolic + strlen(bobj->val.symbolic) - 1;
*basename != '\\' && *basename != '/' && basename >= lpPathName;
--basename)
;
change_dir = basename >= lpPathName;
if (bobj->otype == OBJ_PATH) {
GLB_getAppDir(appdir_path);
strcat(appdir_path, PATHSEP "include" PATHSEP);
if (change_dir) {
*basename = 0;
strcat(appdir_path, lpPathName);
filename = basename + 1;
} else
filename = lpPathName;
lpPathName = appdir_path;
change_dir = true;
} else if (!change_dir) {
filename = bobj->val.symbolic;
} else {
*basename = 0;
filename = basename + 1;
if (!strncasecmp(lpPathName, "c:\\db\\", 6) ||
!strncasecmp(lpPathName, "$INC", 4)) {
GLB_getAppDir(appdir_path);
strcat(appdir_path, PATHSEP);
if (lpPathName[0] == '$') {
strcat(appdir_path, "include" PATHSEP);
strcat(appdir_path, lpPathName + 4);
} else
strcat(appdir_path, lpPathName + 6);
lpPathName = appdir_path;
}
}
if (change_dir)
GLB_pushDir(lpPathName);
parseFile(filename);
compile();
parser_saved->symbol_head = parser->symbol_head;
if (parser)
delete parser;
line_no = line_no_saved;
parser = parser_saved;
if (change_dir)
GLB_popDir();
emitTin("#FILE\" %s\" ", parser->filename);
}
void Compiler::doDirImport()
{
BasicObject *bobj;
checkNotSegment(SEG_INTR | SEG_SUB, "#IMPORT");
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
emitTin("IMPORT\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirBitmap()
{
BasicObject *bobj;
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
checkNotSegment(SEG_INTR | SEG_SUB, "#BITMAP");
emitTin("BITMAP\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirMap()
{
BasicObject *bobj;
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
checkNotSegment(SEG_INTR | SEG_SUB, "#MAP");
emitTin("MAP\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirPalette()
{
BasicObject *bobj;
checkNotSegment(SEG_INTR | SEG_SUB, "#PALETTE");
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
emitTin("PALETTE\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirSound()
{
BasicObject *bobj;
checkNotSegment(SEG_INTR | SEG_SUB, "#SOUND");
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
emitTin("SOUND\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirMusic()
{
BasicObject *bobj;
option_mod = true;
checkNotSegment(SEG_INTR | SEG_SUB, "#MUSIC");
bobj = parser->getObjectWithType(OBJ_STR, "\"filename\"");
emitTin("MUSIC\" %s\" ", bobj->val.symbolic);
}
void Compiler::doDirConstant()
{
BasicObject *i;
BasicObject *bobj;
bobj = parser->getObjectWithType(OBJ_IDENT, "Identifier");
checkNotSegment(SEG_INTR | SEG_SUB, "#SOUND"); // XXX: #SOUND?
if ((sub_head && sub_head->findByIdent(bobj->val.symbolic))
|| var_head->findByIdent(bobj->val.symbolic))
GLB_error(ERR_DUP_IDENT, bobj->val.symbolic);
parser->addNewSymbol(bobj->val.symbolic);
for (i = parser->retrieveNextBasicObject();
i->otype != OBJ_EOL && i->otype != OBJ_EOT;
i = parser->retrieveNextBasicObject())
parser->appendNextBasicObjCopyToSymbolList();
}
void Compiler::doCommand(BasicObject *bobj)
{
switch (bobj->val.numeric) {
case CMD_PROTOTYPE:
doCmdPrototype();
break;
case CMD_WHILE:
doCmdWhile();
break;
case CMD_LOOP:
doCmdLoop();
break;
case CMD_REPEAT:
doCmdRepeat();
break;
case CMD_UNTIL:
doCmdUntil();
break;
case CMD_FOR:
doCmdFor();
break;
case CMD_NEXT:
doCmdNext();
break;
case CMD_DIM:
doCmdDim();
break;
case CMD_LOCAL:
doCmdLocal();
break;
case CMD_RETURN:
doCmdReturn(false);
break;
case CMD_MAP:
doCmdMap();
break;
case CMD_DATA:
doCmdData(4);
break;
case CMD_DATAB:
doCmdData(1);
break;
case CMD_DATAH:
doCmdData(2);
break;
case CMD_READ:
doCmdRead(4);
break;
case CMD_READB:
doCmdRead(1);
break;
case CMD_READH:
doCmdRead(2);
break;
case CMD_RESTORE:
doCmdRestore();
break;
case CMD_FUNCTION:
doCmdFunction();
break;
case CMD_SUB:
doCmdSub();
break;
case CMD_IF:
doCmdIf();
break;
case CMD_ELSE:
doCmdElse();
break;
case CMD_SELECT:
doCmdSelect();
break;
case CMD_CASE:
doCmdCase();
break;
case CMD_DEFAULT:
doCmdDefault();
break;
case CMD_END:
doCmdEnd();
break;
case CMD_RESET:
doCmdReset();
break;
case CMD_INTERRUPT:
doCmdInterrupt();
break;
case CMD_EXIT:
doCmdExit();
break;
case CMD_INC:
doCmdInc();
break;
case CMD_DEC:
doCmdDec();
break;
case CMD_GOTO:
doCmdGoto();
break;
case CMD_SWI:
doCmdSwi();
break;
default:
GLB_error(ERR_SYNTAX);
return;
}
}
void Compiler::doCmdPrototype()
{
bool is_function;
bool is_inline = false;
BasicObject *next_bobj;
BasicObject *bobj;
bobj = parser->getObjectWithType(OBJ_CMD, "SUB or FUNCTION");
if (bobj->val.numeric == CMD_INLINE) {
is_inline = true;
bobj = parser->getObjectWithType(OBJ_CMD, "SUB or FUNCTION");
}
checkNotSegment(SEG_INTR | SEG_SUB, "PROTOTYPE");
if (bobj->val.numeric != CMD_SUB && bobj->val.numeric != CMD_FUNCTION)
GLB_error(ERR_NOTOKEN, "SUB or FUNCTION");
is_function = bobj->val.numeric == CMD_FUNCTION;
next_bobj = parser->consumeNextBasicObj();
doSubroutine(next_bobj, is_function, false);
sub_head->is_inline = is_inline;
}
void Compiler::doCmdWhile()
{
BasicObject *bobj;
bobj = parser->retrieveNextBasicObject();
checkNotSegment(SEG_TOP, "WHILE");
emitTin("BEGIN ");
if (bobj->otype == OBJ_EOL) {
if (!loop_stack.addEntry(CMD_LOOP, line_no))
GLB_error(ERR_STACK_OVER, "WHILE");
} else {
if (compileExpression())
GLB_error(ERR_TYPE_MISMATCH);
emitTin("WHILE ");
loop_stack.addEntry(CMD_WHILE, line_no);
}
}
void Compiler::doCmdLoop()
{
int line;
enum cmd_t cmd;
checkNotSegment(SEG_TOP, "LOOP");
if (!loop_stack.popEntry(&cmd, &line))
GLB_error(ERR_WITHOUT, "LOOP", "WHILE");
if (cmd == CMD_WHILE) {
emitTin("REPEAT ");
} else {
if (cmd != CMD_LOOP)
GLB_error(ERR_TERM_MISMATCH, line, "LOOP");
emitTin("AGAIN ");
is_top_level = true;
}
}
void Compiler::doCmdRepeat()
{
checkNotSegment(SEG_TOP, "REPEAT");
if (!loop_stack.addEntry(CMD_REPEAT, line_no))
GLB_error(ERR_STACK_OVER, "REPEAT");
emitTin("BEGIN ");
}
void Compiler::doCmdUntil()
{
int line;
enum cmd_t cmd;
checkNotSegment(SEG_TOP, "UNTIL");
if (!loop_stack.popEntry(&cmd, &line))
GLB_error(ERR_WITHOUT, "UNTIL", "REPEAT");
if (cmd != CMD_REPEAT)
GLB_error(ERR_TERM_MISMATCH, line, "UNTIL");
if (compileExpression())
GLB_error(ERR_TYPE_MISMATCH);
emitTin("UNTIL ");
}
void Compiler::doCmdFor()
{
char buf[12];
int direction;
BasicObject *bobj;
checkNotSegment(SEG_TOP, "FOR");
if (!loop_stack.addEntry(CMD_FOR, line_no))
GLB_error(ERR_STACK_OVER, "FOR");
bobj = parser->consumeNextBasicObj();
if (bobj->vtype != VAR_SCALAR)
GLB_error(ERR_TYPE_MISMATCH);
doAssign(bobj);