# primitive for editing text type gap-buffer { left: code-point-utf8-stack right: code-point-utf8-stack # some fields for scanning incrementally through a gap-buffer left-read-index: int right-read-index: int } fn initialize-gap-buffer _self: (addr gap-buffer), capacity: int { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr code-point-utf8-stack) <- get self, left initialize-code-point-utf8-stack left, capacity var right/eax: (addr code-point-utf8-stack) <- get self, right initialize-code-point-utf8-stack right, capacity } fn clear-gap-buffer _self: (addr gap-buffer) { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr code-point-utf8-stack) <- get self, left clear-code-point-utf8-stack left var right/eax: (addr code-point-utf8-stack) <- get self, right clear-code-point-utf8-stack right } fn gap-buffer-empty? _self: (addr gap-buffer) -> _/eax: boolean { var self/esi: (addr gap-buffer) <- copy _self # if !empty?(left) return false { var left/eax: (addr code-point-utf8-stack) <- get self, left var result/eax: boolean <- code-point-utf8-stack-empty? left compare result, 0/false break-if-!= return 0/false } # return empty?(right) var left/eax: (addr code-point-utf8-stack) <- get self, left var result/eax: boolean <- code-point-utf8-stack-empty? left return result } fn gap-buffer-capacity _gap: (addr gap-buffer) -> _/edx: int { var gap/esi: (addr gap-buffer) <- copy _gap var left/eax: (addr code-point-utf8-stack) <- get gap, left var left-data-ah/eax: (addr handle array code-point-utf8) <- get left, data var left-data/eax: (addr array code-point-utf8) <- lookup *left-data-ah var result/eax: int <- length left-data return result } fn initialize-gap-buffer-with self: (addr gap-buffer), keys: (addr array byte) { initialize-gap-buffer self, 0x40/capacity var input-stream-storage: (stream byte 0x40/capacity) var input-stream/ecx: (addr stream byte) <- address input-stream-storage write input-stream, keys { var done?/eax: boolean <- stream-empty? input-stream compare done?, 0/false break-if-!= var g/eax: code-point-utf8 <- read-code-point-utf8 input-stream add-code-point-utf8-at-gap self, g loop } } fn load-gap-buffer-from-stream self: (addr gap-buffer), in: (addr stream byte) { rewind-stream in { var done?/eax: boolean <- stream-empty? in compare done?, 0/false break-if-!= var key/eax: byte <- read-byte in compare key, 0/null break-if-= var g/eax: code-point-utf8 <- copy key edit-gap-buffer self, g loop } } fn emit-gap-buffer self: (addr gap-buffer), out: (addr stream byte) { clear-stream out append-gap-buffer self, out } fn append-gap-buffer _self: (addr gap-buffer), out: (addr stream byte) { var self/esi: (addr gap-buffer) <- copy _self var left/eax: (addr code-point-utf8-stack) <- get self, left emit-stack-from-bottom left, out var right/eax: (addr code-point-utf8-stack) <- get self, right emit-stack-from-top right, out } # dump stack from bottom to top fn emit-stack-from-bottom _self: (addr code-point-utf8-stack), out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy 0 { compare i, *top-addr break-if->= var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- increment loop } } # dump stack from top to bottom fn emit-stack-from-top _self: (addr code-point-utf8-stack), out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy *top-addr i <- decrement { compare i, 0 break-if-< var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- decrement loop } } fn word-at-gap _self: (addr gap-buffer), out: (addr stream byte) { var self/esi: (addr gap-buffer) <- copy _self clear-stream out { var g/eax: code-point-utf8 <- code-point-utf8-at-gap self var at-word?/eax: boolean <- is-ascii-word-code-point-utf8? g compare at-word?, 0/false break-if-!= return } var left/ecx: (addr code-point-utf8-stack) <- get self, left var left-index/eax: int <- top-most-word left emit-stack-from-index left, left-index, out var right/ecx: (addr code-point-utf8-stack) <- get self, right var right-index/eax: int <- top-most-word right emit-stack-to-index right, right-index, out } fn test-word-at-gap-single-word-with-gap-at-end { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc" # gap is at end (right is empty) var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "abc", "F - test-word-at-gap-single-word-with-gap-at-end" } fn test-word-at-gap-single-word-with-gap-at-start { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc" gap-to-start g # var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "abc", "F - test-word-at-gap-single-word-with-gap-at-start" } fn test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-end { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc " # gap is at end (right is empty) var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "", "F - test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-end" } fn test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-start { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, " abc" gap-to-start g # var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "", "F - test-word-at-gap-multiple-words-with-gap-at-non-word-code-point-utf8-at-start" } fn test-word-at-gap-multiple-words-with-gap-at-end { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "a bc d" # gap is at end (right is empty) var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "d", "F - test-word-at-gap-multiple-words-with-gap-at-end" } fn test-word-at-gap-multiple-words-with-gap-at-initial-word { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "a bc d" gap-to-start g # var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "a", "F - test-word-at-gap-multiple-words-with-gap-at-initial-word" } fn test-word-at-gap-multiple-words-with-gap-at-final-word { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "a bc d" var dummy/eax: code-point-utf8 <- gap-left g # gap is at final word var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "d", "F - test-word-at-gap-multiple-words-with-gap-at-final-word" } fn test-word-at-gap-multiple-words-with-gap-at-final-non-word { var _g: gap-buffer var g/esi: (addr gap-buffer) <- address _g initialize-gap-buffer-with g, "abc " var dummy/eax: code-point-utf8 <- gap-left g # gap is at final word var out-storage: (stream byte 0x10) var out/eax: (addr stream byte) <- address out-storage word-at-gap g, out check-stream-equal out, "", "F - test-word-at-gap-multiple-words-with-gap-at-final-non-word" } fn code-point-utf8-at-gap _self: (addr gap-buffer) -> _/eax: code-point-utf8 { # send top of right most of the time var self/esi: (addr gap-buffer) <- copy _self var right/edi: (addr code-point-utf8-stack) <- get self, right var data-ah/eax: (addr handle array code-point-utf8) <- get right, data var data/eax: (addr array code-point-utf8) <- lookup *data-ah var top-addr/ecx: (addr int) <- get right, top { compare *top-addr, 0 break-if-<= var top/ecx: int <- copy *top-addr top <- decrement var result/eax: (addr code-point-utf8) <- index data, top return *result } # send top of left only if right is empty var left/edi: (addr code-point-utf8-stack) <- get self, left var data-ah/eax: (addr handle array code-point-utf8) <- get left, data var data/eax: (addr array code-point-utf8) <- lookup *data-ah var top-addr/ecx: (addr int) <- get left, top { compare *top-addr, 0 break-if-<= var top/ecx: int <- copy *top-addr top <- decrement var result/eax: (addr code-point-utf8) <- index data, top return *result } # send null if everything is empty return 0 } fn top-most-word _self: (addr code-point-utf8-stack) -> _/eax: int { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/ebx: int <- copy *top-addr i <- decrement { compare i, 0 break-if-< var g/edx: (addr code-point-utf8) <- index data, i var is-word?/eax: boolean <- is-ascii-word-code-point-utf8? *g compare is-word?, 0/false break-if-= i <- decrement loop } i <- increment return i } fn emit-stack-from-index _self: (addr code-point-utf8-stack), start: int, out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy start { compare i, *top-addr break-if->= var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- increment loop } } fn emit-stack-to-index _self: (addr code-point-utf8-stack), end: int, out: (addr stream byte) { var self/esi: (addr code-point-utf8-stack) <- copy _self var data-ah/edi: (addr handle array code-point-utf8) <- get self, data var _data/eax: (addr array code-point-utf8) <- lookup *data-ah var data/edi: (addr array code-point-utf8) <- copy _data var top-addr/ecx: (addr int) <- get self, top var i/eax: int <- copy *top-addr i <- decrement { compare i, 0 break-if-< compare i, end break-if-< var g/edx: (addr code-point-utf8) <- index data, i write-code-point-utf8 out, *g i <- decrement loop } } fn is-ascii-word-code-point-utf8? g: code-point-utf8 -> _/eax: boolean { compare g, 0x21/! { break-if-!= return 1/true } compare g, 0x30/0 { break-if->= return 0/false } compare g, 0x39/9 { break-if-> return 1/true } compare g, 0x3f/? { break-if-!=
# Comparing 'regular' size-prefixed strings.

