diff options
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 |
