formatting/prettiness
authorken <ken@mihrtec.com>
Mon, 26 Feb 2018 11:11:39 +0000 (03:11 -0800)
committerken <ken@mihrtec.com>
Mon, 26 Feb 2018 11:11:39 +0000 (03:11 -0800)
forth.forth
forth.js
forth.wat

index aa90b03..e7dc229 100644 (file)
@@ -17,12 +17,12 @@ word : here define
 : IWRITE-MODE ' dup , ' JNZ: , here 12 + , ' 2drop , ' ; ,
 ' 2dup , ' find , ' dup , ' JZ: , here 16 + , ' , , ' 2drop , ' ; ,
 ' drop , ' LIT , ' LIT , ' , , 16720 , ' , , ' ; ,
 : IWRITE-MODE ' dup , ' JNZ: , here 12 + , ' 2drop , ' ; ,
 ' 2dup , ' find , ' dup , ' JZ: , here 16 + , ' , , ' 2drop , ' ; ,
 ' drop , ' LIT , ' LIT , ' , , 16720 , ' , , ' ; ,
-: ^i ' LIT , ' MODE , ' LIT , ' IWRITE-MODE , ' ! , ' ; ,
-: ^e ' LIT , ' MODE , ' LIT , ' EXECUTE-MODE , ' ! , ' ; ,
-^i
+: i ' LIT , ' MODE , ' LIT , ' IWRITE-MODE , ' ! , ' ; ,
+: e ' LIT , ' MODE , ' LIT , ' EXECUTE-MODE , ' ! , ' ; ,
+\i
 \: FINISH-STRING DROP STRING-END ;
 \: " STRING-START
 \: KEYPUMP KEY 34 =? JNZ: FINISH-STRING STRING-PUT JMP: KEYPUMP
 \: FINISH-STRING DROP STRING-END ;
 \: " STRING-START
 \: KEYPUMP KEY 34 =? JNZ: FINISH-STRING STRING-PUT JMP: KEYPUMP
-\^e
+\e
 " watForth-32 Interactive CLI:
 " .s
 " watForth-32 Interactive CLI:
 " .s
index c05d444..27804d0 100644 (file)
--- a/forth.js
+++ b/forth.js
@@ -16,30 +16,60 @@ const initialize = Promise.all([
   fetch('forth.wasm').then(re => re.arrayBuffer())
 ])
 window.onload = () => {
   fetch('forth.wasm').then(re => re.arrayBuffer())
 ])
 window.onload = () => {
+  const simstack = []
+  const rstack = []
+
   let forthdiv = document.getElementById("forth")
   if (forthdiv === null) {
     forthdiv = document.createElement("div")
   let forthdiv = document.getElementById("forth")
   if (forthdiv === null) {
     forthdiv = document.createElement("div")
-    forthdiv.setAttribute("style","height:400px;")
+    forthdiv.setAttribute("style","height:100%;margin:auto;width:80%;")
     document.body.appendChild(forthdiv)
   }
   const outframe = document.createElement("div")
     document.body.appendChild(forthdiv)
   }
   const outframe = document.createElement("div")
-  outframe.setAttribute("style", "overflow-y:hidden;height:40%;")
+  outframe.setAttribute("style", "background-color:black;padding-left:6px;padding-right:6px;color:chartreuse;height:256px;resize:vertical;display:flex;align-items:flex-end;")
+  const stackview = document.createElement("pre")
+  stackview.setAttribute("style", "white-space:pre-wrap;flex:0 1 8%;")
+  outframe.appendChild(stackview)
   const txtoutput = document.createElement("pre")
   const txtoutput = document.createElement("pre")
-  txtoutput.setAttribute("style", "white-space:pre-wrap;")
-  const txtinput = document.createElement("textarea")
-  let txtinputrows = "1"
-  txtinput.setAttribute("autofocus", "true")  
-  txtinput.setAttribute("cols", "60")
-  txtinput.setAttribute("rows", txtinputrows)
+  txtoutput.setAttribute("style", "white-space:pre-wrap;overflow-y:scroll;flex:1 0 342px;")
   outframe.appendChild(txtoutput)
   outframe.appendChild(txtoutput)
+  const rstackview = document.createElement("pre")
+  rstackview.setAttribute("style", "white-space:pre-wrap;flex:0 1 8%;")
+  outframe.appendChild(rstackview)
+  const memview = document.createElement("pre")
+  memview.setAttribute("style", "white-space:pre-wrap;flex:0 0 356px;")
+  outframe.appendChild(memview)
+  const txtinput = document.createElement("textarea")
+  txtinput.setAttribute("autofocus", "true")
+  txtinput.setAttribute("rows", "1")
+  txtinput.setAttribute("style", "white-space:pre;margin-left:8%;width:60%;")
+  txtinput.oninput = () => txtinput.rows = (txtinput.value.match(/[\n]/g) || [1]).length;
   forthdiv.appendChild(outframe)
   forthdiv.appendChild(txtinput)
   forthdiv.appendChild(outframe)
   forthdiv.appendChild(txtinput)
-  const output = {
-    print: (string) => txtoutput.textContent += string
-  }
   let wasmMem
   let forth
   let wasmMem
   let forth
-
+  const output = {
+    print: (string) => txtoutput.textContent += `\\\ => ${string} \n`
+  }
+  const updateViews = () => {
+    stackview.textContent = simstack.join('\n')
+    rstackview.textContent = rstack.join('\n')
+    let cnt = 0;
+    const maxBytes = 256
+    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'
+      if ((cnt % 4) === 0)
+        return v + ', '
+      return v + ' '
+    }).join('')
+    outframe.scrollTop = outframe.scrollHeight
+  }
   /* Input capture */
   let stdin = ""
   txtinput.addEventListener('keydown', (event) => {
   /* Input capture */
   let stdin = ""
   txtinput.addEventListener('keydown', (event) => {
@@ -47,61 +77,26 @@ window.onload = () => {
     case "Enter":
       if (event.ctrlKey) {
         txtinput.value += '\n'
     case "Enter":
       if (event.ctrlKey) {
         txtinput.value += '\n'
-        txtinputrows = (Number.parseInt(txtinputrows, 10) + 1).toString()
-        txtinput.setAttribute("rows", txtinputrows)
+        txtinput.oninput()
       }
       else {
         stdin += txtinput.value
       }
       else {
         stdin += txtinput.value
-        txtoutput.textContent += txtinput.value + '\n'
+        txtoutput.textContent += txtinput.value
+        if (!/\s/.test(txtinput.value.slice(-1)))
+          txtoutput.textContent += " "
         txtinput.value = ""
         txtinput.value = ""
-        txtinputrows = "1"
-        txtinput.setAttribute("rows", txtinputrows)
         event.preventDefault()
         event.stopPropagation()
         forth()
         event.preventDefault()
         event.stopPropagation()
         forth()
-        txtoutput.textContent += " _ok.\n"
-        outframe.scrollTop = outframe.scrollHeight;
+        updateViews()
       }
       break
     case "Backspace":
       }
       break
     case "Backspace":
-      if (txtinput.value.charCodeAt(txtinput.value.length - 1) === 10) {
-        txtinputrows = (Number.parseInt(txtinputrows, 10) - 1).toString()
-        txtinput.setAttribute("rows", txtinputrows)
-      }
       break
     default:
       break
     }
   })
       break
     default:
       break
     }
   })
