moved a subset of source file to the kern package
This commit is contained in:
+20
-16
@@ -4,6 +4,10 @@
|
||||
// operator-index.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
)
|
||||
|
||||
// -------- index term
|
||||
func newIndexTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
@@ -15,15 +19,15 @@ func newIndexTerm(tk *Token) (inst *term) {
|
||||
}
|
||||
}
|
||||
|
||||
func verifyKey(indexList *ListType) (index any, err error) {
|
||||
func verifyKey(indexList *kern.ListType) (index any, err error) {
|
||||
index = (*indexList)[0]
|
||||
return
|
||||
}
|
||||
|
||||
func verifyIndex(indexTerm *term, indexList *ListType, maxValue int) (index int, err error) {
|
||||
func verifyIndex(indexTerm *term, indexList *kern.ListType, maxValue int) (index int, err error) {
|
||||
var v int
|
||||
|
||||
if v, err = ToGoInt((*indexList)[0], "index expression"); err == nil {
|
||||
if v, err = kern.ToGoInt((*indexList)[0], "index expression"); err == nil {
|
||||
if v < 0 && v >= -maxValue {
|
||||
v = maxValue + v
|
||||
}
|
||||
@@ -36,11 +40,11 @@ func verifyIndex(indexTerm *term, indexList *ListType, maxValue int) (index int,
|
||||
return
|
||||
}
|
||||
|
||||
func verifyRange(indexTerm *term, indexList *ListType, maxValue int) (startIndex, endIndex int, err error) {
|
||||
func verifyRange(indexTerm *term, indexList *kern.ListType, maxValue int) (startIndex, endIndex int, err error) {
|
||||
v, _ := ((*indexList)[0]).(*intPair)
|
||||
startIndex = v.a
|
||||
endIndex = v.b
|
||||
if endIndex == ConstLastIndex {
|
||||
if endIndex == kern.ConstLastIndex {
|
||||
endIndex = maxValue
|
||||
}
|
||||
if startIndex < 0 && startIndex >= -maxValue {
|
||||
@@ -59,9 +63,9 @@ func verifyRange(indexTerm *term, indexList *ListType, maxValue int) (startIndex
|
||||
return
|
||||
}
|
||||
|
||||
func evalIndex(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
func evalIndex(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
var indexList *ListType
|
||||
var indexList *kern.ListType
|
||||
var ok bool
|
||||
|
||||
if leftValue, rightValue, err = opTerm.evalInfix(ctx); err != nil {
|
||||
@@ -69,7 +73,7 @@ func evalIndex(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
}
|
||||
|
||||
indexTerm := opTerm.children[1]
|
||||
if indexList, ok = rightValue.(*ListType); !ok {
|
||||
if indexList, ok = rightValue.(*kern.ListType); !ok {
|
||||
err = opTerm.Errorf("invalid index expression")
|
||||
return
|
||||
} else if len(*indexList) != 1 {
|
||||
@@ -77,9 +81,9 @@ func evalIndex(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if IsInteger((*indexList)[0]) {
|
||||
if kern.IsInteger((*indexList)[0]) {
|
||||
switch unboxedValue := leftValue.(type) {
|
||||
case *ListType:
|
||||
case *kern.ListType:
|
||||
var index int
|
||||
if index, err = verifyIndex(indexTerm, indexList, len(*unboxedValue)); err == nil {
|
||||
v = (*unboxedValue)[index]
|
||||
@@ -89,17 +93,17 @@ func evalIndex(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
if index, err = verifyIndex(indexTerm, indexList, len(unboxedValue)); err == nil {
|
||||
v = string(unboxedValue[index])
|
||||
}
|
||||
case *DictType:
|
||||
case *kern.DictType:
|
||||
v, err = getDictItem(unboxedValue, indexTerm, indexList, rightValue)
|
||||
default:
|
||||
err = opTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
} else if isIntPair((*indexList)[0]) {
|
||||
switch unboxedValue := leftValue.(type) {
|
||||
case *ListType:
|
||||
case *kern.ListType:
|
||||
var start, end int
|
||||
if start, end, err = verifyRange(indexTerm, indexList, len(*unboxedValue)); err == nil {
|
||||
sublist := ListType((*unboxedValue)[start:end])
|
||||
sublist := kern.ListType((*unboxedValue)[start:end])
|
||||
v = &sublist
|
||||
}
|
||||
case string:
|
||||
@@ -110,8 +114,8 @@ func evalIndex(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
default:
|
||||
err = opTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
} else if IsDict(leftValue) {
|
||||
d := leftValue.(*DictType)
|
||||
} else if kern.IsDict(leftValue) {
|
||||
d := leftValue.(*kern.DictType)
|
||||
v, err = getDictItem(d, indexTerm, indexList, rightValue)
|
||||
} else {
|
||||
rightChild := opTerm.children[1]
|
||||
@@ -120,7 +124,7 @@ func evalIndex(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func getDictItem(d *DictType, indexTerm *term, indexList *ListType, rightValue any) (v any, err error) {
|
||||
func getDictItem(d *kern.DictType, indexTerm *term, indexList *kern.ListType, rightValue any) (v any, err error) {
|
||||
var ok bool
|
||||
var indexValue any
|
||||
|
||||
|
||||
Reference in New Issue
Block a user