summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/d5/tengo')
-rw-r--r--vendor/github.com/d5/tengo/v2/builtins.go69
-rw-r--r--vendor/github.com/d5/tengo/v2/errors.go3
2 files changed, 72 insertions, 0 deletions
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.