+/* 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 <http://www.gnu.org/licenses/>. */
'use strict'
let txtdiv = document.createElement("div")
document.body.appendChild(txtdiv)
txtdiv = document.createElement("div")
document.body.appendChild(txtdiv)
forth()
- output.print("__ok.")
+ output.print("ok.")
txtdiv = document.createElement("div")
document.body.appendChild(txtdiv)
break
write: (readAddr, maxBytes) =>
output.print(String.fromCharCode.apply(
null,
- new Uint16Array(wasmMem.buffer, readAddr, maxBytes)
+ new Uint16Array(wasmMem.buffer, readAddr, maxBytes >> 1)
))
}]
const simstack = []
';': 1,
'LIT': 2,
RINIT: 3,
- WORD: 696,
+ WORD: 16500,
KEY: 5,
DUP: 6,
'+': 7,
'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: {
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}`)
.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}`)
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
},
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(' '))
}
}
-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 => {
console.log('wasm loaded');
forth()
})
+})