added the prefix operator '#' (length of string and array)

This commit is contained in:
Celestino Amoroso 2024-04-17 07:12:32 +02:00
parent 353d495c50
commit b6887af77a

44
operator-length.go Normal file
View File

@ -0,0 +1,44 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-length.go
package expr
//-------- length term
func newLengthTerm(tk *Token) (inst *term) {
return &term{
tk: *tk,
children: make([]*term, 0, 1),
position: posPrefix,
priority: priSign,
evalFunc: evalLength,
}
}
func evalLength(ctx ExprContext, self *term) (v any, err error) {
var rightValue any
if rightValue, err = self.evalPrefix(ctx); err != nil {
return
}
if isList(rightValue) {
list, _ := rightValue.([]any)
v = len(list)
} else if isString(rightValue) {
s, _ := rightValue.(string)
v = len(s)
// } else {
// v = 1
// }
} else {
err = self.errIncompatibleType(rightValue)
}
return
}
// init
func init() {
registerTermConstructor(SymHash, newLengthTerm)
}