Skip to content

Commit

Permalink
INSTR with 2 parameters; reset also instructionMap; not too many newl…
Browse files Browse the repository at this point in the history
…ines
  • Loading branch information
benchmarko committed Dec 8, 2024
1 parent 7354891 commit 512f1dc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dist/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ if a=1 then a=2 else a=1
''input ;"para noCRLF";a$,b
''input#2,;"para noCRLF";a$,b
''input#stream,;"string";a$,b
''a=instr("key","ey")
''a=instr(s$,find$)
a=instr("key","ey")
a=instr(s$,find$)
''a=instr(start,s$,find$)
a=int(-2.3)
a=int(b+2.3)
Expand Down
4 changes: 2 additions & 2 deletions src/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class Core implements ICore {
this.vm.setOutput("");

if (compiledScript.startsWith("ERROR")) {
return "ERROR" + "\n";
return "ERROR";
}

let output: string;
Expand Down Expand Up @@ -127,6 +127,6 @@ export class Core implements ICore {
output += "unknown";
}
}
return output + "\n";
return output;
}
}
15 changes: 9 additions & 6 deletions src/Semantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ function getSemantics(semanticsHelper: SemanticsHelper) {

const commentStr = comment.sourceString ? `; //${comment.sourceString.substring(1)}` : "";
const semi = lineStr === "" || lineStr.endsWith("{") || lineStr.endsWith("}") || lineStr.startsWith("//") || commentStr ? "" : ";";
//lineIndex += 1;

const indentStr = semanticsHelper.getIndentStr();
semanticsHelper.applyNextIndent();
Expand Down Expand Up @@ -192,7 +191,6 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
}

dataList.push(argList.join(", "));
//defineData(argList);
return "";
},

Expand Down Expand Up @@ -311,6 +309,10 @@ function getSemantics(semanticsHelper: SemanticsHelper) {
return `${ident} = await _input(${msgStr}${isNumStr})`;
},

Instr(_instrLit: Node, _open: Node, e1: Node, _comma: Node, e2: Node, _close: Node) { // eslint-disable-line @typescript-eslint/no-unused-vars
return `((${e1.eval()}).indexOf(${e2.eval()}) + 1)`;
},

Int(_intLit: Node, _open: Node, e: Node, _close: Node) { // eslint-disable-line @typescript-eslint/no-unused-vars
return `Math.floor(${e.eval()})`;
},
Expand Down Expand Up @@ -734,7 +736,7 @@ export class Semantics {
return name;
}

private deleteAllItems(items: Record<string, any>) {
private static deleteAllItems(items: Record<string, any>) {
for (const name in items) { // eslint-disable-line guard-for-in
delete items[name];
}
Expand All @@ -761,11 +763,12 @@ export class Semantics {
this.lineIndex = 0;
this.indent = 0;
this.indentAdd = 0;
this.deleteAllItems(this.variables);
Semantics.deleteAllItems(this.variables);
this.definedLabels.length = 0;
this.deleteAllItems(this.gosubLabels);
Semantics.deleteAllItems(this.gosubLabels);
this.dataList.length = 0;
this.deleteAllItems(this.restoreMap);
Semantics.deleteAllItems(this.restoreMap);
Semantics.deleteAllItems(this.instrMap);
}

public getSemantics() {
Expand Down
2 changes: 1 addition & 1 deletion src/Ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Ui implements IUi {

const output = await this.core.executeScript(compiledScript);

this.setOutputText(this.getOutputText() + output);
this.setOutputText(this.getOutputText() + output + (output.endsWith("\n") ? "" : "\n"));
}

private oncompiledTextChange(_event: Event) { // 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 @@ -108,6 +108,9 @@ export const arithmetic = {
Input
= caseInsensitive<"input"> (string (";" | ","))? AnyIdent // or NonemptyListOf?
Instr
= caseInsensitive<"instr"> "(" StrExp "," StrExp ")"
Int
= caseInsensitive<"int"> "(" NumExp ")"
Expand Down Expand Up @@ -320,6 +323,7 @@ export const arithmetic = {
| Cos
| Exp
| Fix
| Instr
| Int
| Len
| Log
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ function start(input: string) {
if (input !== "") {
const compiledScript = core.compileScript(input);

console.log("INFO: Compiled:\n" + compiledScript + "\n");
console.log("INFO: Compiled:\n" + compiledScript + "\n---");

return keepRunning(async () => {
const output = await core.executeScript(compiledScript);
console.log(output);
console.log(output.replace(/\n$/, ""));
}, 5000);
} else {
console.log("No input");
Expand Down

0 comments on commit 512f1dc

Please sign in to comment.