mirror of
https://github.com/remko/waforth
synced 2024-12-27 09:59:29 +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)
|
(func (export "here") (result i32)
|
||||||
(global.get $here))
|
(global.get $here))
|
||||||
|
|
||||||
(func (export "interpret") (result i32)
|
(func (export "interpret") (param $silent i32) (result i32)
|
||||||
(local $result i32)
|
(local $result i32)
|
||||||
(local $tos i32)
|
(local $tos i32)
|
||||||
(local.tee $tos (global.get $tos))
|
(local.tee $tos (global.get $tos))
|
||||||
|
@ -2768,25 +2768,28 @@
|
||||||
;; Check for stack underflow
|
;; Check for stack underflow
|
||||||
(if (i32.lt_s (local.get $tos) (i32.const 0x10000 (; = STACK_BASE ;)))
|
(if (i32.lt_s (local.get $tos) (i32.const 0x10000 (; = STACK_BASE ;)))
|
||||||
(drop (call $fail (local.get $tos) (i32.const 0x200B2 (; stack empty ;)))))
|
(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
|
(then
|
||||||
;; Write ok
|
(if (i32.ge_s (local.get $result) (i32.const 0))
|
||||||
(call $shell_emit (i32.const 111))
|
(then
|
||||||
(call $shell_emit (i32.const 107)))
|
;; Write ok
|
||||||
(else
|
(call $shell_emit (i32.const 111))
|
||||||
;; Write error
|
(call $shell_emit (i32.const 107)))
|
||||||
(call $shell_emit (i32.const 101))
|
(else
|
||||||
(call $shell_emit (i32.const 114))
|
;; Write error
|
||||||
(call $shell_emit (i32.const 114))
|
(call $shell_emit (i32.const 101))
|
||||||
(call $shell_emit (i32.const 111))
|
(call $shell_emit (i32.const 114))
|
||||||
(call $shell_emit (i32.const 114))))
|
(call $shell_emit (i32.const 114))
|
||||||
(call $shell_emit (i32.const 10))
|
(call $shell_emit (i32.const 111))
|
||||||
|
(call $shell_emit (i32.const 114))))
|
||||||
|
(call $shell_emit (i32.const 10))))
|
||||||
(local.get $tos)
|
(local.get $tos)
|
||||||
(br $loop)))
|
(br $loop)))
|
||||||
(global.set $tos)
|
(global.set $tos)
|
||||||
(local.get $result))
|
(local.get $result))
|
||||||
|
|
||||||
(func (export "push") (param $v i32)
|
(func (export "push") (param $v i32)
|
||||||
(global.set $tos (call $push (global.get $tos) (local.get $v))))
|
(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);
|
console.log("Entry:", p, previous, length, name, code, data, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
function run(ss, expectErrors = false) {
|
function run(s, expectErrors = false) {
|
||||||
ss.split("\n").forEach((s) => {
|
const r = forth.interpret(s, true);
|
||||||
// console.log("Running: ", s);
|
if (expectErrors) {
|
||||||
const r = forth.interpret(s);
|
expect(r).to.be.undefined;
|
||||||
if (expectErrors) {
|
} else {
|
||||||
expect(r).to.be.undefined;
|
expect(r).to.not.be.an(
|
||||||
output = output.substr(0, output.length);
|
"undefined",
|
||||||
} else {
|
"Error running: " + s + "; Output: " + output
|
||||||
expect(r).to.not.be.an(
|
);
|
||||||
"undefined",
|
expect(r).to.not.be.below(0);
|
||||||
"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() {
|
function here() {
|
||||||
|
|
|
@ -105,7 +105,7 @@ export default async function draw({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
forth.interpret(program);
|
forth.interpret(program, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
|
|
|
@ -112,11 +112,11 @@ class WAForth {
|
||||||
|
|
||||||
read: (addr: number, length: number): number => {
|
read: (addr: number, length: number): number => {
|
||||||
let input: string;
|
let input: string;
|
||||||
if (this.#buffer!.length <= length) {
|
const i = this.#buffer!.indexOf("\n");
|
||||||
|
if (i === -1) {
|
||||||
input = this.#buffer!;
|
input = this.#buffer!;
|
||||||
this.#buffer = "";
|
this.#buffer = "";
|
||||||
} else {
|
} else {
|
||||||
const i = this.#buffer!.lastIndexOf("\n", length - 1);
|
|
||||||
input = this.#buffer!.substring(0, i + 1);
|
input = this.#buffer!.substring(0, i + 1);
|
||||||
this.#buffer = this.#buffer!.substring(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")) {
|
if (!s.endsWith("\n")) {
|
||||||
s = s + "\n";
|
s = s + "\n";
|
||||||
}
|
}
|
||||||
this.read(s);
|
this.read(s);
|
||||||
try {
|
try {
|
||||||
return (this.core!.exports.interpret as any)();
|
return (this.core!.exports.interpret as any)(silent);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Exceptions thrown from the core means QUIT or ABORT is called, or an error
|
// 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.
|
// has occurred. Assume what has been done has been done, and ignore here.
|
||||||
|
|
Loading…
Reference in a new issue