Skip to content

Commit 30ca9c6

Browse files
committed
add missing line numbers on load
1 parent bf53a36 commit 30ca9c6

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

Controller.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ Controller.prototype = {
175175
}
176176
},
177177

178+
addLineNumbers: function (input) {
179+
var lineParts, lastLine, lineNum, i;
180+
lineParts = input.split("\n");
181+
lastLine = 0;
182+
183+
for (i = 0; i < lineParts.length; i += 1) {
184+
lineNum = parseInt(lineParts[i], 10);
185+
186+
if (isNaN(lineNum)) {
187+
lineNum = lastLine + 1;
188+
lineParts[i] = String(lastLine + 1) + " " + lineParts[i];
189+
}
190+
lastLine = lineNum;
191+
}
192+
return lineParts.join("\n");
193+
},
194+
178195
// Also called from example files xxxxx.js
179196
addItem: function (sKey, sInput) { // optional sKey
180197
var oExample;
@@ -186,6 +203,10 @@ Controller.prototype = {
186203

187204
sInput = sInput.replace(/^\n/, "").replace(/\n$/, ""); // remove preceding and trailing newlines
188205
// beware of data files ending with newlines! (do not use trimEnd)
206+
207+
if (sInput.startsWith("REM ")) {
208+
sInput = this.addLineNumbers(sInput);
209+
}
189210

190211
oExample = this.model.getExample(sKey);
191212
oExample.key = sKey; // maybe changed

examples/0index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ cpcBasic.addIndex("./examples", {
2121
"key": "test/testpage.dat",
2222
"title": "Test data for Test Page",
2323
"meta": "D"
24+
},
25+
{
26+
"key": "test/testsub",
27+
"title": "Test Subroutines"
2428
}
2529
]
2630
});

examples/test/testsub.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* globals cpcBasic */
2+
3+
"use strict";
4+
5+
cpcBasic.addItem("", function () { /*
6+
REM testsub - Test Subroutines TS
7+
REM only required line numbers; no GOTO
8+
CLS
9+
PRINT "start"
10+
GOSUB 350
11+
PRINT "end"
12+
END
13+
'
14+
100 PRINT "sub100"
15+
RETURN
16+
'
17+
200 PRINT "sub200"
18+
PRINT "inside sub200"
19+
GOSUB 100
20+
RETURN
21+
'
22+
GOSUB 200
23+
PRINT "in between"
24+
'
25+
300 PRINT "sub300"
26+
PRINT "inside sub300"
27+
RETURN
28+
'
29+
350 'main
30+
GOSUB 100
31+
GOSUB 200
32+
GOSUB 300
33+
a=1
34+
ON a GOSUB 200, 300
35+
RETURN
36+
'
37+
*/ });

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<link rel="stylesheet" href="cpcbasic.css" />
7-
<title id="title">CPC Basic v0.10.17</title>
7+
<title id="title">CPC Basic v0.10.18</title>
88
</head>
99

1010
<body id="pageBody">

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cpcbasic",
3-
"version": "0.10.17",
3+
"version": "0.10.18",
44
"description": "# CPCBasic - Run CPC BASIC in a Browser",
55
"main": "cpcbasic.js",
66
"directories": {

test/testParseExamples.qunit.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,23 @@ cpcBasic = {
169169
}
170170
},
171171

172+
addLineNumbers: function (input) {
173+
var lineParts, lastLine, lineNum, i;
174+
lineParts = input.split("\n");
175+
lastLine = 0;
176+
177+
for (i = 0; i < lineParts.length; i += 1) {
178+
lineNum = parseInt(lineParts[i], 10);
179+
180+
if (isNaN(lineNum)) {
181+
lineNum = lastLine + 1;
182+
lineParts[i] = String(lastLine + 1) + " " + lineParts[i];
183+
}
184+
lastLine = lineNum;
185+
}
186+
return lineParts.join("\n");
187+
},
188+
172189
// Also called from example files xxxxx.js
173190
addItem2: function (sKey, sInput) { // optional sKey
174191
var oExample;
@@ -180,6 +197,10 @@ cpcBasic = {
180197
sInput = sInput.replace(/^\n/, "").replace(/\n$/, ""); // remove preceding and trailing newlines
181198
// beware of data files ending with newlines! (do not use trimEnd)
182199

200+
if (sInput.startsWith("REM ")) {
201+
sInput = this.addLineNumbers(sInput);
202+
}
203+
183204
oExample = this.model.getExample(sKey);
184205
oExample.key = sKey; // maybe changed
185206
oExample.script = sInput;

0 commit comments

Comments
 (0)