diff --git a/src/waforth.wat b/src/waforth.wat index e427be6..1e426f0 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -2754,7 +2754,7 @@ (func (export "here") (result i32) (global.get $here)) - (func (export "interpret") (result i32) + (func (export "interpret") (param $silent i32) (result i32) (local $result i32) (local $tos i32) (local.tee $tos (global.get $tos)) @@ -2768,25 +2768,28 @@ ;; Check for stack underflow (if (i32.lt_s (local.get $tos) (i32.const 0x10000 (; = STACK_BASE ;))) (drop (call $fail (local.get $tos) (i32.const 0x200B2 (; stack empty ;))))) - - (if (i32.ge_s (local.get $result) (i32.const 0)) + + ;; Show prompt + (if (i32.eqz (local.get $silent)) (then - ;; Write ok - (call $shell_emit (i32.const 111)) - (call $shell_emit (i32.const 107))) - (else - ;; Write error - (call $shell_emit (i32.const 101)) - (call $shell_emit (i32.const 114)) - (call $shell_emit (i32.const 114)) - (call $shell_emit (i32.const 111)) - (call $shell_emit (i32.const 114)))) - (call $shell_emit (i32.const 10)) + (if (i32.ge_s (local.get $result) (i32.const 0)) + (then + ;; Write ok + (call $shell_emit (i32.const 111)) + (call $shell_emit (i32.const 107))) + (else + ;; Write error + (call $shell_emit (i32.const 101)) + (call $shell_emit (i32.const 114)) + (call $shell_emit (i32.const 114)) + (call $shell_emit (i32.const 111)) + (call $shell_emit (i32.const 114)))) + (call $shell_emit (i32.const 10)))) (local.get $tos) (br $loop))) (global.set $tos) (local.get $result)) - + (func (export "push") (param $v i32) (global.set $tos (call $push (global.get $tos) (local.get $v)))) diff --git a/src/web/tests/suite.js b/src/web/tests/suite.js index 4df2bc0..2a850c5 100644 --- a/src/web/tests/suite.js +++ b/src/web/tests/suite.js @@ -87,22 +87,17 @@ function loadTests() { console.log("Entry:", p, previous, length, name, code, data, end); } - function run(ss, expectErrors = false) { - ss.split("\n").forEach((s) => { - // console.log("Running: ", s); - const r = forth.interpret(s); - if (expectErrors) { - expect(r).to.be.undefined; - output = output.substr(0, output.length); - } else { - expect(r).to.not.be.an( - "undefined", - "Error running: " + s + "; Output: " + output - ); - expect(r).to.not.be.below(0); - output = output.substr(0, output.length - 3); // Strip 'ok\n' from output - } - }); + function run(s, expectErrors = false) { + const r = forth.interpret(s, true); + if (expectErrors) { + expect(r).to.be.undefined; + } else { + expect(r).to.not.be.an( + "undefined", + "Error running: " + s + "; Output: " + output + ); + expect(r).to.not.be.below(0); + } } function here() { diff --git a/src/web/thurtle/draw.tsx b/src/web/thurtle/draw.tsx index 2887877..1d0bef2 100644 --- a/src/web/thurtle/draw.tsx +++ b/src/web/thurtle/draw.tsx @@ -105,7 +105,7 @@ export default async function draw({ } }; } - forth.interpret(program); + forth.interpret(program, true); } // Draw diff --git a/src/web/waforth.ts b/src/web/waforth.ts index b412950..4560969 100644 --- a/src/web/waforth.ts +++ b/src/web/waforth.ts @@ -112,11 +112,11 @@ class WAForth { read: (addr: number, length: number): number => { let input: string; - if (this.#buffer!.length <= length) { + const i = this.#buffer!.indexOf("\n"); + if (i === -1) { input = this.#buffer!; this.#buffer = ""; } else { - const i = this.#buffer!.lastIndexOf("\n", length - 1); input = this.#buffer!.substring(0, i + 1); this.#buffer = this.#buffer!.substring(i + 1); } @@ -224,15 +224,15 @@ class WAForth { } /** - * Read data `s` into the input buffer, and interpret. + * Read data `s` into the input buffer, and start interpreter. */ - interpret(s: string) { + interpret(s: string, silent = false) { if (!s.endsWith("\n")) { s = s + "\n"; } this.read(s); try { - return (this.core!.exports.interpret as any)(); + return (this.core!.exports.interpret as any)(silent); } catch (e) { // Exceptions thrown from the core means QUIT or ABORT is called, or an error // has occurred. Assume what has been done has been done, and ignore here.