-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
324 lines (282 loc) · 8.14 KB
/
index.mjs
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
/**
* @typedef { import(".").ParseOptions } ParseOptions
* @typedef { import(".").Statement } Statement
* @typedef { import(".").ParseLineResult } ParseLineResult
*/
import { asTap, plus3DOSHeader } from './headers.mjs';
import {
parseLines,
validate,
parseLineWithData,
statementsToBytes,
} from './txt2bas/index.mjs';
import * as transform from './txt2bas/transform.mjs';
import { tap2txt, bas2txt, bas2txtLines } from './bas2txt.mjs';
import * as parser from './parser-version.mjs';
export { plus3DOSHeader, tapHeader } from './headers.mjs';
export { default as codes } from './codes.mjs';
export { renumber, shift } from './renumber.mjs';
import pkg from './package.json' assert { type: 'json' };
export const version = pkg.version;
export const line2bas = parseLineWithData;
export const line2txt = bas2txtLines;
/**
* Reformats string into visually valid NextBASIC
*
* @param {string} line
* @param {number|null} [autoline=null] Whether to use autoline, must be > 0 if a number is passed
* @returns {string}
*/
export const formatText = (line, autoline = null) => {
let lineNumber = '';
if (autoline === true) {
// test if the line actually has a line number in it
const match = line.match(/^(\d+)\s+(.*)$/); //?
if (match && match.length === 3) {
autoline = match[1];
lineNumber = autoline;
line = match[2];
}
}
if (line.includes('\n')) {
return line
.split('\n')
.map((line) => formatText(line, autoline))
.join('\n');
}
if (line.startsWith('#') || line.trim() === '') {
// this is a directive or blank line - give it back
return line;
}
const res = parseLineWithData(line, autoline);
let text = bas2txtLines(res.basic);
if (autoline) {
// manually remove the line number
text = text
.split(' ')
.filter((_, i) => {
if (i === 0) {
return false;
}
return true;
})
.join(' ');
if (lineNumber !== '') {
text = lineNumber + ' ' + text;
}
}
return text;
};
/**
* Validates multiple lines of NextBASIC
*
* @param {string|Buffer} text multiline NextBASIC
* @param {object} [debug]
* @param {string} [parserVersion]
* @returns {string[]} Any errors found
*/
export const validateTxt = (text, debug, parserVersion) => {
if (parserVersion) {
parser.setParser(parserVersion);
}
if (typeof text !== 'string') {
text = text.toString('binary');
}
return validate(text, debug);
};
/**
* Get the statement objects for source code
*
* @param {string} source NextBASIC text
* @param {ParseOptions} options
* @returns {Statement[]}
*/
export const statements = (source, options) => {
return parseLines(source, options).statements;
};
/**
* Tokenises source text into statements
*
* @param {string} src
* @param {ParseOptions} options
* @param {boolean} options.stripComments removes all comments from the result
* @param {boolean} options.defines convert #define to inline
* @returns {ParseLineResult}
*/
export const tokens = (
src,
{ stripComments, defines, inlineLoad, ...options }
) => {
if (typeof src !== 'string') {
src = src.toString('binary');
}
let { statements, ...rest } = parseLines(src, options);
if (defines) {
statements = transform.replaceDefines(statements, rest.defines);
}
if (stripComments) {
statements = transform.stripComments(statements);
}
if (inlineLoad) {
statements = transform.inlineLoad(statements);
}
return { statements, ...rest };
};
/**
* Converts plain text to NextBASIC binary
*
* @param {string} src plain text source of NextBASIC
* @param {object} [options]
* @param {string} [options.format=3dos]
* @param {boolean} [options.includeHeader=true]
* @param {boolean} [options.bank=false]
* @param {string} [options.filename='untitled']
* @param {number|null} [options.autostart=0x8000]
* @param {boolean} [options.stripComments=false]
* @param {boolean} [options.validate=false]
* @param {boolean} [options.defines=false]
* @param {boolean} [options.bankOutputDir=process.cwd()] directory to save banks to, if this is empty or false, it doesn't write banks when split
* @param {string} [options.parser] parser version to use
* @returns {Uint8Array}
*/
export const file2bas = (src, options = {}) => {
if (!src.toString) {
throw new Error('Source must be string or string-able');
}
const {
format = '3dos',
binary = false, // used if source has UDGs
includeHeader = true,
bankOutputDir = false,
...parseOptions
} = options;
if (parseOptions.parser) {
parser.setParser(parseOptions.parser);
}
const bank = parseOptions.bank;
let { filename = 'untitled', autostart = 0x8000 } = options;
if (typeof src !== 'string') {
if (binary) {
src = src.toString('binary');
} else {
src = src.toString();
}
}
const directives = {
filename,
autostart,
};
let { statements, ...rest } = tokens(src, parseOptions);
const bytes = statementsToBytes(statements);
const length = bytes.length;
if (rest.autostart && rest.autostart !== 0x8000) {
directives.autostart = rest.autostart;
}
if (rest.filename) {
directives.filename = rest.filename;
filename = rest.filename;
}
if (!includeHeader) {
if (!bank) {
return bytes;
}
const file = new Uint8Array(length + 2);
file[0] = 'B'.charCodeAt(0);
file[1] = 'C'.charCodeAt(0);
file.set(bytes, 2);
return file;
}
if (format === 'tap') {
return asTap(bytes, directives);
}
if (rest.bankSplits.length === 0) {
return generateFile({ bytes, bank, directives });
}
const file = generateFile({ bytes, bank, directives });
if (bankOutputDir) {
rest.bankSplits.forEach((bank) => {
const file = generateFile({
bytes: bank.bytes,
bank: true,
directives: { ...directives, filename: bank.filename },
});
// save the bank as a file
const { join } = require('path');
require('fs').writeFileSync(
join(bankOutputDir, bank.filename),
Buffer.from(file)
);
});
}
// generate the file, but also save the actual banks as files
return file;
};
/**
* Generates a file based on the provided bytes, bank flag, and directives.
*
* @param {object} options - The options object.
* @param {Uint8Array} options.bytes - The bytes to generate the file from.
* @param {boolean} [options.bank] - Indicates if the file is a bank.
* @param {object} options.directives - The directives for the file.
* @returns {Uint8Array} The generated file.
*/
function generateFile({ bytes, bank, directives }) {
// Function implementation goes here
const length = bytes.length;
let fileLength = length + 128;
let offset = 128;
if (bank) {
if (parser.getParser() < parser.v208) {
fileLength = 0x4000 + 128;
directives.hOffset = 0x8000;
} else {
fileLength += 3;
directives.hOffset = 0x005a;
}
directives.hType = 3;
directives.autostart = 0xc000; // unsure why, but autostart doesn't make sense in a BANK
offset = 130;
}
const file = new Uint8Array(fileLength);
file.fill(0x80);
file.set(plus3DOSHeader(file, directives)); // set the header (128)
if (bank) {
file[128] = 'B'.charCodeAt(0);
file[129] = 'C'.charCodeAt(0);
}
try {
file.set(bytes, offset);
} catch (e) {
if (bank) {
throw new Error('Too large for bank');
} else {
throw e;
}
}
if (bank && parser.getParser() >= parser.v208) {
file[fileLength] = 0x80;
}
return file;
}
/**
* Converts byte data to plain text
*
* @param {Uint8Array} src byte data buffer
* @param {object} [options]
* @param {string} [options.format=3dos] format type: "3dos", "tap"
* @param {boolean} [options.includeHeader=true]
* @returns {string}
*/
export const file2txt = (src, options = {}) => {
const { format = '3dos' } = options;
if (!src || !src.length) {
throw new Error('Source must be an array of byte data');
}
if (options.includeHeader === false) {
return bas2txtLines(new Uint8Array(src)) + '\n';
} else if (format === 'tap') {
return tap2txt(new Uint8Array(src)) + '\n';
}
// else format = '3dos'
return bas2txt(new Uint8Array(src)) + '\n';
};