interpret: Add silent mode

This commit is contained in:
Remko Tronçon 2022-05-24 22:49:10 +02:00
parent 236ad04aa5
commit 6738aff33e
4 changed files with 35 additions and 37 deletions

View file

@ -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))
@ -2769,6 +2769,9 @@
(if (i32.lt_s (local.get $tos) (i32.const 0x10000 (; = STACK_BASE ;)))
(drop (call $fail (local.get $tos) (i32.const 0x200B2 (; stack empty ;)))))
;; Show prompt
(if (i32.eqz (local.get $silent))
(then
(if (i32.ge_s (local.get $result) (i32.const 0))
(then
;; Write ok
@ -2781,7 +2784,7 @@
(call $shell_emit (i32.const 114))
(call $shell_emit (i32.const 111))
(call $shell_emit (i32.const 114))))
(call $shell_emit (i32.const 10))
(call $shell_emit (i32.const 10))))
(local.get $tos)
(br $loop)))
(global.set $tos)

View file

@ -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);
function run(s, expectErrors = false) {
const r = forth.interpret(s, true);
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 here() {

View file

@ -105,7 +105,7 @@ export default async function draw({
}
};
}
forth.interpret(program);
forth.interpret(program, true);
}
// Draw

View file

@ -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.