about summary refs log tree commit diff stats
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/tile/environment.mu153
-rw-r--r--apps/tile/gap-buffer.mu48
-rw-r--r--apps/tile/main.mu45
-rw-r--r--apps/tui.mu2
4 files changed, 195 insertions, 53 deletions
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu
new file mode 100644
index 00000000..c14d34af
--- /dev/null
+++ b/apps/tile/environment.mu
@@ -0,0 +1,153 @@
+type environment {
+  screen: (handle screen)
+  buf: gap-buffer
+  cursor-row: int
+  cursor-col: int
+}
+
+fn initialize-environment _env: (addr environment) {
+  var env/esi: (addr environment) <- copy _env
+  var screen-ah/edi: (addr handle screen) <- get env, screen
+  var _screen/eax: (addr screen) <- lookup *screen-ah
+  var screen/edi: (addr screen) <- copy _screen
+  var nrows/eax: int <- copy 0
+  var ncols/ecx: int <- copy 0
+  nrows, ncols <- screen-size screen
+  # cursor-col
+  var midcol/edx: int <- copy ncols
+  midcol <- shift-right 1
+  var start-col/edx: int <- copy midcol
+  start-col <- add 2
+  {
+    var cursor-col/eax: (addr int) <- get env, cursor-col
+    copy-to *cursor-col start-col
+  }
+  # cursor-row
+  var midrow/ebx: int <- copy 0
+  {
+    var tmp/eax: int <- try-divide nrows, 3
+    midrow <- copy tmp
+  }
+  var start-row/ebx: int <- copy midrow
+  start-row <- subtract 3
+  {
+    var cursor-row/eax: (addr int) <- get env, cursor-row
+    copy-to *cursor-row start-row
+  }
+  # buf
+  var gap/eax: (addr gap-buffer) <- get env, buf
+  initialize-gap-buffer gap
+}
+
+fn initialize-environment-with-fake-screen _self: (addr environment), nrows: int, ncols: int {
+  var self/esi: (addr environment) <- copy _self
+  var screen-ah/eax: (addr handle screen) <- get self, screen
+  allocate screen-ah
+  var screen-addr/eax: (addr screen) <- lookup *screen-ah
+  initialize-screen screen-addr, nrows, ncols
+  initialize-environment self
+}
+
+fn render-loop _self: (addr environment) {
+  var self/esi: (addr environment) <- copy _self
+  render self
+  #
+  $interactive:loop: {
+    var key/eax: grapheme <- read-key-from-real-keyboard
+    compare key, 0x71  # 'q'
+    break-if-=
+    process self, key
+    loop
+  }
+}
+
+fn process _self: (addr environment), key: grapheme {
+$process:body: {
+    var self/esi: (addr environment) <- copy _self
+    var screen-ah/edi: (addr handle screen) <- get self, screen
+    var _screen/eax: (addr screen) <- lookup *screen-ah
+    var screen/edi: (addr screen) <- copy _screen
+    var buf/ebx: (addr gap-buffer) <- get self, buf
+    compare key, 0x445b1b  # left-arrow
+    {
+      break-if-!=
+      var foo/eax: grapheme <- gap-left buf
+      compare foo, -1
+      {
+        break-if-=
+#?         print-string-to-real-screen "XXX\n"
+        var cursor-col/eax: (addr int) <- get self, cursor-col
+        decrement *cursor-col
+#?         print-int32-decimal-to-real-screen *cursor-col
+#?         print-string-to-real-screen "ZZZ\n"
+      }
+      var cursor-row/eax: (addr int) <- get self, cursor-row
+      var cursor-col/ecx: (addr int) <- get self, cursor-col
+      move-cursor screen, *cursor-row, *cursor-col
+      break $process:body
+    }
+    compare key, 0x435b1b  # right-arrow
+    {
+      break-if-!=
+      var foo/eax: grapheme <- gap-right buf
+      compare foo, -1
+      {
+        break-if-=
+        var cursor-col/eax: (addr int) <- get self, cursor-col
+        increment *cursor-col
+      }
+      break $process:body
+    }
+    var g/ecx: grapheme <- copy key
+    var print?/eax: boolean <- real-grapheme? key
+    {
+      compare print?, 0  # false
+      break-if-=
+      add-grapheme-at-gap buf, g
+      var cursor-col/eax: (addr int) <- get self, cursor-col
+      increment *cursor-col
+      render self
+      break $process:body
+    }
+    # silently ignore other hotkeys
+}
+}
+
+fn render _env: (addr environment) {
+  var env/esi: (addr environment) <- copy _env
+  var screen-ah/edi: (addr handle screen) <- get env, screen
+  var _screen/eax: (addr screen) <- lookup *screen-ah
+  var screen/edi: (addr screen) <- copy _screen
+  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
+#?   # some debug info
+#?   ncols <- subtract 2
+#?   move-cursor screen, 1, ncols
+  var buf/ecx: (addr gap-buffer) <- get env, buf
+#?   var foo/eax: int <- gap-buffer-length buf
+#?   print-int32-decimal screen, foo
+  #
+  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
+  # update cursor
+  var cursor-row/eax: (addr int) <- get env, cursor-row
+  var cursor-col/ecx: (addr int) <- get env, cursor-col
+  move-cursor screen, *cursor-row, *cursor-col
+}
diff --git a/apps/tile/gap-buffer.mu b/apps/tile/gap-buffer.mu
index 11cd34b5..9d6b7161 100644
--- a/apps/tile/gap-buffer.mu
+++ b/apps/tile/gap-buffer.mu
@@ -19,6 +19,17 @@ fn render-gap-buffer screen: (addr screen), _gap: (addr gap-buffer) {
   render-stack-from-top right, screen
 }
 
