<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/walk.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2023-04-19 16:00:04 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-19 16:00:04 +0100
commitb80b72cfe2e02ce7b9467e161703a47e433f992d (patch)
tree81ad3202715f807cb4bf2987b8068e73f44e2a1a /walk/walk.go
parentdae74317d84471f6a859117fcbba1cf546be8ea1 (diff)
downloadstred-go-b80b72cfe2e02ce7b9467e161703a47e433f992d.tar
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)
Diffstat (limited to 'walk/walk.go')
-rw-r--r--walk/walk.go31
1 files changed, 31 insertions, 0 deletions
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 (