1
0
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:
Tim Voronov
2018-09-28 13:56:25 -04:00
parent 2a98c5fb66
commit 74c03367cc
5 changed files with 522 additions and 437 deletions

View File

@@ -1553,6 +1553,36 @@ func TestRangeOperator(t *testing.T) {
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) {

View File

@@ -664,20 +664,19 @@ func (v *visitor) doVisitVariableDeclaration(ctx *fql.VariableDeclarationContext
}
func (v *visitor) doVisitRangeOperator(ctx *fql.RangeOperatorContext, scope *scope) (collections.IterableExpression, error) {
ints := ctx.AllIntegerLiteral()
left, err := v.doVisitIntegerLiteral(ints[0].(*fql.IntegerLiteralContext))
exp, err := v.doVisitChildren(ctx, scope)
if err != nil {
return nil, err
}
right, err := v.doVisitIntegerLiteral(ints[1].(*fql.IntegerLiteralContext))
if err != nil {
return nil, err
if len(exp) < 2 {
return nil, errors.Wrap(ErrInvalidToken, ctx.GetText())
}
left := exp[0]
right := exp[1]
return operators.NewRangeOperator(
v.getSourceMap(ctx),
left,

View File

@@ -133,7 +133,7 @@ variable
;
rangeOperator
: integerLiteral Range integerLiteral
: (integerLiteral | variable) Range (integerLiteral | variable)
;
arrayLiteral

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff