Eprx now supports range of indeces to extract parts of strings or lists
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-range.go
|
||||
package expr
|
||||
|
||||
import "fmt"
|
||||
|
||||
// -------- range term
|
||||
type intPair struct {
|
||||
a, b int
|
||||
}
|
||||
|
||||
func (p *intPair) TypeName() string {
|
||||
return typePair
|
||||
}
|
||||
|
||||
func (p *intPair) ToString(opt FmtOpt) string {
|
||||
return fmt.Sprintf("(%d, %d)", p.a, p.b)
|
||||
}
|
||||
|
||||
func isIntPair(v any) bool {
|
||||
_, ok := v.(*intPair)
|
||||
return ok
|
||||
}
|
||||
|
||||
func newRangeTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priDot,
|
||||
evalFunc: evalRange,
|
||||
}
|
||||
}
|
||||
|
||||
func evalRange(ctx ExprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
// if err = self.checkOperands(); err != nil {
|
||||
// return
|
||||
// }
|
||||
if len(self.children) == 0 {
|
||||
leftValue = int64(0)
|
||||
rightValue = int64(-1)
|
||||
} else if len(self.children) == 1 {
|
||||
if leftValue, err = self.children[0].compute(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
rightValue = int64(-1)
|
||||
} else if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
if !(IsInteger(leftValue) && IsInteger(rightValue)) {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
return
|
||||
}
|
||||
|
||||
startIndex, _ := leftValue.(int64)
|
||||
endIndex, _ := rightValue.(int64)
|
||||
|
||||
v = &intPair{int(startIndex), int(endIndex)}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymColon, newRangeTerm)
|
||||
}
|
||||
Reference in New Issue
Block a user