Skip to content

Commit

Permalink
code snippets for ROUND, TIME; TIME now int and converted; dataPtr co…
Browse files Browse the repository at this point in the history
…rrected; FRAME
  • Loading branch information
benchmarko committed Dec 10, 2024
1 parent 05f5378 commit 213c8ce
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
32 changes: 28 additions & 4 deletions dist/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ DIM r(5)
ms=100/10:mxcpc=90/10
'
FOR i=0 TO 4
c=0:t1=INT(TIME*300/1000)
t=t1: WHILE t=t1:t=INT(TIME*300/1000):c=c+1:WEND
c=0:t1=TIME
t=t1: WHILE t=t1:t=TIME:c=c+1:WEND
c=0:t1=t+ms
WHILE t<t1:t=INT(TIME*300/1000):c=c+1:WEND
WHILE t<t1:t=TIME:c=c+1:WEND
r(i)=c
NEXT
PRINT "In";ms;"ms we can count to:";
Expand Down Expand Up @@ -570,6 +570,30 @@ cpcBasic.addItem("", `
400 '
`);

cpcBasic.addItem("", `
100 REM seconds - Seconds Test
110 REM Marco Vieth, 2019
115 cls
120 print"Timing 1 (FRAME):"
125 for cnt=1 to 3
130 t1=time
140 for i=1 to 50:FRAME:next
150 t1=time-t1
160 print int(1000*t1/300)/1000
170 next
190 '
200 print"Timing 2 (check time):"
210 for cnt=1 to 3
220 t1=time
230 civ=50:ct=time+civ*6:while time<ct:FRAME:wend
240 t1=time-t1
250 print int(1000*t1/300)/1000
260 next
270 '
300 print"Timing 3 (after):"
310 stop
`);

cpcBasic.addItem("", `
REM sieve - Sieve
n=1000000
Expand Down Expand Up @@ -949,7 +973,7 @@ for a=2 to 1 step -&x1: next
''1 for a$=1 to 2: next
for abc=1 to 10 step 3:next abc
''for a=b to c step s:a=0:next
''frame
frame
''a=fre(0)
''a=fre("")
''a=fre(b-2)
Expand Down
30 changes: 24 additions & 6 deletions src/Semantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ function getCodeSnippets() {
};
return createRecursiveArray(0);
},
_frame: function _frame() {
return new Promise<void>(resolve => setTimeout(() => resolve(), Date.now() % 50)); //TODO
},
_input: function _input(msg: string, isNum: boolean) {
return new Promise(resolve => setTimeout(() => resolve(isNum ? Number(prompt(msg)) : prompt(msg)), 0));
},
Expand All @@ -66,6 +69,12 @@ function getCodeSnippets() {
},
_restore: function _restore(label: string) {
_dataPtr = _restoreMap[label];
},
_round: function _round(num: number, dec: number) {
return (Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec))
},
_time: function _time() {
return (Date.now() * 3 / 10) | 0;
}
};
return codeSnippets;
Expand Down Expand Up @@ -126,7 +135,7 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
restoreMap[key] = index; //TODO
}
}
lineList.unshift(`const {_data, _restoreMap} = _defineData();\nlet _dataPrt = 0;`);
lineList.unshift(`const {_data, _restoreMap} = _defineData();\nlet _dataPtr = 0;`);
lineList.push(`function _defineData() {\n const _data = [\n${dataList.join(",\n")}\n ];\nconst _restoreMap =\n ${JSON.stringify(restoreMap)};\nreturn {_data, _restoreMap}\n}`);
}

Expand All @@ -142,11 +151,13 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
lineList.unshift(varStr);
}

if (instrKeys.includes("_input")) {
if (instrKeys.includes("_frame") || instrKeys.includes("_input")) {
lineList.unshift(`return async function() {`);
lineList.push('}();');
}

lineList.unshift(`"use strict"`);

const lineStr = lineList.filter((line) => line !== "").join('\n');
return lineStr;
},
Expand Down Expand Up @@ -335,6 +346,11 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
return result;
},

Frame(_frameLit: Node) { // eslint-disable-line @typescript-eslint/no-unused-vars
semanticsHelper.addInstr("_frame");
return `await _frame()`;
},

Gosub(_gosubLit: Node, e: Node) {
const labelStr = e.sourceString;
semanticsHelper.addGosubLabel(labelStr);
Expand Down Expand Up @@ -444,7 +460,6 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
const argList = args.asIteration().children.map(c => c.eval());
const results: string[] = [];
for (const ident of argList) {
//results.push(`${ident} = _data[_dataPrt++]`);
results.push(`${ident} = _read()`);
}
return results.join("; ");
Expand Down Expand Up @@ -478,9 +493,11 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
Round(_roundLit: Node, _open: Node, e: Node, _comma: Node, e2: Node, _close: Node) { // eslint-disable-line @typescript-eslint/no-unused-vars
const dec = e2.child(0)?.eval();
if (dec) {
return `(Math.round(${e.eval()} * Math.pow(10, ${dec})) / Math.pow(10, ${dec}))`;
//return `(Math.round(${e.eval()} * Math.pow(10, ${dec})) / Math.pow(10, ${dec}))`;
semanticsHelper.addInstr("_round");
return `_round(${e.eval()}, ${dec})`;
}
return `Math.round(${e.eval()})`;
return `Math.round(${e.eval()})`; // common round without decimals places
// A better way to avoid rounding errors: https://www.jacklmoore.com/notes/rounding-in-javascript
},

Expand Down Expand Up @@ -526,7 +543,8 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
},

Time(_timeLit: Node) { // eslint-disable-line @typescript-eslint/no-unused-vars
return `Date.now()`; // TODO; or *300/1000
semanticsHelper.addInstr("_time");
return `_time()`;
},

Upper(_upperLit: Node, _open: Node, e: Node, _close: Node) { // eslint-disable-line @typescript-eslint/no-unused-vars
Expand Down
4 changes: 4 additions & 0 deletions src/arithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const arithmetic = {
| End
| Erase
| ForLoop
| Frame
| Gosub
| Input
| Next
Expand Down Expand Up @@ -99,6 +100,9 @@ export const arithmetic = {
ForLoop
= caseInsensitive<"for"> variable "=" NumExp caseInsensitive<"to"> NumExp (caseInsensitive<"step"> NumExp)?
Frame
= caseInsensitive<"frame">
Gosub
= caseInsensitive<"gosub"> label
Expand Down

0 comments on commit 213c8ce

Please sign in to comment.