Skip to content

Commit

Permalink
handle duplicated tuple pattern name (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChloePlanet authored May 14, 2020
1 parent 279544e commit 452302e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 10 additions & 0 deletions rel/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ type TuplePattern struct {
}

func NewTuplePattern(attrs ...AttrPattern) TuplePattern {
names := make(map[string]bool)
for _, attr := range attrs {
if names[attr.name] {
panic(fmt.Sprintf("name %s is duplicated in tuple", attr.name))
}
}
return TuplePattern{attrs}
}

Expand All @@ -125,6 +131,10 @@ func (p TuplePattern) Bind(scope Scope, value Value) Scope {
panic(fmt.Sprintf("%s is not a tuple", value))
}

if len(p.attrs) != tuple.Count() {
panic(fmt.Sprintf("tuples %s and %s cannot match", p, tuple))
}

result := EmptyScope
for _, attr := range p.attrs {
tupleExpr := tuple.MustGet(attr.name)
Expand Down
5 changes: 3 additions & 2 deletions syntax/expr_let_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ func TestExprLetArrayPattern(t *testing.T) {
func TestExprLetTuplePattern(t *testing.T) {
AssertCodesEvalToSameValue(t, `4`, `let () = (); 4`)
AssertCodesEvalToSameValue(t, `4`, `let (a:x, b:y) = (a:4, b:7); x`)
AssertCodesEvalToSameValue(t, `4`, `let (a:x) = (b:7, a:4); x`)
AssertCodesEvalToSameValue(t, `4`, `let (a:x, b:x) = (a:4, b:4); x`)
AssertCodesEvalToSameValue(t, `4`, `let (a:x, a:x) = (a:4, a:4); x`)
AssertCodesEvalToSameValue(t, `4`, `let x = 4; let (a:x) = (a:4); x`)
AssertCodesEvalToSameValue(t, `4`, `let x = 5; let (a:x) = (a:4); x`)
AssertCodePanics(t, `let (a:x) = (b:7, a:4); x`)
AssertCodePanics(t, `let (a:x, a:x) = (a:4, a:4); x`)
AssertCodePanics(t, `let (a:x, a:x) = (a:4); x`)
AssertCodePanics(t, `let x = 5; let (a:(x)) = (a:4); x`)
AssertCodePanics(t, `let (a:x, b:x) = (a:4, b:7); x`)
}

0 comments on commit 452302e

Please sign in to comment.