diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-10-25 21:33:24 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-10-25 21:35:24 -0700 |
commit | 5f5579e1682fbcea3340a036ef57684cacb835e4 (patch) | |
tree | ac5798c83cca7f5d5fe86d1aba954ebd5c7e0abd /apps/tile | |
parent | 2d7960d493c11ba6286193ec01b7f6771cd0b695 (diff) | |
download | mu-5f5579e1682fbcea3340a036ef57684cacb835e4.tar.gz |
7107 - tile: file handles
Diffstat (limited to 'apps/tile')
-rw-r--r-- | apps/tile/data.mu | 1 | ||||
-rw-r--r-- | apps/tile/environment.mu | 20 | ||||
-rw-r--r-- | apps/tile/rpn.mu | 32 | ||||
-rw-r--r-- | apps/tile/value-stack.mu | 13 |
4 files changed, 65 insertions, 1 deletions
diff --git a/apps/tile/data.mu b/apps/tile/data.mu index 7575461c..8a23fea9 100644 --- a/apps/tile/data.mu +++ b/apps/tile/data.mu @@ -38,6 +38,7 @@ type value { int-data: int # if type = 0 text-data: (handle array byte) # if type = 1 array-data: (handle array int) # if type = 2 + file-data: (handle buffered-file) # if type = 3 } type table { diff --git a/apps/tile/environment.mu b/apps/tile/environment.mu index e37b32ba..757e62b9 100644 --- a/apps/tile/environment.mu +++ b/apps/tile/environment.mu @@ -778,6 +778,14 @@ fn bound-function? w: (addr word), functions-ah: (addr handle function) -> resul subresult <- word-equal? w, "len" compare subresult, 0 # false break-if-!= + # if w == "open" return true + subresult <- word-equal? w, "open" + compare subresult, 0 # false + break-if-!= + # if w == "read" return true + subresult <- word-equal? w, "read" + compare subresult, 0 # false + break-if-!= # return w in functions var out-h: (handle function) var out/eax: (addr handle function) <- address out-h @@ -1372,6 +1380,16 @@ fn render-column screen: (addr screen), functions: (addr handle function), bindi render-array screen, val break $render-column:render-value } + { + compare *val-type, 3 # file + break-if-!= + var val-ah/eax: (addr handle buffered-file) <- get val-addr, file-data + var val/eax: (addr buffered-file) <- lookup *val-ah + start-color screen, 0, 7 + # TODO + print-string screen, " FILE " + break $render-column:render-value + } # render ints by default for now var val-addr2/eax: (addr int) <- get val-addr, int-data render-integer screen, *val-addr2, max-width @@ -1498,7 +1516,7 @@ fn clear-canvas _env: (addr environment) { print-string screen, "functions:" start-col <- add 2 move-cursor screen, 2, start-col - print-string screen, "+ - * len" + print-string screen, "+ - * len open read" # currently defined functions var row/ebx: int <- copy 5 var functions/esi: (addr handle function) <- get env, functions diff --git a/apps/tile/rpn.mu b/apps/tile/rpn.mu index ef53bb7e..c206e997 100644 --- a/apps/tile/rpn.mu +++ b/apps/tile/rpn.mu @@ -102,6 +102,38 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch: break $evaluate:process-word } } + { + var is-open?/eax: boolean <- stream-data-equal? curr-stream, "open" + compare is-open?, 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 string + var target-type-addr/eax: (addr int) <- get target-val, type + compare *target-type-addr, 1 # string + break-if-!= + # open target-val as a filename and save the handle in target-val + var src-ah/eax: (addr handle array byte) <- get target-val, text-data + var src/eax: (addr array byte) <- lookup *src-ah + var result-ah/ecx: (addr handle buffered-file) <- get target-val, file-data + open src, 0, result-ah # write? = false + # save result into target-val + var type-addr/eax: (addr int) <- get target-val, type + copy-to *type-addr, 3 # file + var target-string-ah/eax: (addr handle array byte) <- get target-val, text-data + var empty: (handle array byte) + copy-handle empty, target-string-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 dbe25b74..2eae94d3 100644 --- a/apps/tile/value-stack.mu +++ b/apps/tile/value-stack.mu @@ -176,6 +176,19 @@ fn value-stack-max-width _self: (addr value-stack) -> result/eax: int { break-if-<= copy-to out, w } + { + compare *type, 3 # file handle + break-if-!= + var f-ah/eax: (addr handle buffered-file) <- get g, file-data + var f/eax: (addr buffered-file) <- lookup *f-ah + compare f, 0 + break-if-= + # TODO + var w/eax: int <- copy 4 + compare w, out + break-if-<= + copy-to out, w + } i <- increment loop } |