about summary refs log tree commit diff stats
path: root/baremetal/shell
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-02-22 20:23:43 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-02-22 20:23:43 -0800
commit2387a8fba4e6b58c71cb44784ef46af1be80729c (patch)
treed1fca8dfec5fd430c5dbd067334b1e682deaf7b1 /baremetal/shell
parent421ed4651f0f9ceb23f07f8d00797e61d63a29ad (diff)
downloadmu-2387a8fba4e6b58c71cb44784ef46af1be80729c.tar.gz
7785 - baremetal/shell: trace primitives done
Rendering traces will be an ongoing journey.
Diffstat (limited to 'baremetal/shell')
-rw-r--r--baremetal/shell/read.mu2
-rw-r--r--baremetal/shell/sandbox.mu16
-rw-r--r--baremetal/shell/trace.mu73
3 files changed, 86 insertions, 5 deletions
diff --git a/baremetal/shell/read.mu b/baremetal/shell/read.mu
index 5c6014d9..e619affd 100644
--- a/baremetal/shell/read.mu
+++ b/baremetal/shell/read.mu
@@ -121,7 +121,7 @@ fn next-number-token in: (addr gap-buffer), out: (addr stream byte), trace: (add
     var digit?/eax: boolean <- is-decimal-digit? g
     compare digit?, 0/false
     break-if-!=
-    abort "invalid number"
+    error trace, "invalid number"
   }
   var g/eax: grapheme <- read-from-gap-buffer in
   write-grapheme out, g
diff --git a/baremetal/shell/sandbox.mu b/baremetal/shell/sandbox.mu
index 6ba2e4ac..a766bc4d 100644
--- a/baremetal/shell/sandbox.mu
+++ b/baremetal/shell/sandbox.mu
@@ -12,6 +12,10 @@ fn initialize-sandbox _self: (addr sandbox) {
   initialize-gap-buffer data, 0x1000/4KB
   var value-ah/eax: (addr handle stream byte) <- get self, value
   populate-stream value-ah, 0x1000/4KB
+  var trace-ah/eax: (addr handle trace) <- get self, trace
+  allocate trace-ah
+  var trace/eax: (addr trace) <- lookup *trace-ah
+  initialize-trace trace, 0x100/lines
 }
 
 ## some helpers for tests
