Skip to content

Commit

Permalink
Fixed var declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Mar 26, 2024
1 parent 9dcd7e5 commit 930adef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 13 additions & 2 deletions pkg/compiler/compiler_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestFor(t *testing.T) {
},
{
`FOR val, counter IN 1..5
LET x = val
PRINT(counter)
LET x = val
PRINT(counter)
LET y = counter
RETURN [x, y]
`,
Expand Down Expand Up @@ -103,5 +103,16 @@ func TestFor(t *testing.T) {
[]any{[]any{map[string]any{"a": 1}, map[string]any{"b": 1}, map[string]any{"c": 1}}, []any{map[string]any{"a": 2}, map[string]any{"b": 2}, map[string]any{"c": 2}}, []any{map[string]any{"a": 3}, map[string]any{"b": 3}, map[string]any{"c": 3}}},
ShouldEqualJSON,
},
{
`FOR val IN [1, 2, 3]
LET sub = (
FOR prop IN ["a", "b", "c"]
RETURN { [prop]: val }
)
RETURN sub`,
[]any{[]any{map[string]any{"a": 1}, map[string]any{"b": 1}, map[string]any{"c": 1}}, []any{map[string]any{"a": 2}, map[string]any{"b": 2}, map[string]any{"c": 2}}, []any{map[string]any{"a": 3}, map[string]any{"b": 3}, map[string]any{"c": 3}}},
ShouldEqualJSON,
},
})
}
5 changes: 2 additions & 3 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,9 @@ func (v *visitor) VisitVariableDeclaration(ctx *fql.VariableDeclarationContext)
name = reserved.GetText()
}

index := v.declareVariable(name)

ctx.Expression().Accept(v)

// we do not have custom functions, thus this feature is not needed at this moment
index := v.declareVariable(name)
v.defineVariable(index)

return nil
Expand Down

0 comments on commit 930adef

Please sign in to comment.