From ab2f2cc7527736876112f54b49a86c1fda84a89a Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 19 Sep 2025 02:40:14 +0100 Subject: [PATCH] simplify 'vi-hl-dfa's DFA, removing one state --- fcore/cursor-dfa/vi-hl-dfa.sml | 51 +++++++++++++--------------------- temp.txt | 4 --- 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/fcore/cursor-dfa/vi-hl-dfa.sml b/fcore/cursor-dfa/vi-hl-dfa.sml index 0dd131c..e1f5eb8 100644 --- a/fcore/cursor-dfa/vi-hl-dfa.sml +++ b/fcore/cursor-dfa/vi-hl-dfa.sml @@ -4,30 +4,19 @@ struct val notNewlineState: Word8.word = 0w1 val oneNewlineState: Word8.word = 0w2 val twoNewlineState: Word8.word = 0w3 - val notNewlineAfterNewlineState: Word8.word = 0w4 fun makeStart i = if Char.chr i = #"\n" then oneNewlineState else notNewlineState fun makeOneNewline i = - if Char.chr i = #"\n" then twoNewlineState else notNewlineAfterNewlineState - - fun makeTwoNeline i = - if Char.chr i = #"\n" then oneNewlineState else notNewlineAfterNewlineState + if Char.chr i = #"\n" then twoNewlineState else notNewlineState val startTable = Vector.tabulate (255, makeStart) val notNewlineTable = startTable val oneNewlineTable = Vector.tabulate (255, makeOneNewline) val twoNewLineTable = startTable - val notNewlineAfterNewlineTable = notNewlineTable - val tables = - #[ startTable - , notNewlineTable - , oneNewlineTable - , twoNewLineTable - , notNewlineAfterNewlineTable - ] + val tables = #[startTable, notNewlineTable, oneNewlineTable, twoNewLineTable] fun next (currentState, chr) = let val table = Vector.sub (tables, Word8.toInt currentState) @@ -35,7 +24,7 @@ struct end structure ViL = - MakeNextDfaLoop + MakeNextDfaLoopPlus1 (struct val startState = startState @@ -49,18 +38,13 @@ struct val chr = String.sub (str, idx) val newState = next (currentState, chr) in - if - newState = twoNewlineState - then - if counter - 1 = ~1 then + if newState = twoNewlineState then + if counter - 1 = 0 then absIdx - 1 else loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1) - else if - newState = notNewlineState - orelse newState = notNewlineAfterNewlineState - then - if counter - 1 = ~1 then + else if newState = notNewlineState then + if counter - 1 = 0 then absIdx else loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1) @@ -89,15 +73,10 @@ struct val newState = next (currentState, chr) in if newState = twoNewlineState then - if counter - 1 = ~1 then absIdx + if counter - 1 = 0 then absIdx else loop (idx - 1, absIdx - 1, str, tl, newState, counter - 1) else if newState = notNewlineState then - if counter - 1 = ~1 then - absIdx - else - loop (idx - 1, absIdx - 1, str, tl, startState, counter - 1) - else if newState = notNewlineAfterNewlineState then - if counter - 1 <= 0 then + if counter - 1 = 0 then absIdx else loop (idx - 1, absIdx - 1, str, tl, startState, counter - 1) @@ -105,7 +84,17 @@ struct loop (idx - 1, absIdx - 1, str, tl, newState, counter) end - val fStart = loop + fun fStart (idx, absIdx, str, tl, _, counter) = + if idx < 0 then + case tl of + str :: tl => + loop + (String.size str - 1, absIdx, str, tl, startState, counter) + | [] => 0 + else if String.sub (str, idx) = #"\n" then + loop (idx - 1, absIdx - 1, str, tl, oneNewlineState, counter) + else + loop (idx - 1, absIdx - 1, str, tl, startState, counter) end) val l = ViL.next diff --git a/temp.txt b/temp.txt index e9e1990..ccf1377 100644 --- a/temp.txt +++ b/temp.txt @@ -5,10 +5,6 @@ struct val ySpace = 25 val fontSize = 30.0 - fun accToDrawMsg (textAcc, cursorAcc) = - let - open MailboxType - open DrawMsg val textAcc = Vector.concat textAcc val cursorAcc = Vector.concat cursorAcc