== code
#   instruction                     effective address                                                   register    displacement    immediate
# . op          subop               mod             rm32          base        index         scale       r32
# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes

string-equal?:  # s: (addr array byte), benchmark: (addr array byte) -> result/eax: boolean
    # pseudocode:
    #   if (s->size != benchmark->size) return false
    #   return string-starts-with?(s, benchmark)
    #
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    56/push-esi
    57/push-edi
    # esi = s
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
    # edi = benchmark
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
    # ecx = s->size
    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # copy *esi to ecx
$string-equal?:sizes:
    # if (ecx != benchmark->size) return false
    39/compare                      0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # compare *edi and ecx
    b8/copy-to-eax  0/imm32/false
    75/jump-if-!=  $string-equal?:end/disp8
$string-equal?:contents:
    # string-starts-with?(s, benchmark)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  string-starts-with?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
$string-equal?:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

string-starts-with?:  # s: (addr array byte), benchmark: (addr array byte) -> result/eax: boolean
    # pseudocode:
    #   if (s->size < benchmark->size) return false
    #   currs = s->data
    #   currb = benchmark->data
    #   maxb = &benchmark->data[benchmark->size]
    #   while currb < maxb
    #     c1 = *currs
    #     c2 = *currb
    #     if (c1 != c2) return false
    #     ++currs, ++currb
    #   return true
    #
    # registers:
    #   currs: esi
    #   maxs: ecx
    #   currb: edi
    #   c1: eax
    #   c2: ebx
    #
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    52/push-edx
    56/push-esi
    57/push-edi
    # esi = s
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
    # edi = benchmark
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
    # var bsize/ecx: int = benchmark->size
    8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
