Skip to content

Commit

Permalink
Improve checker for builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 27, 2020
1 parent 10df616 commit 5cb0507
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
52 changes: 28 additions & 24 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,60 +441,64 @@ func (v *visitor) BuiltinNode(node *ast.BuiltinNode) reflect.Type {

case "all", "none", "any", "one":
collection := v.visit(node.Arguments[0])
if !isArray(collection) {
panic(v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection))
}

v.collections = append(v.collections, collection)
closure := v.visit(node.Arguments[1])
v.collections = v.collections[:len(v.collections)-1]

if isArray(collection) {
if isFunc(closure) &&
closure.NumOut() == 1 && isBool(closure.Out(0)) &&
closure.NumIn() == 1 && isInterface(closure.In(0)) {

return boolType
if isFunc(closure) &&
closure.NumOut() == 1 &&
closure.NumIn() == 1 && isInterface(closure.In(0)) {

if !isBool(closure.Out(0)) {
panic(v.error(node.Arguments[1], "closure should return boolean (got %v)", closure.Out(0).String()))
}
panic(v.error(node.Arguments[1], "closure should return bool"))
return boolType
}
panic(v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection))
panic(v.error(node.Arguments[1], "closure should has one input and one output param"))

case "filter":
collection := v.visit(node.Arguments[0])
if !isArray(collection) {
panic(v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection))
}

v.collections = append(v.collections, collection)
closure := v.visit(node.Arguments[1])
v.collections = v.collections[:len(v.collections)-1]

if isArray(collection) {
if isFunc(closure) &&
closure.NumOut() == 1 && isBool(closure.Out(0)) &&
closure.NumIn() == 1 && isInterface(closure.In(0)) {

return collection
if isFunc(closure) &&
closure.NumOut() == 1 &&
closure.NumIn() == 1 && isInterface(closure.In(0)) {

if !isBool(closure.Out(0)) {
panic(v.error(node.Arguments[1], "closure should return boolean (got %v)", closure.Out(0).String()))
}
panic(v.error(node.Arguments[1], "closure should return bool"))
return arrayType
}
panic(v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection))
panic(v.error(node.Arguments[1], "closure should has one input and one output param"))

case "map":
collection := v.visit(node.Arguments[0])
if !isArray(collection) {
panic(v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection))
}

v.collections = append(v.collections, collection)
closure := v.visit(node.Arguments[1])
v.collections = v.collections[:len(v.collections)-1]

if isArray(collection) {
if isFunc(closure) &&
closure.NumOut() == 1 &&
closure.NumIn() == 1 && isInterface(closure.In(0)) {
if isFunc(closure) &&
closure.NumOut() == 1 &&
closure.NumIn() == 1 && isInterface(closure.In(0)) {

return reflect.ArrayOf(0, closure.Out(0))
return reflect.ArrayOf(0, closure.Out(0))

}
panic(v.error(node.Arguments[1], "closure should has one input and one output param"))
}
panic(v.error(node.Arguments[0], "builtin %v takes only array (got %v)", node.Name, collection))
panic(v.error(node.Arguments[1], "closure should has one input and one output param"))

case "count":
collection := v.visit(node.Arguments[0])
Expand Down
15 changes: 15 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,21 @@ count(ArrayOfInt, {#})
closure should return boolean (got int) (1:19)
| count(ArrayOfInt, {#})
| ..................^
all(ArrayOfInt, {# + 1})
closure should return boolean (got int) (1:17)
| all(ArrayOfInt, {# + 1})
| ................^
filter(ArrayOfFoo, {.Int64})
closure should return boolean (got int64) (1:20)
| filter(ArrayOfFoo, {.Int64})
| ...................^
map(1, {2})
builtin map takes only array (got int) (1:5)
| map(1, {2})
| ....^
`

func TestCheck_error(t *testing.T) {
Expand Down

0 comments on commit 5cb0507

Please sign in to comment.