Skip to content

Commit

Permalink
Added support of ignoring expression results in variable declaration …
Browse files Browse the repository at this point in the history
…using '_' as a variable name
  • Loading branch information
ziflex committed Sep 20, 2021
1 parent f52b5db commit 7dea258
Show file tree
Hide file tree
Showing 14 changed files with 1,018 additions and 914 deletions.
37 changes: 37 additions & 0 deletions pkg/compiler/compiler_let_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,41 @@ func TestLet(t *testing.T) {
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `false`)
})

Convey("Should use ignorable variable name", t, func() {
out, err := newCompilerWithObservable().MustCompile(`
LET _ = (FOR i IN 1..100 RETURN NONE)
RETURN TRUE
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
})

Convey("Should allow to declare a variable name using _", t, func() {
c := compiler.New()

out, err := c.MustCompile(`
LET _ = (FOR i IN 1..100 RETURN NONE)
LET _ = (FOR i IN 1..100 RETURN NONE)
RETURN TRUE
`).Run(context.Background())

So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
})

Convey("Should not allow to use ignorable variable name", t, func() {
c := compiler.New()

_, err := c.Compile(`
LET _ = (FOR i IN 1..100 RETURN NONE)
RETURN _
`)

So(err, ShouldNotBeNil)
})
}
4 changes: 4 additions & 0 deletions pkg/compiler/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (s *scope) HasVariable(name string) bool {
}

func (s *scope) SetVariable(name string) error {
if name == core.IgnorableVariable {
return nil
}

_, exists := s.vars[name]

if exists {
Expand Down
6 changes: 5 additions & 1 deletion pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,8 +1142,12 @@ func (v *visitor) doVisitVariable(ctx *fql.VariableContext, scope *scope) (core.
func (v *visitor) doVisitVariableDeclaration(ctx *fql.VariableDeclarationContext, scope *scope) (core.Expression, error) {
var init core.Expression
var err error
name := core.IgnorableVariable

if id := ctx.Identifier(); id != nil {
name = id.GetText()
}

name := ctx.Identifier().GetText()
err = scope.SetVariable(name)

if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/parser/antlr/FqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ While: 'WHILE';
// Literals
Param: '@';
Identifier: Letter+ (Symbols (Identifier)*)* (Digit (Identifier)*)*;
IgnoreIdentifier: Underscore;
StringLiteral: SQString | DQSring | BacktickString | TickString;
IntegerLiteral: [0-9]+;
FloatLiteral
Expand All @@ -111,7 +112,8 @@ fragment ExponentPart
fragment Letter
: 'A'..'Z' | 'a'..'z'
;
fragment Symbols: '_';
fragment Symbols: Underscore;
fragment Underscore: '_';
fragment Digit
: '0'..'9'
;
Expand Down
11 changes: 6 additions & 5 deletions pkg/parser/antlr/FqlLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ Do=61
While=62
Param=63
Identifier=64
StringLiteral=65
IntegerLiteral=66
FloatLiteral=67
NamespaceSegment=68
UnknownIdentifier=69
IgnoreIdentifier=65
StringLiteral=66
IntegerLiteral=67
FloatLiteral=68
NamespaceSegment=69
UnknownIdentifier=70
':'=5
';'=6
'.'=7
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/antlr/FqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bodyExpression

variableDeclaration
: Let Identifier Assign expression
| Let IgnoreIdentifier Assign expression
;

returnExpression
Expand Down
6 changes: 5 additions & 1 deletion pkg/parser/fql/FqlLexer.interp

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions pkg/parser/fql/FqlLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ Do=61
While=62
Param=63
Identifier=64
StringLiteral=65
IntegerLiteral=66
FloatLiteral=67
NamespaceSegment=68
UnknownIdentifier=69
IgnoreIdentifier=65
StringLiteral=66
IntegerLiteral=67
FloatLiteral=68
NamespaceSegment=69
UnknownIdentifier=70
':'=5
';'=6
'.'=7
Expand Down
4 changes: 3 additions & 1 deletion pkg/parser/fql/FqlParser.interp

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions pkg/parser/fql/FqlParser.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ Do=61
While=62
Param=63
Identifier=64
StringLiteral=65
IntegerLiteral=66
FloatLiteral=67
NamespaceSegment=68
UnknownIdentifier=69
IgnoreIdentifier=65
StringLiteral=66
IntegerLiteral=67
FloatLiteral=68
NamespaceSegment=69
UnknownIdentifier=70
':'=5
';'=6
'.'=7
Expand Down
524 changes: 265 additions & 259 deletions pkg/parser/fql/fql_lexer.go

Large diffs are not rendered by default.

1,293 changes: 663 additions & 630 deletions pkg/parser/fql/fql_parser.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pkg/runtime/core/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core

const (
IgnorableVariable = "_"
)
15 changes: 9 additions & 6 deletions pkg/runtime/core/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,24 @@ func newScope(root *RootScope, parent *Scope) *Scope {
}

func (s *Scope) SetVariable(name string, val Value) error {
_, exists := s.vars[name]
if name != IgnorableVariable {
_, exists := s.vars[name]

// it already has been declared in the current scope
if exists {
return Errorf(ErrNotUnique, "variable is already declared: '%s'", name)
}

// it already has been declared in the current scope
if exists {
return Errorf(ErrNotUnique, "variable is already declared: '%s'", name)
s.vars[name] = val
}

// we still want to make sure that nothing than needs to be closed leaks out
disposable, ok := val.(io.Closer)

if ok {
s.root.AddDisposable(disposable)
}

s.vars[name] = val

return nil
}

Expand Down

0 comments on commit 7dea258

Please sign in to comment.