@@ -48,12 +52,13 @@ fn delete-grapheme-before-cursor _self: (addr sandbox) {
 fn render-sandbox screen: (addr screen), _self: (addr sandbox), _x: int, _y: int {
   clear-screen screen
   var self/esi: (addr sandbox) <- copy _self
+  # data
   var data-ah/eax: (addr handle gap-buffer) <- get self, data
   var _data/eax: (addr gap-buffer) <- lookup *data-ah
   var data/edx: (addr gap-buffer) <- copy _data
   var x/eax: int <- copy _x
   var y/ecx: int <- copy _y
-  x, y <- render-gap-buffer-wrapping-right-then-down screen, data, x, y, 0x20/xmax, 0x20/ymax, x, y, 1/true
+  x, y <- render-gap-buffer-wrapping-right-then-down screen, data, x, y, 0x20/xmax, 0x20/ymax, x, y, 1/show-cursor
   {
     var value-ah/eax: (addr handle stream byte) <- get self, value
     var value/eax: (addr stream byte) <- lookup *value-ah
@@ -63,6 +68,14 @@ fn render-sandbox screen: (addr screen), _self: (addr sandbox), _x: int, _y: int
     return
   }
   y <- increment
+  # trace
+  var trace-ah/eax: (addr handle trace) <- get self, trace
+  var _trace/eax: (addr trace) <- lookup *trace-ah
+  var trace/edx: (addr trace) <- copy _trace
+  y <- render-trace screen, trace, _x, y, 0x20/xmax, 0x20/ymax
+  y <- increment
+  # value
+  var x/eax: int <- copy 0
   x, y <- draw-text-wrapping-right-then-down screen, "=> ", _x, y, 0x20/xmax, 0x20/ymax, _x, y, 7/fg, 0/bg
   var x2/edx: int <- copy x
   var value-ah/eax: (addr handle stream byte) <- get self, value
@@ -99,6 +112,7 @@ fn edit-sandbox _self: (addr sandbox), key: byte {
     var value/edx: (addr stream byte) <- copy _value
     var trace-ah/eax: (addr handle trace) <- get self, trace
     var trace/eax: (addr trace) <- lookup *trace-ah
+    clear-trace trace
     run data, value, trace
     return
   }
diff --git a/baremetal/shell/trace.mu b/baremetal/shell/trace.mu
index 88ee0860..90a2a844 100644
--- a/baremetal/shell/trace.mu
+++ b/baremetal/shell/trace.mu
@@ -12,21 +12,88 @@ type trace-line {
   data: (handle array byte)
 }
 
+fn initialize-trace _self: (addr trace), capacity: int {
+  var self/eax: (addr trace) <- copy _self
+  var trace-ah/eax: (addr handle stream trace-line) <- get self, data
+  populate-stream trace-ah, capacity
+}
+
+fn clear-trace _self: (addr trace) {
+  var self/eax: (addr trace) <- copy _self
+  var trace-ah/eax: (addr handle stream trace-line) <- get self, data
+  var trace/eax: (addr stream trace-line) <- lookup *trace-ah
+  clear-stream trace  # leaks memory
+}
+
 fn has-errors? _self: (addr trace) -> _/eax: boolean {
+  var self/eax: (addr trace) <- copy _self
+  var trace-ah/eax: (addr handle stream trace-line) <- get self, data
+  var _trace/eax: (addr stream trace-line) <- lookup *trace-ah
+  var trace/esi: (addr stream trace-line) <- copy _trace
+  {
+    var done?/eax: boolean <- stream-empty? trace
+    compare done?, 0/false
+    break-if-!=
+    var curr-storage: trace-line
+    var curr/eax: (addr trace-line) <- address curr-storage
+    read-from-stream trace, curr
+    var curr-label-ah/eax: (addr handle array byte) <- get curr, label
+    var curr-label/eax: (addr array byte) <- lookup *curr-label-ah
+    var is-error?/eax: boolean <- string-equal? curr-label, "error"
+    compare is-error?, 0/false
+    loop-if-=
+    return 1/true
+  }
   return 0/false
 }
 
-fn trace _self: (addr trace), label: (addr array byte), data: (array stream byte) {
+fn trace _self: (addr trace), label: (addr array byte), data: (addr stream byte) {
+  var self/esi: (addr trace) <- copy _self
+  var line-storage: trace-line
+  var line/ecx: (addr trace-line) <- address line-storage
+  var depth/eax: (addr int) <- get self, curr-depth
+  initialize-trace-line *depth, label, data, line
+  var dest-ah/eax: (addr handle stream trace-line) <- get self, data
+  var dest/eax: (addr stream trace-line) <- lookup *dest-ah
+  write-to-stream dest, line
+}
+
+fn error self: (addr trace), data: (addr array byte) {
+  var s: (stream byte 0x100)
+  var s-a/eax: (addr stream byte) <- address s
+  write s-a, data
+  trace self, "error", s-a
 }
 
-fn new-trace-line depth: int, label: (addr array byte), data: (array stream byte), out: (addr trace-line) {
+fn initialize-trace-line depth: int, label: (addr array byte), data: (addr stream byte), _out: (addr trace-line) {
+  var out/edi: (addr trace-line) <- copy _out
+  # depth
+  var src/eax: int <- copy depth
+  var dest/ecx: (addr int) <- get out, depth
+  copy-to *dest, src
+  # label
+  var dest/eax: (addr handle array byte) <- get out, label
+  copy-array-object label, dest
+  # data
+  var dest/eax: (addr handle array byte) <- get out, data
+  stream-to-array data, dest
 }
 
 fn trace-lower _self: (addr trace) {
+  var self/esi: (addr trace) <- copy _self
+  var depth/eax: (addr int) <- get self, curr-depth
+  increment *depth
 }
 
 fn trace-higher _self: (addr trace) {
+  var self/esi: (addr trace) <- copy _self
+  var depth/eax: (addr int) <- get self, curr-depth
+  decrement *depth
 }
 
-fn render-trace screen: (addr screen), _self: (addr trace), _x: int, _y: int {
+fn render-trace screen: (addr screen), _self: (addr trace), xmin: int, ymin: int, xmax: int, ymax: int -> _/ecx: int {
+  var x/eax: int <- copy xmin
+  var y/ecx: int <- copy ymin
+  x, y <- draw-text-wrapping-right-then-down screen, "...", xmin, ymin, xmax, ymax, x, y, 9/fg=trace, 0/bg
+  return y
 }