diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-10-26 22:35:20 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-10-26 23:45:40 -0700 |
commit | 0f9a65dc0d10d93319eacf8ccff75a068e5f97a8 (patch) | |
tree | a34427229e12d9cbea59d71d4a94825ee24f4c85 /apps/tile | |
parent | f3d1929033856ac12db84435c317c5a288f898b3 (diff) | |
download | mu-0f9a65dc0d10d93319eacf8ccff75a068e5f97a8.tar.gz |
7120 - tile: array of lines from file
Requires a quick hacky change to Mu compiler.
Diffstat (limited to 'apps/tile')
-rw-r--r-- | apps/tile/environment.mu | 6 | ||||
-rw-r--r-- | apps/tile/rpn.mu | 46 | ||||
-rw-r--r-- | apps/tile/value-stack.mu | 26 |
3 files changed, 77 insertions, 1 deletions
diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu index 230fc1be..971e8d9f 100644 --- a/apps/tile/environment.mu +++ b/apps/tile/environment.mu @@ -790,6 +790,10 @@ fn bound-function? w: (addr word), functions-ah: (addr handle function) -> resul subresult <- word-equal? w, "slurp" compare subresult, 0 # false break-if-!= + # if w == "lines" return true + subresult <- word-equal? w, "lines" + compare subresult, 0 # false + break-if-!= # if w == "dup" return true subresult <- word-equal? w, "dup" compare subresult, 0 # false @@ -1558,7 +1562,7 @@ fn clear-canvas _env: (addr environment) { move-cursor screen, 2, start-col print-string screen, "+ - * len" move-cursor screen, 3, start-col - print-string screen, "open read slurp" + print-string screen, "open read slurp lines" move-cursor screen, 4, start-col print-string screen, "dup swap" # currently defined functions diff --git a/apps/tile/rpn.mu b/apps/tile/rpn.mu index 7b65b71c..2529e5ca 100644 --- a/apps/tile/rpn.mu +++ b/apps/tile/rpn.mu @@ -206,6 +206,52 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch: copy-handle empty, target-file-ah break $evaluate:process-word } + { + var is-lines?/eax: boolean <- stream-data-equal? curr-stream, "lines" + compare is-lines?, 0 + break-if-= + # pop target-val from out + var out2/esi: (addr value-stack) <- copy out + var top-addr/ecx: (addr int) <- get out2, top + compare *top-addr, 0 + break-if-<= + var data-ah/eax: (addr handle array value) <- get out2, data + var data/eax: (addr array value) <- lookup *data-ah + var top/edx: int <- copy *top-addr + top <- decrement + var dest-offset/edx: (offset value) <- compute-offset data, top + var target-val/edx: (addr value) <- index data, dest-offset + # check target-val is a file + var target-type-addr/eax: (addr int) <- get target-val, type + compare *target-type-addr, 3 # file + break-if-!= + # read all lines from file and save as an array of strings in target-val + # read target-val as a filename and save the handle in target-val + var file-ah/eax: (addr handle buffered-file) <- get target-val, file-data + var file/eax: (addr buffered-file) <- lookup *file-ah + var h: (handle array (handle array byte)) + var ah/ecx: (addr handle array (handle array byte)) <- address h +#? enable-screen-type-mode +#? clear-screen 0 + read-lines file, ah +#? { +#? var x/eax: (addr array (handle array byte)) <- lookup h +#? var len/eax: int <- length x +#? var foo/eax: int <- copy len +#? print-string 0, "aa: " +#? print-int32-hex 0, foo +#? print-string 0, "\n" +#? } + var target/eax: (addr handle array value) <- get target-val, array-data + save-lines h, target + # save result into target-val + var type-addr/eax: (addr int) <- get target-val, type + copy-to *type-addr, 2 # array + var target-file-ah/eax: (addr handle buffered-file) <- get target-val, file-data + var empty: (handle buffered-file) + copy-handle empty, target-file-ah + break $evaluate:process-word + } # if curr-stream defines a binding, save top of stack to bindings { var done?/eax: boolean <- stream-empty? curr-stream diff --git a/apps/tile/value-stack.mu b/apps/tile/value-stack.mu index 83b1262a..a6ba4f0f 100644 --- a/apps/tile/value-stack.mu +++ b/apps/tile/value-stack.mu @@ -227,3 +227,29 @@ fn array-width _a: (addr array value) -> result/eax: int { # we won't add 2 for surrounding brackets since we don't surround arrays in # spaces like other value types } + +fn save-lines in-h: (handle array (handle array byte)), _out-ah: (addr handle array value) { + var _in/eax: (addr array (handle array byte)) <- lookup in-h + var in/esi: (addr array (handle array byte)) <- copy _in + var len/ecx: int <- length in + var out-ah/edi: (addr handle array value) <- copy _out-ah + populate out-ah, len + var out/eax: (addr array value) <- lookup *out-ah + # copy in into out + var i/ebx: int <- copy 0 + { + compare i, len + break-if->= +#? print-int32-hex 0, i +#? print-string 0, "\n" + var src/ecx: (addr handle array byte) <- index in, i + var dest-offset/edx: (offset value) <- compute-offset out, i + var dest-val/edx: (addr value) <- index out, dest-offset + var dest/eax: (addr handle array byte) <- get dest-val, text-data + copy-object src, dest + var type/edx: (addr int) <- get dest-val, type + copy-to *type, 1 # string + i <- increment + loop + } +} |