// 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: priRange, evalFunc: evalRange, } } func changeColonToRange(t *term) { if t.tk.IsSymbol(SymColon) { t.tk.Sym = SymRange t.evalFunc = evalRange } } func evalRange(ctx ExprContext, opTerm *term) (v any, err error) { var leftValue, rightValue any if len(opTerm.children) == 0 { leftValue = int64(0) rightValue = int64(-1) } else if len(opTerm.children) == 1 { if leftValue, err = opTerm.children[0].compute(ctx); err != nil { return } rightValue = int64(ConstLastIndex) } else if leftValue, rightValue, err = opTerm.evalInfix(ctx); err != nil { return } if !(IsInteger(leftValue) && IsInteger(rightValue)) { // err = opTerm.errIncompatibleTypes(leftValue, rightValue) err = errRangeInvalidSpecification(opTerm) return } startIndex, _ := leftValue.(int64) endIndex, _ := rightValue.(int64) v = &intPair{int(startIndex), int(endIndex)} return } func errRangeInvalidSpecification(t *term) error { return t.Errorf("invalid range specification") } func errRangeUnexpectedExpression(t *term) error { return t.Errorf("unexpected range expression") } // init func init() { registerTermConstructor(SymRange, newRangeTerm) }