replace 'hline' with Mu implementation

This commit is contained in:
Kartik K. Agaram 2021-07-05 18:08:40 -07:00
parent 0b07a43367
commit 5d8a858a6f
2 changed files with 165 additions and 9 deletions

View File

@ -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

View File

@ -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
@ -440,6 +441,13 @@ fn apply-primitive _f: (addr cell), args-ah: (addr handle cell), out: (addr hand
apply-line args-ah, out, trace
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
@ -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