61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
// 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,
|
|
}
|
|
}
|
|
|
|
func evalInclude(ctx ExprContext, opTerm *term) (v any, err error) {
|
|
var childValue any
|
|
|
|
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
|
return
|
|
}
|
|
|
|
count := 0
|
|
if IsList(childValue) {
|
|
list, _ := childValue.(*ListType)
|
|
for i, filePathSpec := range *list {
|
|
if filePath, ok := filePathSpec.(string); ok {
|
|
if v, err = EvalFile(ctx, filePath); err == nil {
|
|
count++
|
|
} else {
|
|
err = opTerm.Errorf("can't load file %q", filePath)
|
|
break
|
|
}
|
|
} else {
|
|
err = opTerm.Errorf("expected string at item nr %d, got %T", i+1, filePathSpec)
|
|
break
|
|
}
|
|
}
|
|
} else if IsString(childValue) {
|
|
filePath, _ := childValue.(string)
|
|
if v, err = EvalFile(ctx, filePath); err == nil {
|
|
count++
|
|
}
|
|
} else {
|
|
err = opTerm.errIncompatibleType(childValue)
|
|
}
|
|
if err != nil {
|
|
//v = count
|
|
v = nil
|
|
}
|
|
return
|
|
}
|
|
|
|
// init
|
|
func init() {
|
|
registerTermConstructor(SymKwInclude, newIncludeTerm)
|
|
}
|