mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-23 21:54:45 +02:00
Extended range operator
This commit is contained in:
@@ -1553,6 +1553,36 @@ func TestRangeOperator(t *testing.T) {
|
|||||||
|
|
||||||
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Should use variables", t, func() {
|
||||||
|
out := compiler.New().MustCompile(`
|
||||||
|
LET max = 10
|
||||||
|
|
||||||
|
FOR i IN 1..max
|
||||||
|
RETURN i * 2
|
||||||
|
`).MustRun(context.Background())
|
||||||
|
|
||||||
|
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||||||
|
|
||||||
|
out2 := compiler.New().MustCompile(`
|
||||||
|
LET min = 1
|
||||||
|
|
||||||
|
FOR i IN min..10
|
||||||
|
RETURN i * 2
|
||||||
|
`).MustRun(context.Background())
|
||||||
|
|
||||||
|
So(string(out2), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||||||
|
|
||||||
|
out3 := compiler.New().MustCompile(`
|
||||||
|
LET min = 1
|
||||||
|
LET max = 10
|
||||||
|
|
||||||
|
FOR i IN min..max
|
||||||
|
RETURN i * 2
|
||||||
|
`).MustRun(context.Background())
|
||||||
|
|
||||||
|
So(string(out3), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInOperator(t *testing.T) {
|
func TestInOperator(t *testing.T) {
|
||||||
|
|||||||
@@ -664,20 +664,19 @@ func (v *visitor) doVisitVariableDeclaration(ctx *fql.VariableDeclarationContext
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *visitor) doVisitRangeOperator(ctx *fql.RangeOperatorContext, scope *scope) (collections.IterableExpression, error) {
|
func (v *visitor) doVisitRangeOperator(ctx *fql.RangeOperatorContext, scope *scope) (collections.IterableExpression, error) {
|
||||||
ints := ctx.AllIntegerLiteral()
|
exp, err := v.doVisitChildren(ctx, scope)
|
||||||
|
|
||||||
left, err := v.doVisitIntegerLiteral(ints[0].(*fql.IntegerLiteralContext))
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
right, err := v.doVisitIntegerLiteral(ints[1].(*fql.IntegerLiteralContext))
|
if len(exp) < 2 {
|
||||||
|
return nil, errors.Wrap(ErrInvalidToken, ctx.GetText())
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
left := exp[0]
|
||||||
|
right := exp[1]
|
||||||
|
|
||||||
return operators.NewRangeOperator(
|
return operators.NewRangeOperator(
|
||||||
v.getSourceMap(ctx),
|
v.getSourceMap(ctx),
|
||||||
left,
|
left,
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ variable
|
|||||||
;
|
;
|
||||||
|
|
||||||
rangeOperator
|
rangeOperator
|
||||||
: integerLiteral Range integerLiteral
|
: (integerLiteral | variable) Range (integerLiteral | variable)
|
||||||
;
|
;
|
||||||
|
|
||||||
arrayLiteral
|
arrayLiteral
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user