diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-05-31 23:43:49 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-05-31 23:43:49 -0700 |
commit | 9cc69d69c6cd83ed4aa367b4a08ca379cae45805 (patch) | |
tree | b469fd1bf0b3e46258cd835f82da72ee3d26e310 /prototypes/browse/15-headers-broken.mu | |
parent | 2f16a74783d830caf2cc01bd67996317195c94c9 (diff) | |
download | mu-9cc69d69c6cd83ed4aa367b4a08ca379cae45805.tar.gz |
6456 - new directory for live-blogging prototypes
I don't have layers yet in Mu. But I can still create lots of versions of a program.
Diffstat (limited to 'prototypes/browse/15-headers-broken.mu')
-rw-r--r-- | prototypes/browse/15-headers-broken.mu | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/prototypes/browse/15-headers-broken.mu b/prototypes/browse/15-headers-broken.mu new file mode 100644 index 00000000..7b152bbc --- /dev/null +++ b/prototypes/browse/15-headers-broken.mu @@ -0,0 +1,216 @@ +# Incomplete first attempt at parsing headings. +# +# The state machine is getting out of control, and I notice old bugs like +# turning '*abc_' into bold text. + +fn main args: (addr array (addr array byte)) -> exit-status/ebx: int { + var filename/eax: (addr array byte) <- first-arg args + var file/esi: (addr buffered-file) <- load-file filename + enable-screen-grid-mode + var nrows/eax: int <- copy 0 + var ncols/ecx: int <- copy 0 + nrows, ncols <- screen-size + enable-keyboard-immediate-mode + { + render file, nrows, ncols + var key/eax: byte <- read-key + compare key, 0x71 # 'q' + loop-if-!= + } + enable-keyboard-type-mode + enable-screen-type-mode + exit-status <- copy 0 +} + +type render-state { + current-state: int # enum 0: normal, 1: bold, 2: heading + start-of-line?: boolean + num-hashes-seen?: int +} + +# decide how to lay out pages on screen +fn render in: (addr buffered-file), nrows: int, ncols: int { + # Fit multiple pages on screen on separate columns, each wide enough to read + # comfortably. + # Pages are separated horizontally by a 'page margin'. Among other reasons, + # this allows the odd line to bleed out on the right if necessary. + # + # hardcoded parameters: + # top-margin + # page-margin + # text-width + var _r: render-state + var r/edi: (addr render-state) <- address _r + # r->start-of-line? = true + var s/eax: (addr boolean) <- get r, start-of-line? + copy-to *s, 1 # true + var toprow/eax: int <- copy 2 # top-margin + var botrow/ecx: int <- copy nrows + var leftcol/edx: int <- copy 5 # page-margin + var rightcol/ebx: int <- copy leftcol + rightcol <- add 0x40 # text-width = 64 characters + start-color 0xec, 7 # 236 = darkish gray + { + compare rightcol, ncols + break-if->= + render-page in, toprow, leftcol, botrow, rightcol, r + leftcol <- copy rightcol + leftcol <- add 5 # page-margin + rightcol <- copy leftcol + rightcol <- add 0x40 # text-width + loop + } +} + +fn render-page in: (addr buffered-file), toprow: int, leftcol: int, botrow: int, rightcol: int, _r: (addr render-state) { + var r/edi: (addr render-state) <- copy _r + var state/esi: (addr int) <- get r, current-state + clear toprow, leftcol, botrow, rightcol + var row/ecx: int <- copy toprow +$line-loop: { + compare row, botrow + break-if->= + var col/edx: int <- copy leftcol + move-cursor row, col +$char-loop: { + compare col, rightcol + break-if->= + var c/eax: byte <- read-byte-buffered in + compare c, 0xffffffff # EOF marker + break-if-= $line-loop +$change-state: { + compare *state, 0 # normal + { + break-if-!= + compare c, 0x2a # '*' + { + break-if-!= + # r->current-state == 0 && c == '*' => bold text + start-bold + copy-to *state, 1 + break $change-state + } + compare c, 0x5f # '_' + { + break-if-!= + # r->current-state == 0 && c == '_' => bold text + start-bold + copy-to *state, 1 + break $change-state + } + compare c, 0x23 # '#' + { + break-if-!= + var s/eax: (addr boolean) <- get r, start-of-line? + compare *s, 1 + { + break-if-!= + # r->current-state == 0 && c == '#' && at start of line => count '#'s + var h/eax: (addr int) <- get r, num-hashes-seen? + increment *h + break $change-state + } + break $change-state + } + break $change-state + } + compare *state, 1 # bold + { + break-if-!= + compare c, 0x2a # '*' + { + break-if-!= + # r->current-state == 1 && c == '*' => print c, then normal text + print-byte c + col <- increment + reset-formatting + start-color 0xec, 7 # 236 = darkish gray + copy-to *state, 0 + loop $char-loop + } + compare c, 0x5f # '_' + { + break-if-!= + # r->current-state == 1 && c == '_' => print c, then normal text + print-byte c + col <- increment + reset-formatting + start-color 0xec, 7 # 236 = darkish gray + copy-to *state, 0 + loop $char-loop + } + break $change-state + } + } # $change-state + # update a few attributes of the state based on c without changing the state itself + compare c, 0xa # newline + { + break-if-!= + # c is newline + var s/eax: (addr boolean) <- get r, start-of-line? + copy-to *s, 1 # true + # switch to normal text + reset-formatting + start-color 0xec, 7 # 236 = darkish gray + # no need to print newlines + break $char-loop + } + compare c, 0x20 # space + { + break-if-= + # c is not newline or space + var s/eax: (addr boolean) <- get r, start-of-line? + copy-to *s, 0 # false + } + # print c + print-byte c + col <- increment + loop + } + row <- increment + loop + } +} + +fn clear toprow: int, leftcol: int, botrow: int, rightcol: int { + var row/ecx: int <- copy toprow + { + compare row, botrow + break-if->= + var col/edx: int <- copy leftcol + move-cursor row, col + { + compare col, rightcol + break-if->= + print-string " " + col <- increment + loop + } + row <- increment + loop + } +} + +fn first-arg args-on-stack: (addr array (addr array byte)) -> out/eax: (addr array byte) { + var args/eax: (addr array (addr array byte)) <- copy args-on-stack + var result/eax: (addr addr array byte) <- index args, 1 + out <- copy *result +} + +fn load-file filename: (addr array byte) -> out/esi: (addr buffered-file) { + var result: (handle buffered-file) + { + var tmp1/eax: (addr handle buffered-file) <- address result + open filename, 0, tmp1 + } + var tmp2/eax: (addr buffered-file) <- lookup result + out <- copy tmp2 +} + +fn dump in: (addr buffered-file) { + var c/eax: byte <- read-byte-buffered in + compare c, 0xffffffff # EOF marker + break-if-= + print-byte c + loop +} |