diff --git a/src/waforth.wat b/src/waforth.wat index 140e25e..87d5a5d 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -1331,7 +1331,7 @@ (call $FIND) (local.set $FINDResult (call $pop)) (if (param i32) (result i32) (i32.eqz (local.get $FINDResult)) - (call $fail (i32.const 0x20000))) ;; undefined word + (call $failUndefinedWord)) (local.set $FINDToken (call $pop)) (if (param i32) (result i32) (i32.eq (local.get $FINDResult) (i32.const 1)) (then @@ -1529,18 +1529,11 @@ ;; 6.1.2310 TYPE (func $TYPE (param $tos i32) (result i32) (local $p i32) - (local $end i32) (local $len i32) (local.get $tos) (local.set $len (call $pop)) (local.set $p (call $pop)) - (local.set $end (i32.add (local.get $p) (local.get $len))) - (block $endLoop - (loop $loop - (br_if $endLoop (i32.eq (local.get $p) (local.get $end))) - (call $shell_emit (i32.load8_u (local.get $p))) - (local.set $p (i32.add (local.get $p) (i32.const 1))) - (br $loop)))) + (call $type (local.get $len) (local.get $p))) ;; WARNING: If you change this table index, make sure the emitted ICalls are also updated (data (i32.const 136844) "|\16\02\00" "\04" "TYPE\00\00\00" "\85\00\00\00") (elem (i32.const 0x85) $TYPE) ;; none @@ -1749,7 +1742,7 @@ (call $fail (i32.const 0x20028))) ;; incomplete input (call $FIND) (if (param i32) (result i32) (i32.eqz (call $pop)) - (call $fail (i32.const 0x20000))) ;; undefined word + (call $failUndefinedWord)) (local.set $xt (call $pop)) (local.set $v (call $pop)) (i32.store (i32.add (call $body (local.get $xt)) (i32.const 4)) (local.get $v))) @@ -1963,7 +1956,7 @@ (call $push (local.get $number))))) (else ;; It's not a number. (drop) - (call $fail (i32.const 0x20000))))) ;; undefined word + (call $failUndefinedWord)))) ;; undefined word (else ;; Found the word. ;; Are we compiling or is it immediate? (if (param i32) (result i32) (i32.or (i32.eqz (i32.load (i32.const 0x218f8 (; body(STATE) ;)))) @@ -2622,12 +2615,32 @@ (unreachable)) (func $fail (param $tos i32) (param $str i32) (result i32) - (local.get $tos) - (call $push (local.get $str)) - (call $COUNT) - (call $TYPE) + (call $type + (i32.load8_u (local.get $str)) + (i32.add (local.get $str) (i32.const 1))) (call $shell_emit (i32.const 10)) - (call $ABORT)) + (call $ABORT (local.get $tos))) + + (func $type (param $len i32) (param $p i32) + (local $end i32) + (local.set $end (i32.add (local.get $p) (local.get $len))) + (block $endLoop + (loop $loop + (br_if $endLoop (i32.eq (local.get $p) (local.get $end))) + (call $shell_emit (i32.load8_u (local.get $p))) + (local.set $p (i32.add (local.get $p) (i32.const 1))) + (br $loop)))) + + (func $failUndefinedWord (param $tos i32) (result i32) + (local $wordBase i32) + (call $type (i32.load8_u (i32.const 0x20000)) (i32.const 0x20001)) + (call $shell_emit (i32.const 0x3a)) + (call $shell_emit (i32.const 0x20)) + (call $type + (i32.load8_u (local.tee $wordBase (call $wordBase))) + (i32.add (local.get $wordBase) (i32.const 1))) + (call $shell_emit (i32.const 0x0a)) + (call $ABORT (local.get $tos))) (func $setFlag (param $v i32) (i32.store diff --git a/src/web/tests/suite.js b/src/web/tests/suite.js index 85efca7..ff2d625 100644 --- a/src/web/tests/suite.js +++ b/src/web/tests/suite.js @@ -184,7 +184,7 @@ function loadTests() { it("should return an error when word is not found", () => { forth.read("BADWORD"); expect(() => core.interpret()).to.throw(); - expect(output.trim()).to.eql("undefined word"); + expect(output.trim()).to.eql("undefined word: BADWORD"); }); it("should interpret a positive number", () => { @@ -206,13 +206,13 @@ function loadTests() { it("should not interpret hex in decimal mode", () => { forth.read("DF"); expect(() => core.interpret()).to.throw(); - expect(output.trim()).to.eql("undefined word"); + expect(output.trim()).to.eql("undefined word: DF"); }); it("should fail on half a word", () => { forth.read("23FOO"); expect(() => core.interpret()).to.throw(); - expect(output.trim()).to.eql("undefined word"); + expect(output.trim()).to.eql("undefined word: 23FOO"); }); });