simplify 'vi-hl-dfa's DFA, removing one state

This commit is contained in:
2025-09-19 02:40:14 +01:00
parent ad7d103f61
commit ab2f2cc752
2 changed files with 20 additions and 35 deletions

View File

@@ -4,30 +4,19 @@ struct
val notNewlineState: Word8.word = 0w1 val notNewlineState: Word8.word = 0w1
val oneNewlineState: Word8.word = 0w2 val oneNewlineState: Word8.word = 0w2
val twoNewlineState: Word8.word = 0w3 val twoNewlineState: Word8.word = 0w3
val notNewlineAfterNewlineState: Word8.word = 0w4
fun makeStart i = fun makeStart i =
if Char.chr i = #"\n" then oneNewlineState else notNewlineState if Char.chr i = #"\n" then oneNewlineState else notNewlineState
fun makeOneNewline i = fun makeOneNewline i =
if Char.chr i = #"\n" then twoNewlineState else notNewlineAfterNewlineState if Char.chr i = #"\n" then twoNewlineState else notNewlineState
fun makeTwoNeline i =
if Char.chr i = #"\n" then oneNewlineState else notNewlineAfterNewlineState
val startTable = Vector.tabulate (255, makeStart) val startTable = Vector.tabulate (255, makeStart)
val notNewlineTable = startTable val notNewlineTable = startTable
val oneNewlineTable = Vector.tabulate (255, makeOneNewline) val oneNewlineTable = Vector.tabulate (255, makeOneNewline)
val twoNewLineTable = startTable val twoNewLineTable = startTable
val notNewlineAfterNewlineTable = notNewlineTable
val tables = val tables = #[startTable, notNewlineTable, oneNewlineTable, twoNewLineTable]
#[ startTable
, notNewlineTable
, oneNewlineTable
, twoNewLineTable
, notNewlineAfterNewlineTable
]
fun next (currentState, chr) = fun next (currentState, chr) =
let val table = Vector.sub (tables, Word8.toInt currentState) let val table = Vector.sub (tables, Word8.toInt currentState)
@@ -35,7 +24,7 @@ struct
end end
structure ViL = structure ViL =
MakeNextDfaLoop MakeNextDfaLoopPlus1
(struct (struct
val startState = startState val startState = startState
@@ -49,18 +38,13 @@ struct
val chr = String.sub (str, idx) val chr = String.sub (str, idx)
val newState = next (currentState, chr) val newState = next (currentState, chr)
in in
if if newState = twoNewlineState then
newState = twoNewlineState if counter - 1 = 0 then
then
if counter - 1 = ~1 then
absIdx - 1 absIdx - 1
else else
loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1) loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
else if else if newState = notNewlineState then
newState = notNewlineState if counter - 1 = 0 then
orelse newState = notNewlineAfterNewlineState
then
if counter - 1 = ~1 then
absIdx absIdx
else else
loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1) loop (idx + 1, absIdx + 1, str, tl, startState, counter - 1)
@@ -89,15 +73,10 @@ struct
val newState = next (currentState, chr) val newState = next (currentState, chr)
in in
if newState = twoNewlineState then 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 loop (idx - 1, absIdx - 1, str, tl, newState, counter - 1)
else if newState = notNewlineState then else if newState = notNewlineState then
if counter - 1 = ~1 then if counter - 1 = 0 then
absIdx
else
loop (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else if newState = notNewlineAfterNewlineState then
if counter - 1 <= 0 then
absIdx absIdx
else else
loop (idx - 1, absIdx - 1, str, tl, startState, counter - 1) loop (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
@@ -105,7 +84,17 @@ struct
loop (idx - 1, absIdx - 1, str, tl, newState, counter) loop (idx - 1, absIdx - 1, str, tl, newState, counter)
end 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) end)
val l = ViL.next val l = ViL.next

View File

@@ -5,10 +5,6 @@ struct
val ySpace = 25 val ySpace = 25
val fontSize = 30.0 val fontSize = 30.0
fun accToDrawMsg (textAcc, cursorAcc) =
let
open MailboxType
open DrawMsg
val textAcc = Vector.concat textAcc val textAcc = Vector.concat textAcc
val cursorAcc = Vector.concat cursorAcc val cursorAcc = Vector.concat cursorAcc