<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/main.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2024-03-31 21:23:17 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2024-03-31 21:23:17 +0100
commit81925b6ad5212512d27365b8224b76095191431f (patch)
treef625a8eddb556a58c45c7e16a205df1ed6b92d3a /subex/main.go
parent256450cc3dcdd9a9b92a33642739f7143526e9b9 (diff)
downloadstred-go-81925b6ad5212512d27365b8224b76095191431f.tar
Add " shorthand for string destructure
Diffstat (limited to 'subex/main.go')
-rw-r--r--subex/main.go29
1 files changed, 16 insertions, 13 deletions
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()