about summary refs log tree commit diff stats
path: root/shell
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-07-05 18:08:40 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-07-05 18:08:40 -0700
commit5d8a858a6f0bbaaec13c39b503b8e180f11c5d6c (patch)
tree4ac801d5058e194215bd908d203271bd3fe88826 /shell
parent0b07a433674d8fe9ad66c559e022732dfe9faf01 (diff)
downloadmu-5d8a858a6f0bbaaec13c39b503b8e180f11c5d6c.tar.gz
replace 'hline' with Mu implementation
Diffstat (limited to 'shell')
-rw-r--r--shell/data.limg12
-rw-r--r--shell/primitives.mu162
2 files changed, 165 insertions, 9 deletions
diff --git a/shell/data.limg b/shell/data.limg
index 576d91a0..e4a5c8cb 100644
--- a/shell/data.limg
+++ b/shell/data.limg
@@ -122,17 +122,13 @@
      (while ,test
        ,@body
        ,update))])
-    (hline1 . [def (hline1 screen y x xmax color)
-  while (x < xmax)
-    (pixel screen x y color)
-    ++x])
     (vline1 . [def (vline1 screen x y ymax color)
   while (y < ymax)
     (pixel screen x y color)
     ++y])
-    (hline . [def (hline scr y color)
-  (hline1 scr y 0 (width scr) color)])
-    (vline . [def (vline scr x color)
+    (hborder . [def (hborder scr y color)
+  (hline scr y 0 (width scr) color)])
+    (vborder . [def (vborder scr x color)
   (vline1 scr x 0 (height scr) color)])
     (read_line . [def (read_line keyboard)
   ret str (stream)
@@ -147,7 +143,7 @@
     (cube . [def (cube n) (n * n * n)])
     (fill_rect . [def (fill_rect screen x1 y1 x2 y2 color)
   for y y1 (y < y2) ++y
-    (hline1 screen y x1 x2 color)])
+    (hline screen y x1 x2 color)])
     (circle . [def (circle scr cx cy r clr)
   with (x (0 - r)
         y 0
diff --git a/shell/primitives.mu b/shell/primitives.mu
index af89c690..6458684c 100644
--- a/shell/primitives.mu
+++ b/shell/primitives.mu
@@ -39,6 +39,7 @@ fn initialize-primitives _self: (addr global-table) {
   append-primitive self, "cr"
   append-primitive self, "pixel"
   append-primitive self, "line"
+  append-primitive self, "hline"
   append-primitive self, "width"
   append-primitive self, "height"
   # for keyboards
@@ -126,7 +127,7 @@ fn render-primitives screen: (addr screen), xmin: int, xmax: int, ymax: int {
   tmpx <- draw-text-rightward screen, "pixel graphics", tmpx, left-max, y, 7/fg=grey, 0xdc/bg=green-bg
   y <- increment
   var tmpx/eax: int <- copy xmin
-  tmpx <- draw-text-rightward screen, "  line pixel", tmpx, left-max, y, 0x2a/fg=orange, 0xdc/bg=green-bg
+  tmpx <- draw-text-rightward screen, "  line hline pixel", tmpx, left-max, y, 0x2a/fg=orange, 0xdc/bg=green-bg
   y <- increment
   var tmpx/eax: int <- copy xmin
   tmpx <- draw-text-rightward screen, "  width height", tmpx, left-max, y, 0x2a/fg=orange, 0xdc/bg=green-bg
@@ -441,6 +442,13 @@ fn apply-primitive _f: (addr cell), args-ah: (addr handle cell), out: (addr hand
     return
   }
   {
+    var hline?/eax: boolean <- string-equal? f-name, "hline"
+    compare hline?, 0/false
+    break-if-=
+    apply-hline args-ah, out, trace
+    return
+  }
+  {
     var width?/eax: boolean <- string-equal? f-name, "width"
     compare width?, 0/false
     break-if-=
@@ -2119,6 +2127,158 @@ fn apply-line _args-ah: (addr handle cell), out: (addr handle cell), trace: (add
   # return nothing
 }
 
+fn apply-hline _args-ah: (addr handle cell), out: (addr handle cell), trace: (addr trace) {
+  trace-text trace, "eval", "apply 'hline'"
+  var args-ah/eax: (addr handle cell) <- copy _args-ah
+  var _args/eax: (addr cell) <- lookup *args-ah
+  var args/esi: (addr cell) <- copy _args
+  {
+    var args-type/eax: (addr int) <- get args, type
+    compare *args-type, 0/pair
+    break-if-=
+    error trace, "args to 'hline' are not a list"
+    return
+  }
+  var empty-args?/eax: boolean <- nil? args
+  compare empty-args?, 0/false
+  {
+    break-if-=
+    error trace, "'hline' needs 5 args but got 0"
+    return
+  }
+  # screen = args->left
+  var first-ah/eax: (addr handle cell) <- get args, left
+  var first/eax: (addr cell) <- lookup *first-ah
+  {
+    var first-type/eax: (addr int) <- get first, type
+    compare *first-type, 5/screen
+    break-if-=
+    error trace, "first arg for 'hline' is not a screen"
+    return
+  }
+  var screen-ah/eax: (addr handle screen) <- get first, screen-data
+  var _screen/eax: (addr screen) <- lookup *screen-ah
+  var screen/edi: (addr screen) <- copy _screen
+  # y = args->right->left->value
+  var rest-ah/eax: (addr handle cell) <- get args, right
+  var _rest/eax: (addr cell) <- lookup *rest-ah
+  var rest/esi: (addr cell) <- copy _rest
+  {
+    var rest-type/eax: (addr int) <- get rest, type
+    compare *rest-type, 0/pair
+    break-if-=
+    error trace, "'hline' encountered non-pair"
+    return
+  }
+  {
+    var rest-nil?/eax: boolean <- nil? rest
+    compare rest-nil?, 0/false
+    break-if-=
+    error trace, "'hline' needs 5 args but got 1"
+    return
+  }
+  var second-ah/eax: (addr handle cell) <- get rest, left
+  var second/eax: (addr cell) <- lookup *second-ah
+  {
+    var second-type/eax: (addr int) <- get second, type
+    compare *second-type, 1/number
+    break-if-=
+    error trace, "second arg for 'hline' is not a number (screen y coordinate)"
+    return
+  }
+  var second-value/eax: (addr float) <- get second, number-data
+  var y/edx: int <- convert *second-value
+  # x1 = rest->right->left->value
+  var rest-ah/eax: (addr handle cell) <- get rest, right
+  var _rest/eax: (addr cell) <- lookup *rest-ah
+  rest <- copy _rest
+  {
+    var rest-type/eax: (addr int) <- get rest, type
+    compare *rest-type, 0/pair
+    break-if-=
+    error trace, "'hline' encountered non-pair"
+    return
+  }
+  {
+    var rest-nil?/eax: boolean <- nil? rest
+    compare rest-nil?, 0/false
+    break-if-=
+    error trace, "'hline' needs 5 args but got 2"
+    return
+  }
+  var third-ah/eax: (addr handle cell) <- get rest, left
+  var third/eax: (addr cell) <- lookup *third-ah
+  {
+    var third-type/eax: (addr int) <- get third, type
+    compare *third-type, 1/number
+    break-if-=
+    error trace, "third arg for 'hline' is not a number (screen x coordinate of start point)"
+    return
+  }
+  var third-value/eax: (addr float) <- get third, number-data
+  var x1/ebx: int <- convert *third-value
+  # x2 = rest->right->left->value
+  var rest-ah/eax: (addr handle cell) <- get rest, right
+  var _rest/eax: (addr cell) <- lookup *rest-ah
+  var rest/esi: (addr cell) <- copy _rest
+  {
+    var rest-type/eax: (addr int) <- get rest, type
+    compare *rest-type, 0/pair
+    break-if-=
+    error trace, "'hline' encountered non-pair"
+    return
+  }
+  {
+    var rest-nil?/eax: boolean <- nil? rest
+    compare rest-nil?, 0/false
+    break-if-=
+    error trace, "'hline' needs 5 args but got 3"
+    return
+  }
+  var fourth-ah/eax: (addr handle cell) <- get rest, left
+  var fourth/eax: (addr cell) <- lookup *fourth-ah
+  {
+    var fourth-type/eax: (addr int) <- get fourth, type
+    compare *fourth-type, 1/number
+    break-if-=
+    error trace, "fourth arg for 'hline' is not a number (screen x coordinate of end point)"
+    return
+  }
+  var fourth-value/eax: (addr float) <- get fourth, number-data
+  var x2/ecx: int <- convert *fourth-value
+  # color = rest->right->left->value
+  var rest-ah/eax: (addr handle cell) <- get rest, right
+  var _rest/eax: (addr cell) <- lookup *rest-ah
+  rest <- copy _rest
+  {
+    var rest-type/eax: (addr int) <- get rest, type
+    compare *rest-type, 0/pair
+    break-if-=
+    error trace, "'hline' encountered non-pair"
+    return
+  }
+  {
+    var rest-nil?/eax: boolean <- nil? rest
+    compare rest-nil?, 0/false
+    break-if-=
+    error trace, "'hline' needs 5 args but got 5"
+    return
+  }
+  var fifth-ah/eax: (addr handle cell) <- get rest, left
+  var fifth/eax: (addr cell) <- lookup *fifth-ah
+  {
+    var fifth-type/eax: (addr int) <- get fifth, type
+    compare *fifth-type, 1/number
+    break-if-=
+    error trace, "fifth arg for 'hline' is not an int (color; 0..0xff)"
+    return
+  }
+  var fifth-value/eax: (addr float) <- get fifth, number-data
+  var color/eax: int <- convert *fifth-value
+  draw-horizontal-line screen, y, x1, x2, color
+  # return nothing
+}
+
 fn apply-wait-for-key _args-ah: (addr handle cell), out: (addr handle cell), trace: (addr trace) {
   trace-text trace, "eval", "apply 'key'"
   var args-ah/eax: (addr handle cell) <- copy _args-ah