2024-03-26 08:45:18 +01:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
2024-03-26 07:00:53 +01:00
|
|
|
// context.go
|
|
|
|
package expr
|
|
|
|
|
2024-04-02 04:36:03 +02:00
|
|
|
// ---- Function template
|
2024-04-08 23:17:56 +02:00
|
|
|
type FuncTemplate func(ctx ExprContext, name string, args []any) (result any, err error)
|
2024-03-30 06:54:43 +01:00
|
|
|
|
2024-04-02 04:36:03 +02:00
|
|
|
// ---- Functor interface
|
|
|
|
type Functor interface {
|
2024-04-08 23:17:56 +02:00
|
|
|
Invoke(ctx ExprContext, name string, args []any) (result any, err error)
|
2024-04-02 04:36:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type simpleFunctor struct {
|
|
|
|
f FuncTemplate
|
|
|
|
}
|
|
|
|
|
2024-05-22 20:52:44 +02:00
|
|
|
func newSimpleFunctor(f FuncTemplate) *simpleFunctor {
|
|
|
|
return &simpleFunctor{f: f}
|
|
|
|
}
|
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func (functor *simpleFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
|
2024-04-02 04:36:03 +02:00
|
|
|
return functor.f(ctx, name, args)
|
|
|
|
}
|
|
|
|
|
2024-05-22 20:52:44 +02:00
|
|
|
func (functor *simpleFunctor) ToString(opt FmtOpt) string {
|
|
|
|
return "func() {<body>}"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- Function Param Info
|
|
|
|
type ExprFuncParam interface {
|
|
|
|
Name() string
|
|
|
|
Type() string
|
|
|
|
IsOptional() bool
|
|
|
|
IsRepeat() bool
|
|
|
|
DefaultValue() any
|
|
|
|
}
|
|
|
|
|
2024-04-02 04:36:03 +02:00
|
|
|
// ---- Function Info
|
2024-04-08 23:17:56 +02:00
|
|
|
type ExprFunc interface {
|
2024-03-26 07:00:53 +01:00
|
|
|
Name() string
|
|
|
|
MinArgs() int
|
|
|
|
MaxArgs() int
|
2024-04-06 01:00:29 +02:00
|
|
|
Functor() Functor
|
2024-05-22 20:52:44 +02:00
|
|
|
Params() []ExprFuncParam
|
|
|
|
ReturnType() string
|
2024-03-26 07:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-04-02 04:36:03 +02:00
|
|
|
// ----Expression Context
|
2024-04-08 23:17:56 +02:00
|
|
|
type ExprContext interface {
|
|
|
|
Clone() ExprContext
|
2024-04-03 06:29:57 +02:00
|
|
|
GetVar(varName string) (value any, exists bool)
|
|
|
|
SetVar(varName string, value any)
|
2024-05-19 01:27:44 +02:00
|
|
|
UnsafeSetVar(varName string, value any)
|
2024-04-04 12:54:26 +02:00
|
|
|
EnumVars(func(name string) (accept bool)) (varNames []string)
|
2024-04-06 01:00:29 +02:00
|
|
|
EnumFuncs(func(name string) (accept bool)) (funcNames []string)
|
2024-04-26 04:28:50 +02:00
|
|
|
GetFuncInfo(name string) (item ExprFunc, exists bool)
|
2024-03-26 07:00:53 +01:00
|
|
|
Call(name string, args []any) (result any, err error)
|
2024-05-22 20:52:44 +02:00
|
|
|
// RegisterFunc(name string, f Functor, minArgs, maxArgs int)
|
|
|
|
RegisterFuncInfo(info ExprFunc)
|
|
|
|
RegisterFunc2(name string, f Functor, returnType string, param []ExprFuncParam) error
|
2024-03-26 07:00:53 +01:00
|
|
|
}
|