support non-line-oriented processing in next-word

Immediately this simplifies support for comments in image data.
This commit is contained in:
Kartik K. Agaram 2021-07-29 20:07:13 -07:00
parent e55d3f5814
commit b625c6304e
16 changed files with 73 additions and 36 deletions

View File

@ -38,29 +38,44 @@ $next-word:check0:
# . return out
c7 0/subop/copy 0/mod/direct 7/rm32/edi . . . . . 0/imm32 # copy to *edi
c7 0/subop/copy 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 0/imm32 # copy to *(edi+4)
eb/jump $next-word:end/disp8
e9/jump $next-word:end/disp32
$next-word:check-for-comment:
# out->start = &line->data[line->read]
8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax
89/copy 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # copy eax to *edi
# if (line->data[line->read] == '#') out->end = &line->data[line->write]), skip rest of stream and return
# if (line->data[line->read] == '#') return rest of line
# . eax = line->data[line->read]
31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax
8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL
# . compare
3d/compare-eax-and 0x23/imm32/pound
75/jump-if-!= $next-word:regular-word/disp8
0f 85/jump-if-!= $next-word:regular-word/disp32
$next-word:comment:
# . out->end = &line->data[line->write]
8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 0/index/eax . 0/r32/eax 0xc/disp8 . # copy esi+eax+12 to eax
# out->end = out->start
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax
89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4)
# . line->read = line->write
8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax
89/copy 1/mod/*+disp8 6/rm32/esi . . . 0/r32/eax 4/disp8 . # copy eax to *(esi+4)
# . return
eb/jump $next-word:end/disp8
# var write/ecx: int = line->write
8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx
$next-word:comment-loop:
# if (line->read >= line->write) break
39/compare 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # compare *(esi+4) with ecx
0f 8d/jump-if->= $next-word:comment-break/disp32
# ++line->read
ff 0/subop/increment 1/mod/*+disp8 6/rm32/esi . . . . 4/disp8 . # increment *(esi+4)
# ++out->end
ff 0/subop/increment 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 . # increment *(edi+4)
# if (*out->end == newline) break
8b/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy *(edi+4) to eax
8a/copy-byte 0/mod/indirect 0/rm32/eax . . . 0/r32/AL . . # copy byte at *eax to AL
25/and-eax-with 0xff/imm32
3d/compare-eax-and 0xa/imm32/newline
0f 84/jump-if-= $next-word:comment-break/disp32
# loop
e9/jump $next-word:comment-loop/disp32
$next-word:comment-break:
# return
e9/jump $next-word:end/disp32
$next-word:regular-word:
# otherwise skip-chars-not-matching-whitespace(line) # including trailing newline
# . . push args

4
400.mu
View File

@ -96,8 +96,8 @@ sig to-decimal-digit in: grapheme -> _/eax: int
# bad name alert
# next-word really tokenizes
# next-raw-word really reads whitespace-separated words
sig next-word line: (addr stream byte), out: (addr slice) # skips '#' comments
sig next-raw-word line: (addr stream byte), out: (addr slice) # does not skip '#' comments
sig next-word line: (addr stream byte), out: (addr slice) # merges '#' comments into a single word
sig next-raw-word line: (addr stream byte), out: (addr slice) # does not merge '#' comments
sig skip-chars-matching in: (addr stream byte), delimiter: byte
sig skip-chars-matching-whitespace in: (addr stream byte)
sig skip-chars-not-matching in: (addr stream byte), delimiter: byte

View File

@ -18,7 +18,7 @@ fn initialize-image _self: (addr image), in: (addr stream byte) {
var self/esi: (addr image) <- copy _self
var mode-storage: slice
var mode/ecx: (addr slice) <- address mode-storage
next-word in, mode
next-word-skipping-comments in, mode
{
var P1?/eax: boolean <- slice-equal? mode, "P1"
compare P1?, 0/false
@ -90,10 +90,10 @@ fn initialize-image-from-pbm _self: (addr image), in: (addr stream byte) {
var curr-word-storage: slice
var curr-word/ecx: (addr slice) <- address curr-word-storage
# load width, height
next-word in, curr-word
next-word-skipping-comments in, curr-word
var tmp/eax: int <- parse-decimal-int-from-slice curr-word
var width/edx: int <- copy tmp
next-word in, curr-word
next-word-skipping-comments in, curr-word
tmp <- parse-decimal-int-from-slice curr-word
var height/ebx: int <- copy tmp
# save width, height
@ -112,7 +112,7 @@ fn initialize-image-from-pbm _self: (addr image), in: (addr stream byte) {
{
compare i, capacity
break-if->=
next-word in, curr-word
next-word-skipping-comments in, curr-word
var src/eax: int <- parse-decimal-int-from-slice curr-word
{
var dest/ecx: (addr byte) <- index data, i
@ -202,14 +202,14 @@ fn initialize-image-from-pgm _self: (addr image), in: (addr stream byte) {
var curr-word-storage: slice
var curr-word/ecx: (addr slice) <- address curr-word-storage
# load width, height
next-word in, curr-word
next-word-skipping-comments in, curr-word
var tmp/eax: int <- parse-decimal-int-from-slice curr-word
var width/edx: int <- copy tmp
next-word in, curr-word
next-word-skipping-comments in, curr-word
tmp <- parse-decimal-int-from-slice curr-word
var height/ebx: int <- copy tmp
# check and save color levels
next-word in, curr-word
next-word-skipping-comments in, curr-word
{
tmp <- parse-decimal-int-from-slice curr-word
compare tmp, 0xff
@ -234,7 +234,7 @@ fn initialize-image-from-pgm _self: (addr image), in: (addr stream byte) {
{
compare i, capacity
break-if->=
next-word in, curr-word
next-word-skipping-comments in, curr-word
var src/eax: int <- parse-decimal-int-from-slice curr-word
{
var dest/ecx: (addr byte) <- index data, i
@ -688,13 +688,13 @@ fn initialize-image-from-ppm _self: (addr image), in: (addr stream byte) {
var curr-word-storage: slice
var curr-word/ecx: (addr slice) <- address curr-word-storage
# load width, height
next-word in, curr-word
next-word-skipping-comments in, curr-word
var tmp/eax: int <- parse-decimal-int-from-slice curr-word
var width/edx: int <- copy tmp
next-word in, curr-word
next-word-skipping-comments in, curr-word
tmp <- parse-decimal-int-from-slice curr-word
var height/ebx: int <- copy tmp
next-word in, curr-word
next-word-skipping-comments in, curr-word
# check color levels
{
tmp <- parse-decimal-int-from-slice curr-word
@ -725,7 +725,7 @@ fn initialize-image-from-ppm _self: (addr image), in: (addr stream byte) {
{
compare i, capacity
break-if->=
next-word in, curr-word
next-word-skipping-comments in, curr-word
var src/eax: int <- parse-decimal-int-from-slice curr-word
{
var dest/ecx: (addr byte) <- index data, i
@ -1111,3 +1111,10 @@ fn scale-image-height _img: (addr image), width: int -> _/ebx: int {
var result/ebx: int <- convert result-f
return result
}
fn next-word-skipping-comments line: (addr stream byte), out: (addr slice) {
next-word line, out
var retry?/eax: boolean <- slice-starts-with? out, "#"
compare retry?, 0/false
loop-if-!=
}

View File

@ -38,29 +38,44 @@ $next-word:check0:
# . return out
c7 0/subop/copy 0/mod/direct 7/rm32/edi . . . . . 0/imm32 # copy to *edi
c7 0/subop/copy 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 0/imm32 # copy to *(edi+4)
eb/jump $next-word:end/disp8
e9/jump $next-word:end/disp32
$next-word:check-for-comment:
# out->start = &line->data[line->read]
8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax
89/copy 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # copy eax to *edi
# if (line->data[line->read] == '#') out->end = &line->data[line->write]), skip rest of stream and return
# if (line->data[line->read] == '#') return rest of line
# . eax = line->data[line->read]
31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax
8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL
# . compare
3d/compare-eax-and 0x23/imm32/pound
75/jump-if-!= $next-word:regular-word/disp8
0f 85/jump-if-!= $next-word:regular-word/disp32
$next-word:comment:
# . out->end = &line->data[line->write]
8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 0/index/eax . 0/r32/eax 0xc/disp8 . # copy esi+eax+12 to eax
# out->end = out->start
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax
89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4)
# . line->read = line->write
8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax
89/copy 1/mod/*+disp8 6/rm32/esi . . . 0/r32/eax 4/disp8 . # copy eax to *(esi+4)
# . return
eb/jump $next-word:end/disp8
# var write/ecx: int = line->write
8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx
$next-word:comment-loop:
# if (line->read >= line->write) break
39/compare 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # compare *(esi+4) with ecx
0f 8d/jump-if->= $next-word:comment-break/disp32
# ++line->read
ff 0/subop/increment 1/mod/*+disp8 6/rm32/esi . . . . 4/disp8 . # increment *(esi+4)
# ++out->end
ff 0/subop/increment 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 . # increment *(edi+4)
# if (*out->end == newline) break
8b/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy *(edi+4) to eax
8a/copy-byte 0/mod/indirect 0/rm32/eax . . . 0/r32/AL . . # copy byte at *eax to AL
25/and-eax-with 0xff/imm32
3d/compare-eax-and 0xa/imm32/newline
0f 84/jump-if-= $next-word:comment-break/disp32
# loop
e9/jump $next-word:comment-loop/disp32
$next-word:comment-break:
# return
e9/jump $next-word:end/disp32
$next-word:regular-word:
# otherwise skip-chars-not-matching-whitespace(line) # including trailing newline
# . . push args

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
linux/hex

Binary file not shown.

Binary file not shown.

BIN
linux/mu

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.