Added return count mismatch error
This commit is contained in:
@ -14,6 +14,10 @@ func (f *Function) CompileReturn(node *ast.Return) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(node.Values) != len(f.ReturnTypes) {
|
||||
return errors.New(&errors.ReturnCountMismatch{Count: len(node.Values), ExpectedCount: len(f.ReturnTypes)}, f.File, node.Values[0].Token.Position)
|
||||
}
|
||||
|
||||
for i := len(node.Values) - 1; i >= 0; i-- {
|
||||
typ, err := f.ExpressionToRegister(node.Values[i], f.CPU.Output[i])
|
||||
|
||||
|
26
src/errors/ReturnCountMismatch.go
Normal file
26
src/errors/ReturnCountMismatch.go
Normal file
@ -0,0 +1,26 @@
|
||||
package errors
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ReturnCountMismatch error is created when the number of returned values doesn't match the return type.
|
||||
type ReturnCountMismatch struct {
|
||||
Count int
|
||||
ExpectedCount int
|
||||
}
|
||||
|
||||
// Error generates the string representation.
|
||||
func (err *ReturnCountMismatch) Error() string {
|
||||
values := "values"
|
||||
|
||||
if err.Count == 1 {
|
||||
values = "value"
|
||||
}
|
||||
|
||||
types := "types"
|
||||
|
||||
if err.ExpectedCount == 1 {
|
||||
types = "type"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Returns %d %s in a function with %d return %s", err.Count, values, err.ExpectedCount, types)
|
||||
}
|
Reference in New Issue
Block a user