X-Git-Url: https://git.kengrimes.com/?p=watForth.git;a=blobdiff_plain;f=forth.js;h=7e2f5a3b849f694c21ac6794c30829e49af4a56d;hp=486a50cf14db0a3c1e4c3e6dea169fbe99226873;hb=a83e7e42d457eb562902eea8090c3f2b51351686;hpb=0096f7eba3c6c1584234866ee3cbfe5b1df765c2 diff --git a/forth.js b/forth.js index 486a50c..7e2f5a3 100644 --- a/forth.js +++ b/forth.js @@ -1,3 +1,15 @@ +/* This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ 'use strict' let txtdiv = document.createElement("div") document.body.appendChild(txtdiv) @@ -19,7 +31,7 @@ document.addEventListener('keydown', (event) => { txtdiv = document.createElement("div") document.body.appendChild(txtdiv) forth() - output.print("__ok.") + output.print("ok.") txtdiv = document.createElement("div") document.body.appendChild(txtdiv) break @@ -50,7 +62,7 @@ const channels = [{ write: (readAddr, maxBytes) => output.print(String.fromCharCode.apply( null, - new Uint16Array(wasmMem.buffer, readAddr, maxBytes) + new Uint16Array(wasmMem.buffer, readAddr, maxBytes >> 1) )) }] const simstack = [] @@ -59,7 +71,7 @@ const dictionary = { ';': 1, 'LIT': 2, RINIT: 3, - WORD: 696, + WORD: 16500, KEY: 5, DUP: 6, '+': 7, @@ -85,12 +97,20 @@ const dictionary = { 'WORDS': 27, 'HERE': 28, 'DEFINE': 29, - ':': 900, - 'MODE': 384, - 'WBUF': 256, - 'EXECUTE-MODE': 800, - 'QUIT': 600, - 'INTERPRET': 512 + '2DUP': 30, + 'ROT': 31, + '2DROP': 32, + ',': 33, + '-': 34, + 'CHANNEL!': 35, + 'HERE!': 36, + '=?': 37, + '.s': 38, + ':': 16800, + 'MODE': 14336, + 'EXECUTE-MODE': 16680, + 'QUIT': 16384, + 'INTERPRET': 16400 } const wasmImport = { env: { @@ -99,16 +119,11 @@ const wasmImport = { rinit: () => rstack.length = 0, rpop: () => rstack.pop(), rpush: (val) => rstack.push(val), - sys_write: (channel, fromBuffer) => { + sys_write: (channel, addr, u) => { if (channels[channel] === undefined) return - const maxBytes = new DataView( - wasmMem.buffer, - fromBuffer, - 4 - ).getUint32(0,true) - console.log(`write ch:${channel} addr:${fromBuffer} len:${maxBytes}`) - channels[channel].write(fromBuffer + 4, maxBytes) + console.log(`write ch:${channel} addr:${addr} len:${u}`) + channels[channel].write(addr, u) }, sys_read: (channel, toBuffer) => { console.log(`read ch:${channel} buf:${toBuffer} current: ${stdin}`) @@ -147,15 +162,10 @@ const wasmImport = { .getUint32(0,true) }`) }, - vocab_get: (addr) => { - const bytes = new DataView( - wasmMem.buffer, - addr, - 4 - ).getUint32(0,true) + vocab_get: (addr, u) => { const word = String.fromCharCode.apply( null, - new Uint16Array(wasmMem.buffer, addr + 4, bytes >> 1) + new Uint16Array(wasmMem.buffer, addr, u >> 1) ) const answer = dictionary[word.toUpperCase()] console.log(`vocab_get ${word}: ${answer}`) @@ -163,15 +173,11 @@ const wasmImport = { return 0 return answer }, - vocab_set: (addr, num) => { - const bytes = new DataView( - wasmMem.buffer, - addr, - 4 - ).getUint32(0,true) + vocab_set: (addr, u, num) => { + console.log(`vocab set ${addr} ${u} ${num}`) const word = String.fromCharCode.apply( null, - new Uint16Array(wasmMem.buffer, addr + 4, bytes >> 1) + new Uint16Array(wasmMem.buffer, addr, u >> 1) ) console.log(`vocab_set ${word}: ${num}`) dictionary[word.toUpperCase()] = num @@ -179,19 +185,19 @@ const wasmImport = { }, is_whitespace: (key) => /\s/.test(String.fromCharCode(key)), sys_stack: () => console.log(`[${simstack}]`), - sys_parsenum: (addr, base) => { - const byteV = new DataView( - wasmMem.buffer, - addr, - 4 - ) + sys_parsenum: (addr, u, base) => { const word = String.fromCharCode.apply( null, - new Uint16Array(wasmMem.buffer, addr + 4, byteV.getUint32(0,true) >> 1) + new Uint16Array(wasmMem.buffer, addr, u >> 1) ) const answer = Number.parseInt(word, base) - byteV.setUint32(0,Number.isNaN(answer),true) - return answer + console.log(`parsenum: ${addr} ${u} ${base}`) + console.log(`parsenum: ${word} ${answer}`) + console.log(`parsenum: ${Number.isNaN(answer)}`) + if (Number.isNaN(answer)) + return -1 + new DataView(wasmMem.buffer, addr, 4).setUint32(0,answer,true) + return 0 }, sys_words: () => { output.print(Object.getOwnPropertyNames(dictionary).toString().split(',').join(' ')) @@ -199,7 +205,9 @@ const wasmImport = { } } -fetch('forth.wasm') +fetch('forth.forth').then((re) => re.text()).then((txt) => { + stdin = txt + fetch('forth.wasm') .then(re => re.arrayBuffer()) .then(buf => WebAssembly.instantiate(buf, wasmImport)) .then(result => { @@ -208,3 +216,4 @@ fetch('forth.wasm') console.log('wasm loaded'); forth() }) +})