Some data-type check functions (e.g. IsInteger()) exported
This commit is contained in:
parent
b2b0bb04c5
commit
8ee0bb5701
@ -35,7 +35,7 @@ func importGeneral(ctx ExprContext, name string, args []any) (result any, err er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) {
|
func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) {
|
||||||
if !(isString(paramValue) /*|| isList(paramValue)*/) {
|
if !(IsString(paramValue) /*|| isList(paramValue)*/) {
|
||||||
err = fmt.Errorf("%s(): param nr %d has wrong type %T, string expected", funcName, paramPos+1, paramValue)
|
err = fmt.Errorf("%s(): param nr %d has wrong type %T, string expected", funcName, paramPos+1, paramValue)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func checkNumberParamExpected(funcName string, paramValue any, paramPos, level, subPos int) (err error) {
|
func checkNumberParamExpected(funcName string, paramValue any, paramPos, level, subPos int) (err error) {
|
||||||
if !(isNumber(paramValue) || isFraction(paramValue)) /*|| isList(paramValue)*/ {
|
if !(IsNumber(paramValue) || isFraction(paramValue)) /*|| isList(paramValue)*/ {
|
||||||
err = fmt.Errorf("%s(): param nr %d (%d in %d) has wrong type %T, number expected",
|
err = fmt.Errorf("%s(): param nr %d (%d in %d) has wrong type %T, number expected",
|
||||||
funcName, paramPos+1, subPos+1, level, paramValue)
|
funcName, paramPos+1, subPos+1, level, paramValue)
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func doAdd(ctx ExprContext, name string, it Iterator, count, level int) (result
|
|||||||
count++
|
count++
|
||||||
|
|
||||||
if !sumAsFloat {
|
if !sumAsFloat {
|
||||||
if isFloat(v) {
|
if IsFloat(v) {
|
||||||
sumAsFloat = true
|
sumAsFloat = true
|
||||||
if sumAsFract {
|
if sumAsFract {
|
||||||
floatSum = fractSum.toFloat()
|
floatSum = fractSum.toFloat()
|
||||||
@ -120,7 +120,7 @@ func doMul(ctx ExprContext, name string, it Iterator, count, level int) (result
|
|||||||
count++
|
count++
|
||||||
|
|
||||||
if !mulAsFloat {
|
if !mulAsFloat {
|
||||||
if isFloat(v) {
|
if IsFloat(v) {
|
||||||
mulAsFloat = true
|
mulAsFloat = true
|
||||||
if mulAsFract {
|
if mulAsFract {
|
||||||
floatProd = fractProd.toFloat()
|
floatProd = fractProd.toFloat()
|
||||||
|
@ -26,7 +26,7 @@ func evalBuiltin(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
if isString(childValue) {
|
if IsString(childValue) {
|
||||||
module, _ := childValue.(string)
|
module, _ := childValue.(string)
|
||||||
count, err = ImportInContextByGlobPattern(ctx, module)
|
count, err = ImportInContextByGlobPattern(ctx, module)
|
||||||
} else {
|
} else {
|
||||||
|
@ -25,7 +25,7 @@ func evalFact(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isInteger(leftValue) {
|
if IsInteger(leftValue) {
|
||||||
if i, _ := leftValue.(int64); i >= 0 {
|
if i, _ := leftValue.(int64); i >= 0 {
|
||||||
f := int64(1)
|
f := int64(1)
|
||||||
for k := int64(1); k <= i; k++ {
|
for k := int64(1); k <= i; k++ {
|
||||||
|
@ -24,7 +24,7 @@ func evalInclude(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
if isList(childValue) {
|
if IsList(childValue) {
|
||||||
list, _ := childValue.([]any)
|
list, _ := childValue.([]any)
|
||||||
for i, filePathSpec := range list {
|
for i, filePathSpec := range list {
|
||||||
if filePath, ok := filePathSpec.(string); ok {
|
if filePath, ok := filePathSpec.(string); ok {
|
||||||
@ -39,7 +39,7 @@ func evalInclude(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if isString(childValue) {
|
} else if IsString(childValue) {
|
||||||
filePath, _ := childValue.(string)
|
filePath, _ := childValue.(string)
|
||||||
v, err = EvalFile(ctx, filePath)
|
v, err = EvalFile(ctx, filePath)
|
||||||
} else {
|
} else {
|
||||||
|
@ -33,7 +33,7 @@ func evalInsert(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isList(rightValue) {
|
if IsList(rightValue) {
|
||||||
list, _ := rightValue.(*ListType)
|
list, _ := rightValue.(*ListType)
|
||||||
newList := append(ListType{leftValue}, *list...)
|
newList := append(ListType{leftValue}, *list...)
|
||||||
v = &newList
|
v = &newList
|
||||||
@ -50,7 +50,7 @@ func evalAppend(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isList(leftValue) {
|
if IsList(leftValue) {
|
||||||
list, _ := leftValue.(*ListType)
|
list, _ := leftValue.(*ListType)
|
||||||
newList := append(*list, rightValue)
|
newList := append(*list, rightValue)
|
||||||
v = &newList
|
v = &newList
|
||||||
|
@ -23,10 +23,10 @@ func evalLength(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isList(childValue) {
|
if IsList(childValue) {
|
||||||
list, _ := childValue.([]any)
|
list, _ := childValue.([]any)
|
||||||
v = int64(len(list))
|
v = int64(len(list))
|
||||||
} else if isString(childValue) {
|
} else if IsString(childValue) {
|
||||||
s, _ := childValue.(string)
|
s, _ := childValue.(string)
|
||||||
v = int64(len(s))
|
v = int64(len(s))
|
||||||
} else if it, ok := childValue.(Iterator); ok {
|
} else if it, ok := childValue.(Iterator); ok {
|
||||||
|
@ -25,7 +25,7 @@ func evalPostInc(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
|
|
||||||
if it, ok := childValue.(Iterator); ok {
|
if it, ok := childValue.(Iterator); ok {
|
||||||
v, err = it.Next()
|
v, err = it.Next()
|
||||||
} else if isInteger(childValue) && self.children[0].symbol() == SymIdentifier {
|
} else if IsInteger(childValue) && self.children[0].symbol() == SymIdentifier {
|
||||||
v = childValue
|
v = childValue
|
||||||
i, _ := childValue.(int64)
|
i, _ := childValue.(int64)
|
||||||
ctx.SetVar(self.children[0].source(), i+1)
|
ctx.SetVar(self.children[0].source(), i+1)
|
||||||
|
@ -30,12 +30,12 @@ func evalMultiply(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isString(leftValue) && isInteger(rightValue) {
|
if IsString(leftValue) && IsInteger(rightValue) {
|
||||||
s, _ := leftValue.(string)
|
s, _ := leftValue.(string)
|
||||||
n, _ := rightValue.(int64)
|
n, _ := rightValue.(int64)
|
||||||
v = strings.Repeat(s, int(n))
|
v = strings.Repeat(s, int(n))
|
||||||
} else if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
} else if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
||||||
if isFloat(leftValue) || isFloat(rightValue) {
|
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||||
v = numAsFloat(leftValue) * numAsFloat(rightValue)
|
v = numAsFloat(leftValue) * numAsFloat(rightValue)
|
||||||
} else if isFraction(leftValue) || isFraction(rightValue) {
|
} else if isFraction(leftValue) || isFraction(rightValue) {
|
||||||
v, err = mulAnyFract(leftValue, rightValue)
|
v, err = mulAnyFract(leftValue, rightValue)
|
||||||
@ -72,7 +72,7 @@ func evalDivide(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
||||||
if isFloat(leftValue) || isFloat(rightValue) {
|
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||||
d := numAsFloat(rightValue)
|
d := numAsFloat(rightValue)
|
||||||
if d == 0.0 {
|
if d == 0.0 {
|
||||||
err = errors.New("division by zero")
|
err = errors.New("division by zero")
|
||||||
@ -148,7 +148,7 @@ func evalReminder(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isInteger(leftValue) && isInteger(rightValue) {
|
if IsInteger(leftValue) && IsInteger(rightValue) {
|
||||||
rightInt, _ := rightValue.(int64)
|
rightInt, _ := rightValue.(int64)
|
||||||
if rightInt == 0 {
|
if rightInt == 0 {
|
||||||
err = errors.New("division by zero")
|
err = errors.New("division by zero")
|
||||||
|
@ -25,15 +25,15 @@ func evalEqual(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isNumber(leftValue) && isNumber(rightValue) {
|
if IsNumber(leftValue) && IsNumber(rightValue) {
|
||||||
if isInteger(leftValue) && isInteger(rightValue) {
|
if IsInteger(leftValue) && IsInteger(rightValue) {
|
||||||
li, _ := leftValue.(int64)
|
li, _ := leftValue.(int64)
|
||||||
ri, _ := rightValue.(int64)
|
ri, _ := rightValue.(int64)
|
||||||
v = li == ri
|
v = li == ri
|
||||||
} else {
|
} else {
|
||||||
v = numAsFloat(leftValue) == numAsFloat(rightValue)
|
v = numAsFloat(leftValue) == numAsFloat(rightValue)
|
||||||
}
|
}
|
||||||
} else if isString(leftValue) && isString(rightValue) {
|
} else if IsString(leftValue) && IsString(rightValue) {
|
||||||
ls, _ := leftValue.(string)
|
ls, _ := leftValue.(string)
|
||||||
rs, _ := rightValue.(string)
|
rs, _ := rightValue.(string)
|
||||||
v = ls == rs
|
v = ls == rs
|
||||||
@ -111,15 +111,15 @@ func evalLess(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isNumber(leftValue) && isNumber(rightValue) {
|
if IsNumber(leftValue) && IsNumber(rightValue) {
|
||||||
if isInteger(leftValue) && isInteger(rightValue) {
|
if IsInteger(leftValue) && IsInteger(rightValue) {
|
||||||
li, _ := leftValue.(int64)
|
li, _ := leftValue.(int64)
|
||||||
ri, _ := rightValue.(int64)
|
ri, _ := rightValue.(int64)
|
||||||
v = li < ri
|
v = li < ri
|
||||||
} else {
|
} else {
|
||||||
v = numAsFloat(leftValue) < numAsFloat(rightValue)
|
v = numAsFloat(leftValue) < numAsFloat(rightValue)
|
||||||
}
|
}
|
||||||
} else if isString(leftValue) && isString(rightValue) {
|
} else if IsString(leftValue) && IsString(rightValue) {
|
||||||
ls, _ := leftValue.(string)
|
ls, _ := leftValue.(string)
|
||||||
rs, _ := rightValue.(string)
|
rs, _ := rightValue.(string)
|
||||||
v = ls < rs
|
v = ls < rs
|
||||||
@ -148,15 +148,15 @@ func evalLessEqual(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isNumber(leftValue) && isNumber(rightValue) {
|
if IsNumber(leftValue) && IsNumber(rightValue) {
|
||||||
if isInteger(leftValue) && isInteger(rightValue) {
|
if IsInteger(leftValue) && IsInteger(rightValue) {
|
||||||
li, _ := leftValue.(int64)
|
li, _ := leftValue.(int64)
|
||||||
ri, _ := rightValue.(int64)
|
ri, _ := rightValue.(int64)
|
||||||
v = li <= ri
|
v = li <= ri
|
||||||
} else {
|
} else {
|
||||||
v = numAsFloat(leftValue) <= numAsFloat(rightValue)
|
v = numAsFloat(leftValue) <= numAsFloat(rightValue)
|
||||||
}
|
}
|
||||||
} else if isString(leftValue) && isString(rightValue) {
|
} else if IsString(leftValue) && IsString(rightValue) {
|
||||||
ls, _ := leftValue.(string)
|
ls, _ := leftValue.(string)
|
||||||
rs, _ := rightValue.(string)
|
rs, _ := rightValue.(string)
|
||||||
v = ls <= rs
|
v = ls <= rs
|
||||||
|
@ -35,14 +35,14 @@ func evalSign(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isFloat(rightValue) {
|
if IsFloat(rightValue) {
|
||||||
if self.tk.Sym == SymChangeSign {
|
if self.tk.Sym == SymChangeSign {
|
||||||
f, _ := rightValue.(float64)
|
f, _ := rightValue.(float64)
|
||||||
v = -f
|
v = -f
|
||||||
} else {
|
} else {
|
||||||
v = rightValue
|
v = rightValue
|
||||||
}
|
}
|
||||||
} else if isInteger(rightValue) {
|
} else if IsInteger(rightValue) {
|
||||||
if self.tk.Sym == SymChangeSign {
|
if self.tk.Sym == SymChangeSign {
|
||||||
i, _ := rightValue.(int64)
|
i, _ := rightValue.(int64)
|
||||||
v = -i
|
v = -i
|
||||||
|
@ -28,17 +28,17 @@ func evalPlus(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isString(leftValue) && isNumberString(rightValue)) || (isString(rightValue) && isNumberString(leftValue)) {
|
if (IsString(leftValue) && isNumberString(rightValue)) || (IsString(rightValue) && isNumberString(leftValue)) {
|
||||||
v = fmt.Sprintf("%v%v", leftValue, rightValue)
|
v = fmt.Sprintf("%v%v", leftValue, rightValue)
|
||||||
} else if isNumber(leftValue) && isNumber(rightValue) {
|
} else if IsNumber(leftValue) && IsNumber(rightValue) {
|
||||||
if isFloat(leftValue) || isFloat(rightValue) {
|
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||||
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
||||||
} else {
|
} else {
|
||||||
leftInt, _ := leftValue.(int64)
|
leftInt, _ := leftValue.(int64)
|
||||||
rightInt, _ := rightValue.(int64)
|
rightInt, _ := rightValue.(int64)
|
||||||
v = leftInt + rightInt
|
v = leftInt + rightInt
|
||||||
}
|
}
|
||||||
} else if isList(leftValue) || isList(rightValue) {
|
} else if IsList(leftValue) || IsList(rightValue) {
|
||||||
var leftList, rightList *ListType
|
var leftList, rightList *ListType
|
||||||
var ok bool
|
var ok bool
|
||||||
if leftList, ok = leftValue.(*ListType); !ok {
|
if leftList, ok = leftValue.(*ListType); !ok {
|
||||||
@ -56,7 +56,7 @@ func evalPlus(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
}
|
}
|
||||||
v = &sumList
|
v = &sumList
|
||||||
} else if isFraction(leftValue) || isFraction(rightValue) {
|
} else if isFraction(leftValue) || isFraction(rightValue) {
|
||||||
if isFloat(leftValue) || isFloat(rightValue) {
|
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||||
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
||||||
} else {
|
} else {
|
||||||
v, err = sumAnyFract(leftValue, rightValue)
|
v, err = sumAnyFract(leftValue, rightValue)
|
||||||
@ -87,7 +87,7 @@ func evalMinus(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
if isNumOrFract(leftValue) && isNumOrFract(rightValue) {
|
||||||
if isFloat(leftValue) || isFloat(rightValue) {
|
if IsFloat(leftValue) || IsFloat(rightValue) {
|
||||||
v = numAsFloat(leftValue) - numAsFloat(rightValue)
|
v = numAsFloat(leftValue) - numAsFloat(rightValue)
|
||||||
} else if isFraction(leftValue) || isFraction(rightValue) {
|
} else if isFraction(leftValue) || isFraction(rightValue) {
|
||||||
v, err = subAnyFract(leftValue, rightValue)
|
v, err = subAnyFract(leftValue, rightValue)
|
||||||
@ -96,7 +96,7 @@ func evalMinus(ctx ExprContext, self *term) (v any, err error) {
|
|||||||
rightInt, _ := rightValue.(int64)
|
rightInt, _ := rightValue.(int64)
|
||||||
v = leftInt - rightInt
|
v = leftInt - rightInt
|
||||||
}
|
}
|
||||||
} else if isList(leftValue) && isList(rightValue) {
|
} else if IsList(leftValue) && IsList(rightValue) {
|
||||||
leftList, _ := leftValue.(*ListType)
|
leftList, _ := leftValue.(*ListType)
|
||||||
rightList, _ := rightValue.(*ListType)
|
rightList, _ := rightValue.(*ListType)
|
||||||
diffList := make(ListType, 0, len(*leftList)-len(*rightList))
|
diffList := make(ListType, 0, len(*leftList)-len(*rightList))
|
||||||
|
18
utils.go
18
utils.go
@ -9,41 +9,41 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func isString(v any) (ok bool) {
|
func IsString(v any) (ok bool) {
|
||||||
_, ok = v.(string)
|
_, ok = v.(string)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInteger(v any) (ok bool) {
|
func IsInteger(v any) (ok bool) {
|
||||||
_, ok = v.(int64)
|
_, ok = v.(int64)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func isFloat(v any) (ok bool) {
|
func IsFloat(v any) (ok bool) {
|
||||||
_, ok = v.(float64)
|
_, ok = v.(float64)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func isList(v any) (ok bool) {
|
func IsList(v any) (ok bool) {
|
||||||
_, ok = v.(*ListType)
|
_, ok = v.(*ListType)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func isDict(v any) (ok bool) {
|
func IsDict(v any) (ok bool) {
|
||||||
_, ok = v.(map[any]any)
|
_, ok = v.(map[any]any)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNumber(v any) (ok bool) {
|
func IsNumber(v any) (ok bool) {
|
||||||
return isFloat(v) || isInteger(v)
|
return IsFloat(v) || IsInteger(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNumOrFract(v any) (ok bool) {
|
func isNumOrFract(v any) (ok bool) {
|
||||||
return isFloat(v) || isInteger(v) || isFraction(v)
|
return IsFloat(v) || IsInteger(v) || isFraction(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNumberString(v any) (ok bool) {
|
func isNumberString(v any) (ok bool) {
|
||||||
return isString(v) || isNumber(v)
|
return IsString(v) || IsNumber(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isFunctor(v any) (ok bool) {
|
func isFunctor(v any) (ok bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user