$string-starts-with?:sizes:
    # if (s->size < bsize) return false
    39/compare                      0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare *esi with ecx
    7c/jump-if-<  $string-starts-with?:false/disp8
    # var currs/esi: (addr byte) = s->data
    81          0/subop/add         3/mod/direct    6/rm32/esi    .           .             .           .           .               4/imm32           # add to esi
    # var currb/edi: (addr byte) = benchmark->data
    81          0/subop/add         3/mod/direct    7/rm32/edi    .           .             .           .           .               4/imm32           # add to edi
    # var maxb/ecx: (addr byte) = &benchmark->data[benchmark->size]
    01/add                          3/mod/direct    1/rm32/ecx    .           .             .           7/r32/edi   .               .                 # add edi to ecx
    # var c1/eax: byte = 0
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    # var c2/edx: byte = 0
    31/xor                          3/mod/direct    2/rm32/edx    .           .             .           2/r32/edx   .               .                 # clear edx
$string-starts-with?:loop:
    # if (currs >= maxs) return true
    39/compare                      3/mod/direct    7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # compare edi with ecx
    73/jump-if-addr>=  $string-starts-with?:true/disp8
    # c1 = *currs
    8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           0/r32/AL    .               .                 # copy byte at *esi to AL
    # c2 = *currb
    8a/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           2/r32/DL    .               .                 # copy byte at *edi to DL
    # if (c1 != c2) return false
    39/compare                      3/mod/direct    0/rm32/eax    .           .             .           2/r32/edx   .               .                 # compare eax and edx
    75/jump-if-!=  $string-starts-with?:false/disp8
    # ++currs
    46/increment-esi
    # ++currb
    47/increment-edi
    eb/jump  $string-starts-with?:loop/disp8
