about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-02-21 22:28:35 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-02-21 22:28:35 -0800
commite3f7765cb791c45513c67aadc0abc4865831b1c5 (patch)
tree1b11e5d79b76f349abcced91c35501008b672d0b
parent87fe53e691d3bbd78fa5c3d25e0228b469802040 (diff)
downloadmu-e3f7765cb791c45513c67aadc0abc4865831b1c5.tar.gz
7780
Start trimming whitespace from the input.

It's probably a bad idea to rebuild all our primitives for gap buffers.
-rw-r--r--baremetal/shell/gap-buffer.mu52
-rw-r--r--baremetal/shell/read.mu1
2 files changed, 53 insertions, 0 deletions
diff --git a/baremetal/shell/gap-buffer.mu b/baremetal/shell/gap-buffer.mu
index f077c3d9..6e5d7f6d 100644
--- a/baremetal/shell/gap-buffer.mu
+++ b/baremetal/shell/gap-buffer.mu
@@ -611,6 +611,42 @@ fn gap-buffer-scan-done? _self: (addr gap-buffer) -> _/eax: boolean {
   return 1/true
 }
 
+fn peek-from-gap-buffer _self: (addr gap-buffer) -> _/eax: grapheme {
+  var self/esi: (addr gap-buffer) <- copy _self
+  # more in left?
+  var left/ecx: (addr grapheme-stack) <- get self, left
+  var left-size/eax: int <- grapheme-stack-length left
+  var left-read-index-a/edx: (addr int) <- get self, left-read-index
+  compare *left-read-index-a, left-size
+  {
+    break-if->=
+    var left-data-ah/eax: (addr handle array grapheme) <- get left, data
+    var left-data/eax: (addr array grapheme) <- lookup *left-data-ah
+    var left-read-index/ecx: int <- copy *left-read-index-a
+    var result/eax: (addr grapheme) <- index left-data, left-read-index
+    return *result
+  }
+  # more in right?
+  var right/ecx: (addr grapheme-stack) <- get self, right
+  var _right-size/eax: int <- grapheme-stack-length right
+  var right-size/ebx: int <- copy _right-size
+  var right-read-index-a/edx: (addr int) <- get self, right-read-index
+  compare *right-read-index-a, right-size
+  {
+    break-if->=
+    # read the right from reverse
+    var right-data-ah/eax: (addr handle array grapheme) <- get right, data
+    var right-data/eax: (addr array grapheme) <- lookup *right-data-ah
+    var right-read-index/ebx: int <- copy right-size
+    right-read-index <- subtract *right-read-index-a
+    right-read-index <- subtract 1
+    var result/eax: (addr grapheme) <- index right-data, right-read-index
+    return *result
+  }
+  # if we get here there's nothing left
+  return 0/nul
+}
+
 fn read-from-gap-buffer _self: (addr gap-buffer) -> _/eax: grapheme {
   var self/esi: (addr gap-buffer) <- copy _self
   # more in left?
@@ -698,3 +734,19 @@ fn test-read-from-gap-buffer {
   var x/ecx: int <- copy g
   check-ints-equal x, 0/nul, "F - test-read-from-gap-buffer/right-4"
 }
+
+fn skip-whitespace-from-gap-buffer self: (addr gap-buffer) {
+  var done?/eax: boolean <- gap-buffer-scan-done? self
+  compare done?, 0/false
+  break-if-!=
+  var g/eax: grapheme <- peek-from-gap-buffer self
+  {
+    compare g, 0x20/space
+    break-if-=
+    compare g, 0xa/newline
+    break-if-=
+    return
+  }
+  g <- read-from-gap-buffer self
+  loop
+}
diff --git a/baremetal/shell/read.mu b/baremetal/shell/read.mu
index b6446b55..81dd6f07 100644
--- a/baremetal/shell/read.mu
+++ b/baremetal/shell/read.mu
@@ -21,6 +21,7 @@ fn read-cell in: (addr gap-buffer), out: (addr handle cell) {
 
 fn next-token in: (addr gap-buffer), out: (addr stream byte) {
   clear-stream out
+  skip-whitespace-from-gap-buffer in
   {
     var done?/eax: boolean <- gap-buffer-scan-done? in
     compare done?, 0/false