new operator 'builtin'

This commit is contained in:
Celestino Amoroso 2024-04-19 00:19:11 +02:00
parent 4f05e5c90a
commit b76481bbf2
2 changed files with 64 additions and 5 deletions

57
operator-builtin.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-length.go
package expr
//-------- builtin term
func newBuiltinTerm(tk *Token) (inst *term) {
return &term{
tk: *tk,
children: make([]*term, 0, 1),
position: posPrefix,
priority: priSign,
evalFunc: evalBuiltin,
}
}
func evalBuiltin(ctx ExprContext, self *term) (v any, err error) {
var rightValue any
if rightValue, err = self.evalPrefix(ctx); err != nil {
return
}
count := 0
if isList(rightValue) {
list, _ := rightValue.([]any)
for i, moduleSpec := range list {
if module, ok := moduleSpec.(string); ok {
if ImportInContext(ctx, module) {
count++
} else {
err = self.Errorf("unknown module %q", module)
break
}
} else {
err = self.Errorf("expected string at item nr %d, got %T", i+1, moduleSpec)
break
}
}
} else if isString(rightValue) {
module, _ := rightValue.(string)
count, err = ImportInContextByGlobPattern(ctx, module)
} else {
err = self.errIncompatibleType(rightValue)
}
if err == nil {
v = count
}
return
}
// init
func init() {
registerTermConstructor(SymKwBuiltin, newBuiltinTerm)
}

View File

@ -89,6 +89,7 @@ const (
SymKwOr SymKwOr
SymKwBut SymKwBut
SymKwFunc SymKwFunc
SymKwBuiltin
) )
var keywords map[string]Symbol var keywords map[string]Symbol
@ -96,10 +97,11 @@ var keywords map[string]Symbol
func init() { func init() {
//keywords = make(map[string]Symbol) //keywords = make(map[string]Symbol)
keywords = map[string]Symbol{ keywords = map[string]Symbol{
"AND": SymKwAnd, "AND": SymKwAnd,
"BUT": SymKwBut, "BUILTIN": SymKwBuiltin,
"FUNC": SymKwFunc, "BUT": SymKwBut,
"NOT": SymKwNot, "FUNC": SymKwFunc,
"OR": SymKwOr, "NOT": SymKwNot,
"OR": SymKwOr,
} }
} }