$string-starts-with?:true:
    b8/copy-to-eax  1/imm32
    eb/jump  $string-starts-with?:end/disp8
$string-starts-with?:false:
    b8/copy-to-eax  0/imm32
$string-starts-with?:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5a/pop-to-edx
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# - tests

test-compare-empty-with-empty-string:
    # eax = string-equal?("", "")
    # . . push args
    68/push  ""/imm32
    68/push  ""/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compare-empty-with-empty-string"/imm32
    68/push  1/imm32/true
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

test-compare-empty-with-non-empty-string:  # also checks size-mismatch code path
    # eax = string-equal?("", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  ""/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-compare-empty-with-non-empty-string"/imm32
    68/push  0/imm32/false
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

test-compare-equal-strings:
    # eax = string-equal?("Abc", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compare-equal-strings"/imm32
    68/push  1/imm32/true
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

test-compare-inequal-strings-equal-sizes:
    # eax = string-equal?("Abc", "Adc")
    # . . push args
    68/push  "Adc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-compare-inequal-strings-equal-sizes"/imm32
    68/push  0/imm32/false
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    c3/return

# helper for later tests
check-strings-equal:  # s: (addr array byte), expected: (addr array byte), msg: (addr array byte)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    # var eax: boolean = string-equal?(s, expected)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
    68/push  1/imm32
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$check-strings-equal:end:
    # . restore registers
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# test the helper
test-check-strings-equal:
    # check-strings-equal("Abc", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  check-strings-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    c3/return

