moved a subset of source file to the kern package
This commit is contained in:
@@ -6,6 +6,8 @@ package expr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
)
|
||||
|
||||
//-------- ast
|
||||
@@ -19,33 +21,33 @@ func NewAst() *ast {
|
||||
return &ast{}
|
||||
}
|
||||
|
||||
func (expr *ast) TypeName() string {
|
||||
func (ast *ast) TypeName() string {
|
||||
return "Expression"
|
||||
}
|
||||
|
||||
func (expr *ast) ToForest() {
|
||||
if expr.root != nil {
|
||||
if expr.forest == nil {
|
||||
expr.forest = make([]*term, 0)
|
||||
func (ast *ast) ToForest() {
|
||||
if ast.root != nil {
|
||||
if ast.forest == nil {
|
||||
ast.forest = make([]*term, 0)
|
||||
}
|
||||
expr.forest = append(expr.forest, expr.root)
|
||||
expr.root = nil
|
||||
ast.forest = append(ast.forest, ast.root)
|
||||
ast.root = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (expr *ast) String() string {
|
||||
func (ast *ast) String() string {
|
||||
var sb strings.Builder
|
||||
if expr.root == nil {
|
||||
if ast.root == nil {
|
||||
sb.WriteString("(nil)")
|
||||
} else {
|
||||
expr.root.toString(&sb)
|
||||
ast.root.toString(&sb)
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (expr *ast) addTokens(tokens ...*Token) (err error) {
|
||||
func (ast *ast) addTokens(tokens ...*Token) (err error) {
|
||||
for _, tk := range tokens {
|
||||
if _, err = expr.addToken(tk); err != nil {
|
||||
if _, err = ast.addToken(tk); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -57,31 +59,31 @@ func (expr *ast) addTokens(tokens ...*Token) (err error) {
|
||||
// return
|
||||
// }
|
||||
|
||||
func (expr *ast) addToken(tk *Token) (t *term, err error) {
|
||||
func (ast *ast) addToken(tk *Token) (t *term, err error) {
|
||||
if t = newTerm(tk); t != nil {
|
||||
err = expr.addTerm(t)
|
||||
err = ast.addTerm(t)
|
||||
} else {
|
||||
err = tk.Errorf("unexpected token %q", tk.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (expr *ast) addTerm(node *term) (err error) {
|
||||
if expr.root == nil {
|
||||
expr.root = node
|
||||
func (ast *ast) addTerm(node *term) (err error) {
|
||||
if ast.root == nil {
|
||||
ast.root = node
|
||||
} else {
|
||||
expr.root, err = expr.insert(expr.root, node)
|
||||
ast.root, err = ast.insert(ast.root, node)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (expr *ast) insert(tree, node *term) (root *term, err error) {
|
||||
func (ast *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 = expr.insert(last, node); err == nil {
|
||||
if subRoot, err = ast.insert(last, node); err == nil {
|
||||
subRoot.setParent(tree)
|
||||
}
|
||||
} else {
|
||||
@@ -96,22 +98,22 @@ func (expr *ast) insert(tree, node *term) (root *term, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
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 (ast *ast) Finish() {
|
||||
if ast.root == nil && ast.forest != nil && len(ast.forest) >= 1 {
|
||||
ast.root = ast.forest[len(ast.forest)-1]
|
||||
ast.forest = ast.forest[0 : len(ast.forest)-1]
|
||||
}
|
||||
}
|
||||
|
||||
func (expr *ast) Eval(ctx ExprContext) (result any, err error) {
|
||||
expr.Finish()
|
||||
func (ast *ast) Eval(ctx kern.ExprContext) (result any, err error) {
|
||||
ast.Finish()
|
||||
|
||||
if expr.root != nil {
|
||||
if ast.root != nil {
|
||||
// initDefaultVars(ctx)
|
||||
if expr.forest != nil {
|
||||
for _, root := range expr.forest {
|
||||
if result, err = root.compute(ctx); err == nil {
|
||||
ctx.UnsafeSetVar(ControlLastResult, result)
|
||||
if ast.forest != nil {
|
||||
for _, root := range ast.forest {
|
||||
if result, err = root.Compute(ctx); err == nil {
|
||||
ctx.UnsafeSetVar(kern.ControlLastResult, result)
|
||||
} else {
|
||||
//err = fmt.Errorf("error in expression nr %d: %v", i+1, err)
|
||||
break
|
||||
@@ -119,8 +121,8 @@ func (expr *ast) Eval(ctx ExprContext) (result any, err error) {
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
if result, err = expr.root.compute(ctx); err == nil {
|
||||
ctx.UnsafeSetVar(ControlLastResult, result)
|
||||
if result, err = ast.root.Compute(ctx); err == nil {
|
||||
ctx.UnsafeSetVar(kern.ControlLastResult, result)
|
||||
}
|
||||
}
|
||||
// } else {
|
||||
|
||||
Reference in New Issue
Block a user