about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-06-04 21:30:43 -0700
committerKartik Agaram <vc@akkartik.com>2020-06-04 21:30:43 -0700
commit536058af78a1f3f7dfd9bae6e3da7281941cb01c (patch)
tree541a1d0b459e34d0b9d62a855057afeeabe4ab9b
parentcde591e03e3e94415ebc2f3a094507edef940c02 (diff)
downloadmu-536058af78a1f3f7dfd9bae6e3da7281941cb01c.tar.gz
6467
-rw-r--r--prototypes/browse/18/README.md8
-rw-r--r--prototypes/browse/18/file-state.mu44
-rw-r--r--prototypes/browse/18/main.mu34
3 files changed, 86 insertions, 0 deletions
diff --git a/prototypes/browse/18/README.md b/prototypes/browse/18/README.md
new file mode 100644
index 00000000..51f9878a
--- /dev/null
+++ b/prototypes/browse/18/README.md
@@ -0,0 +1,8 @@
+Start fleshing out the previous prototype's plans into something working.
+
+We're back to just plain text without bold formatting. Let's get something
+like prototype 3 working with the new architecture. Since Mu still has no
+checks we need to move slowly.
+
+One issue with this architecture: I have separate checks for `next-char ==
+EOF` and `done-reading? fs`. I'm gonna tolerate that duplication for now.
diff --git a/prototypes/browse/18/file-state.mu b/prototypes/browse/18/file-state.mu
new file mode 100644
index 00000000..35f40229
--- /dev/null
+++ b/prototypes/browse/18/file-state.mu
@@ -0,0 +1,44 @@
+type file-state {
+  source: (handle buffered-file)
+  eof?: boolean
+}
+
+fn init-file-state _self: (addr file-state), filename: (addr array byte) {
+  var self/eax: (addr file-state) <- copy _self
+  load-file self, filename
+  var eof/eax: (addr boolean) <- get self, eof?
+  copy-to *eof, 0  # false
+}
+
+fn load-file _self: (addr file-state), filename: (addr array byte) {
+  var self/eax: (addr file-state) <- copy _self
+  var out/esi: (addr handle buffered-file) <- get self, source
+  open filename, 0, out  # 0 = read mode
+}
+
+fn next-char _self: (addr file-state) -> result/eax: byte {
+  var self/ecx: (addr file-state) <- copy _self
+  var source/eax: (addr handle buffered-file) <- get self, source
+  var in/eax: (addr buffered-file) <- lookup *source
+  result <- read-byte-buffered in
+  # if result == EOF, set eof?
+  compare result, 0xffffffff  # EOF marker
+  {
+    var eof/ecx: (addr boolean) <- get self, eof?
+    copy-to *eof, 1  # true
+  }
+}
+
+fn done-reading? _self: (addr file-state) -> result/eax: boolean {
+  var self/eax: (addr file-state) <- copy _self
+  var eof/eax: (addr boolean) <- get self, eof?
+  result <- copy *eof
+}
+
+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
+}
diff --git a/prototypes/browse/18/main.mu b/prototypes/browse/18/main.mu
new file mode 100644
index 00000000..e2b41b73
--- /dev/null
+++ b/prototypes/browse/18/main.mu
@@ -0,0 +1,34 @@
+fn main args: (addr array (addr array byte)) -> exit-status/ebx: int {
+  # initialize fs from args[1]
+  var filename/eax: (addr array byte) <- first-arg args
+#?   print-string filename
+#?   print-string "\n"
+  var file-state-storage: file-state
+  var fs/esi: (addr file-state) <- address file-state-storage
+  init-file-state fs, filename
+  render fs
+  exit-status <- copy 0
+}
+
+fn render fs: (addr file-state) {
+  render-normal fs
+}
+
+fn render-normal fs: (addr file-state) {
+  {
+    var c/eax: byte <- next-char fs
+    # if (c == EOF) break
+    compare c, 0xffffffff  # EOF marker
+    break-if-=
+    #
+    print-byte c
+    #
+    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
+}