Switch statement analysis passes tests
This commit is contained in:
parent
f3bdfef5c5
commit
4931f97496
|
@ -475,7 +475,7 @@ func isNumeric (ty entity.Type) bool {
|
|||
}
|
||||
}
|
||||
|
||||
// isInteger returns whether or not the specified type is an integer.
|
||||
// isInteger returns whether or not the specified type is an integer or word.
|
||||
func isInteger (ty entity.Type) bool {
|
||||
switch ReduceToBase(ty).(type) {
|
||||
case *entity.TypeInt, *entity.TypeWord: return true
|
||||
|
@ -483,6 +483,14 @@ func isInteger (ty entity.Type) bool {
|
|||
}
|
||||
}
|
||||
|
||||
// isWord returns whether or not the specified type is a word.
|
||||
func isWord (ty entity.Type) bool {
|
||||
switch ReduceToBase(ty).(type) {
|
||||
case *entity.TypeWord: return true
|
||||
default: return false
|
||||
}
|
||||
}
|
||||
|
||||
// isPointer returns whether or not the specified type is a pointer.
|
||||
func isPointer (ty entity.Type) bool {
|
||||
switch ReduceToBase(ty).(type) {
|
||||
|
|
|
@ -815,7 +815,8 @@ func (this *Tree) analyzeSwitch (
|
|||
if err != nil { return nil, err }
|
||||
switc.Value = value
|
||||
width, integerOk := integerWidth(value.Type())
|
||||
if !integerOk {
|
||||
wordOk := isWord(value.Type())
|
||||
if !integerOk && !wordOk {
|
||||
return nil, errors.Errorf (
|
||||
value.Position(), "cannot switch on type %v",
|
||||
value.Type())
|
||||
|
@ -843,6 +844,14 @@ func (this *Tree) analyzeSwitch (
|
|||
case *entity.LiteralInt:
|
||||
keyInt = int64(key.Value)
|
||||
case *entity.LiteralString:
|
||||
if !integerOk {
|
||||
return nil, errors.Errorf (
|
||||
key.Position(),
|
||||
"cannot use string literal when " +
|
||||
"switching on %v",
|
||||
value.Type())
|
||||
}
|
||||
|
||||
switch {
|
||||
case width >= 32:
|
||||
keyInt = int64(key.ValueUTF32[0])
|
||||
|
@ -852,9 +861,17 @@ func (this *Tree) analyzeSwitch (
|
|||
keyInt = int64(key.ValueUTF8[0])
|
||||
}
|
||||
default:
|
||||
return nil, errors.Errorf (
|
||||
key.Position(), "%v cannot represent a constant integer",
|
||||
key)
|
||||
return nil, errors.Errorf (
|
||||
key.Position(),
|
||||
"%v cannot represent a constant integer",
|
||||
key)
|
||||
}
|
||||
|
||||
if previous, exists := switc.CaseMap[keyInt]; exists {
|
||||
return nil, errors.Errorf (
|
||||
cas.Key.Position(),
|
||||
"%v already listed in match at %v",
|
||||
keyInt, previous.Position())
|
||||
}
|
||||
|
||||
switc.Cases[index] = cas
|
||||
|
@ -873,7 +890,7 @@ func (this *Tree) analyzeSwitch (
|
|||
switc.Default.Expression = expression
|
||||
}
|
||||
|
||||
if into != nil && switc.Default != nil {
|
||||
if into != nil && switc.Default == nil {
|
||||
return nil, errors.Errorf (
|
||||
switc.Position(),
|
||||
"switch must have a default case")
|
||||
|
|
Loading…
Reference in New Issue