Skip to content

Commit 969e5d3

Browse files
committed
|DIR with header and footer output; INSTR with empty search string; ROUND for negative numbers fixed; USING with escape character "_"; WRITE: allow also semicolon as separator; test page extended; no warning for unknown RSX during compile time
1 parent 668b8a1 commit 969e5d3

File tree

7 files changed

+700
-583
lines changed

7 files changed

+700
-583
lines changed

BasicParser.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,26 @@ BasicParser.prototype = {
15001500
return fnCreateCmdCall(sName);
15011501
});
15021502

1503+
stmt("write", function () {
1504+
var oNode = oPreviousToken,
1505+
mCloseTokens = BasicParser.mCloseTokens,
1506+
oStream = fnGetOptionalStream();
1507+
1508+
if (oStream.len !== 0) { // not an inserted stream?
1509+
if (!mCloseTokens[oToken.type]) {
1510+
advance(",");
1511+
}
1512+
}
1513+
1514+
oNode.args = fnGetArgsSepByCommaSemi();
1515+
oNode.args.unshift(oStream);
1516+
1517+
if ((oPreviousToken.type === "," && oNode.args.length > 1) || oPreviousToken.type === ";") {
1518+
throw that.composeError(Error(), "Operand missing", oPreviousToken.type, oPreviousToken.pos);
1519+
}
1520+
return oNode;
1521+
});
1522+
15031523

15041524
// line
15051525
this.sLine = "0"; // for error messages

CodeGeneratorJs.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ CodeGeneratorJs.prototype = {
449449
},
450450

