Skip to content

Commit

Permalink
types: Add IsInteger function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed May 3, 2016
1 parent 09888fa commit c8f6f7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions sem/typecheck/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ func check(file *ast.File, exprTypes map[ast.Expr]types.Type) error {
if !ok {
panic(fmt.Sprintf("unable to locate type of expression %v", n.Index))
}
num, ok := indexType.(types.Numerical)
if !ok || !num.IsNumerical() {
if !types.IsInteger(indexType) {
return errors.Newf(n.Index.Start(), "invalid array index; expected integer, got %q", indexType)
}
default:
Expand Down
12 changes: 12 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ func IsVoid(t Type) bool {
return false
}

// IsInteger reports whether the given type is an integer (i.e. "int" or
// "char").
func IsInteger(t Type) bool {
if t, ok := t.(*Basic); ok {
switch t.Kind {
case Int, Char:
return true
}
}
return false
}

// IsNumerical reports whether the given type is numerical.
func (t *Basic) IsNumerical() bool {
switch t.Kind {
Expand Down

0 comments on commit c8f6f7a

Please sign in to comment.