mirror of
https://github.com/remko/waforth
synced 2024-12-26 09:59:09 +01:00
interpret: Add silent mode
This commit is contained in:
parent
236ad04aa5
commit
6738aff33e
4 changed files with 35 additions and 37 deletions
|
@ -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))))
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -105,7 +105,7 @@ export default async function draw({
|
|||
}
|
||||
};
|
||||
}
|
||||
forth.interpret(program);
|
||||
forth.interpret(program, true);
|
||||
}
|
||||
|
||||
// Draw
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue