2024-05-11 10:45:38 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// global-context.go
|
|
|
|
package expr
|
|
|
|
|
|
|
|
import "path/filepath"
|
|
|
|
|
2024-05-23 07:46:31 +02:00
|
|
|
var globalCtx *SimpleStore
|
2024-05-11 10:45:38 +02:00
|
|
|
|
|
|
|
func ImportInContext(name string) (exists bool) {
|
|
|
|
var mod *module
|
|
|
|
if mod, exists = moduleRegister[name]; exists {
|
|
|
|
mod.importFunc(globalCtx)
|
|
|
|
mod.imported = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ImportInContextByGlobPattern(pattern string) (count int, err error) {
|
|
|
|
var matched bool
|
|
|
|
for name, mod := range moduleRegister {
|
|
|
|
if matched, err = filepath.Match(pattern, name); err == nil {
|
|
|
|
if matched {
|
|
|
|
count++
|
|
|
|
mod.importFunc(globalCtx)
|
|
|
|
mod.imported = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetVar(ctx ExprContext, name string) (value any, exists bool) {
|
|
|
|
if value, exists = ctx.GetVar(name); !exists {
|
|
|
|
value, exists = globalCtx.GetVar(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-04 11:01:04 +02:00
|
|
|
func GetLocalFuncInfo(ctx ExprContext, name string) (item ExprFunc, exists bool) {
|
|
|
|
var v any
|
|
|
|
if len(name) > 0 {
|
|
|
|
if v, exists = ctx.GetVar(name); exists && isFunctor(v) {
|
|
|
|
f, _ := v.(Functor)
|
|
|
|
item = f.GetFunc()
|
|
|
|
} else {
|
|
|
|
item, exists = ctx.GetFuncInfo(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-11 10:45:38 +02:00
|
|
|
func GetFuncInfo(ctx ExprContext, name string) (item ExprFunc, exists bool, ownerCtx ExprContext) {
|
2024-06-04 11:01:04 +02:00
|
|
|
if len(name) > 0 {
|
|
|
|
if item, exists = GetLocalFuncInfo(ctx, name); exists {
|
|
|
|
ownerCtx = ctx
|
|
|
|
} else if item, exists = globalCtx.GetFuncInfo(name); exists {
|
|
|
|
ownerCtx = globalCtx
|
|
|
|
}
|
2024-05-11 10:45:38 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2024-05-23 07:46:31 +02:00
|
|
|
globalCtx = NewSimpleStore()
|
2024-05-22 20:52:44 +02:00
|
|
|
ImportBuiltinsFuncs(globalCtx)
|
2024-05-11 10:45:38 +02:00
|
|
|
}
|