forked from batari-Basic/batari-Basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2600bas.c
More file actions
266 lines (243 loc) · 7.33 KB
/
2600bas.c
File metadata and controls
266 lines (243 loc) · 7.33 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
// Provided under the GPL v2 license. See the included LICENSE.txt for details.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "statements.h"
#include "keywords.h"
#include <math.h>
#define BB_VERSION_INFO "batari Basic v1.9 (c)2025\n"
extern int bank;
extern int bs;
extern int isPXE;
extern int numconstants;
extern int playfield_index[];
extern int line;
int main(int argc, char *argv[])
{
char **statement;
int i, j, k;
int unnamed = 0;
int defcount = 0;
char *c;
char single;
char code[500];
char displaycode[500];
FILE *header = NULL;
int multiplespace = 0;
char *includes_file = "default.inc";
char *filename = "2600basic_variable_redefs.h";
char *path = 0;
char def[500][100];
char defr[500][100];
char finalcode[500];
char *codeadd;
char mycode[500];
int defi = 0;
// get command line arguments
while ((i = getopt(argc, argv, "i:r:v")) != -1)
{
switch (i)
{
case 'i':
path = (char *) malloc(500);
path = optarg;
break;
case 'r':
filename = (char *) malloc(100);
//strcpy(filename, optarg);
filename = optarg;
break;
case 'v':
printf("%s", BB_VERSION_INFO);
exit(0);
case '?':
fprintf(stderr, "usage: %s -r <variable redefs file> -i <includes path>\n", argv[0]);
exit(1);
}
}
fprintf(stderr, BB_VERSION_INFO);
printf("game\n"); // label for start of game
header_open(header);
init_includes(path);
playfield_index[0]=0;
statement = (char **) malloc(sizeof(char *) * 200);
for (i = 0; i < 200; ++i)
{
statement[i] = (char *) malloc(sizeof(char) * 200);
}
while (1)
{ // clear out statement cache
for (i = 0; i < 200; ++i)
{
for (j = 0; j < 200; ++j)
{
statement[i][j] = '\0';
}
}
c = fgets(code, 500, stdin); // get next line from input
incline();
strcpy(displaycode, code);
// look for defines and remember them
strcpy(mycode, code);
int k_def_search; // Use a different loop variable to avoid conflict with outer 'i'
for (k_def_search = 0; k_def_search < 495; ++k_def_search)
if (code[k_def_search] == ' ')
break;
if (k_def_search < 495 && code[k_def_search] == ' ' && /* Ensure space was found */
(k_def_search + 4 < 499) && /* Bounds check for code access */
code[k_def_search + 1] == 'd' && code[k_def_search + 2] == 'e' &&
code[k_def_search + 3] == 'f' && code[k_def_search + 4] == ' ')
{ // found a define
int current_pos = k_def_search + 5; // current_pos now points to start of define name.
if (defi >= 499) { // Max 500 defines (0-499)
fprintf(stderr, "(%d) ERROR: Maximum number of defines (500) reached.\n", bbgetline());
exit(1);
}
for (j = 0; current_pos < 499 && code[current_pos] != ' ' && code[current_pos] != '\0' && code[current_pos] != '\n' && code[current_pos] != '\r'; current_pos++)
{
if (j >= 99) {
fprintf(stderr, "(%d) ERROR: Define name too long (max 99 chars).\n", bbgetline());
exit(1);
}
def[defi][j++] = code[current_pos];
}
def[defi][j] = '\0';
if (j == 0) { // Empty define name
fprintf(stderr, "(%d) ERROR: Malformed define statement. Empty define name.\n", bbgetline());
exit(1);
}
// Expect " = " sequence after define name
if (!(current_pos < 497 && code[current_pos] == ' ' && code[current_pos+1] == '=' && code[current_pos+2] == ' ')) {
fprintf(stderr, "(%d) ERROR: Malformed define statement. Expected \" = \" after define name '%s'.\n", bbgetline(), def[defi]);
exit(1);
}
current_pos += 3; // Skip " = "
for (j = 0; current_pos < 499 && code[current_pos] != '\0' && code[current_pos] != '\n' && code[current_pos] != '\r'; current_pos++)
{
if (j >= 99) {
fprintf(stderr, "(%d) ERROR: Define replacement string too long (max 99 chars) for define '%s'.\n", bbgetline(), def[defi]);
exit(1);
}
defr[defi][j++] = code[current_pos];
}
defr[defi][j] = '\0';
removeCR(defr[defi]);
printf (";PARSED_DEFINE: .%s. = .%s.\n", def[defi], defr[defi]); // Clarified debug print
defi++;
}
else if (defi) // This 'i' refers to the outer loop variable for iterating through existing defines
{
int def_idx;
for (def_idx = 0; def_idx < defi; ++def_idx) // Use new loop var def_idx
{
codeadd = NULL;
finalcode[0] = '\0';
defcount = 0;
while (1)
{
if (defcount++ > 500)
{
fprintf(stderr, "(%d) Infinitely repeating definition or too many instances of a definition\n",
bbgetline());
exit(1);
}
codeadd = strstr (mycode, def[def_idx]);
if (codeadd == NULL)
break;
for (j = 0; j < 500; ++j)
finalcode[j] = '\0';
strncpy(finalcode, mycode, strlen(mycode) - strlen(codeadd));
strcat (finalcode, defr[def_idx]);
strcat (finalcode, codeadd + strlen (def[def_idx]));
strcpy(mycode, finalcode);
}
}
}
if (strcmp(mycode, code))
strcpy(code, mycode);
if (!c)
break; //end of file
// preprocessing removed in favor of a simplistic lex-based preprocessor
i = 0;
j = 0;
k = 0;
// look for spaces, reject multiples
while (code[i] != '\0')
{
single = code[i++];
if (single == ' ')
{
if (!multiplespace)
{
j++;
k = 0;
}
multiplespace++;
}
else
{
multiplespace = 0;
if (k < 199) // avoid overrun with long horizontal separators
statement[j][k++] = single;
}
}
if (j > 190)
{
fprintf(stderr, "(%d) Warning: long line\n", bbgetline());
}
if (statement[0][0] == '\0')
{
sprintf(statement[0], "L0%d", unnamed++);
}
else
{
if (strchr(statement[0], '.') != NULL)
{
fprintf(stderr, "(%d) Invalid character in label.\n", bbgetline());
exit(1);
}
}
if (strncmp(statement[0], "end\0", 3))
printf (".%s ;;line %d;; %s\n", statement[0], line, displaycode);
else
doend();
keywords(statement);
if(numconstants==(MAXCONSTANTS-1))
{
fprintf(stderr, "(%d) Maximum number of constants exceeded.\n", bbgetline());
exit(1);
}
}
bank = bbank();
bs = bbs();
barf_sprite_data();
printf(" if ECHOFIRST\n");
if (bs == 28){
if(isPXE)
printf(" echo \" \",[(end_of_address_space - *)]d , \"bytes of ROM space left");
else
printf(" echo \" \",[(DPC_graphics_end - *)]d , \"bytes of ROM space left");
} else
printf(" echo \" \",[(scoretable - *)]d , \"bytes of ROM space left");
if (bs == 8)
printf(" in bank 2");
if (bs == 16)
printf(" in bank 4");
if ((bs == 28) && !isPXE)
printf(" in graphics bank");
if (bs == 32)
printf(" in bank 8");
if (bs == 64)
printf(" in bank 16");
printf("\")\n");
printf(" endif \n");
printf("ECHOFIRST = 1\n");
printf(" \n");
printf(" \n");
printf(" \n");
header_write(header, filename);
create_includes(includes_file);
fprintf(stderr, "2600 Basic compilation complete.\n");
return 0;
}