word belts, utf16 strings, interrupt words, compiler modes
[watForth.git] / forth.js
index 486a50c..ffe8044 100644 (file)
--- a/forth.js
+++ b/forth.js
@@ -19,7 +19,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
@@ -59,7 +59,7 @@ const dictionary = {
   ';': 1,
   'LIT': 2,
   RINIT: 3,
-  WORD: 696,
+  WORD: 16500,
   KEY: 5,
   DUP: 6,
   '+': 7,
@@ -85,12 +85,19 @@ 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,
+  ':': 16800,
+  'MODE': 14336,
+  'EXECUTE-MODE': 16680,
+  'QUIT': 16384,
+  'INTERPRET': 16400
 }
 const wasmImport = {
   env: {
@@ -147,15 +154,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 +165,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 +177,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 +197,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 +208,4 @@ fetch('forth.wasm')
     console.log('wasm loaded');
     forth()
   })
+})