diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-05-07 21:57:47 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-05-07 21:57:47 +0900 |
commit | ff10e6c495101bc0a73de66d1f28f180f3b562a7 (patch) | |
tree | e92904c540855e52f5dce3ccd53fb9a586657b04 /compiler/compiler.go | |
parent | Remove Peek* functions (diff) | |
download | tre-ff10e6c495101bc0a73de66d1f28f180f3b562a7.tar.gz tre-ff10e6c495101bc0a73de66d1f28f180f3b562a7.tar.xz |
Add transition table compressor
Diffstat (limited to 'compiler/compiler.go')
-rw-r--r-- | compiler/compiler.go | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/compiler/compiler.go b/compiler/compiler.go index 6f878c5..ffe0511 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -5,6 +5,7 @@ import ( "io" "strings" + "github.com/nihei9/maleeni/compressor" "github.com/nihei9/maleeni/log" "github.com/nihei9/maleeni/spec" ) @@ -160,14 +161,19 @@ func compile(entries []*spec.LexEntry, modeNums map[spec.LexModeName]spec.LexMod } config.logger.Log(`DFA: - States: %v states - Initial State: %v`, len(tranTab.Transition), tranTab.InitialState) + States: %v states (%v entries) + Initial State: %v`, tranTab.RowCount, tranTab.RowCount*tranTab.ColCount, tranTab.InitialState) config.logger.Log(" Accepting States:") for state, symbol := range tranTab.AcceptingStates { config.logger.Log(" %v: %v", state, symbol) } } + tranTab, err := compressTransitionTable(tranTab) + if err != nil { + return nil, err + } + return &spec.CompiledLexModeSpec{ Kinds: kinds, Push: push, @@ -175,3 +181,46 @@ func compile(entries []*spec.LexEntry, modeNums map[spec.LexModeName]spec.LexMod DFA: tranTab, }, nil } + +func compressTransitionTable(tranTab *spec.TransitionTable) (*spec.TransitionTable, error) { + ueTab := compressor.NewUniqueEntriesTable() + { + orig, err := compressor.NewOriginalTable(tranTab.UncompressedTransition, tranTab.ColCount) + if err != nil { + return nil, err + } + err = ueTab.Compress(orig) + if err != nil { + return nil, err + } + } + + rdTab := compressor.NewRowDisplacementTable(0) + { + orig, err := compressor.NewOriginalTable(ueTab.UniqueEntries, ueTab.OriginalColCount) + if err != nil { + return nil, err + } + err = rdTab.Compress(orig) + if err != nil { + return nil, err + } + } + + tranTab.Transition = &spec.UniqueEntriesTable{ + UniqueEntries: &spec.RowDisplacementTable{ + OriginalRowCount: rdTab.OriginalRowCount, + OriginalColCount: rdTab.OriginalColCount, + EmptyValue: rdTab.EmptyValue, + Entries: rdTab.Entries, + Bounds: rdTab.Bounds, + RowDisplacement: rdTab.RowDisplacement, + }, + RowNums: ueTab.RowNums, + OriginalRowCount: ueTab.OriginalRowCount, + OriginalColCount: ueTab.OriginalColCount, + } + tranTab.UncompressedTransition = nil + + return tranTab, nil +} |