Skip to content

Commit

Permalink
fix: don't renumber when stripping comments
Browse files Browse the repository at this point in the history
Also fixes int parse bug when inside brackets and int keywords
  • Loading branch information
remy committed Mar 15, 2024
1 parent 894698e commit e76cc20
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 47 deletions.
1 change: 1 addition & 0 deletions .nvm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
21
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ Problematically using the library exposes a number of paired functions:
- `file2bas(String: source, Object<String=3dos: format, filename=UNTITLED: String, validate=false: Boolean>): Uint8Array` - results full byte array with correct format header, if `validate` is true, will throw on token errors
- `bas2file(Uint8Array: source, String=3dos: format): String` - formatted BASIC text
- `formatText(String: line): String` - processes the line through `line2bas` then `bas2line` to result the formatted line
- `validateTxt(String: source): Arrary[String]` - parses each line collecting and returning any token errors
- `validateTxt(String: source): Array[String]` - parses each line collecting and returning any token errors
- `plus3DOSHeader` and `tapHeader` - file headers for the appropriate data formats
- `codes` an object lookup from NextBASIC numerical value to text value, ie. `0xf5 = 'PRINT'`
- `statements(String: source): Array[Statement]` - returns the parsed statement which include `lineNumber` and `tokens` for each line.
- `renumber(String: source, Object<start: Number, end: Number, step=10: Number, base=start: Number>)` - renumbers source lines and `GO TO` line number targets.

## Development

- Currently the latest code uses node@21 (due to specific use of syntax)
- To test with another project: `npm link`
- For new features of language or validation changes, ensure a test is provided
- txt2bas also support different NextOS versions, specified through `parser-version`

## Licence

- [MIT](https://rem.mit-license.org/)
2 changes: 1 addition & 1 deletion __tests__/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('strip comments and autoline works', (t) => {

const txt = file2txt(bytes);

t.is(txt, '#autostart 1\n1 PAUSE 0\n', 'matches');
t.is(txt, '#autostart 1\n2 PAUSE 0\n', 'matches');
});

test('formatText', (t) => {
Expand Down
4 changes: 2 additions & 2 deletions cli/index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync, writeFileSync, statSync } from 'fs';
import { dirname, resolve, basename, extname } from 'path';
import * as cli from '../index';
import * as cli from '../index.mjs';
import pkg from '../package.json' with { type: 'json' };
import { LATEST } from '../parser-version';
import { LATEST } from '../parser-version.mjs';

const { version } = pkg;

Expand Down
2 changes: 1 addition & 1 deletion cli/txt2bas.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import main from './index';
import main from './index.mjs';
main('txt');
9 changes: 7 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ export type Expect = {
value?: string;
};

import { Statement as StatementInstance } from './txt2bas/index';
type Statement = InstanceType<typeof StatementInstance>;
import {
Statement as StatementClass,
Autoline as AutolineClass,
} from './txt2bas/index';

export type Statement = InstanceType<typeof StatementClass>;
export type Autoline = InstanceType<typeof AutolineClass>;

export type RenumberOptions = {
/** The line number to affect */
Expand Down
61 changes: 31 additions & 30 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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' with { type: 'json' };
import pkg from './package.json' assert { type: 'json' };

export const version = pkg.version;

Expand Down Expand Up @@ -125,7 +125,7 @@ export const tokens = (
}

if (stripComments) {
statements = transform.stripComments(statements, rest.autoline);
statements = transform.stripComments(statements);
}

if (inlineLoad) {
Expand Down Expand Up @@ -207,34 +207,34 @@ export const file2bas = (src, options = {}) => {
return file;
}

if (format === '3dos') {
if (rest.bankSplits.length === 0) {
return generateFile({ bytes, bank, directives });
}
if (format === 'tap') {
return asTap(bytes, 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)
);
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 },
});
}
// generate the file, but also save the actual banks as files
return file;
} else if (format === 'tap') {
return asTap(bytes, directives);
// 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;
};

/**
Expand Down Expand Up @@ -297,7 +297,7 @@ function generateFile({ bytes, bank, directives }) {
* @param {object} [options]
* @param {string} [options.format=3dos] format type: "3dos", "tap"
* @param {boolean} [options.includeHeader=true]
* @returns {Uint8Array}
* @returns {string}
*/
export const file2txt = (src, options = {}) => {
const { format = '3dos' } = options;
Expand All @@ -306,9 +306,10 @@ export const file2txt = (src, options = {}) => {
}
if (options.includeHeader === false) {
return bas2txtLines(new Uint8Array(src)) + '\n';
} else if (format === '3dos') {
return bas2txt(new Uint8Array(src)) + '\n';
} else if (format === 'tap') {
return tap2txt(new Uint8Array(src)) + '\n';
}

// else format = '3dos'
return bas2txt(new Uint8Array(src)) + '\n';
};
4 changes: 1 addition & 3 deletions txt2bas/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,7 @@ export class Statement {
) {
this.in.push(INT_EXPRESSION);
}
}

if (!this.isIn(IF) && !this.isIn(INT_EXPRESSION)) {
} else if (!this.isIn(IF) && !this.isIn(INT_EXPRESSION)) {
this.inIntExpression = false;
}

Expand Down
12 changes: 5 additions & 7 deletions txt2bas/transform.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { opTable } from './op-table.mjs';
import * as types from './types.mjs';
import { renumberStatements } from '../renumber.mjs';
import { basicToBytes, parseBasic } from './index.mjs';
import { bas2txtLines } from '../bas2txt.mjs';

Expand All @@ -22,6 +21,8 @@ export function inlineLoad(statements) {
const { join } = require('path');
const cwd = process.cwd();
let index;

/** @type {Statement} */
let st;

const getLengthAndOffset = () => {
Expand Down Expand Up @@ -254,10 +255,10 @@ function removeTrailingWhiteSpace(tokens) {
* Remove comments from statements
*
* @param {Statement[]} statements
* @param {Autoline} autoline
* @returns {Statement[]}
*/
export function stripComments(statements, autoline) {
export function stripComments(statements) {
/** @type {Statement[]} */
const res = [];
for (let i = 0; i < statements.length; i++) {
const st = statements[i];
Expand All @@ -281,8 +282,5 @@ export function stripComments(statements, autoline) {
}
}

return renumberStatements(res, {
start: autoline.start,
step: autoline.step,
});
return res;
}

0 comments on commit e76cc20

Please sign in to comment.