Added return count mismatch error

This commit is contained in:
2025-02-03 13:13:17 +01:00
parent 715635aaa7
commit 646dafd216
4 changed files with 35 additions and 1 deletions

View File

@ -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])

View 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)
}