451451
"|": function (node) { // rsx
452-
var sRsxName, bRsxAvailable, aNodeArgs, sLabel, oError;
452+
var sRsxName, bRsxAvailable, aNodeArgs, sLabel;
453453

454454
sRsxName = node.value.substr(1).toLowerCase().replace(/\./g, "_");
455455
bRsxAvailable = that.rsx && that.rsx.rsxIsAvailable(sRsxName);
@@ -458,10 +458,6 @@ CodeGeneratorJs.prototype = {
458458
that.iStopCount += 1;
459459

460460
if (!bRsxAvailable) { // if RSX not available, we delay the error until it is executed (or catched by on error goto)
461-
if (!that.bQuiet) {
462-
oError = that.composeError(Error(), "Unknown RSX command", node.value, node.pos);
463-
Utils.console.warn(oError);
464-
}
465461
aNodeArgs.unshift('"' + sRsxName + '"'); // put as first arg
466462
sRsxName = "rsxExec"; // and call special handler which triggers error if not available
467463
}

Controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,12 +706,12 @@ Controller.prototype = {
706706
aDir.sort();
707707
}
708708

709-
this.oVm.print(iStream, "\r\n");
709+
this.oVm.print(iStream, "\r\nDrive A: user 0\r\n\r\n");
710710
for (i = 0; i < aDir.length; i += 1) {
711711
sKey = aDir[i] + " ";
712712
this.oVm.print(iStream, sKey);
713713
}
714-
this.oVm.print(iStream, "\r\n");
714+
this.oVm.print(iStream, "\r\n\r\n999K free\r\n\r\n");
715715
},
716716

717717
fnFileCat: function (oParas) {

CpcVm.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,13 +1864,20 @@ CpcVm.prototype = {
18641864
},
18651865

18661866
instr: function (p1, p2, p3) { // optional startpos as first parameter
1867-
this.vmAssertString(p2, "INSTR");
1868-
if (typeof p1 === "string") { // p1=string, p2=search string
1869-
return p1.indexOf(p2) + 1;
1867+
var startPos = typeof p1 === "number" ? this.vmInRangeRound(p1, 1, 255, "INSTR") - 1 : 0, // p1=startpos
1868+
str = typeof p1 === "number" ? p2 : p1,
1869+
search = typeof p1 === "number" ? p3 : p2;
1870+
1871+
this.vmAssertString(str, "INSTR");
1872+
this.vmAssertString(search, "INSTR");
1873+
1874+
if (startPos >= str.length) {
1875+
return 0; // not found
1876+
}
1877+
if (!search.length) {
1878+
return startPos + 1;
18701879
}
1871-
p1 = this.vmInRangeRound(p1, 1, 255, "INSTR"); // p1=startpos
1872-
this.vmAssertString(p3, "INSTR");
1873-
return p2.indexOf(p3, p1 - 1) + 1; // p2=string, p3=search string
1880+
return str.indexOf(search, startPos) + 1;
18741881
},
18751882

18761883
"int": function (n) {
@@ -3104,11 +3111,19 @@ CpcVm.prototype = {
31043111
},
31053112

31063113
round: function (n, iDecimals) {
3114+
var iMaxDecimals = 20 - Math.floor(Math.log10(n)); // limit for JS
3115+
31073116
this.vmAssertNumber(n, "ROUND");
31083117
iDecimals = this.vmInRangeRound(iDecimals || 0, -39, 39, "ROUND");
31093118

3119+
if (iDecimals >= 0 && iDecimals > iMaxDecimals) {
3120+
iDecimals = iMaxDecimals;
3121+
}
3122+
31103123
// To avoid rounding errors: https://www.jacklmoore.com/notes/rounding-in-javascript
3111-
return Number(Math.round(Number(n + "e" + iDecimals)) + "e" + ((iDecimals >= 0) ? "-" + iDecimals : "+" + -iDecimals));
3124+
// Use Math.abs(n) and Math.sign(n) To round negative numbers to larger negative numbers
3125+
return Math.sign(n) * Number(Math.round(Number(Math.abs(n) + "e" + iDecimals)) + "e" + ((iDecimals >= 0) ? "-" + iDecimals : "+" + -iDecimals));
3126+
//return Number(Math.round(Number(n + "e" + iDecimals)) + "e" + ((iDecimals >= 0) ? "-" + iDecimals : "+" + -iDecimals));
31123127
},
31133128

31143129
vmRunCallback: function (sInput, oMeta) {
@@ -3470,22 +3485,44 @@ CpcVm.prototype = {
34703485
},
34713486

34723487
using: function (sFormat) { // varargs
3473-
var reFormat = /(!|&|\\ *\\|(?:\*\*|\$\$|\*\*\$)?\+?(?:#|,)+\.?#*(?:\^\^\^\^)?[+-]?)/g,
3488+
var reFormat = /(_|!|&|\\ *\\|(?:\*\*|\$\$|\*\*\$)?\+?(?:#|,)+\.?#*(?:\^\^\^\^)?[+-]?)/g,
34743489
s = "",
34753490
aFormat = [],
3476-
iIndex, oMatch, sFrmt, iFormat, i, arg;
3491+
iIndex, aMatch, sFrmt, iFormat, i, arg, nonFormChars, nonFormCharsEnd;
34773492

34783493
this.vmAssertString(sFormat, "USING");
34793494

34803495
// We simulate sFormat.split(reFormat) here since it does not work with IE8
34813496
iIndex = 0;
3482-
while ((oMatch = reFormat.exec(sFormat)) !== null) {
3483-
aFormat.push(sFormat.substring(iIndex, oMatch.index)); // non-format characters at the beginning
3484-
aFormat.push(oMatch[0]);
3485-
iIndex = oMatch.index + oMatch[0].length;
3497+
while ((aMatch = reFormat.exec(sFormat)) !== null) {
3498+
nonFormChars = sFormat.substring(iIndex, aMatch.index); // non-format characters at the beginning
3499+
3500+
if (aMatch[0] === "_") { // underscore "_" is escape character
3501+
nonFormChars += sFormat.charAt(aMatch.index + 1) || "_"; // add escaped character
3502+
}
3503+
3504+
if (aFormat.length % 2) { // odd?
3505+
aFormat[aFormat.length - 1] += nonFormChars;
3506+
} else {
3507+
aFormat.push(nonFormChars);
3508+
}
3509+
3510+
if (aMatch[0] === "_") { // underscore "_" is escape character
3511+
reFormat.lastIndex += 1;
3512+
iIndex = reFormat.lastIndex;
3513+
} else {
3514+
aFormat.push(aMatch[0]);
3515+
iIndex = aMatch.index + aMatch[0].length;
3516+
}
34863517
}
34873518
if (iIndex < sFormat.length) { // non-format characters at the end
3488-
aFormat.push(sFormat.substr(iIndex));
3519+
nonFormCharsEnd = sFormat.substring(iIndex);
3520+
3521+
if (aFormat.length % 2) { // odd?
3522+
aFormat[aFormat.length - 1] += nonFormCharsEnd;
3523+
} else {
3524+
aFormat.push(nonFormCharsEnd);
3525+
}
34893526
}
34903527

34913528
if (aFormat.length < 2) {

0 commit comments

Comments
 (0)