func-*.go modules renamed as builtin-*.go.
Also changed and exported some identiefier relatet to the builtin feature
This commit is contained in:
parent
34dc828ead
commit
9df9ad5dd1
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-builtins.go
|
||||
// builtin-base.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
@ -182,5 +182,5 @@ func ImportBuiltinsFuncs(ctx ExprContext) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterImport("base", ImportBuiltinsFuncs, "Base expression tools like isNil(), int(), etc.")
|
||||
RegisterBuiltinModule("base", ImportBuiltinsFuncs, "Base expression tools like isNil(), int(), etc.")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-fmt.go
|
||||
// builtin-fmt.go
|
||||
package expr
|
||||
|
||||
import "fmt"
|
||||
@ -24,13 +24,13 @@ func printLnFunc(ctx ExprContext, name string, args []any) (result any, err erro
|
||||
|
||||
func ImportFmtFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("print", NewGolangFunctor(printFunc), TypeInt, []ExprFuncParam{
|
||||
NewFuncParamFlag(paramItem, PfRepeat),
|
||||
NewFuncParamFlag(ParamItem, PfRepeat),
|
||||
})
|
||||
ctx.RegisterFunc("println", NewGolangFunctor(printLnFunc), TypeInt, []ExprFuncParam{
|
||||
NewFuncParamFlag(paramItem, PfRepeat),
|
||||
NewFuncParamFlag(ParamItem, PfRepeat),
|
||||
})
|
||||
}
|
||||
|
||||
func init() {
|
||||
registerImport("fmt", ImportFmtFuncs, "String and console formatting functions")
|
||||
RegisterBuiltinModule("fmt", ImportFmtFuncs, "String and console formatting functions")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-import.go
|
||||
// builtin-import.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
@ -71,5 +71,5 @@ func ImportImportFuncs(ctx ExprContext) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterImport("import", ImportImportFuncs, "Functions import() and include()")
|
||||
RegisterBuiltinModule("import", ImportImportFuncs, "Functions import() and include()")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// funcs-math.go
|
||||
// builtin-math-arith.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
@ -177,5 +177,5 @@ func ImportMathFuncs(ctx ExprContext) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterImport("math.arith", ImportMathFuncs, "Functions add() and mul()")
|
||||
RegisterBuiltinModule("math.arith", ImportMathFuncs, "Functions add() and mul()")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-os.go
|
||||
// builtin-os-file.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
@ -210,5 +210,5 @@ func ImportOsFuncs(ctx ExprContext) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterImport("os.file", ImportOsFuncs, "Operating system file functions")
|
||||
RegisterBuiltinModule("os.file", ImportOsFuncs, "Operating system file functions")
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-string.go
|
||||
// builtin-string.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
@ -219,5 +219,5 @@ func ImportStringFuncs(ctx ExprContext) {
|
||||
// Register the import function in the import-register.
|
||||
// That will allow to import all function of this module by the "builtin" operator."
|
||||
func init() {
|
||||
RegisterImport("string", ImportStringFuncs, "string utilities")
|
||||
RegisterBuiltinModule("string", ImportStringFuncs, "string utilities")
|
||||
}
|
48
builtins-register.go
Normal file
48
builtins-register.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// builtins-register.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type builtinModule struct {
|
||||
importFunc func(ExprContext)
|
||||
description string
|
||||
imported bool
|
||||
}
|
||||
|
||||
func newBuiltinModule(importFunc func(ExprContext), description string) *builtinModule {
|
||||
return &builtinModule{importFunc, description, false}
|
||||
}
|
||||
|
||||
var builtinModuleRegister map[string]*builtinModule
|
||||
|
||||
func RegisterBuiltinModule(name string, importFunc func(ExprContext), description string) {
|
||||
if builtinModuleRegister == nil {
|
||||
builtinModuleRegister = make(map[string]*builtinModule)
|
||||
}
|
||||
if _, exists := builtinModuleRegister[name]; exists {
|
||||
panic(fmt.Errorf("module %q already registered", name))
|
||||
}
|
||||
builtinModuleRegister[name] = newBuiltinModule(importFunc, description)
|
||||
}
|
||||
|
||||
func IterateBuiltinModules(op func(name, description string, imported bool) bool) {
|
||||
if op != nil {
|
||||
for name, mod := range builtinModuleRegister {
|
||||
if !op(name, mod.description, mod.imported) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
func init() {
|
||||
if builtinModuleRegister == nil {
|
||||
builtinModuleRegister = make(map[string]*builtinModule)
|
||||
}
|
||||
}
|
@ -12,8 +12,8 @@ import (
|
||||
var globalCtx *SimpleStore
|
||||
|
||||
func ImportInContext(name string) (exists bool) {
|
||||
var mod *module
|
||||
if mod, exists = moduleRegister[name]; exists {
|
||||
var mod *builtinModule
|
||||
if mod, exists = builtinModuleRegister[name]; exists {
|
||||
mod.importFunc(globalCtx)
|
||||
mod.imported = true
|
||||
}
|
||||
@ -22,7 +22,7 @@ func ImportInContext(name string) (exists bool) {
|
||||
|
||||
func ImportInContextByGlobPattern(pattern string) (count int, err error) {
|
||||
var matched bool
|
||||
for name, mod := range moduleRegister {
|
||||
for name, mod := range builtinModuleRegister {
|
||||
if matched, err = filepath.Match(pattern, name); err == nil {
|
||||
if matched {
|
||||
count++
|
||||
|
@ -1,48 +0,0 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// module-register.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type module struct {
|
||||
importFunc func(ExprContext)
|
||||
description string
|
||||
imported bool
|
||||
}
|
||||
|
||||
func newModule(importFunc func(ExprContext), description string) *module {
|
||||
return &module{importFunc, description, false}
|
||||
}
|
||||
|
||||
var moduleRegister map[string]*module
|
||||
|
||||
func RegisterImport(name string, importFunc func(ExprContext), description string) {
|
||||
if moduleRegister == nil {
|
||||
moduleRegister = make(map[string]*module)
|
||||
}
|
||||
if _, exists := moduleRegister[name]; exists {
|
||||
panic(fmt.Errorf("module %q already registered", name))
|
||||
}
|
||||
moduleRegister[name] = newModule(importFunc, description)
|
||||
}
|
||||
|
||||
func IterateModules(op func(name, description string, imported bool) bool) {
|
||||
if op != nil {
|
||||
for name, mod := range moduleRegister {
|
||||
if !op(name, mod.description, mod.imported) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
func init() {
|
||||
if moduleRegister == nil {
|
||||
moduleRegister = make(map[string]*module)
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ func TestIterateModules(t *testing.T) {
|
||||
section := "Module-Register"
|
||||
|
||||
mods := make([]string, 0, 100)
|
||||
IterateModules(func(name, description string, imported bool) bool {
|
||||
IterateBuiltinModules(func(name, description string, imported bool) bool {
|
||||
mods = append(mods, name)
|
||||
return true
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user