added the prefix operator '#' (length of string and array)
This commit is contained in:
parent
353d495c50
commit
b6887af77a
44
operator-length.go
Normal file
44
operator-length.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user