Skip to content

Commit

Permalink
feat: add support to define the parser version
Browse files Browse the repository at this point in the history
It's pretty loose, and works mostly around IF/ELSE logic. Tokens in 208 parse in 207 (probably a bug on my side)
  • Loading branch information
remy committed Mar 18, 2024
1 parent e138765 commit 7af5ba2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
18 changes: 15 additions & 3 deletions cli/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync, writeFileSync, statSync } from 'fs';
import { dirname, resolve, basename, extname } from 'path';
import * as cli from '../index.mjs';
import pkg from '../package.json' with { type: 'json' };
import { LATEST } from '../parser-version.mjs';
import { LATEST, setParser, valid } from '../parser-version.mjs';

const { version } = pkg;

Expand All @@ -27,7 +27,7 @@ async function main(type) {
L: 'inline-load',
A: 'autostart',
C: 'comments-off',
P: 'parser', // currently not exposed
P: 'parser',
};
const bools = [
'bank',
Expand Down Expand Up @@ -60,6 +60,18 @@ async function main(type) {
return help(type);
}

if (options.parser) {
options.parser = options.parser.toUpperCase();
if (!valid.includes(options.parser)) {
console.error(`Unknown parser: ${options.parser} - valid options: ${valid.join(', ')}`);
process.exit(1);
}
} else {
options.parser = LATEST;
}

setParser(options.parser);

if (options.input) {
// check the file actually exists
try {
Expand Down Expand Up @@ -173,7 +185,7 @@ async function main(type) {
: undefined,
inlineLoad: options['inline-load'],
stripComments: options['comments-off'],
parser: LATEST,
parser: options.parser,
bankOutputDir: cwd,
});
}
Expand Down
5 changes: 5 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const tokens = (
* @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 = {}) => {
Expand All @@ -164,6 +165,10 @@ export const file2bas = (src, options = {}) => {
...parseOptions
} = options;

if (parseOptions.parser) {
parser.setParser(parseOptions.parser);
}

const bank = parseOptions.bank;

let { filename = 'untitled', autostart = 0x8000 } = options;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"build": "rm -rf dist; rollup --config && chmod 744 dist/cli/*.js",
"dev": "npm run build; rollup --config --watch",
"prepare": "npm run build",
"test:dev": "ava __tests__/*.test.js --no-coverage-report --watch",
"test:dev": "ava __tests__/*.test.mjs --no-coverage-report --watch",
"test": "nyc --reporter=lcov ava",
"semantic-release": "semantic-release"
},
Expand Down
8 changes: 7 additions & 1 deletion parser-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ export const v208 = '208';
export const v207 = '207';
export const FIRST = v207;
export const LATEST = v208;
export const LATEST_TEXT = 'LATEST';

const valid = [v208, v207];
export const valid = [LATEST_TEXT, v208, v207];

global.parser = LATEST;

Expand All @@ -13,6 +14,11 @@ global.parser = LATEST;
* @param {string} value
*/
export function setParser(value) {
value = value.toUpperCase();
if (value === LATEST_TEXT) {
value = LATEST;
}

if (!valid.includes(value)) {
throw new Error(`Unknown parser: ${value} - try "${LATEST}"`);
}
Expand Down

0 comments on commit 7af5ba2

Please sign in to comment.