about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-09-16 15:37:35 -0700
committerKartik Agaram <vc@akkartik.com>2020-09-16 15:39:05 -0700
commit5029dac235741025ccec7d2c3d5609724cb14525 (patch)
treeb867bf22143c31043de38f9cabc12a1d7ac37bd8
parent985e85cc56cd8099a04b46bf0e5a2d2ca521e862 (diff)
downloadmu-5029dac235741025ccec7d2c3d5609724cb14525.tar.gz
6789 - tile: print keystrokes to screen
-rw-r--r--apps/tile/box.mu44
-rw-r--r--apps/tile/gap-buffer.mu137
-rw-r--r--apps/tile/main.mu44
3 files changed, 225 insertions, 0 deletions
diff --git a/apps/tile/box.mu b/apps/tile/box.mu
new file mode 100644
index 00000000..7cb69206
--- /dev/null
+++ b/apps/tile/box.mu
@@ -0,0 +1,44 @@
+fn draw-box screen: (addr screen), row1: int, col1: int, row2: int, col2: int {
+  draw-horizontal-line screen, row1, col1, col2
+  draw-vertical-line screen, row1, row2, col1
+  draw-horizontal-line screen, row2, col1, col2
+  draw-vertical-line screen, row1, row2, col2
+}
+
+fn draw-hatching screen: (addr screen), row1: int, col1: int, row2: int, col2: int {
+  var c/eax: int <- copy col1
+  var r1/ecx: int <- copy row1
+  r1 <- increment
+  c <- add 2
+  {
+    compare c, col2
+    break-if->=
+    draw-vertical-line screen, r1, row2, c
+    c <- add 2
+    loop
+  }
+}
+
+fn draw-horizontal-line screen: (addr screen), row: int, col1: int, col2: int {
+  var col/eax: int <- copy col1
+  move-cursor 0, row, col
+  {
+    compare col, col2
+    break-if->=
+    print-code-point screen, 0x2500
+    col <- increment
+    loop
+  }
+}
+
+fn draw-vertical-line screen: (addr screen), row1: int, row2: int, col: int {
+  var row/eax: int <- copy row1
+  {
+    compare row, row2
+    break-if->=
+    move-cursor 0, row, col
+    print-code-point screen, 0x2502
+    row <- increment
+    loop
+  }
+}
diff --git a/apps/tile/gap-buffer.mu b/apps/tile/gap-buffer.mu
new file mode 100644
index 00000000..11cd34b5
--- /dev/null
+++ b/apps/tile/gap-buffer.mu
@@ -0,0 +1,137 @@
+type gap-buffer {
+  left: grapheme-stack
+  right: grapheme-stack
+}
+
+fn initialize-gap-buffer _self: (addr gap-buffer) {
+  var self/esi: (addr gap-buffer) <- copy _self
+  var left/eax: (addr grapheme-stack) <- get self, left
+  initialize-grapheme-stack left, 0x10
+  var right/eax: (addr grapheme-stack) <- get self, right
+  initialize-grapheme-stack right, 0x10
+}
+
+fn render-gap-buffer screen: (addr screen), _gap: (addr gap-buffer) {
+  var gap/esi: (addr gap-buffer) <- copy _gap
+  var left/eax: (addr grapheme-stack) <- get gap, left
+  render-stack-from-bottom left, screen
+  var right/eax: (addr grapheme-stack) <- get gap, right
+  render-stack-from-top right, screen
+}
+
+# dump stack to screen from bottom to top
+# don't move the cursor or anything
+fn render-stack-from-bottom _self: (addr grapheme-stack), screen: (addr screen) {
+  var self/esi: (addr grapheme-stack) <- copy _self
+  var data-ah/edi: (addr handle array grapheme) <- get self, data
+  var _data/eax: (addr array grapheme) <- lookup *data-ah
+  var data/edi: (addr array grapheme) <- copy _data
+  var top-addr/ecx: (addr int) <- get self, top
+  var i/eax: int <- copy 0
+  {
+    compare i, *top-addr
+    break-if->=
+    var g/edx: (addr grapheme) <- index data, i
+    print-grapheme screen, *g
+    i <- increment
+    loop
+  }
+}
+
+# dump stack to screen from top to bottom
+# don't move the cursor or anything
+fn render-stack-from-top _self: (addr grapheme-stack), screen: (addr screen) {
+  var self/esi: (addr grapheme-stack) <- copy _self
+  var data-ah/edi: (addr handle array grapheme) <- get self, data
+  var _data/eax: (addr array grapheme) <- lookup *data-ah
+  var data/edi: (addr array grapheme) <- copy _data
+  var top-addr/ecx: (addr int) <- get self, top
+  var i/eax: int <- copy *top-addr
+  i <- decrement
+  {
+    compare i, 0
+    break-if-<
+    var g/edx: (addr grapheme) <- index data, i
+    print-grapheme screen, *g
+    i <- decrement
+    loop
+  }
+}
+
+fn add-grapheme _self: (addr gap-buffer), g: grapheme {
+  var self/esi: (addr gap-buffer) <- copy _self
+  var left/eax: (addr grapheme-stack) <- get self, left
+  push-grapheme-stack left, g
+}
+
+fn cursor-right _self: (addr gap-buffer) {
+  var self/esi: (addr gap-buffer) <- copy _self
+  var g/ecx: grapheme <- copy 0
+  {
+    var right/eax: (addr grapheme-stack) <- get self, right
+    var tmp/eax: grapheme <- pop-grapheme-stack right
+    g <- copy tmp
+  }
+  {
+    var left/eax: (addr grapheme-stack) <- get self, left
+    push-grapheme-stack left, g
+  }
+}
+
+fn cursor-left _self: (addr gap-buffer) {
+  var self/esi: (addr gap-buffer) <- copy _self
+  var g/ecx: grapheme <- copy 0
+  {
+    var left/eax: (addr grapheme-stack) <- get self, left
+    var tmp/eax: grapheme <- pop-grapheme-stack left
+    g <- copy tmp
+  }
+  {
+    var right/eax: (addr grapheme-stack) <- get self, right
+    push-grapheme-stack right, g
+  }
+}
+
+type grapheme-stack {
+  data: (handle array grapheme)
+  top: int
+}
+
+fn initialize-grapheme-stack _self: (addr grapheme-stack), n: int {
+  var self/esi: (addr grapheme-stack) <- copy _self
+  var d/edi: (addr handle array grapheme) <- get self, data
+  populate d, n
+  var top/eax: (addr int) <- get self, top
+  copy-to *top, 0
+}
+
+fn push-grapheme-stack _self: (addr grapheme-stack), _val: grapheme {
+  var self/esi: (addr grapheme-stack) <- copy _self
+  var top-addr/ecx: (addr int) <- get self, top
+  var data-ah/edx: (addr handle array grapheme) <- get self, data
+  var data/eax: (addr array grapheme) <- lookup *data-ah
+  var top/edx: int <- copy *top-addr
+  var dest-addr/edx: (addr grapheme) <- index data, top
+  var val/eax: grapheme <- copy _val
+  copy-to *dest-addr, val
+  add-to *top-addr, 1
+}
+
+fn pop-grapheme-stack _self: (addr grapheme-stack) -> val/eax: grapheme {
+$pop-grapheme-stack:body: {
+  var self/esi: (addr grapheme-stack) <- copy _self
+  var top-addr/ecx: (addr int) <- get self, top
+  {
+    compare *top-addr, 0
+    break-if->
+    val <- copy 0
+    break $pop-grapheme-stack:body
+  }
+  subtract-from *top-addr, 1
+  var data-ah/edx: (addr handle array grapheme) <- get self, data
+  var data/eax: (addr array grapheme) <- lookup *data-ah
+  var top/edx: int <- copy *top-addr
+  var result-addr/eax: (addr grapheme) <- index data, top
+  val <- copy *result-addr
+}
+}
diff --git a/apps/tile/main.mu b/apps/tile/main.mu
index 9a83ed5a..7c7065fc 100644
--- a/apps/tile/main.mu
+++ b/apps/tile/main.mu
@@ -31,4 +31,48 @@ fn main args-on-stack: (addr array addr array byte) -> exit-status/ebx: int {
 }
 
 fn interactive args: (addr array addr array byte) -> exit-status/ebx: int {
+  enable-screen-grid-mode
+  enable-keyboard-immediate-mode
+  var buf-storage: gap-buffer
+  var buf/esi: (addr gap-buffer) <- address buf-storage
+  initialize-gap-buffer buf
+  #
+  {
+    render 0, buf
+    var key/eax: byte <- read-key-from-real-keyboard
+    compare key, 0x71  # 'q'
+    break-if-=
+    var g/ecx: grapheme <- copy key
+    add-grapheme buf, g
+    loop
+  }
+  enable-keyboard-type-mode
+  enable-screen-type-mode
+  exit-status <- copy 0
+}
+
+fn render screen: (addr screen), buf: (addr gap-buffer) {
+  clear-screen screen
+  var nrows/eax: int <- copy 0
+  var ncols/ecx: int <- copy 0
+  nrows, ncols <- screen-size screen
+  var midcol/edx: int <- copy ncols
+  midcol <- shift-right 1
+  draw-vertical-line screen, 1, nrows, midcol
+  var midrow/ebx: int <- copy 0
+  {
+    var tmp/eax: int <- try-divide nrows, 3
+    midrow <- copy tmp
+  }
+  var left-col/edx: int <- copy midcol
+  left-col <- increment
+  draw-horizontal-line screen, midrow, left-col, ncols
+  # initialize cursor
+  var start-row/ebx: int <- copy midrow
+  start-row <- subtract 3
+  var start-col/edx: int <- copy left-col
+  start-col <- increment
+  move-cursor screen, start-row, start-col
+  #
+  render-gap-buffer screen, buf
 }