-/*  document.addEventListener('keydown', (event) => {
-    //console.log(`keydown: ${event.key}`)
-    if (event.key != "F5") {
-      event.preventDefault()
-      event.stopPropagation()
-      switch (event.key) {
-      case "Enter":
-        txtoutput.textContent += '\n'
-        stdin += '\n'
-        forth()
-        output.print(" _ok.")
-        outframe.scrollTop = outframe.scrollHeight;
-        txtoutput.textContent += '\n'
-        break
-      case "Backspace":
-        stdin = stdin.substring(0, stdin.length - 1)
-        txtoutput.textContent = txtoutput.textContent.substring(0, txtoutput.textContent.length - 1)
-        break
-      default:
-        if (event.key.length == 1) {
-          const input = event.ctrlKey ? ` \\\^${event.key} ` : event.key
-          stdin += input
-          output.print(input)
-        }
-        break
-      }
-    }
-  })*/
-
   const channels = [{
     read: (writeAddr, maxBytes) => {
       const maxChars = maxBytes >> 1
   const channels = [{
     read: (writeAddr, maxBytes) => {
       const maxChars = maxBytes >> 1
@@ -118,8 +113,6 @@ window.onload = () => {
         new Uint16Array(wasmMem.buffer, readAddr, maxBytes >> 1)
       ))
   }]
         new Uint16Array(wasmMem.buffer, readAddr, maxBytes >> 1)
       ))
   }]
-  const simstack = []
-  const rstack = []
   const dictionary = {
     ';': 1,
     'LIT': 2,
   const dictionary = {
     ';': 1,
     'LIT': 2,
@@ -241,6 +234,7 @@ window.onload = () => {
       forth = module.instance.exports.main
       console.log('wasm loaded')
       forth()
       forth = module.instance.exports.main
       console.log('wasm loaded')
       forth()
+      updateViews()
     })
   })
 }
     })
   })
 }
index a3a61e5..0c4b767 100644 (file)
--- a/forth.wat
+++ b/forth.wat
         br $next
       end ;; rot
         call $pop
         br $next
       end ;; rot
         call $pop
-        set_local $eax
         call $pop
         call $pop
+        set_local $eax
         call $pop
         call $pop
-        get_local $eax
+        call $rpush
         call $push
         call $push
+        call $rpop
         call $push
         call $push
+        get_local $eax
         call $push
         br $next
       end ;; dup2
         call $push
         br $next
       end ;; dup2