array and linked-list: at operator

This commit is contained in:
2026-07-13 14:39:26 +02:00
parent daca05e637
commit 6b561fea45
6 changed files with 176 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
// 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/types/array"
"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
}
if array.IsArray(rightValue) {
a, _ := rightValue.(*array.ArrayType)
v = a.IndexDeepSameCmp(leftValue)
// } else if dict.IsDict(rightValue) {
// dict, _ := rightValue.(*dict.DictType)
// v = dict.HasKey(leftValue)
} else if list.IsLinkedList(rightValue) {
ls, _ := rightValue.(*list.LinkedList)
v = ls.Index(leftValue)
} else {
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
}
return
}
// init
func init() {
scan.RegisterTermConstructor(scan.SymKwAt, newAtTerm)
}