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).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
// func-builtins.go
|
// builtin-base.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -182,5 +182,5 @@ func ImportBuiltinsFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
// func-fmt.go
|
// builtin-fmt.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
@ -24,13 +24,13 @@ func printLnFunc(ctx ExprContext, name string, args []any) (result any, err erro
|
|||||||
|
|
||||||
func ImportFmtFuncs(ctx ExprContext) {
|
func ImportFmtFuncs(ctx ExprContext) {
|
||||||
ctx.RegisterFunc("print", NewGolangFunctor(printFunc), TypeInt, []ExprFuncParam{
|
ctx.RegisterFunc("print", NewGolangFunctor(printFunc), TypeInt, []ExprFuncParam{
|
||||||
NewFuncParamFlag(paramItem, PfRepeat),
|
NewFuncParamFlag(ParamItem, PfRepeat),
|
||||||
})
|
})
|
||||||
ctx.RegisterFunc("println", NewGolangFunctor(printLnFunc), TypeInt, []ExprFuncParam{
|
ctx.RegisterFunc("println", NewGolangFunctor(printLnFunc), TypeInt, []ExprFuncParam{
|
||||||
NewFuncParamFlag(paramItem, PfRepeat),
|
NewFuncParamFlag(ParamItem, PfRepeat),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
// func-import.go
|
// builtin-import.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -71,5 +71,5 @@ func ImportImportFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
// funcs-math.go
|
// builtin-math-arith.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -177,5 +177,5 @@ func ImportMathFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
// func-os.go
|
// builtin-os-file.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -210,5 +210,5 @@ func ImportOsFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
// func-string.go
|
// builtin-string.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -219,5 +219,5 @@ func ImportStringFuncs(ctx ExprContext) {
|
|||||||
// Register the import function in the import-register.
|
// Register the import function in the import-register.
|
||||||
// That will allow to import all function of this module by the "builtin" operator."
|
// That will allow to import all function of this module by the "builtin" operator."
|
||||||
func init() {
|
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
|
var globalCtx *SimpleStore
|
||||||
|
|
||||||
func ImportInContext(name string) (exists bool) {
|
func ImportInContext(name string) (exists bool) {
|
||||||
var mod *module
|
var mod *builtinModule
|
||||||
if mod, exists = moduleRegister[name]; exists {
|
if mod, exists = builtinModuleRegister[name]; exists {
|
||||||
mod.importFunc(globalCtx)
|
mod.importFunc(globalCtx)
|
||||||
mod.imported = true
|
mod.imported = true
|
||||||
}
|
}
|
||||||
@ -22,7 +22,7 @@ func ImportInContext(name string) (exists bool) {
|
|||||||
|
|
||||||
func ImportInContextByGlobPattern(pattern string) (count int, err error) {
|
func ImportInContextByGlobPattern(pattern string) (count int, err error) {
|
||||||
var matched bool
|
var matched bool
|
||||||
for name, mod := range moduleRegister {
|
for name, mod := range builtinModuleRegister {
|
||||||
if matched, err = filepath.Match(pattern, name); err == nil {
|
if matched, err = filepath.Match(pattern, name); err == nil {
|
||||||
if matched {
|
if matched {
|
||||||
count++
|
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"
|
section := "Module-Register"
|
||||||
|
|
||||||
mods := make([]string, 0, 100)
|
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)
|
mods = append(mods, name)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user