Skip to content

Commit

Permalink
Fixed LOOP body statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Nov 1, 2024
1 parent 72d8863 commit 03167dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
23 changes: 11 additions & 12 deletions pkg/compiler/compiler_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func TestUnaryOperators(t *testing.T) {
RETURN { enabled: !val }
`)

v1, err := runtime.NewVM(p1).Run(context.Background())
v1, err := runtime.NewVM(p1).Run(context.Background(), nil)

So(err, ShouldBeNil)

Expand All @@ -339,7 +339,7 @@ func TestUnaryOperators(t *testing.T) {
RETURN { enabled: !!val }
`)

v2, err := runtime.NewVM(p2).Run(context.Background())
v2, err := runtime.NewVM(p2).Run(context.Background(), nil)

So(err, ShouldBeNil)

Expand Down Expand Up @@ -1121,20 +1121,19 @@ func TestFor(t *testing.T) {
// ShouldEqualJSON,
//},
RunUseCases(t, []UseCase{
{
"FOR i IN 1..5 RETURN i",
[]any{1, 2, 3, 4, 5},
ShouldEqualJSON,
},
//{
// `FOR i IN 1..5
// LET x = i
// PRINT(x)
// RETURN i
// `,
// "FOR i IN 1..5 RETURN i",
// []any{1, 2, 3, 4, 5},
// ShouldEqualJSON,
//},
{
`FOR i IN 1..5
LET x = i * 2
RETURN x
`,
[]any{2, 4, 6, 8, 10},
ShouldEqualJSON,
},
//{
// `FOR val, counter IN 1..5
// LET x = val
Expand Down
18 changes: 13 additions & 5 deletions pkg/compiler/compiler_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Compile(expression string) (*runtime.Program, error) {
func Run(p *runtime.Program, opts ...runtime.EnvironmentOption) ([]byte, error) {
vm := runtime.NewVM(p)

out, err := vm.Run(context.Background(), opts...)
out, err := vm.Run(context.Background(), opts)

if err != nil {
return nil, err
Expand Down Expand Up @@ -126,7 +126,12 @@ func RunAsmUseCases(t *testing.T, useCases []ByteCodeUseCase) {

func RunUseCasesWith(t *testing.T, c *compiler.Compiler, useCases []UseCase, opts ...runtime.EnvironmentOption) {
for _, useCase := range useCases {
t.Run(useCase.Expression, func(t *testing.T) {
name := strings.TrimSpace(useCase.Expression)
name = strings.Replace(name, "\n", " ", -1)
name = strings.Replace(name, "\t", " ", -1)
// Replace multiple spaces with a single space
name = strings.Join(strings.Fields(name), " ")
t.Run(name, func(t *testing.T) {
Convey(useCase.Expression, t, func() {
// catch panic
//defer func() {
Expand Down Expand Up @@ -188,10 +193,13 @@ func RunBenchmarkWith(b *testing.B, c *compiler.Compiler, expression string, opt
}
options = append(options, opts...)

for n := 0; n < b.N; n++ {
vm := runtime.NewVM(prog)
ctx := context.Background()
vm := runtime.NewVM(prog)

b.ResetTimer()

_, err := vm.Run(context.Background(), opts...)
for n := 0; n < b.N; n++ {
_, err := vm.Run(ctx, opts)

if err != nil {
panic(err)
Expand Down
16 changes: 8 additions & 8 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ func (v *visitor) VisitFilterClause(ctx *fql.FilterClauseContext) interface{} {
}

func (v *visitor) VisitForExpressionStatement(ctx *fql.ForExpressionStatementContext) interface{} {
//if c := ctx.VariableDeclaration(); c != nil {
// c.Accept(v)
//} else if c := ctx.FunctionCallExpression(); c != nil {
// c.Accept(v)
// // remove un-used return value
// v.emitPop()
//}
if c := ctx.VariableDeclaration(); c != nil {
return c.Accept(v)
}

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

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

func (v *visitor) VisitFunctionCallExpression(ctx *fql.FunctionCallExpressionContext) interface{} {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewVM(program *Program) *VM {
return vm
}

func (vm *VM) Run(ctx context.Context, opts ...EnvironmentOption) (core.Value, error) {
func (vm *VM) Run(ctx context.Context, opts []EnvironmentOption) (core.Value, error) {
tryCatch := func(pos int) bool {
for _, pair := range vm.program.CatchTable {
if pos >= pair[0] && pos <= pair[1] {
Expand Down

0 comments on commit 03167dc

Please sign in to comment.