# . . vim:nowrap:textwidth=0
0/y, "| ", "F - test-render-gap-buffer-with-cursor-at-start: bg" } fn test-render-gap-buffer-highlight-matching-close-paren { var gap-storage: gap-buffer var gap/esi: (addr gap-buffer) <- address gap-storage initialize-gap-buffer-with gap, "(a)" gap-to-start gap # setup: screen var screen-storage: screen var screen/edi: (addr screen) <- address screen-storage initialize-screen screen, 5, 4, 0/no-pixel-graphics # var x/eax: int <- render-gap-buffer screen, gap, 0/x, 0/y, 1/show-cursor, 3/fg, 0xc5/bg=blue-bg check-screen-row screen, 0/y, "(a) ", "F - test-render-gap-buffer-highlight-matching-close-paren" check-ints-equal x, 4, "F - test-render-gap-buffer-highlight-matching-close-paren: result" check-background-color-in-screen-row screen, 3/bg=reverse, 0/y, "| ", "F - test-render-gap-buffer-highlight-matching-close-paren: cursor" check-screen-row-in-color screen, 0xf/fg=highlight, 0/y, " ) ", "F - test-render-gap-buffer-highlight-matching-close-paren: matching paren" } fn test-render-gap-buffer-highlight-matching-open-paren { var gap-storage: gap-buffer var gap/esi: (addr gap-buffer) <- address gap-storage initialize-gap-buffer-with gap, "(a)" gap-to-end gap var dummy/eax: code-point-utf8 <- gap-left gap # setup: screen var screen-storage: screen var screen/edi: (addr screen) <- address screen-storage initialize-screen screen, 5, 4, 0/no-pixel-graphics # var x/eax: int <- render-gap-buffer screen, gap, 0/x, 0/y, 1/show-cursor, 3/fg, 0xc5/bg=blue-bg check-screen-row screen, 0/y, "(a) ", "F - test-render-gap-buffer-highlight-matching-open-paren" check-ints-equal x, 4, "F - test-render-gap-buffer-highlight-matching-open-paren: result" check-background-color-in-screen-row screen, 3/bg=reverse, 0/y, " | ", "F - test-render-gap-buffer-highlight-matching-open-paren: cursor" check-screen-row-in-color screen, 0xf/fg=highlight, 0/y, "( ", "F - test-render-gap-buffer-highlight-matching-open-paren: matching paren" } fn test-render-gap-buffer-highlight-matching-open-paren-of-end { var gap-storage: gap-buffer var gap/esi: (addr gap-buffer) <- address gap-storage initialize-gap-buffer-with gap, "(a)" gap-to-end gap # setup: screen var screen-storage: screen var screen/edi: (addr screen) <- address screen-storage initialize-screen screen, 5, 4, 0/no-pixel-graphics # var x/eax: int <- render-gap-buffer screen, gap, 0/x, 0/y, 1/show-cursor, 3/fg, 0xc5/bg=blue-bg check-screen-row screen, 0/y, "(a) ", "F - test-render-gap-buffer-highlight-matching-open-paren-of-end" check-ints-equal x, 4, "F - test-render-gap-buffer-highlight-matching-open-paren-of-end: result" check-background-color-in-screen-row screen, 3/bg=reverse, 0/y, " |", "F - test-render-gap-buffer-highlight-matching-open-paren-of-end: cursor" check-screen-row-in-color screen, 0xf/fg=highlight, 0/y, "( ", "F - test-render-gap-buffer-highlight-matching-open-paren-of-end: matching paren" } # should I highlight a matching open paren? And if so, at what depth from top of left? # basically there are two cases to disambiguate here: # Usually the cursor is at top of right. Highlight first '(' at depth 0 from top of left. # If right is empty, match the ')' _before_ cursor. Highlight first '(' at depth _1_ from top of left. fn highlight-matching-open-paren? _gap: (addr gap-buffer), render-cursor?: boolean -> _/ebx: boolean, _/edi: int { # if not rendering cursor, return compare render-cursor?, 0/false { break-if-!= return 0/false, 0 } var gap/esi: (addr gap-buffer) <- copy _gap var stack/edi: (addr code-point-utf8-stack) <- get gap, right var top-addr/eax: (addr int) <- get stack, top var top-index/ecx: int <- copy *top-addr compare top-index, 0 { break-if-> # if cursor at end, return (char before cursor == ')', 1) stack <- get gap, left top-addr <- get stack, top top-index <- copy *top-addr compare top-index, 0 { break-if-> return 0/false, 0 } top-index <- decrement var data-ah/eax: (addr handle array code-point-utf8) <- get stack, data var data/eax: (addr array code-point-utf8) <- lookup *data-ah var g/eax: (addr code-point-utf8) <- index data, top-index compare *g, 0x29/close-paren { break-if-= return 0/false, 0 } return 1/true, 1 } # cursor is not at end; return (char at cursor == ')') top-index <- decrement var data-ah/eax: (addr handle array code-point-utf8) <- get stack, data var data/eax: (addr array code-point-utf8) <- lookup *data-ah var g/eax: (addr code-point-utf8) <- index data, top-index compare *g, 0x29/close-paren { break-if-= return 0/false, 0 } return 1/true, 0 } fn test-highlight-matching-open-paren { var gap-storage: gap-buffer var gap/esi: (addr gap-buffer) <- address gap-storage initialize-gap-buffer-with gap, "(a)" gap-to-end gap var highlight-matching-open-paren?/ebx: boolean <- copy 0/false var open-paren-depth/edi: int <- copy 0 highlight-matching-open-paren?, open-paren-depth <- highlight-matching-open-paren? gap, 0/no-cursor check-not highlight-matching-open-paren?, "F - test-highlight-matching-open-paren: no cursor" highlight-matching-open-paren?, open-paren-depth <- highlight-matching-open-paren? gap, 1/render-cursor check highlight-matching-open-paren?, "F - test-highlight-matching-open-paren: at end immediately after ')'" check-ints-equal open-paren-depth, 1, "F - test-highlight-matching-open-paren: depth at end immediately after ')'" var dummy/eax: code-point-utf8 <- gap-left gap highlight-matching-open-paren?, open-paren-depth <- highlight-matching-open-paren? gap, 1/render-cursor check highlight-matching-open-paren?, "F - test-highlight-matching-open-paren: on ')'" dummy <- gap-left gap highlight-matching-open-paren?, open-paren-depth <- highlight-matching-open-paren? gap, 1/render-cursor check-not highlight-matching-open-paren?, "F - test-highlight-matching-open-paren: not on ')'" } ## some primitives for scanning through a gap buffer # don't modify the gap buffer while scanning # this includes moving the cursor around # restart scan without affecting gap-buffer contents fn rewind-gap-buffer _self: (addr gap-buffer) { var self/esi: (addr gap-buffer) <- copy _self var dest/eax: (addr int) <- get self, left-read-index copy-to *dest, 0 dest <- get self, right-read-index copy-to *dest, 0 } fn gap-buffer-scan-done? _self: (addr gap-buffer) -> _/eax: boolean { var self/esi: (addr gap-buffer) <- copy _self # more in left? var left/eax: (addr code-point-utf8-stack) <- get self, left var left-size/eax: int <- code-point-utf8-stack-length left var left-read-index/ecx: (addr int) <- get self, left-read-index compare *left-read-index, left-size { break-if->= return 0/false } # more in right? var right/eax: (addr code-point-utf8-stack) <- get self, right var right-size/eax: int <- code-point-utf8-stack-length right var right-read-index/ecx: (addr int) <- get self, right-read-index compare *right-read-index, right-size { break-if->= return 0/false } # return 1/true } fn peek-from-gap-buffer _self: (addr gap-buffer) -> _/eax: code-point-utf8 { var self/esi: (addr gap-buffer) <- copy _self # more in left? var left/ecx: (addr code-point-utf8-stack) <- get self, left var left-size/eax: int <- code-point-utf8-stack-length left var left-read-index-a/edx: (addr int) <- get self, left-read-index compare *left-read-index-a, left-size { break-if->= var left-data-ah/eax: (addr handle array code-point-utf8) <- get left, data var left-data/eax: (addr array code-point-utf8) <- lookup *left-data-ah var left-read-index/ecx: int <- copy *left-read-index-a var result/eax: (addr code-point-utf8) <- index left-data, left-read-index return *result } # more in right? var right/ecx: (addr code-point-utf8-stack) <- get self, right var _right-size/eax: int <- code-point-utf8-stack-length right var right-size/ebx: int <- copy _right-size var right-read-index-a/edx: (addr int) <- get self, right-read-index compare *right-read-index-a, right-size { break-if->= # read the right from reverse var right-data-ah/eax: (addr handle array code-point-utf8) <- get right, data var right-data/eax: (addr array code-point-utf8) <- lookup *right-data-ah var right-read-index/ebx: int <- copy right-size right-read-index <- subtract *right-read-index-a right-read-index <- subtract 1 var result/eax: (addr code-point-utf8) <- index right-data, right-read-index return *result } # if we get here there's nothing left return 0/nul } fn read-from-gap-buffer _self: (addr gap-buffer) -> _/eax: code-point-utf8 { var self/esi: (addr gap-buffer) <- copy _self # more in left? var left/ecx: (addr code-point-utf8-stack) <- get self, left var left-size/eax: int <- code-point-utf8-stack-length left var left-read-index-a/edx: (addr int) <- get self, left-read-index compare *left-read-index-a, left-size { break-if->= var left-data-ah/eax: (addr handle array code-point-utf8) <- get left, data var left-data/eax: (addr array code-point-utf8) <- lookup *left-data-ah var left-read-index/ecx: int <- copy *left-read-index-a var result/eax: (addr code-point-utf8) <- index left-data, left-read-index increment *left-read-index-a return *result } # more in right? var right/ecx: (addr code-point-utf8-stack) <- get self, right var _right-size/eax: int <- code-point-utf8-stack-length right var right-size/ebx: int <- copy _right-size var right-read-index-a/edx: (addr int) <- get self, right-read-index compare *right-read-index-a, right-size { break-if->= # read the right from reverse var right-data-ah/eax: (addr handle array code-point-utf8) <- get right, data var right-data/eax: (addr array code-point-utf8) <- lookup *right-data-ah var right-read-index/ebx: int <- copy right-size right-read-index <- subtract *right-read-index-a right-read-index <- subtract 1 var result/eax: (addr code-point-utf8) <- index right-data, right-read-index increment *right-read-index-a return *result } # if we get here there's nothing left return 0/nul } fn put-back-from-gap-buffer _self: (addr gap-buffer) { var self/esi: (addr gap-buffer) <- copy _self # more in right? var right/eax: (addr code-point-utf8-stack) <- get self, right var right-size/eax: int <- code-point-utf8-stack-length right var right-read-index-a/eax: (addr int) <- get self, right-read-index compare *right-read-index-a, 0 { break-if-<= decrement *right-read-index-a return } # more in left? var left/eax: (addr code-point-utf8-stack) <- get self, left var left-size/eax: int <- code-point-utf8-stack-length left var left-read-index-a/eax: (addr int) <- get self, left-read-index decrement *left-read-index-a } fn test-read-from-gap-buffer { var gap-storage: gap-buffer var gap/esi: (addr gap-buffer) <- address gap-storage initialize-gap-buffer-with gap, "abc" # gap is at end, all contents are in left var done?/eax: boolean <- gap-buffer-scan-done? gap check-not done?, "F - test-read-from-gap-buffer/left-1/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0x61/a, "F - test-read-from-gap-buffer/left-1" var done?/eax: boolean <- gap-buffer-scan-done? gap check-not done?, "F - test-read-from-gap-buffer/left-2/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0x62/b, "F - test-read-from-gap-buffer/left-2" var done?/eax: boolean <- gap-buffer-scan-done? gap check-not done?, "F - test-read-from-gap-buffer/left-3/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0x63/c, "F - test-read-from-gap-buffer/left-3" var done?/eax: boolean <- gap-buffer-scan-done? gap check done?, "F - test-read-from-gap-buffer/left-4/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0/nul, "F - test-read-from-gap-buffer/left-4" # now check when everything is to the right gap-to-start gap rewind-gap-buffer gap var done?/eax: boolean <- gap-buffer-scan-done? gap check-not done?, "F - test-read-from-gap-buffer/right-1/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0x61/a, "F - test-read-from-gap-buffer/right-1" var done?/eax: boolean <- gap-buffer-scan-done? gap check-not done?, "F - test-read-from-gap-buffer/right-2/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0x62/b, "F - test-read-from-gap-buffer/right-2" var done?/eax: boolean <- gap-buffer-scan-done? gap check-not done?, "F - test-read-from-gap-buffer/right-3/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0x63/c, "F - test-read-from-gap-buffer/right-3" var done?/eax: boolean <- gap-buffer-scan-done? gap check done?, "F - test-read-from-gap-buffer/right-4/done" var g/eax: code-point-utf8 <- read-from-gap-buffer gap var x/ecx: int <- copy g check-ints-equal x, 0/nul, "F - test-read-from-gap-buffer/right-4" } fn skip-spaces-from-gap-buffer self: (addr gap-buffer) { var done?/eax: boolean <- gap-buffer-scan-done? self compare done?, 0/false break-if-!= var g/eax: code-point-utf8 <- peek-from-gap-buffer self { compare g, 0x20/space break-if-= return } g <- read-from-gap-buffer self loop } fn edit-gap-buffer self: (addr gap-buffer), key: code-point-utf8 { var g/edx: code-point-utf8 <- copy key { compare g, 8/backspace break-if-!= delete-before-gap self return } { compare g, 0x80/left-arrow break-if-!= var dummy/eax: code-point-utf8 <- gap-left self return } { compare g, 0x83/right-arrow break-if-!= var dummy/eax: code-point-utf8 <- gap-right self return } { compare g, 6/ctrl-f break-if-!= gap-to-start-of-next-word self return } { compare g, 2/ctrl-b break-if-!= gap-to-end-of-previous-word self return } { compare g, 1/ctrl-a break-if-!= gap-to-previous-start-of-line self return } { compare g, 5/ctrl-e break-if-!= gap-to-next-end-of-line self return } { compare g, 0x81/down-arrow break-if-!= gap-down self return } { compare g, 0x82/up-arrow break-if-!= gap-up self return } { compare g, 0x15/ctrl-u break-if-!= clear-gap-buffer self return } { compare g, 9/tab break-if-!= # tab = 2 spaces add-code-point-at-gap self, 0x20/space add-code-point-at-gap self, 0x20/space return } # default: insert character add-code-point-utf8-at-gap self, g } fn gap-to-start-of-next-word self: (addr gap-buffer) { var curr/eax: code-point-utf8 <- copy 0 # skip to next space { curr <- gap-right self compare curr, -1 break-if-= compare curr, 0x20/space break-if-= compare curr, 0xa/newline break-if-= loop } # skip past spaces { curr <- gap-right self compare curr, -1 break-if-= compare curr, 0x20/space loop-if-= compare curr, 0xa/space loop-if-= curr <- gap-left self break } } fn gap-to-end-of-previous-word self: (addr gap-buffer) { var curr/eax: code-point-utf8 <- copy 0 # skip to previous space { curr <- gap-left self compare curr, -1 break-if-= compare curr, 0x20/space break-if-= compare curr, 0xa/newline break-if-= loop } # skip past all spaces but one { curr <- gap-left self compare curr, -1 break-if-= compare curr, 0x20/space loop-if-= compare curr, 0xa/space loop-if-= curr <- gap-right self break } } fn gap-to-previous-start-of-line self: (addr gap-buffer) { # skip past immediate newline var dummy/eax: code-point-utf8 <- gap-left self # skip to previous newline { dummy <- gap-left self { compare dummy, -1 break-if-!= return } { compare dummy, 0xa/newline break-if-!= dummy <- gap-right self return } loop } } fn gap-to-next-end-of-line self: (addr gap-buffer) { # skip past immediate newline var dummy/eax: code-point-utf8 <- gap-right self # skip to next newline { dummy <- gap-right self { compare dummy, -1 break-if-!= return } { compare dummy, 0xa/newline break-if-!= dummy <- gap-left self return } loop } } fn gap-up self: (addr gap-buffer) { # compute column var col/edx: int <- count-columns-to-start-of-line self # gap-to-previous-start-of-line self # skip ahead by up to col on previous line var i/ecx: int <- copy 0 { compare i, col break-if->= var curr/eax: code-point-utf8 <- gap-right self { compare curr, -1 break-if-!= return } compare curr, 0xa/newline { break-if-!= curr <- gap-left self return } i <- increment loop } } fn gap-down self: (addr gap-buffer) { # compute column var col/edx: int <- count-columns-to-start-of-line self # skip to start of next line gap-to-end-of-line self var dummy/eax: code-point-utf8 <- gap-right self # skip ahead by up to col on previous line var i/ecx: int <- copy 0 { compare i, col break-if->= var curr/eax: code-point-utf8 <- gap-right self { compare curr, -1 break-if-!= return } compare curr, 0xa/newline { break-if-!= curr <- gap-left self return } i <- increment loop } } fn count-columns-to-start-of-line self: (addr gap-buffer) -> _/edx: int { var count/edx: int <- copy 0 var dummy/eax: code-point-utf8 <- copy 0 # skip to previous newline { dummy <- gap-left self { compare dummy, -1 break-if-!= return count } { compare dummy, 0xa/newline break-if-!= dummy <- gap-right self return count } count <- increment loop } return count } fn gap-to-end-of-line self: (addr gap-buffer) { var dummy/eax: code-point-utf8 <- copy 0 # skip to next newline { dummy <- gap-right self { compare dummy, -1 break-if-!= return } { compare dummy, 0xa/newline break-if-!= dummy <- gap-left self return } loop } }