diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-10-30 22:30:58 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-10-30 22:30:58 +0900 |
| commit | 1b9124e88ab86e8a91384066cd96adbdfcf8bc4a (patch) | |
| tree | 97416b4fd4cefe27fd3ab051c79fc968abd50fd9 /internal/rosa/azalea/evaluate.go | |
| parent | 8969642abedfdc2d7e70fa695709b891ff631bae (diff) | |
internal/rosa/azalea: re-construct expr text
This significantly helps with debugging: the %#v text is incredibly
unhelpful to read, and just ends up filling the terminal with useless
information.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/rosa/azalea/evaluate.go')
| -rw-r--r-- | internal/rosa/azalea/evaluate.go | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/internal/rosa/azalea/evaluate.go b/internal/rosa/azalea/evaluate.go index 1fb730f3..3b55f863 100644 --- a/internal/rosa/azalea/evaluate.go +++ b/internal/rosa/azalea/evaluate.go @@ -6,6 +6,7 @@ import ( "maps" "reflect" "slices" + "strconv" "strings" "unique" ) @@ -34,9 +35,6 @@ type ( V map[unique.Handle[Ident]]any } - // Nil represents an untyped nil. - Nil struct{} - // AString represents an argument string that may be nil. AString struct { V string @@ -184,6 +182,35 @@ func store[T Value](rp any, r T) { } } +// ExprText is like the %#v printf verb, but for azalea expressions. +func ExprText(expr any) string { + switch v := expr.(type) { + case fmt.Stringer: + return v.String() + + case bool: + return strconv.FormatBool(v) + + case []KV: + if len(v) == 0 { + return "{}" + } + s := make([]string, len(v)) + for i, kv := range v { + s[i] = kv.String() + } + return "{\n\t" + strings.Join(s, "\n\t") + "\n}" + + default: + return fmt.Sprint(expr) + } +} + +// exprTextIndent is like [ExprText], but advances indentation by one level. +func exprTextIndent(expr any) string { + return strings.ReplaceAll(ExprText(expr), "\n", "\n\t") +} + // EvaluationError is an error and the expression it occurred in. type EvaluationError struct { Expr any |
