new prefix operator 'include'
This commit is contained in:
parent
824b9382be
commit
60effe8f1b
57
operator-include.go
Normal file
57
operator-include.go
Normal file
@ -0,0 +1,57 @@
|
||||
// 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, self *term) (v any, err error) {
|
||||
var childValue any
|
||||
|
||||
if childValue, err = self.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
count := 0
|
||||
if isList(childValue) {
|
||||
list, _ := childValue.([]any)
|
||||
for i, filePathSpec := range list {
|
||||
if filePath, ok := filePathSpec.(string); ok {
|
||||
if v, err = EvalFile(ctx, filePath); err == nil {
|
||||
count++
|
||||
} else {
|
||||
err = self.Errorf("can't load file %q", filePath)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
err = self.Errorf("expected string at item nr %d, got %T", i+1, filePathSpec)
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if isString(childValue) {
|
||||
filePath, _ := childValue.(string)
|
||||
v, err = EvalFile(ctx, filePath)
|
||||
} else {
|
||||
err = self.errIncompatibleType(childValue)
|
||||
}
|
||||
if err == nil {
|
||||
v = count
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymKwInclude, newIncludeTerm)
|
||||
}
|
Loading…
Reference in New Issue
Block a user