Compare commits
3 Commits
54bc759f70
...
b76481bbf2
Author | SHA1 | Date | |
---|---|---|---|
b76481bbf2 | |||
4f05e5c90a | |||
8ad25afdc4 |
@ -108,3 +108,7 @@ func ImportMathFuncs(ctx ExprContext) {
|
|||||||
ctx.RegisterFunc("add", &simpleFunctor{f: addFunc}, 0, -1)
|
ctx.RegisterFunc("add", &simpleFunctor{f: addFunc}, 0, -1)
|
||||||
ctx.RegisterFunc("mul", &simpleFunctor{f: mulFunc}, 0, -1)
|
ctx.RegisterFunc("mul", &simpleFunctor{f: mulFunc}, 0, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerImport("math.arith", ImportMathFuncs)
|
||||||
|
}
|
||||||
|
47
function-register.go
Normal file
47
function-register.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
|
// All rights reserved.
|
||||||
|
|
||||||
|
// function-register.go
|
||||||
|
package expr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
var functionRegister map[string]func(ExprContext)
|
||||||
|
|
||||||
|
func registerImport(name string, importFunc func(ExprContext)) {
|
||||||
|
if functionRegister == nil {
|
||||||
|
functionRegister = make(map[string]func(ExprContext))
|
||||||
|
}
|
||||||
|
functionRegister[name] = importFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func ImportInContext(ctx ExprContext, name string) (exists bool) {
|
||||||
|
var importFunc func(ExprContext)
|
||||||
|
if importFunc, exists = functionRegister[name]; exists {
|
||||||
|
importFunc(ctx)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ImportInContextByGlobPattern(ctx ExprContext, pattern string) (count int, err error) {
|
||||||
|
var matched bool
|
||||||
|
for name, importFunc := range functionRegister {
|
||||||
|
if matched, err = filepath.Match(pattern, name); err == nil {
|
||||||
|
if matched {
|
||||||
|
count++
|
||||||
|
importFunc(ctx)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if functionRegister == nil {
|
||||||
|
functionRegister = make(map[string]func(ExprContext))
|
||||||
|
}
|
||||||
|
}
|
57
operator-builtin.go
Normal file
57
operator-builtin.go
Normal 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)
|
||||||
|
}
|
12
symbol.go
12
symbol.go
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user