diff --git a/dist/examples/examples.js b/dist/examples/examples.js index 50e38d4..f7f8a8c 100644 --- a/dist/examples/examples.js +++ b/dist/examples/examples.js @@ -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) diff --git a/src/Core.ts b/src/Core.ts index b701c59..9432b59 100644 --- a/src/Core.ts +++ b/src/Core.ts @@ -96,7 +96,7 @@ export class Core implements ICore { this.vm.setOutput(""); if (compiledScript.startsWith("ERROR")) { - return "ERROR" + "\n"; + return "ERROR"; } let output: string; @@ -127,6 +127,6 @@ export class Core implements ICore { output += "unknown"; } } - return output + "\n"; + return output; } } diff --git a/src/Semantics.ts b/src/Semantics.ts index 65c5b51..b2eafd5 100644 --- a/src/Semantics.ts +++ b/src/Semantics.ts @@ -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(); @@ -192,7 +191,6 @@ function getSemantics(semanticsHelper: SemanticsHelper) { } dataList.push(argList.join(", ")); - //defineData(argList); return ""; }, @@ -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()})`; }, @@ -734,7 +736,7 @@ export class Semantics { return name; } - private deleteAllItems(items: Record) { + private static deleteAllItems(items: Record) { for (const name in items) { // eslint-disable-line guard-for-in delete items[name]; } @@ -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() { diff --git a/src/Ui.ts b/src/Ui.ts index ccf7d47..a203b4a 100644 --- a/src/Ui.ts +++ b/src/Ui.ts @@ -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 diff --git a/src/arithmetic.ts b/src/arithmetic.ts index fc5d88a..02c84c6 100644 --- a/src/arithmetic.ts +++ b/src/arithmetic.ts @@ -108,6 +108,9 @@ export const arithmetic = { Input = caseInsensitive<"input"> (string (";" | ","))? AnyIdent // or NonemptyListOf? + Instr + = caseInsensitive<"instr"> "(" StrExp "," StrExp ")" + Int = caseInsensitive<"int"> "(" NumExp ")" @@ -320,6 +323,7 @@ export const arithmetic = { | Cos | Exp | Fix + | Instr | Int | Len | Log diff --git a/src/main.ts b/src/main.ts index 919281c..865e8ed 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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");