8e596d5979
1. moved scan/symbol.go to new package sym and adapted all files that reference Symbol type and its values. 2. new type interval defined by begin, end and step values. 3. replaced operator-range.go with operator-interval.go. 4. replaced range operator begin:end with begin..end..step. 5. new implementation of sub-collection extraction based on new interval literal.
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// operator-at.go
|
|
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/sym"
|
|
"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"
|
|
)
|
|
|
|
//-------- at term
|
|
|
|
func newAtTerm(tk *scan.Token) (inst *scan.Term) {
|
|
return &scan.Term{
|
|
Tk: *tk,
|
|
Children: make([]*scan.Term, 0, 2),
|
|
Position: scan.PosInfix,
|
|
Priority: scan.PriRelational,
|
|
EvalFunc: evalAt,
|
|
}
|
|
}
|
|
|
|
// func hasKey(d map[any]any, target any) (ok bool) {
|
|
// _, ok = d[target]
|
|
// return
|
|
// }
|
|
|
|
func evalAt(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
|
var leftValue, rightValue any
|
|
|
|
if leftValue, rightValue, err = opTerm.EvalInfix(ctx); err != nil {
|
|
return
|
|
}
|
|
|
|
v = int64(-1) // default value if not found
|
|
if array.IsArray(rightValue) {
|
|
a, _ := rightValue.(*array.ArrayType)
|
|
if index := a.IndexDeepSameCmp(leftValue); index >= 0 {
|
|
v = index
|
|
}
|
|
} else if dict.IsDict(rightValue) {
|
|
dict, _ := rightValue.(*dict.DictType)
|
|
if k, exists := dict.FindKey(leftValue); exists {
|
|
v = k
|
|
} else {
|
|
v = nil
|
|
}
|
|
} else if list.IsLinkedList(rightValue) {
|
|
ls, _ := rightValue.(*list.LinkedList)
|
|
if index := ls.Index(leftValue); index >= 0 {
|
|
v = index
|
|
}
|
|
} else {
|
|
v = nil
|
|
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
|
}
|
|
return
|
|
}
|
|
|
|
// init
|
|
func init() {
|
|
scan.RegisterTermConstructor(sym.SymKwAt, newAtTerm)
|
|
}
|