moved a subset of source file to the kern package
This commit is contained in:
+33
-31
@@ -7,6 +7,8 @@ package expr
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
)
|
||||
|
||||
//-------- plus term
|
||||
@@ -22,46 +24,46 @@ func newPlusTerm(tk *Token) (inst *term) {
|
||||
}
|
||||
|
||||
func sumValues(plusTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
if (IsString(leftValue) && isNumberString(rightValue)) || (IsString(rightValue) && isNumberString(leftValue)) {
|
||||
if (kern.IsString(leftValue) && kern.IsNumberString(rightValue)) || (kern.IsString(rightValue) && kern.IsNumberString(leftValue)) {
|
||||
v = fmt.Sprintf("%v%v", leftValue, rightValue)
|
||||
} else if IsNumber(leftValue) && IsNumber(rightValue) {
|
||||
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
||||
} else if kern.IsNumber(leftValue) && kern.IsNumber(rightValue) {
|
||||
if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
|
||||
v = kern.NumAsFloat(leftValue) + kern.NumAsFloat(rightValue)
|
||||
} else {
|
||||
leftInt, _ := leftValue.(int64)
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt + rightInt
|
||||
}
|
||||
} else if IsList(leftValue) && IsList(rightValue) {
|
||||
var leftList, rightList *ListType
|
||||
leftList, _ = leftValue.(*ListType)
|
||||
rightList, _ = rightValue.(*ListType)
|
||||
} else if kern.IsList(leftValue) && kern.IsList(rightValue) {
|
||||
var leftList, rightList *kern.ListType
|
||||
leftList, _ = leftValue.(*kern.ListType)
|
||||
rightList, _ = rightValue.(*kern.ListType)
|
||||
|
||||
sumList := make(ListType, 0, len(*leftList)+len(*rightList))
|
||||
sumList := make(kern.ListType, 0, len(*leftList)+len(*rightList))
|
||||
sumList = append(sumList, *leftList...)
|
||||
sumList = append(sumList, *rightList...)
|
||||
v = &sumList
|
||||
} else if (isFraction(leftValue) && IsNumber(rightValue)) || (isFraction(rightValue) && IsNumber(leftValue)) {
|
||||
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
||||
} else if (kern.IsFraction(leftValue) && kern.IsNumber(rightValue)) || (kern.IsFraction(rightValue) && kern.IsNumber(leftValue)) {
|
||||
if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
|
||||
v = kern.NumAsFloat(leftValue) + kern.NumAsFloat(rightValue)
|
||||
} else {
|
||||
v, err = sumAnyFract(leftValue, rightValue)
|
||||
v, err = kern.SumAnyFract(leftValue, rightValue)
|
||||
}
|
||||
} else if IsDict(leftValue) && IsDict(rightValue) {
|
||||
leftDict, _ := leftValue.(*DictType)
|
||||
rightDict, _ := rightValue.(*DictType)
|
||||
c := leftDict.clone()
|
||||
c.merge(rightDict)
|
||||
} else if kern.IsDict(leftValue) && kern.IsDict(rightValue) {
|
||||
leftDict, _ := leftValue.(*kern.DictType)
|
||||
rightDict, _ := rightValue.(*kern.DictType)
|
||||
c := leftDict.Clone()
|
||||
c.Merge(rightDict)
|
||||
v = c
|
||||
} else if isFraction(leftValue) && isFraction(rightValue) {
|
||||
v, err = sumAnyFract(leftValue, rightValue)
|
||||
} else if kern.IsFraction(leftValue) && kern.IsFraction(rightValue) {
|
||||
v, err = kern.SumAnyFract(leftValue, rightValue)
|
||||
} else {
|
||||
err = plusTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return v, err
|
||||
}
|
||||
|
||||
func evalPlus(ctx ExprContext, plusTerm *term) (v any, err error) {
|
||||
func evalPlus(ctx kern.ExprContext, plusTerm *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = plusTerm.evalInfix(ctx); err != nil {
|
||||
@@ -84,20 +86,20 @@ func newMinusTerm(tk *Token) (inst *term) {
|
||||
}
|
||||
|
||||
func diffValues(minusTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
||||
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||
v = numAsFloat(leftValue) - numAsFloat(rightValue)
|
||||
} else if isFraction(leftValue) || isFraction(rightValue) {
|
||||
v, err = subAnyFract(leftValue, rightValue)
|
||||
if kern.IsNumOrFract(leftValue) && kern.IsNumOrFract(rightValue) {
|
||||
if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
|
||||
v = kern.NumAsFloat(leftValue) - kern.NumAsFloat(rightValue)
|
||||
} else if kern.IsFraction(leftValue) || kern.IsFraction(rightValue) {
|
||||
v, err = kern.SubAnyFract(leftValue, rightValue)
|
||||
} else {
|
||||
leftInt, _ := leftValue.(int64)
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt - rightInt
|
||||
}
|
||||
} else if IsList(leftValue) && IsList(rightValue) {
|
||||
leftList, _ := leftValue.(*ListType)
|
||||
rightList, _ := rightValue.(*ListType)
|
||||
diffList := make(ListType, 0, len(*leftList)-len(*rightList))
|
||||
} else if kern.IsList(leftValue) && kern.IsList(rightValue) {
|
||||
leftList, _ := leftValue.(*kern.ListType)
|
||||
rightList, _ := rightValue.(*kern.ListType)
|
||||
diffList := make(kern.ListType, 0, len(*leftList)-len(*rightList))
|
||||
for _, item := range *leftList {
|
||||
if slices.Index(*rightList, item) < 0 {
|
||||
diffList = append(diffList, item)
|
||||
@@ -110,7 +112,7 @@ func diffValues(minusTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func evalMinus(ctx ExprContext, minusTerm *term) (v any, err error) {
|
||||
func evalMinus(ctx kern.ExprContext, minusTerm *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = minusTerm.evalInfix(ctx); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user