2024-04-19 00:19:11 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
2024-04-27 09:47:24 +02:00
|
|
|
// operator-builtin.go
|
2024-04-19 00:19:11 +02:00
|
|
|
package expr
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
import "io"
|
|
|
|
|
2024-04-19 00:19:11 +02:00
|
|
|
//-------- 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) {
|
2024-05-03 05:30:58 +02:00
|
|
|
var childValue any
|
2024-04-19 00:19:11 +02:00
|
|
|
|
2024-05-03 05:30:58 +02:00
|
|
|
if childValue, err = self.evalPrefix(ctx); err != nil {
|
2024-04-19 00:19:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count := 0
|
2024-05-03 06:26:17 +02:00
|
|
|
if isString(childValue) {
|
|
|
|
module, _ := childValue.(string)
|
|
|
|
count, err = ImportInContextByGlobPattern(ctx, module)
|
|
|
|
} else {
|
|
|
|
var moduleSpec any
|
|
|
|
it := NewAnyIterator(childValue)
|
|
|
|
for moduleSpec, err = it.Next(); err == nil; moduleSpec, err = it.Next() {
|
2024-04-19 00:19:11 +02:00
|
|
|
if module, ok := moduleSpec.(string); ok {
|
|
|
|
if ImportInContext(ctx, module) {
|
|
|
|
count++
|
|
|
|
} else {
|
|
|
|
err = self.Errorf("unknown module %q", module)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-03 05:30:58 +02:00
|
|
|
err = self.Errorf("expected string at item nr %d, got %T", it.Index()+1, moduleSpec)
|
2024-04-19 00:19:11 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-05-03 06:26:17 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
2024-04-19 00:19:11 +02:00
|
|
|
}
|
|
|
|
if err == nil {
|
2024-05-04 08:07:49 +02:00
|
|
|
v = int64(count)
|
2024-04-19 00:19:11 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// init
|
|
|
|
func init() {
|
|
|
|
registerTermConstructor(SymKwBuiltin, newBuiltinTerm)
|
|
|
|
}
|