7124 - tiles: better 'lines' primitive

This commit is contained in:
Kartik Agaram 2020-10-27 00:29:47 -07:00
parent 307a75530f
commit 332998546d
3 changed files with 58 additions and 14 deletions

View File

@ -84,3 +84,42 @@ fn test-substring {
var out/eax: (addr array byte) <- lookup *out-ah
check-strings-equal out, "bcde", "F - test-substring/middle-too-small"
}
fn split-string in: (addr array byte), delim: grapheme, out: (addr handle array (handle array byte)) {
var in-stream: (stream byte 0x100)
var in-stream-addr/esi: (addr stream byte) <- address in-stream
write in-stream-addr, in
var tokens-stream: (stream (handle array byte) 0x100)
var tokens-stream-addr/edi: (addr stream (handle array byte)) <- address tokens-stream
var curr-stream: (stream byte 0x100)
var curr-stream-addr/ecx: (addr stream byte) <- address curr-stream
$split-string:core: {
var g/eax: grapheme <- read-grapheme in-stream-addr
compare g, 0xffffffff
break-if-=
#? print-grapheme-to-real-screen g
#? print-string-to-real-screen "\n"
compare g, delim
{
break-if-!=
# token complete; flush
var token: (handle array byte)
var token-ah/eax: (addr handle array byte) <- address token
stream-to-array curr-stream-addr, token-ah
write-to-stream tokens-stream-addr, token-ah
clear-stream curr-stream-addr
loop $split-string:core
}
write-grapheme curr-stream-addr, g
loop
}
stream-to-array tokens-stream-addr, out
}
fn test-split-string {
var out-h: (handle array (handle array byte))
var out-ah/edi: (addr handle array (handle array byte)) <- address out-h
# prefix substrings
split-string "bab", 0x61, out-ah
# no crash
}

View File

@ -229,27 +229,30 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
# 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
var s: (stream byte 0x100)
var s-addr/ecx: (addr stream byte) <- address s
slurp file, s-addr
var tmp-ah/eax: (addr handle array byte) <- get target-val, text-data
stream-to-array s-addr, tmp-ah
var tmp/eax: (addr array byte) <- lookup *tmp-ah
#? 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"
#? }
#? print-string 0, tmp
var h: (handle array (handle array byte))
{
var ah/edx: (addr handle array (handle array byte)) <- address h
split-string tmp, 0xa, ah
}
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
var empty-file: (handle buffered-file)
copy-handle empty-file, target-file-ah
var target-text-ah/eax: (addr handle array byte) <- get target-val, text-data
var empty-text: (handle array byte)
copy-handle empty-text, target-text-ah
break $evaluate:process-word
}
# if curr-stream defines a binding, save top of stack to bindings

View File

@ -177,6 +177,8 @@ fn value-width _v: (addr value) -> result/eax: int {
break-if-<=
out <- copy 0xd
}
# we won't add 2 for surrounding quotes since we don't surround arrays
# in spaces like other value types
break $value-width:body
}
{