internal/rosa/azalea: integer arrays
All checks were successful
Test / Create distribution (push) Successful in 1m5s
Test / Sandbox (push) Successful in 2m46s
Test / ShareFS (push) Successful in 3m44s
Test / Hakurei (push) Successful in 3m52s
Test / Sandbox (race detector) (push) Successful in 5m23s
Test / Hakurei (race detector) (push) Successful in 6m28s
Test / Flake checks (push) Successful in 1m22s

This is useful for some helper functions. Performance is unaffected.

Before:
BenchmarkStage3-128     	    8308	   1960687 ns/op	 1023794 B/op	   14755 allocs/op
BenchmarkAll-128        	    3331	   5518571 ns/op	 2902320 B/op	   37993 allocs/op

After:
BenchmarkStage3-128     	    8330	   1946273 ns/op	 1023046 B/op	   14750 allocs/op
BenchmarkAll-128        	    3296	   5585805 ns/op	 2901746 B/op	   37991 allocs/op

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-05-21 22:47:41 +09:00
parent 0615899e56
commit cdd31dd27b
2 changed files with 34 additions and 7 deletions

View File

@@ -11,7 +11,7 @@ import (
// Value are types supported by the language. // Value are types supported by the language.
type Value interface { type Value interface {
bool | int64 | string | []string | [][2]string bool | int64 | string | []string | []int64 | [][2]string
} }
type ( type (
@@ -85,6 +85,18 @@ func evaluate[T Value](d PF, s []Frame, expr any, rp *T) bool {
return evaluateAny(d, s, expr, rp) return evaluateAny(d, s, expr, rp)
} }
// evaluateArray evaluates expr and returns its values as a slice.
func evaluateArray[T Value](d PF, s []Frame, expr Array) []T {
r := make([]T, 0, len(expr))
for i := range expr {
var _r T
if evaluate(d, s, expr[i], &_r) {
r = append(r, _r)
}
}
return r
}
// TypeError is an unexpected type during evaluation. // TypeError is an unexpected type during evaluation.
type TypeError struct { type TypeError struct {
Concrete, Asserted reflect.Type Concrete, Asserted reflect.Type
@@ -232,14 +244,13 @@ func evaluateAny(d PF, s []Frame, expr, rp any) bool {
return true return true
case Array: case Array:
r := make([]string, 0, len(e)) if len(e) > 0 && len(e[0]) == 1 {
for i := range e { if _, ok := e[0][0].(Int); ok {
var _r string store(rp, evaluateArray[int64](d, s, e))
if evaluate(d, s, e[i], &_r) { return true
r = append(r, _r)
} }
} }
store(rp, r) store(rp, evaluateArray[string](d, s, e))
return true return true
case []KV: case []KV:

View File

@@ -25,6 +25,17 @@ func makeStackCheck(check func(args FArgs) (any, error)) []Frame {
}}} }}}
} }
// checkArgs is like makeStackCheck, but the resulting function asserts that its
// args match the expected value.
func checkArgs(want FArgs) []Frame {
return makeStackCheck(func(args FArgs) (any, error) {
if !reflect.DeepEqual(args, want) {
return nil, fmt.Errorf("%#v, want %#v", args, want)
}
return "\xfd", nil
})
}
func TestEvaluate(t *testing.T) { func TestEvaluate(t *testing.T) {
t.Parallel() t.Parallel()
@@ -164,6 +175,11 @@ func TestEvaluate(t *testing.T) {
{"source handle", `package name { source = name; }`, nil, FArgs{ {"source handle", `package name { source = name; }`, nil, FArgs{
{K: unique.Make(Ident("source")), V: Ident("name")}, {K: unique.Make(Ident("source")), V: Ident("name")},
}, nil}, }, nil},
{"integer array", `f { v = [ 0 ]; _v = [ 0, 9 ]; }`, checkArgs(FArgs{
{K: unique.Make(Ident("v")), V: []int64{0}},
{K: unique.Make(Ident("_v")), V: []int64{0, 9}},
}), "\xfd", nil},
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {