From b80b72cfe2e02ce7b9467e161703a47e433f992d Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Wed, 19 Apr 2023 16:00:04 +0100 Subject: Replaces the workspace with 3 distinct registers: path, value and xreg workspace contained a list of WalkItems, pairs of paths and values. The new system can still hold a list of values but only one path, which is in itself a list of values. The X register is miscellaneous. All 3 hold a list of values (which are JSON tokens) --- walk/walk.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'walk') diff --git a/walk/walk.go b/walk/walk.go index 64f16ac..cc17245 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -5,10 +5,41 @@ import ( "encoding/json" "fmt" "strings" + "math" ) +// int or string type PathSegment interface {} type Path []PathSegment +func (path Path) ToWalkValues() []WalkValue { + var values []WalkValue + for _, segment := range path { + switch s := segment.(type) { + case int: + values = append(values, ValueNumber(s)) + case string: + values = append(values, ValueString(s)) + default: + panic("Invalid PathSegment") + } + } + return values +} + +func PathFromWalkValues(values []WalkValue) Path { + var segments []PathSegment + for _, value := range values { + switch v := value.(type) { + case ValueNumber: + segments = append(segments, int(math.Round(float64(v)))) + case ValueString: + segments = append(segments, string(v)) + default: + panic("Invalid value in path") + } + } + return segments +} type TerminalValue int const ( -- cgit v1.2.3