Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type check of binary node #11

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion type.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,24 @@ func (n binaryNode) Type(table typesTable) (Type, error) {
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)

case "|", "^", "&", "<", ">", ">=", "<=", "+", "-", "*", "/", "%", "**", "..":
case "<", ">", ">=", "<=":
if (isNumberType(ltype) || isInterfaceType(ltype)) && (isNumberType(rtype) || isInterfaceType(rtype)) {
return boolType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)

case "/", "+", "-", "*", "**", "|", "^", "&", "%":
if (isNumberType(ltype) || isInterfaceType(ltype)) && (isNumberType(rtype) || isInterfaceType(rtype)) {
return numberType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)

case "..":
if (isNumberType(ltype) || isInterfaceType(ltype)) && (isNumberType(rtype) || isInterfaceType(rtype)) {
return arrayType, nil
}
return nil, fmt.Errorf(`invalid operation: %v (mismatched types %v and %v)`, n, ltype, rtype)

}

return interfaceType, nil
Expand Down
3 changes: 3 additions & 0 deletions type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ var typeTests = []typeTest{
"Int % Num",
"Int ** Num",
"Int .. Num",
"Int + Int + Int",
"Int % Int > 1",
"Int in Int..Int",
}

var typeErrorTests = []typeErrorTest{
Expand Down