<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2023-04-21 11:18:06 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-21 11:18:06 +0100
commit80e7fd0626bfb98f8c1b7f69726d88f8cfa3e4fc (patch)
tree085c0797d5264fe3dad7b6b42b67979fd20dd297
parenta4015bc64d5174f62bc2d150c6a780b89c00a0cc (diff)
downloadstred-go-80e7fd0626bfb98f8c1b7f69726d88f8cfa3e4fc.tar
Adds String methods to all SubexASTs for debugging purposes
-rw-r--r--subex/subexast.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/subex/subexast.go b/subex/subexast.go
index baf7a3b..ee7a959 100644
--- a/subex/subexast.go
+++ b/subex/subexast.go
@@ -48,6 +48,9 @@ func (ast SubexASTOr) compileWith(next SubexState) SubexState {
ast.Second.compileWith(next),
}
}
+func (ast SubexASTOr) String() string {
+ return fmt.Sprintf("(%v)|(%v)", ast.First, ast.Second)
+}
type ConvexRange struct {
Start, End int
@@ -122,6 +125,9 @@ func (ast SubexASTRepeat) compileWith(next SubexState) SubexState {
}
return state
}
+func (ast SubexASTRepeat) String() string {
+ return fmt.Sprintf("(%v){...}", ast.Content)
+}
// Read in a single specific Atom and output it unchanged
type SubexASTCopyAtom struct {
@@ -133,24 +139,36 @@ func (ast SubexASTCopyAtom) compileWith(next SubexState) SubexState {
next: next,
}
}
+func (ast SubexASTCopyAtom) String() string {
+ return fmt.Sprintf("a")
+}
// Read in a single atom that must be a boolean and output it unchanged
type SubexASTCopyBool struct {}
func (ast SubexASTCopyBool) compileWith(next SubexState) SubexState {
return &SubexCopyBoolState {next}
}
+func (ast SubexASTCopyBool) String() string {
+ return "?"
+}
// Read in a single atom that must be a number and output it unchanged
type SubexASTCopyNumber struct {}
func (ast SubexASTCopyNumber) compileWith(next SubexState) SubexState {
return &SubexCopyNumberState {next}
}
+func (ast SubexASTCopyNumber) String() string {
+ return "%"
+}
// Read in a single atom that must be a string atom and output it unchanged
type SubexASTCopyStringAtom struct {}
func (ast SubexASTCopyStringAtom) compileWith(next SubexState) SubexState {
return &SubexCopyStringAtomState {next}
}
+func (ast SubexASTCopyStringAtom) String() string {
+ return "_"
+}
// Read in a full string value and copy it out unchanged
// # is equivalent to "_{-0}"
@@ -172,6 +190,9 @@ func (ast SubexASTCopyString) compileWith(next SubexState) SubexState {
next: stringContentState,
}
}
+func (ast SubexASTCopyString) String() string {
+ return "#"
+}
// Read in a value and copy it out unchanged
// , is equivalent to `null`|?|%|#|[`{}[]`]
@@ -182,6 +203,9 @@ func (ast SubexASTCopyValue) compileWith(next SubexState) SubexState {
&SubexCopyNonStringAtomState {next},
}
}
+func (ast SubexASTCopyValue) String() string {
+ return ","
+}
// Read in any single Atom and output it unchanged
type SubexASTCopyAny struct {}
@@ -202,6 +226,9 @@ func (ast SubexASTOutput) compileWith(next SubexState) SubexState {
next: next,
}
}
+func (ast SubexASTOutput) String() string {
+ return "=...="
+}
// Read in a repeated subex separated by a delimiter. Greedy
type SubexASTJoin struct {
@@ -219,6 +246,9 @@ func (ast SubexASTJoin) compileWith(next SubexState) SubexState {
next,
}
}
+func (ast SubexASTJoin) String() string {
+ return fmt.Sprintf("(%v);(%v)", ast.Content, ast.Delimiter)
+}
// Run each input Atom through a map to produce an output Atom
// Atoms not in the map cause this to not match
@@ -231,6 +261,9 @@ func (ast SubexASTRange) compileWith(next SubexState) SubexState {
next: next,
}
}
+func (ast SubexASTRange) String() string {
+ return fmt.Sprintf("[abc=xyz]")
+}
// Run content, if content is a list of booleans, OR them, if all values are castable to numbers, sum them and output the total
// Reject if neither of these cases match
@@ -245,6 +278,9 @@ func (ast SubexASTSum) compileWith(next SubexState) SubexState {
}),
}
}
+func (ast SubexASTSum) String() string {
+ return fmt.Sprintf("(%v)+", ast.Content)
+}
// Like sum but for AND and product
type SubexASTProduct struct {
@@ -258,6 +294,9 @@ func (ast SubexASTProduct) compileWith(next SubexState) SubexState {
}),
}
}
+func (ast SubexASTProduct) String() string {
+ return fmt.Sprintf("(%v)*", ast.Content)
+}
// Runs the content Subex, if all outputted atoms can be cast to numbers, outputs them all negated
// Rejects if this fails
@@ -272,6 +311,9 @@ func (ast SubexASTNegate) compileWith(next SubexState) SubexState {
}),
}
}
+func (ast SubexASTNegate) String() string {
+ return fmt.Sprintf("(%v)-", ast.Content)
+}
// Runs the content Subex and collects the output
// If it is a list of atoms castable to numbers, it takes the reciprocal of them all and outputs them
@@ -287,6 +329,9 @@ func (ast SubexASTReciprocal) compileWith(next SubexState) SubexState {
}),
}
}
+func (ast SubexASTReciprocal) String() string {
+ return fmt.Sprintf("(%v)/", ast.Content)
+}
// Runs the content Subex and collects the output
// Maps over the values in the output, casting each to a boolean, notting each and then outputs them
@@ -302,9 +347,15 @@ func (ast SubexASTNot) compileWith(next SubexState) SubexState {
}),
}
}
+func (ast SubexASTNot) String() string {
+ return fmt.Sprintf("(%v)!", ast.Content)
+}
// Does nothing
type SubexASTEmpty struct {}
func (ast SubexASTEmpty) compileWith(next SubexState) SubexState {
return next
}
+func (ast SubexASTEmpty) String() string {
+ return "()"
+}