From b6887af77ad7267eb884ed72f2c1b5c053f6f85e Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Wed, 17 Apr 2024 07:12:32 +0200 Subject: [PATCH] added the prefix operator '#' (length of string and array) --- operator-length.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 operator-length.go diff --git a/operator-length.go b/operator-length.go new file mode 100644 index 0000000..5d895e7 --- /dev/null +++ b/operator-length.go @@ -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) +}