From 81925b6ad5212512d27365b8224b76095191431f Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Sun, 31 Mar 2024 21:23:17 +0100 Subject: Add " shorthand for string destructure --- subex/main.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'subex/main.go') diff --git a/subex/main.go b/subex/main.go index 982b585..32a5cf3 100644 --- a/subex/main.go +++ b/subex/main.go @@ -150,8 +150,7 @@ type auxiliaryState struct { outputStack OutputStack // How deeply nested the current execution is inside of the overall value // i.e. starts at zero, is incremented to one when entering an array - nestingLen int - nestingValue bool + nesting []bool } func (aux auxiliaryState) cloneStore() auxiliaryState { @@ -227,8 +226,15 @@ func (pair SubexEatBranch) accepting() []OutputStack { } func equalStates(left SubexEatBranch, right SubexEatBranch) bool { - // Only care about if they are the same pointer - return left.state == right.state && left.aux.nestingLen == right.aux.nestingLen && left.aux.nestingValue == right.aux.nestingValue + if left.state != right.state || len(left.aux.nesting) != len(right.aux.nesting) { + return false + } + for i, l := range left.aux.nesting { + if l != right.aux.nesting[i] { + return false + } + } + return true } // If two branches have the same state, only the first has a chance of being successful @@ -254,9 +260,6 @@ func addStates(curStates []SubexEatBranch, newStates []SubexBranch, nesting []bo case SubexEpsilonState: curStates = addStates(curStates, s.epsilon(state.aux), nesting) case SubexEatState: - if state.aux.nestingLen < len(nesting) && state.aux.nestingLen > 0 { - state.aux.nestingValue = nesting[state.aux.nestingLen - 1] - } curStates = append(curStates, SubexEatBranch{ state: s, aux: state.aux, @@ -270,12 +273,13 @@ func processInput(states []SubexEatBranch, input walk.Edible, nesting []bool) [] newStates := make([]SubexEatBranch, 0, 2) for _, state := range states { - if state.aux.nestingLen > len(nesting) { + if len(state.aux.nesting) > len(nesting) { continue } - if (state.aux.nestingLen == len(nesting) && - (len(nesting) == 0 || state.aux.nestingValue || nesting[len(nesting) - 1])) { + if (len(state.aux.nesting) == len(nesting) && + (len(state.aux.nesting) == 0 || len(nesting) == 0 || + state.aux.nesting[len(nesting) - 1] || nesting[len(nesting) - 1])) { newStates = addStates(newStates, state.eat(input), nesting) } else { newStates = append(newStates, state) @@ -320,8 +324,7 @@ func RunTransducer(transducer Transducer, input []walk.Value) (output []walk.Val values: make([][]walk.Value, transducer.storeSize.values), runes: make([][]rune, transducer.storeSize.runes), }, - nestingLen: 0, - nestingValue: true, + nesting: nil, }, }}, nil) @@ -334,7 +337,7 @@ func RunTransducer(transducer Transducer, input []walk.Value) (output []walk.Val } for _, state := range states { - if state.aux.nestingLen > 0 { + if len(state.aux.nesting) > 0 { continue } acceptingStacks := state.accepting() -- cgit v1.2.3