Files
expr/operator-length.go
T
camoroso 8e596d5979 several changes:
1. moved scan/symbol.go to new package sym and adapted all files that reference Symbol type and its values.
2. new type interval defined by begin, end and step values.
3. replaced operator-range.go with operator-interval.go.
4. replaced range operator begin:end with begin..end..step.
5. new implementation of sub-collection extraction based on new interval literal.
2026-07-27 16:01:52 +02:00

59 lines
1.4 KiB
Go

// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-length.go
package expr
import (
"git.portale-stac.it/go-pkg/expr/kern"
"git.portale-stac.it/go-pkg/expr/scan"
"git.portale-stac.it/go-pkg/expr/sym"
"git.portale-stac.it/go-pkg/expr/types/array"
"git.portale-stac.it/go-pkg/expr/types/dict"
"git.portale-stac.it/go-pkg/expr/types/list"
"git.portale-stac.it/go-pkg/expr/types/str"
)
//-------- length term
func newLengthTerm(tk *scan.Token) (inst *scan.Term) {
return &scan.Term{
Tk: *tk,
Children: make([]*scan.Term, 0, 1),
Position: scan.PosPrefix,
Priority: scan.PriSign,
EvalFunc: evalLength,
}
}
func evalLength(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
var childValue any
if childValue, err = opTerm.EvalPrefix(ctx); err != nil {
return
}
if array.IsArray(childValue) {
ls, _ := childValue.(*array.ArrayType)
v = int64(len(*ls))
} else if str.IsString(childValue) {
s, _ := childValue.(string)
v = int64(len(s))
} else if dict.IsDict(childValue) {
m, _ := childValue.(*dict.DictType)
v = int64(len(*m))
} else if lls, ok := childValue.(*list.LinkedList); ok {
v = int64(lls.Len())
} else if it, ok := childValue.(kern.Iterator); ok {
v = int64(it.Count())
} else {
err = opTerm.ErrIncompatiblePrefixPostfixType(childValue)
}
return
}
// init
func init() {
scan.RegisterTermConstructor(sym.SymHash, newLengthTerm)
}