From e940f390e3d1d6ab01d01e054acd0677c4d9a2cd Mon Sep 17 00:00:00 2001 From: tilleul Date: Mon, 11 Sep 2017 21:59:58 +0200 Subject: [PATCH] Add files via upload --- renumber.js | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 renumber.js diff --git a/renumber.js b/renumber.js new file mode 100644 index 0000000..aef6766 --- /dev/null +++ b/renumber.js @@ -0,0 +1,156 @@ +var debug = false; + +if (process.argv.length<3 || process.argv.length>5) { + console.log(); + console.log(" ***************************************") + console.log(" * BASIC Applesoft RENUMBER (simple) *"); + console.log(" ***************************************") + console.log(); + console.log("Usage: node path/to/renumber.js path/to/basic/file [path/to/new/basic/file] [line_increment]"); + console.log(); + console.log("The file is supposed to contain BASIC APPLESOFT code (extracted with CiderPress for example)"); + console.log("But it will certainly work with any other BASIC code.") + console.log("If omitted, new_basic_file will be the same as original with extension .new.xxx where xxx is original extension.") + console.log("If omitted, line_increment will be set to 10."); + console.log("Renumber does not work with GOTO/GOSUB VARIABLE"); + console.log(); + console.log("(c)- 2017 François Vander Linden - Licence is \"Do What The F*ck You Want (the asterisk is a 'U' in case you're wondering)\""); + + process.exit(); +} + +// https://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer +var isNormalInteger = function (str) { + return /^\+?(0|[1-9]\d*)$/.test(str); +} + +var fs = require("fs"); +var inFile = process.argv[2]; +var outFile; +var inc; +var readlin = fs.readFileSync(inFile).toString().split('\n'); + +if (process.argv[3] && !isNormalInteger(process.argv[3])) { + outFile = process.argv[3]; + inc = process.argv[4] || 10; +} else { + inc = process.argv[3] || 10; + p = inFile.lastIndexOf("."); + if (p<0) outFile = inFile + ".new"; + else outFile = inFile.substring(0,p) + ".new" + inFile.substring(p); +} +console.log("File input: " + inFile); +console.log("File output: " + outFile); +console.log("Line increment: " + inc); + + +var lines = []; +var newLines = []; + +console.log("First pass ..."); + +for (i=0, cnt=0; i< readlin.length; i++, cnt++) { + readlin[i] = readlin[i].replace('\r','').trim(); + //console.log(i + "-" + cnt + ": '" + readlin[i] + "'"); + if (readlin[i]!='') { + sp = readlin[i].indexOf(" "); // first space + if (sp==0) sp = readlin[i].indexOf(" ",1); + if (sp>=0) { + oldNum = parseInt(readlin[i].substring(0, sp)); + code = readlin[i].substr(sp).trim(); + newNum = (cnt+1)*inc; + lines.push({oldNum: oldNum, newNum: newNum, code: code, cnt: cnt}); + } + } else cnt--; +} + +if (debug) console.log(lines); + +//r = /(GOTO|GOSUB|THEN)(\s*)(\d+)(?:(\s*,\s*)(\d+))*/ig ; +r = /\b(GO(?:TO|SUB)|THEN)(\s*)(\d+)((?:\s*,\s*\d+)*)/gi; +r2 = /(\s*,)(\s*)(\d+)/gi + +console.log("Second pass & renumber ..."); +for (i=0; i" + code.substring(cs, match.index) + "<_______...."); + console.log(" MATCH: " + Array(match.index+1).join(" ") + match[0]); + console.log("NEWCODE:'" + newCode + "'"); + } + for (j=0; j=2) { + // we have to refine results ... + while ((match2 = r2.exec(match[j])) != null) { + for (k= 0; k