mirror of
https://github.com/MontFerret/ferret.git
synced 2026-05-22 10:15:21 +02:00
#4 Added range operator
This commit is contained in:
@@ -1395,6 +1395,59 @@ func TestTernaryOperator(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRangeOperator(t *testing.T) {
|
||||
Convey("Should compile RETURN 1..10", t, func() {
|
||||
c := compiler.New()
|
||||
|
||||
prog, err := c.Compile(`
|
||||
RETURN 1..10
|
||||
`)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
out, err := prog.Run(context.Background())
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(string(out), ShouldEqual, `[1,2,3,4,5,6,7,8,9,10]`)
|
||||
})
|
||||
|
||||
Convey("Should compile FOR i IN 1..10 RETURN i * 2", t, func() {
|
||||
c := compiler.New()
|
||||
|
||||
prog, err := c.Compile(`
|
||||
FOR i IN 1..10
|
||||
RETURN i * 2
|
||||
`)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
out, err := prog.Run(context.Background())
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||||
})
|
||||
|
||||
Convey("Should compile LET arr = 1..10 FOR i IN arr RETURN i * 2", t, func() {
|
||||
c := compiler.New()
|
||||
|
||||
prog, err := c.Compile(`
|
||||
LET arr = 1..10
|
||||
FOR i IN arr
|
||||
RETURN i * 2
|
||||
`)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
out, err := prog.Run(context.Background())
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(string(out), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
|
||||
})
|
||||
}
|
||||
|
||||
//func TestHtml(t *testing.T) {
|
||||
// Convey("Should load a document", t, func() {
|
||||
// c := compiler.New()
|
||||
|
||||
@@ -379,6 +379,12 @@ func (v *visitor) doVisitForExpressionSource(ctx *fql.ForExpressionSourceContext
|
||||
return v.doVisitMemberExpression(memberExp.(*fql.MemberExpressionContext), scope)
|
||||
}
|
||||
|
||||
rangeOp := ctx.RangeOperator()
|
||||
|
||||
if rangeOp != nil {
|
||||
return v.doVisitRangeOperator(rangeOp.(*fql.RangeOperatorContext), scope)
|
||||
}
|
||||
|
||||
return nil, core.Error(ErrInvalidDataSource, ctx.GetText())
|
||||
}
|
||||
|
||||
@@ -626,6 +632,28 @@ 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))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
right, err := v.doVisitIntegerLiteral(ints[1].(*fql.IntegerLiteralContext))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return operators.NewRangeOperator(
|
||||
v.getSourceMap(ctx),
|
||||
left,
|
||||
right,
|
||||
)
|
||||
}
|
||||
|
||||
func (v *visitor) doVisitChildren(node antlr.RuleNode, scope *scope) ([]core.Expression, error) {
|
||||
children := node.GetChildren()
|
||||
|
||||
@@ -787,6 +815,12 @@ func (v *visitor) doVisitExpression(ctx *fql.ExpressionContext, scope *scope) (c
|
||||
)
|
||||
}
|
||||
|
||||
rangeOp := ctx.RangeOperator()
|
||||
|
||||
if rangeOp != nil {
|
||||
return v.doVisitRangeOperator(rangeOp.(*fql.RangeOperatorContext), scope)
|
||||
}
|
||||
|
||||
seq := ctx.ExpressionSequence()
|
||||
|
||||
if seq != nil {
|
||||
|
||||
Reference in New Issue
Block a user