1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-12-11 22:47:08 +02:00

Added var uniqueness check

This commit is contained in:
Tim Voronov
2019-11-14 22:10:55 -05:00
parent 8ee36b78d5
commit d4a92fc64b
2 changed files with 30 additions and 4 deletions

View File

@@ -208,4 +208,27 @@ func TestLet(t *testing.T) {
So(err, ShouldNotBeNil)
})
Convey("Should not compile if a variable not defined", t, func() {
c := compiler.New()
_, err := c.Compile(`
RETURN foo
`)
So(err, ShouldNotBeNil)
})
Convey("Should not compile if a variable is not unique", t, func() {
c := compiler.New()
_, err := c.Compile(`
LET foo = "bar"
LET foo = "baz"
RETURN foo
`)
So(err, ShouldNotBeNil)
})
}

View File

@@ -1013,6 +1013,13 @@ func (v *visitor) doVisitVariableDeclaration(ctx *fql.VariableDeclarationContext
var init core.Expression
var err error
name := ctx.Identifier().GetText()
err = scope.SetVariable(name)
if err != nil {
return nil, err
}
exp := ctx.Expression()
if exp != nil {
@@ -1039,10 +1046,6 @@ func (v *visitor) doVisitVariableDeclaration(ctx *fql.VariableDeclarationContext
return nil, err
}
name := ctx.Identifier().GetText()
scope.SetVariable(name)
return expressions.NewVariableDeclarationExpression(
v.getSourceMap(ctx),
name,