diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2024-03-30 21:09:32 +0000 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2024-03-30 21:09:32 +0000 |
commit | fd79fd18c6c32884e757e91b8629c87af4cbf34e (patch) | |
tree | 50e6b49c64b1c6cf230c80ce83a925ec98fd2387 /subex/subexstate.go | |
parent | 976d96af62945178f3a3ab572620026df75003cf (diff) | |
download | stred-go-fd79fd18c6c32884e757e91b8629c87af4cbf34e.tar |
Add map destructure
Diffstat (limited to 'subex/subexstate.go')
-rw-r--r-- | subex/subexstate.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/subex/subexstate.go b/subex/subexstate.go index 45b5d00..26d7347 100644 --- a/subex/subexstate.go +++ b/subex/subexstate.go @@ -373,6 +373,31 @@ func (state SubexConstructArrayValuesState) epsilon(aux auxiliaryState) []SubexB }} } +type SubexConstructMapState struct { + next SubexState +} +func (state SubexConstructMapState) epsilon(aux auxiliaryState) []SubexBranch { + values, aux := aux.popOutput() + var m walk.MapValue + if len(values) % 2 != 0 { + panic("Tried to construct array with odd length input") + } + for i := 0; i < len(values); i += 2 { + key, isNum := values[i].(walk.StringValue) + if !isNum { + panic("Tried to construct array with non-numeric index") + } + m = append(m, walk.MapElement { + Key: string(key), + Value: values[i + 1], + }) + } + return []SubexBranch {{ + state: state.next, + aux: aux.topAppend([]walk.Value {m}), + }} +} + type SubexConstructStringState struct { next SubexState } |