Skip to content

Commit

Permalink
Revert "Work on sorting"
Browse files Browse the repository at this point in the history
This reverts commit c67aa0a.
  • Loading branch information
ziflex committed Nov 20, 2024
1 parent c67aa0a commit 3fc21af
Show file tree
Hide file tree
Showing 14 changed files with 1,023 additions and 1,147 deletions.
3 changes: 1 addition & 2 deletions pkg/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package compiler
import (
"errors"

goruntime "runtime"

"github.com/MontFerret/ferret/pkg/parser"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/stdlib"
goruntime "runtime"
)

type Compiler struct {
Expand Down
196 changes: 1 addition & 195 deletions pkg/compiler/compiler_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package compiler_test
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/parser"
"regexp"
"strconv"
"strings"
"testing"

"github.com/MontFerret/ferret/pkg/parser"

"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
Expand Down Expand Up @@ -1308,196 +1307,3 @@ func TestForLimit(t *testing.T) {
return values.NewInt(2), nil
}))
}

func TestForSort(t *testing.T) {
RunUseCases(t, []UseCase{
CaseArray(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
}
]
FOR u IN users
SORT u.age
RETURN u
`, []any{map[string]any{"active": true, "age": 29, "gender": "f"}, map[string]any{"active": true, "age": 31, "gender": "m"}, map[string]any{"active": true, "age": 36, "gender": "m"}}),
})
//
//
//Convey("Should compile query with SORT DESC statement", t, func() {
// c := compiler.New()
//
// p, err := c.Compile(`
// LET users = [
// {
// active: true,
// age: 31,
// gender: "m"
// },
// {
// active: true,
// age: 29,
// gender: "f"
// },
// {
// active: true,
// age: 36,
// gender: "m"
// }
// ]
// FOR u IN users
// SORT u.age DESC
// RETURN u
// `)
//
// So(err, ShouldBeNil)
//
// out, err := p.Run(context.Background())
//
// So(err, ShouldBeNil)
//
// So(string(out), ShouldEqual, `[{"active":true,"age":36,"gender":"m"},{"active":true,"age":31,"gender":"m"},{"active":true,"age":29,"gender":"f"}]`)
//})
//
//Convey("Should compile query with SORT statement with multiple expressions", t, func() {
// c := compiler.New()
//
// p, err := c.Compile(`
// LET users = [
// {
// active: true,
// age: 31,
// gender: "m"
// },
// {
// active: true,
// age: 29,
// gender: "f"
// },
// {
// active: true,
// age: 31,
// gender: "f"
// },
// {
// active: true,
// age: 36,
// gender: "m"
// }
// ]
// FOR u IN users
// SORT u.age, u.gender
// RETURN u
// `)
//
// So(err, ShouldBeNil)
//
// out, err := p.Run(context.Background())
//
// So(err, ShouldBeNil)
//
// So(string(out), ShouldEqual, `[{"active":true,"age":29,"gender":"f"},{"active":true,"age":31,"gender":"f"},{"active":true,"age":31,"gender":"m"},{"active":true,"age":36,"gender":"m"}]`)
//})
//
//Convey("Should define variables and call functions", t, func() {
// c := compiler.New()
// counter := 0
// c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
// counter++
//
// So(args[0], ShouldEqual, "foo")
//
// return values.None, nil
// })
//
// p, err := c.Compile(`
// LET users = [
// {
// active: true,
// age: 31,
// gender: "m"
// },
// {
// active: true,
// age: 29,
// gender: "f"
// },
// {
// active: true,
// age: 31,
// gender: "f"
// },
// {
// active: true,
// age: 36,
// gender: "m"
// }
// ]
// FOR u IN users
// LET x = "foo"
// TEST(x)
// SORT u.age, u.gender
// RETURN u
// `)
//
// So(err, ShouldBeNil)
//
// out, err := p.Run(context.Background())
//
// So(err, ShouldBeNil)
// So(counter, ShouldEqual, 4)
// So(string(out), ShouldEqual, `[{"active":true,"age":29,"gender":"f"},{"active":true,"age":31,"gender":"f"},{"active":true,"age":31,"gender":"m"},{"active":true,"age":36,"gender":"m"}]`)
//})
//
//Convey("Should be able to reuse values from a source", t, func() {
// c := compiler.New()
//
// p, err := c.Compile(`
// LET users = [
// {
// active: true,
// age: 31,
// gender: "m"
// },
// {
// active: true,
// age: 29,
// gender: "f"
// },
// {
// active: true,
// age: 31,
// gender: "f"
// },
// {
// active: true,
// age: 36,
// gender: "m"
// }
// ]
// FOR u IN users
// LET x = u.gender
// SORT u.age, u.gender
// RETURN {u,x}
// `)
//
// So(err, ShouldBeNil)
//
// out, err := p.Run(context.Background())
//
// So(err, ShouldBeNil)
// So(string(out), ShouldEqual, `[{"u":{"active":true,"age":29,"gender":"f"},"x":"f"},{"u":{"active":true,"age":31,"gender":"f"},"x":"f"},{"u":{"active":true,"age":31,"gender":"m"},"x":"m"},{"u":{"active":true,"age":36,"gender":"m"},"x":"m"}]`)
//})
}
4 changes: 2 additions & 2 deletions pkg/compiler/loops.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type (
Distinct bool
Result runtime.Operand
Iterator runtime.Operand
Allocate bool
Next int
Allocate bool
Next int
}

LoopTable struct {
Expand Down
39 changes: 20 additions & 19 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
dsReg := loop.Result

if loop.Allocate {
v.emitter.EmitAb(runtime.OpLoopInit, dsReg, distinct)
v.emitter.EmitAb(runtime.OpLoopBegin, dsReg, distinct)
}

if isForLoop {
Expand All @@ -132,7 +132,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}

iterReg := v.registers.Allocate(State)

v.emitter.EmitAB(runtime.OpForLoopStart, iterReg, src1)
v.emitter.EmitAB(runtime.OpForLoopInit, iterReg, src1)
// jumpPlaceholder is a placeholder for the exit jump position
loop.Next = v.emitter.EmitJumpc(runtime.OpForLoopNext, jumpPlaceholder, iterReg)

Expand Down Expand Up @@ -168,7 +168,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
srcExpr := ctx.Expression()

// Create initial value for the loop counter
v.emitter.EmitA(runtime.OpWhileLoopStart, counterReg)
v.emitter.EmitA(runtime.OpWhileLoopInit, counterReg)
beforeExp := v.emitter.Size()
// Loop data source to iterate over
cond := srcExpr.Accept(v).(runtime.Operand)
Expand All @@ -187,15 +187,8 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}
jumpIndex := loop.Next
loop.Next -= jumpOffset

// Clauses
if clauses := ctx.AllForExpressionClause(); clauses != nil && len(clauses) > 0 {
for _, clause := range clauses {
clause.Accept(v)
}
}

// body
if body := ctx.AllForExpressionStatement(); body != nil && len(body) > 0 {
if body := ctx.AllForExpressionBody(); body != nil && len(body) > 0 {
for _, b := range body {
b.Accept(v)
}
Expand All @@ -218,7 +211,7 @@ func (v *visitor) VisitForExpression(ctx *fql.ForExpressionContext) interface{}

if loop.Allocate {
// TODO: Reuse the dsReg register
v.emitter.EmitAB(runtime.OpLoopFin, dst, dsReg)
v.emitter.EmitAB(runtime.OpLoopEnd, dst, dsReg)

if isForLoop {
v.emitter.PatchJump(jumpIndex)
Expand Down Expand Up @@ -271,12 +264,12 @@ func (v *visitor) VisitForExpressionSource(ctx *fql.ForExpressionSourceContext)
panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
}

func (v *visitor) VisitForExpressionStatement(ctx *fql.ForExpressionStatementContext) interface{} {
if c := ctx.VariableDeclaration(); c != nil {
func (v *visitor) VisitForExpressionBody(ctx *fql.ForExpressionBodyContext) interface{} {
if c := ctx.ForExpressionClause(); c != nil {
return c.Accept(v)
}

if c := ctx.FunctionCallExpression(); c != nil {
if c := ctx.ForExpressionStatement(); c != nil {
return c.Accept(v)
}

Expand Down Expand Up @@ -305,10 +298,6 @@ func (v *visitor) VisitForExpressionClause(ctx *fql.ForExpressionClauseContext)
panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
}

func (v *visitor) VisitSortClause(ctx *fql.SortClauseContext) interface{} {
return nil
}

func (v *visitor) VisitFilterClause(ctx *fql.FilterClauseContext) interface{} {
src1 := ctx.Expression().Accept(v).(runtime.Operand)
v.emitter.EmitJumpc(runtime.OpJumpIfFalse, v.loops.Loop().Next, src1)
Expand Down Expand Up @@ -375,6 +364,18 @@ func (v *visitor) VisitLimitClauseValue(ctx *fql.LimitClauseValueContext) interf
panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
}

func (v *visitor) VisitForExpressionStatement(ctx *fql.ForExpressionStatementContext) interface{} {
if c := ctx.VariableDeclaration(); c != nil {
return c.Accept(v)
}

if c := ctx.FunctionCallExpression(); c != nil {
return c.Accept(v)
}

panic(core.Error(ErrUnexpectedToken, ctx.GetText()))
}

func (v *visitor) VisitFunctionCallExpression(ctx *fql.FunctionCallExpressionContext) interface{} {
return v.visitFunctionCall(ctx.FunctionCall().(*fql.FunctionCallContext), ctx.ErrorOperator() != nil)
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/parser/antlr/FqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ returnExpression

forExpression
: For valueVariable=(Identifier | IgnoreIdentifier) (Comma counterVariable=Identifier)? In forExpressionSource
forExpressionClause*
forExpressionStatement*
forExpressionBody*
forExpressionReturn
| For counterVariable=(Identifier | IgnoreIdentifier) Do? While expression
forExpressionClause*
forExpressionStatement*
forExpressionBody*
forExpressionReturn
;

Expand All @@ -77,6 +75,11 @@ forExpressionStatement
| functionCallExpression
;

forExpressionBody
: forExpressionStatement
| forExpressionClause
;

forExpressionReturn
: returnExpression
| forExpression
Expand Down
3 changes: 2 additions & 1 deletion pkg/parser/fql/FqlParser.interp

Large diffs are not rendered by default.

Loading

0 comments on commit 3fc21af

Please sign in to comment.