expr/operator-include.go

61 lines
1.2 KiB
Go
Raw Normal View History

2024-04-27 09:48:45 +02:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-include.go
package expr
//-------- include term
func newIncludeTerm(tk *Token) (inst *term) {
return &term{
tk: *tk,
children: make([]*term, 0, 1),
position: posPrefix,
priority: priSign,
evalFunc: evalInclude,
}
}
2024-07-09 07:50:06 +02:00
func evalInclude(ctx ExprContext, opTerm *term) (v any, err error) {
2024-04-27 09:48:45 +02:00
var childValue any
2024-07-09 07:50:06 +02:00
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
2024-04-27 09:48:45 +02:00
return
}
count := 0
if IsList(childValue) {
list, _ := childValue.(*ListType)
for i, filePathSpec := range *list {
2024-04-27 09:48:45 +02:00
if filePath, ok := filePathSpec.(string); ok {
if v, err = EvalFile(ctx, filePath); err == nil {
count++
} else {
2024-07-09 07:50:06 +02:00
err = opTerm.Errorf("can't load file %q", filePath)
2024-04-27 09:48:45 +02:00
break
}
} else {
2024-07-09 07:50:06 +02:00
err = opTerm.Errorf("expected string at item nr %d, got %T", i+1, filePathSpec)
2024-04-27 09:48:45 +02:00
break
}
}
} else if IsString(childValue) {
2024-04-27 09:48:45 +02:00
filePath, _ := childValue.(string)
if v, err = EvalFile(ctx, filePath); err == nil {
count++
}
2024-04-27 09:48:45 +02:00
} else {
2024-07-09 07:50:06 +02:00
err = opTerm.errIncompatibleType(childValue)
2024-04-27 09:48:45 +02:00
}
if err != nil {
//v = count
v = nil
2024-04-27 09:48:45 +02:00
}
return
}
// init
func init() {
registerTermConstructor(SymKwInclude, newIncludeTerm)
}