From ee5d9b43b54a3becf3cb4025198f24608d35500d Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 20 Mar 2021 22:40:23 +0100 Subject: Update vendor (#1414) --- vendor/github.com/d5/tengo/v2/builtins.go | 69 +++++++++++++++++++++++++++++++ vendor/github.com/d5/tengo/v2/errors.go | 3 ++ 2 files changed, 72 insertions(+) (limited to 'vendor/github.com/d5/tengo') diff --git a/vendor/github.com/d5/tengo/v2/builtins.go b/vendor/github.com/d5/tengo/v2/builtins.go index fcda81cd..b954d072 100644 --- a/vendor/github.com/d5/tengo/v2/builtins.go +++ b/vendor/github.com/d5/tengo/v2/builtins.go @@ -121,6 +121,10 @@ var builtinFuncs = []*BuiltinFunction{ Name: "format", Value: builtinFormat, }, + { + Name: "range", + Value: builtinRange, + }, } // GetAllBuiltinFunctions returns all builtin function objects. @@ -323,6 +327,71 @@ func builtinLen(args ...Object) (Object, error) { } } +//range(start, stop[, step]) +func builtinRange(args ...Object) (Object, error) { + numArgs := len(args) + if numArgs < 2 || numArgs > 3 { + return nil, ErrWrongNumArguments + } + var start, stop, step *Int + + for i, arg := range args { + v, ok := args[i].(*Int) + if !ok { + var name string + switch i { + case 0: + name = "start" + case 1: + name = "stop" + case 2: + name = "step" + } + + return nil, ErrInvalidArgumentType{ + Name: name, + Expected: "int", + Found: arg.TypeName(), + } + } + if i == 2 && v.Value <= 0 { + return nil, ErrInvalidRangeStep + } + switch i { + case 0: + start = v + case 1: + stop = v + case 2: + step = v + } + } + + if step == nil { + step = &Int{Value: int64(1)} + } + + return buildRange(start.Value, stop.Value, step.Value), nil +} + +func buildRange(start, stop, step int64) *Array { + array := &Array{} + if start <= stop { + for i := start; i < stop; i += step { + array.Value = append(array.Value, &Int{ + Value: i, + }) + } + } else { + for i := start; i > stop; i -= step { + array.Value = append(array.Value, &Int{ + Value: i, + }) + } + } + return array +} + func builtinFormat(args ...Object) (Object, error) { numArgs := len(args) if numArgs == 0 { diff --git a/vendor/github.com/d5/tengo/v2/errors.go b/vendor/github.com/d5/tengo/v2/errors.go index a3fd1f3b..8ef610a3 100644 --- a/vendor/github.com/d5/tengo/v2/errors.go +++ b/vendor/github.com/d5/tengo/v2/errors.go @@ -49,6 +49,9 @@ var ( // ErrNotImplemented is an error where an Object has not implemented a // required method. ErrNotImplemented = errors.New("not implemented") + + // ErrInvalidRangeStep is an error where the step parameter is less than or equal to 0 when using builtin range function. + ErrInvalidRangeStep = errors.New("range step must be greater than 0") ) // ErrInvalidArgumentType represents an invalid argument value type error. -- cgit v1.2.3