refactored data-types to reduce plugin sizes
This commit is contained in:
+20
-16
@@ -7,6 +7,10 @@ package expr
|
||||
import (
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/scan"
|
||||
"git.portale-stac.it/go-pkg/expr/types"
|
||||
"git.portale-stac.it/go-pkg/expr/types/array"
|
||||
"git.portale-stac.it/go-pkg/expr/types/dict"
|
||||
"git.portale-stac.it/go-pkg/expr/types/list"
|
||||
)
|
||||
|
||||
// -------- index term
|
||||
@@ -20,15 +24,15 @@ func newIndexTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
}
|
||||
}
|
||||
|
||||
func verifyKey(indexList *kern.ListType) (index any, err error) {
|
||||
func verifyKey(indexList *array.ListType) (index any, err error) {
|
||||
index = (*indexList)[0]
|
||||
return
|
||||
}
|
||||
|
||||
func verifyIndex(indexTerm *scan.Term, indexList *kern.ListType, maxValue int) (index int, err error) {
|
||||
func verifyIndex(indexTerm *scan.Term, indexList *array.ListType, maxValue int) (index int, err error) {
|
||||
var v int
|
||||
|
||||
if v, err = kern.ToGoInt((*indexList)[0], "index expression"); err == nil {
|
||||
if v, err = types.ToGoInt((*indexList)[0], "index expression"); err == nil {
|
||||
if v < 0 && v >= -maxValue {
|
||||
v = maxValue + v
|
||||
}
|
||||
@@ -41,7 +45,7 @@ func verifyIndex(indexTerm *scan.Term, indexList *kern.ListType, maxValue int) (
|
||||
return
|
||||
}
|
||||
|
||||
func verifyRange(indexTerm *scan.Term, indexList *kern.ListType, maxValue int) (startIndex, endIndex int, err error) {
|
||||
func verifyRange(indexTerm *scan.Term, indexList *array.ListType, maxValue int) (startIndex, endIndex int, err error) {
|
||||
v, _ := ((*indexList)[0]).(*intPair)
|
||||
startIndex = v.a
|
||||
endIndex = v.b
|
||||
@@ -66,7 +70,7 @@ func verifyRange(indexTerm *scan.Term, indexList *kern.ListType, maxValue int) (
|
||||
|
||||
func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
var indexList *kern.ListType
|
||||
var indexList *array.ListType
|
||||
var ok bool
|
||||
|
||||
if leftValue, rightValue, err = opTerm.EvalInfix(ctx); err != nil {
|
||||
@@ -74,7 +78,7 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
}
|
||||
|
||||
indexTerm := opTerm.Children[1]
|
||||
if indexList, ok = rightValue.(*kern.ListType); !ok {
|
||||
if indexList, ok = rightValue.(*array.ListType); !ok {
|
||||
err = opTerm.Errorf("invalid index expression")
|
||||
return
|
||||
} else if len(*indexList) != 1 {
|
||||
@@ -82,14 +86,14 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if kern.IsInteger((*indexList)[0]) {
|
||||
if types.IsInteger((*indexList)[0]) {
|
||||
switch unboxedValue := leftValue.(type) {
|
||||
case *kern.ListType:
|
||||
case *array.ListType:
|
||||
var index int
|
||||
if index, err = verifyIndex(indexTerm, indexList, len(*unboxedValue)); err == nil {
|
||||
v = (*unboxedValue)[index]
|
||||
}
|
||||
case *kern.LinkedList:
|
||||
case *list.LinkedList:
|
||||
var index int
|
||||
if index, err = verifyIndex(indexTerm, indexList, unboxedValue.Len()); err == nil {
|
||||
v, err = unboxedValue.At(index)
|
||||
@@ -99,20 +103,20 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
if index, err = verifyIndex(indexTerm, indexList, len(unboxedValue)); err == nil {
|
||||
v = string(unboxedValue[index])
|
||||
}
|
||||
case *kern.DictType:
|
||||
case *dict.DictType:
|
||||
v, err = getDictItem(unboxedValue, indexTerm, indexList, rightValue)
|
||||
default:
|
||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
} else if isIntPair((*indexList)[0]) {
|
||||
switch unboxedValue := leftValue.(type) {
|
||||
case *kern.ListType:
|
||||
case *array.ListType:
|
||||
var start, end int
|
||||
if start, end, err = verifyRange(indexTerm, indexList, len(*unboxedValue)); err == nil {
|
||||
sublist := kern.ListType((*unboxedValue)[start:end])
|
||||
sublist := array.ListType((*unboxedValue)[start:end])
|
||||
v = &sublist
|
||||
}
|
||||
case *kern.LinkedList:
|
||||
case *list.LinkedList:
|
||||
var start, end int
|
||||
if start, end, err = verifyRange(indexTerm, indexList, unboxedValue.Len()); err == nil {
|
||||
v = unboxedValue.Sub(start, end)
|
||||
@@ -125,8 +129,8 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
default:
|
||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
} else if kern.IsDict(leftValue) {
|
||||
d := leftValue.(*kern.DictType)
|
||||
} else if dict.IsDict(leftValue) {
|
||||
d := leftValue.(*dict.DictType)
|
||||
v, err = getDictItem(d, indexTerm, indexList, rightValue)
|
||||
} else {
|
||||
rightChild := opTerm.Children[1]
|
||||
@@ -135,7 +139,7 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func getDictItem(d *kern.DictType, indexTerm *scan.Term, indexList *kern.ListType, rightValue any) (v any, err error) {
|
||||
func getDictItem(d *dict.DictType, indexTerm *scan.Term, indexList *array.ListType, rightValue any) (v any, err error) {
|
||||
var ok bool
|
||||
var indexValue any
|
||||
|
||||
|
||||
Reference in New Issue
Block a user