context.go splitted in two files: expr-context.go and expr-function-go.
Expr interface moved from ast.go to the new file expr.go
This commit is contained in:
parent
340b99bad7
commit
76ce0945f7
67
ast.go
67
ast.go
@ -8,11 +8,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Expr interface {
|
||||
Eval(ctx ExprContext) (result any, err error)
|
||||
String() string
|
||||
}
|
||||
|
||||
//-------- ast
|
||||
|
||||
type ast struct {
|
||||
@ -24,65 +19,65 @@ func NewAst() *ast {
|
||||
return &ast{}
|
||||
}
|
||||
|
||||
func (self *ast) ToForest() {
|
||||
if self.root != nil {
|
||||
if self.forest == nil {
|
||||
self.forest = make([]*term, 0)
|
||||
func (expr *ast) ToForest() {
|
||||
if expr.root != nil {
|
||||
if expr.forest == nil {
|
||||
expr.forest = make([]*term, 0)
|
||||
}
|
||||
self.forest = append(self.forest, self.root)
|
||||
self.root = nil
|
||||
expr.forest = append(expr.forest, expr.root)
|
||||
expr.root = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (self *ast) String() string {
|
||||
func (expr *ast) String() string {
|
||||
var sb strings.Builder
|
||||
if self.root == nil {
|
||||
if expr.root == nil {
|
||||
sb.WriteString("(nil)")
|
||||
} else {
|
||||
self.root.toString(&sb)
|
||||
expr.root.toString(&sb)
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (self *ast) addTokens(tokens ...*Token) (err error) {
|
||||
func (expr *ast) addTokens(tokens ...*Token) (err error) {
|
||||
for _, tk := range tokens {
|
||||
if err = self.addToken(tk); err != nil {
|
||||
if err = expr.addToken(tk); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (self *ast) addToken(tk *Token) (err error) {
|
||||
_, err = self.addToken2(tk)
|
||||
func (expr *ast) addToken(tk *Token) (err error) {
|
||||
_, err = expr.addToken2(tk)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *ast) addToken2(tk *Token) (t *term, err error) {
|
||||
func (expr *ast) addToken2(tk *Token) (t *term, err error) {
|
||||
if t = newTerm(tk); t != nil {
|
||||
err = self.addTerm(t)
|
||||
err = expr.addTerm(t)
|
||||
} else {
|
||||
err = tk.Errorf("unexpected token %q", tk.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (self *ast) addTerm(node *term) (err error) {
|
||||
if self.root == nil {
|
||||
self.root = node
|
||||
func (expr *ast) addTerm(node *term) (err error) {
|
||||
if expr.root == nil {
|
||||
expr.root = node
|
||||
} else {
|
||||
self.root, err = self.insert(self.root, node)
|
||||
expr.root, err = expr.insert(expr.root, node)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (self *ast) insert(tree, node *term) (root *term, err error) {
|
||||
func (expr *ast) insert(tree, node *term) (root *term, err error) {
|
||||
if tree.getPriority() < node.getPriority() {
|
||||
root = tree
|
||||
if tree.isComplete() {
|
||||
var subRoot *term
|
||||
last := tree.removeLastChild()
|
||||
if subRoot, err = self.insert(last, node); err == nil {
|
||||
if subRoot, err = expr.insert(last, node); err == nil {
|
||||
subRoot.setParent(tree)
|
||||
}
|
||||
} else {
|
||||
@ -97,20 +92,20 @@ func (self *ast) insert(tree, node *term) (root *term, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (self *ast) Finish() {
|
||||
if self.root == nil && self.forest != nil && len(self.forest) >= 1 {
|
||||
self.root = self.forest[len(self.forest)-1]
|
||||
self.forest = self.forest[0 : len(self.forest)-1]
|
||||
func (expr *ast) Finish() {
|
||||
if expr.root == nil && expr.forest != nil && len(expr.forest) >= 1 {
|
||||
expr.root = expr.forest[len(expr.forest)-1]
|
||||
expr.forest = expr.forest[0 : len(expr.forest)-1]
|
||||
}
|
||||
}
|
||||
|
||||
func (self *ast) Eval(ctx ExprContext) (result any, err error) {
|
||||
self.Finish()
|
||||
func (expr *ast) Eval(ctx ExprContext) (result any, err error) {
|
||||
expr.Finish()
|
||||
|
||||
if self.root != nil {
|
||||
if expr.root != nil {
|
||||
// initDefaultVars(ctx)
|
||||
if self.forest != nil {
|
||||
for _, root := range self.forest {
|
||||
if expr.forest != nil {
|
||||
for _, root := range expr.forest {
|
||||
if result, err = root.compute(ctx); err == nil {
|
||||
ctx.UnsafeSetVar(ControlLastResult, result)
|
||||
} else {
|
||||
@ -120,7 +115,7 @@ func (self *ast) Eval(ctx ExprContext) (result any, err error) {
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
if result, err = self.root.compute(ctx); err == nil {
|
||||
if result, err = expr.root.compute(ctx); err == nil {
|
||||
ctx.UnsafeSetVar(ControlLastResult, result)
|
||||
}
|
||||
}
|
||||
|
23
expr-context.go
Normal file
23
expr-context.go
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// context.go
|
||||
package expr
|
||||
|
||||
// ----Expression Context
|
||||
type ExprContext interface {
|
||||
Clone() ExprContext
|
||||
// Merge(ctx ExprContext)
|
||||
SetParent(ctx ExprContext)
|
||||
GetParent() (ctx ExprContext)
|
||||
GetVar(varName string) (value any, exists bool)
|
||||
GetLast() any
|
||||
SetVar(varName string, value any)
|
||||
UnsafeSetVar(varName string, value any)
|
||||
EnumVars(func(name string) (accept bool)) (varNames []string)
|
||||
EnumFuncs(func(name string) (accept bool)) (funcNames []string)
|
||||
GetFuncInfo(name string) (item ExprFunc, exists bool)
|
||||
Call(name string, args []any) (result any, err error)
|
||||
RegisterFuncInfo(info ExprFunc)
|
||||
RegisterFunc(name string, f Functor, returnType string, param []ExprFuncParam) error
|
||||
}
|
37
expr-function.go
Normal file
37
expr-function.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// context.go
|
||||
package expr
|
||||
|
||||
// ---- Functor interface
|
||||
type Functor interface {
|
||||
Invoke(ctx ExprContext, name string, args []any) (result any, err error)
|
||||
SetFunc(info ExprFunc)
|
||||
GetFunc() ExprFunc
|
||||
GetParams() []ExprFuncParam
|
||||
GetDefinitionContext() ExprContext
|
||||
}
|
||||
|
||||
// ---- Function Param Info
|
||||
type ExprFuncParam interface {
|
||||
Name() string
|
||||
Type() string
|
||||
IsDefault() bool
|
||||
IsOptional() bool
|
||||
IsRepeat() bool
|
||||
DefaultValue() any
|
||||
}
|
||||
|
||||
// ---- Function Info
|
||||
type ExprFunc interface {
|
||||
Formatter
|
||||
Name() string
|
||||
MinArgs() int
|
||||
MaxArgs() int
|
||||
Functor() Functor
|
||||
Params() []ExprFuncParam
|
||||
ReturnType() string
|
||||
PrepareCall(parentCtx ExprContext, name string, varParams *[]any) (ctx ExprContext, err error)
|
||||
AllocContext(parentCtx ExprContext) (ctx ExprContext)
|
||||
}
|
Loading…
Reference in New Issue
Block a user