diff --git a/Makefile b/Makefile index 9da526f..684fe39 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ all: $(WASM_FILES) dev-server: $(WASM_FILES) yarn -s dev-server -wasm: $(WASM_FILES) src/tools/quadruple.wasm.hex +wasm: $(WASM_FILES) src/waforth.assembled.wat src/tools/quadruple.wasm.hex src/waforth.wasm: src/waforth.wat dist racket -f $< > src/waforth.wat.tmp diff --git a/src/waforth.wat b/src/waforth.wat index 9c84d42..09a77d2 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -103,9 +103,10 @@ (define !pushIndex 1) (define !popIndex 2) (define !typeIndex 3) -(define !pushDataAddressIndex 4) -(define !setLatestBodyIndex 5) -(define !tableStartIndex 6) +(define !abortIndex 4) +(define !pushDataAddressIndex 5) +(define !setLatestBodyIndex 6) +(define !tableStartIndex 7) (define !dictionaryLatest 0) (define !dictionaryTop !dictionaryBase) @@ -589,7 +590,16 @@ (func $ABORT (set_global $tos (i32.const !stackBase)) (call $QUIT)) - (!def_word "ABORT" "$ABORT") + (!def_word "ABORT" "$ABORT" !fNone !abortIndex) + + ;; 6.1.0680 ABORT" + (func $ABORT-quote + (call $compileIf) + (call $Sq) + (call $emitICall (i32.const 0) (i32.const !typeIndex)) + (call $emitICall (i32.const 0) (i32.const !abortIndex)) + (call $compileThen)) + (!def_word "ABORT\"" "$ABORT-quote" !fImmediate) ;; 6.1.0690 (func $ABS diff --git a/tests/tests.js b/tests/tests.js index b6ee992..09fcf24 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -76,10 +76,15 @@ function loadTests(wasmModule, arrayToBase64) { console.log("Entry:", p, previous, length, name, code, data, end); } - function run(s) { + function run(s, expectErrors = false) { const r = forth.run(s); - expect(r).to.not.be.below(0); - output = output.substr(0, output.length - 3); // Strip 'ok\n' from output + if (expectErrors) { + expect(r).to.be.undefined; + output = output.substr(0, output.length); + } else { + expect(r).to.not.be.below(0); + output = output.substr(0, output.length - 3); // Strip 'ok\n' from output + } return r; } @@ -851,7 +856,7 @@ function loadTests(wasmModule, arrayToBase64) { it("should find a word", () => { loadString("DUP"); run("FIND"); - expect(stack[0]).to.eql(131792); + expect(stack[0]).to.eql(131824); expect(stack[1]).to.eql(-1); }); @@ -1313,6 +1318,27 @@ function loadTests(wasmModule, arrayToBase64) { }); }); + describe('ABORT"', () => { + it("should not abort if check fails", () => { + run(': FOO 5 = ABORT" Error occurred" 6 ;'); + run("1 2 FOO 7"); + run("8"); + expect(output.trim()).to.eql(""); + expect(stack[0]).to.eql(1); + expect(stack[1]).to.eql(6); + expect(stack[2]).to.eql(7); + expect(stack[3]).to.eql(8); + }); + + it("should abort if check succeeds", () => { + run(': FOO 5 = ABORT" Error occurred" 6 ;'); + run("1 5 FOO 7", true); + run("8"); + expect(output.trim()).to.eql("Error occurred"); + expect(stack[0]).to.eql(8); + }); + }); + describe("system", () => { beforeEach(() => { core.loadPrelude();