+fn gap-buffer-length _gap: (addr gap-buffer) -> result/eax: int {
+  var gap/esi: (addr gap-buffer) <- copy _gap
+  var left/eax: (addr grapheme-stack) <- get gap, left
+  var tmp/eax: (addr int) <- get left, top
+  var left-length/ecx: int <- copy *tmp
+  var right/esi: (addr grapheme-stack) <- get gap, right
+  tmp <- get right, top
+  result <- copy *tmp
+  result <- add left-length
+}
+
 # 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) {
@@ -58,39 +69,48 @@ fn render-stack-from-top _self: (addr grapheme-stack), screen: (addr screen) {
   }
 }
 
-fn add-grapheme _self: (addr gap-buffer), g: grapheme {
+fn add-grapheme-at-gap _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) {
+fn gap-right _self: (addr gap-buffer) -> result/eax: grapheme {
+$gap-right:body: {
   var self/esi: (addr gap-buffer) <- copy _self
-  var g/ecx: grapheme <- copy 0
+  var g/edx: grapheme <- copy 0
   {
-    var right/eax: (addr grapheme-stack) <- get self, right
-    var tmp/eax: grapheme <- pop-grapheme-stack right
-    g <- copy tmp
+    var right/ecx: (addr grapheme-stack) <- get self, right
+    result <- pop-grapheme-stack right
+    compare result, -1
+    break-if-= $gap-right:body
+    g <- copy result
   }
   {
-    var left/eax: (addr grapheme-stack) <- get self, left
+    var left/ecx: (addr grapheme-stack) <- get self, left
+    # HERE: can't use 'result' here: "unknown variable 'result'"
     push-grapheme-stack left, g
   }
 }
+}
 
-fn cursor-left _self: (addr gap-buffer) {
+fn gap-left _self: (addr gap-buffer) -> result/eax: grapheme {
+$gap-left:body: {
   var self/esi: (addr gap-buffer) <- copy _self
-  var g/ecx: grapheme <- copy 0
+  var g/edx: grapheme <- copy 0
   {
-    var left/eax: (addr grapheme-stack) <- get self, left
-    var tmp/eax: grapheme <- pop-grapheme-stack left
-    g <- copy tmp
+    var left/ecx: (addr grapheme-stack) <- get self, left
+    result <- pop-grapheme-stack left
+    compare result, -1
+    break-if-= $gap-left:body
+    g <- copy result
   }
   {
-    var right/eax: (addr grapheme-stack) <- get self, right
+    var right/ecx: (addr grapheme-stack) <- get self, right
     push-grapheme-stack right, g
   }
 }
+}
 
 type grapheme-stack {
   data: (handle array grapheme)
@@ -124,7 +144,7 @@ $pop-grapheme-stack:body: {
   {
     compare *top-addr, 0
     break-if->
-    val <- copy 0
+    val <- copy -1
     break $pop-grapheme-stack:body
   }
   subtract-from *top-addr, 1
diff --git a/apps/tile/main.mu b/apps/tile/main.mu
index 7c7065fc..fdbb69ca 100644
--- a/apps/tile/main.mu
+++ b/apps/tile/main.mu
@@ -15,7 +15,7 @@ fn main args-on-stack: (addr array addr array byte) -> exit-status/ebx: int {
       break-if-!=
       var tmp/ecx: (addr addr array byte) <- index args, 1
       var tmp2/eax: boolean <- string-equal? *tmp, "test"
-      compare tmp2, 0
+      compare tmp2, 0  # false
       {
         break-if-=
         run-tests
@@ -33,46 +33,15 @@ 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
-  }
+  var env-storage: environment
+  var env/esi: (addr environment) <- address env-storage
+  initialize-environment env
+  render-loop env
   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
+fn real-grapheme? g: grapheme -> result/eax: boolean {
+  result <- copy 1  # true
 }
diff --git a/apps/tui.mu b/apps/tui.mu
index a02d8fb0..be74e1c4 100644
--- a/apps/tui.mu
+++ b/apps/tui.mu
@@ -23,7 +23,7 @@ fn main -> exit-status/ebx: int {
 
   print-string 0, "press a key to see its code: "
   enable-keyboard-immediate-mode
-  var x/eax: byte <- read-key-from-real-keyboard
+  var x/eax: grapheme <- read-key-from-real-keyboard
   enable-keyboard-type-mode
   enable-screen-type-mode
   print-string 0, "You pressed "