From fd79fd18c6c32884e757e91b8629c87af4cbf34e Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Sat, 30 Mar 2024 21:09:32 +0000 Subject: Add map destructure --- subex/subexstate.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'subex/subexstate.go') 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 } -- cgit v1.2.3