diff --git a/sem/typecheck/typecheck.go b/sem/typecheck/typecheck.go index 15775d2..a152090 100644 --- a/sem/typecheck/typecheck.go +++ b/sem/typecheck/typecheck.go @@ -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: diff --git a/types/types.go b/types/types.go index 1279f91..f50a489 100644 --- a/types/types.go +++ b/types/types.go @@ -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 {