X-Git-Url: https://git.kengrimes.com/?p=watForth.git;a=blobdiff_plain;f=forth.js;h=9c0e58d2dc57e8128b67b3295ebe37fbec366d6a;hp=57aac39ba44d4e295354fd2c869c1a0fc9a7c65c;hb=94d40c7e5521898acd394ca7a3e30cf20065ee5c;hpb=63e17ddc993a5a04cc6ae55bad8420b61c37d1bf diff --git a/forth.js b/forth.js index 57aac39..9c0e58d 100644 --- a/forth.js +++ b/forth.js @@ -16,59 +16,74 @@ const initialize = Promise.all([ fetch('forth.wasm', {credentials: 'include', headers:{'content-type':'application/wasm'}}).then(re => re.arrayBuffer()) ]) window.onload = () => { - const simstack = [] - const rstack = [] - + /* Initialize Views */ let forthdiv = document.getElementById("forth") if (forthdiv === null) { forthdiv = document.createElement("div") - forthdiv.setAttribute("style","height:100%;margin:auto;width:100%;overflow-x:hidden;") + forthdiv.setAttribute("style","height:100%;margin:auto;width:100%;max-width:640px;overflow-x:hidden;") document.body.appendChild(forthdiv) } const outframe = document.createElement("div") - outframe.setAttribute("style", "background-color:black;padding-left:6px;padding-right:6px;color:chartreuse;height:256px;resize:vertical;display:flex;align-items:flex-end;flex-flow:row") + outframe.setAttribute("style", "background-color:black;padding-left:6px;padding-right:6px;color:chartreuse;height:268px;resize:vertical;display:flex;align-items:flex-end;flex-flow:row;") const stackview = document.createElement("pre") stackview.setAttribute("style", "white-space:pre-wrap;flex:0 0 8%;") outframe.appendChild(stackview) const txtoutput = document.createElement("pre") - txtoutput.setAttribute("style", "white-space:pre-wrap;overflow-y:scroll;flex:1 0 342px;") + txtoutput.setAttribute("style", "white-space:pre-wrap;max-height:256px;overflow-y:scroll;flex:1 0 342px;") outframe.appendChild(txtoutput) - const rstackview = document.createElement("pre") + /* const rstackview = document.createElement("pre") rstackview.setAttribute("style", "white-space:pre-wrap;flex:0 1 8%;") - outframe.appendChild(rstackview) + outframe.appendChild(rstackview) */ const memview = document.createElement("pre") - memview.setAttribute("style", "white-space:pre-wrap;flex:0 0 8%;") + memview.setAttribute("style", "padding-left: 8px; white-space:pre-wrap;flex:0 0 88px;") outframe.appendChild(memview) const txtinput = document.createElement("textarea") txtinput.setAttribute("autofocus", "true") txtinput.setAttribute("wrap", "hard") - txtinput.setAttribute("style", "resize:none;white-space:pre;margin-left:8%;width:60%;") - txtinput.oninput = () => txtinput.rows = (txtinput.value.match(/[\n]/g) || [1]).length + 1; + txtinput.setAttribute("style", "resize:none;white-space:pre;margin-left:8%;width:75%;") + txtinput.oninput = () => txtinput.setAttribute("rows", ((txtinput.value.match(/[\n]/g) || [1]).length + 1).toString()); txtinput.oninput() forthdiv.appendChild(outframe) forthdiv.appendChild(txtinput) + + /* Initialize State */ + const simstack = [] + const rstack = [] let wasmMem let forth + const dictionary = { EXECUTE: 12 } + const doesDictionary = {} + + /* Environment functions */ const output = { print: (string) => txtoutput.textContent += `\\\ => ${string} \n` } + const wasmString = (addr, u) => + String.fromCharCode.apply( + null, + new Uint16Array(wasmMem.buffer, addr, u >> 1) + ) const updateViews = () => { - stackview.textContent = simstack.join('\n') - rstackview.textContent = rstack.join('\n') + const base = new DataView(wasmMem.buffer, 14348 /* base */, 4).getUint32(0,true) + stackview.textContent = simstack.map((v) => v.toString(base)).join('\n') + // rstackview.textContent = rstack.join('\n') let cnt = 0; - const maxBytes = 12 + const maxBytes = 64 let here = new DataView(wasmMem.buffer, 14340 /* here */, 4).getUint32(0,true) memview.textContent = Array.from(new Uint8Array(wasmMem.buffer, here - maxBytes, maxBytes), (v) => { cnt++; v = ('0' + (v & 0xFF).toString(16)).slice(-2) if (cnt === maxBytes) return v + if ((cnt % 16) === 0) + return `${v}\n=> ${(here -maxBytes + cnt).toString(base)}\n` if ((cnt % 4) === 0) - return v + '\n==\n' - return v + '\n' + return `${v}\n` + return `${v} ` }).join('') outframe.scrollTop = outframe.scrollHeight } + /* Input capture */ let stdin = "" txtinput.addEventListener('keydown', (event) => { @@ -93,6 +108,7 @@ window.onload = () => { event.stopPropagation() forth() updateViews() + txtoutput.scrollTop = txtoutput.scrollHeight } break case "Backspace": @@ -117,10 +133,8 @@ window.onload = () => { new Uint16Array(wasmMem.buffer, readAddr, maxBytes >> 1) )) }] - const dictionary = { - EXECUTE: 12 - } - const doesDictionary = {} + + /* System runtime */ const wasmImport = { env: { pop: () => simstack.pop(), @@ -143,7 +157,7 @@ window.onload = () => { //"textEntry" or any third party protocols like activitypub console.log(`fetch ${channel} ${reqAddr}`) }, - sys_echo: (val) => output.print(`${val} `), + sys_echo: (val, base) => output.print(`${val.toString(base)} `), sys_echochar: (val) => output.print(String.fromCharCode(val)), sys_reflect: (addr) => { console.log(`reflect: ${addr}: ${ @@ -151,34 +165,14 @@ window.onload = () => { .getUint32(0,true) }`) }, - vocab_get: (addr, u) => { - const word = String.fromCharCode.apply( - null, - new Uint16Array(wasmMem.buffer, addr, u >> 1) - ) - const answer = dictionary[word.toUpperCase()] - if (answer === undefined) - return 0 - return answer - }, - vocab_set: (addr, u, num) => { - const word = String.fromCharCode.apply( - null, - new Uint16Array(wasmMem.buffer, addr, u >> 1) - ) - dictionary[word.toUpperCase()] = num - return 0 - }, - does_get: (u) => doesDictionary[u] || 0, - does_set: (u, v) => doesDictionary[u] = v, + vocab_get: (addr, u) => dictionary[wasmString(addr, u).toUpperCase()] || 0, + vocab_set: (addr, u, v) => dictionary[wasmString(addr, u).toUpperCase()] = v, + does_get: (addr, u) => doesDictionary[wasmString(addr, u).toUpperCase()] || 0, + does_set: (addr, u, v) => doesDictionary[wasmString(addr, u).toUpperCase()] = v, is_whitespace: (key) => /\s/.test(String.fromCharCode(key)), - sys_stack: () => console.log(`[${simstack}]`), + sys_stack: () => console.log(`[${simstack}][${rstack}]`), sys_parsenum: (addr, u, base) => { - const word = String.fromCharCode.apply( - null, - new Uint16Array(wasmMem.buffer, addr, u >> 1) - ) - const answer = Number.parseInt(word, base) + const answer = Number.parseInt(wasmString(addr, u), base) if (Number.isNaN(answer)) return -1 new DataView(wasmMem.buffer, addr, 4).setUint32(0,answer,true)