Skip to content

Commit

Permalink
Add more error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
peachpit-site committed Jan 31, 2025
1 parent cbdde93 commit 90b5223
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 12 deletions.
16 changes: 7 additions & 9 deletions source/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ NodeTypeSwitch:
leftTypes, rtnConst = cp.CompileNode(node.Args[0], ctxt.x())
overlap := leftTypes.intersect(cp.Common.SharedTypenameToTypeList["listlike"])
if len(overlap) == 0 {
cp.P.Throw("comp/splat/types", node.GetToken(), leftTypes)
cp.P.Throw("comp/splat/type", node.GetToken(), leftTypes)
rtnTypes = altType(values.COMPILE_TIME_ERROR)
break NodeTypeSwitch
}
Expand Down Expand Up @@ -1753,7 +1753,7 @@ func (cp *Compiler) getPartsOfGiven(given ast.Node, ctxt Context) []ast.Node {
rhs := cp.getPartsOfGiven(branch.Right, ctxt)
result = append(result, rhs...)
} else {
cp.P.Throw("comp/unexpected", given.GetToken())
cp.P.Throw("comp/given/unexpected", given.GetToken())
}
default:
result = []ast.Node{given}
Expand Down Expand Up @@ -2276,7 +2276,7 @@ func (cp *Compiler) EmitTypeChecks(loc uint32, types AlternateType, env *Environ
acceptedSingles := AlternateType{}
lastIsTuple := sig.Len() > 0 && cp.getTypes(sig, sig.Len()-1).containsOnlyTuples()
if types.isOnly(values.ERROR) {
cp.P.Throw("comp/typecheck/a", tok)
cp.P.Throw("comp/typecheck/error", tok)
return errorCheck
}
if types.Contains(values.ERROR) {
Expand All @@ -2288,11 +2288,9 @@ func (cp *Compiler) EmitTypeChecks(loc uint32, types AlternateType, env *Environ
acceptedSingles = singles.intersect(cp.getTypes(sig, 0))
}
checkSingleType := bkGoto(DUMMY)
if len(tuples) == 0 {
if sig.Len() != 1 {
cp.P.Throw("comp/typecheck/b", tok)
if len(singles) == 0 && sig.Len() != 1 {
cp.P.Throw("comp/typecheck/values/a", tok)
return errorCheck
}
}
if len(acceptedSingles) != len(singles) {
checkSingleType = cp.emitTypeComparison(sig.GetVarType(0), loc, tok)
Expand Down Expand Up @@ -2337,7 +2335,7 @@ func (cp *Compiler) EmitTypeChecks(loc uint32, types AlternateType, env *Environ
}
}
if badLengths == len(lengths) {
cp.P.Throw("comp/typecheck/c", tok)
cp.P.Throw("comp/typecheck/values/b", tok)
return errorCheck
}

Expand All @@ -2361,7 +2359,7 @@ func (cp *Compiler) EmitTypeChecks(loc uint32, types AlternateType, env *Environ
sigTypes := cp.getTypes(sig, i)
overlap := typesToCheck.intersect(sigTypes)
if len(overlap) == 0 || overlap.isOnly(values.ERROR) {
cp.P.Throw("comp/typecheck/d", tok)
cp.P.Throw("comp/typecheck/type", tok)
return errorCheck
}
if len(overlap) == len(typesToCheck) {
Expand Down
109 changes: 106 additions & 3 deletions source/err/errorfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"comp/given/unexpected": {
Message: func(tok *token.Token, args ...any) string {
return "unexpected occurrence of " + emph(tok.Literal) + " in 'given' block"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "Pipefish has no idea what this is doing here."
},
},

"comp/global/global": {
Message: func(tok *token.Token, args ...any) string {
return "identifier " + emph(tok.Literal) + " doesn't identify a global variable"
Expand Down Expand Up @@ -756,6 +765,15 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"comp/pipe/mf/func": {
Message: func(tok *token.Token, args ...any) string {
return "trying to use " + emph(tok.Literal) + " as a function"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "Pipefish was expecting an expression that it could pipe the left-hand side of the piping operator into."
},
},

"comp/pipe/mf/ident": {
Message: func(tok *token.Token, args ...any) string {
return "unexpected occurrence of " + emph(tok.Literal)
Expand All @@ -765,12 +783,14 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"comp/pipe/mf/func": {
"comp/pipe/mf/list": {
Message: func(tok *token.Token, args ...any) string {
return "trying to use " + emph(tok.Literal) + " as a function"
return "lhs of piping operator is not a list"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "Pipefish was expecting an expression that it could pipe the left-hand side of the piping operator into."
return "Pipefish was expecting a list on the left-hand side of the " +
"piping operator but the typechecker has determined that it can never " +
"be one."
},
},

Expand Down Expand Up @@ -846,6 +866,16 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"comp/return/length": {
Message: func(tok *token.Token, args ...any) string {
return "return has wrong number of values"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "You have given a return type to your function constraining the number of " +
"values it can return, and then tried to return the wrong number of values."
},
},

"comp/return/types": {
Message: func(tok *token.Token, args ...any) string {
return "return " + redescribeType(args[0].(string)) + " cannot satisfy specifed return " + redescribeType(args[1].(string))
Expand Down Expand Up @@ -882,6 +912,25 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"comp/splat/args": {
Message: func(tok *token.Token, args ...any) string {
return "too many arguments for '...'"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "The splat operator '...' takes only one argument, on its left."
},
},

"comp/splat/type": {
Message: func(tok *token.Token, args ...any) string {
return "argument of '...' must be list or listlike, not " + emph(args[0])
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "The purpose of the splat operator '...' is to expand a list or " +
"listlike value into a tuple. You're applying it to the wrong type."
},
},

"comp/snippet/form/a": {
Message: func(tok *token.Token, args ...any) string {
return "unmatched " + emph("|") + " in snippet constructor"
Expand Down Expand Up @@ -927,6 +976,60 @@ var ErrorCreatorMap = map[string]ErrorCreator{
},
},

"comp/typecheck/error": {
Message: func(tok *token.Token, args ...any) string {
return "expression can only have error type"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "When Pipefish typechecks something, it is permissible at compile-time " +
"for that thing to contain an error not declared in the type annotations: " +
"that would be a runtime error. However, it is a compile-time error for this " +
"always to be the case, and the typechecker has determined that this is " +
"one of those situations."
},
},

"comp/typecheck/type": {
Message: func(tok *token.Token, args ...any) string {
return "wrong number of arguments"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "The typechecker has found a situation where your code can only ever " +
"pass/return the wrong number of arguments."
},
},

"comp/typecheck/values/a": {
Message: func(tok *token.Token, args ...any) string {
return "type error"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "The typechecker has determined that this condition can never be " +
"fulfilled by your code as written."
},
},

"comp/typecheck/values/b": {
Message: func(tok *token.Token, args ...any) string {
return "trying to assign multiple values to non-tuple type"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "The typechecker has determined that you're trying to squeeze more " +
"than one value into a place where your type signature says that only " +
"one will go"
},
},

"comp/typecheck/values/c": {
Message: func(tok *token.Token, args ...any) string {
return "wrong number of arguments"
},
Explanation: func(errors Errors, pos int, tok *token.Token, args ...any) string {
return "The typechecker has found a situation where your code can only ever " +
"pass/return the wrong number of arguments."
},
},

"comp/try/return": {
Message: func(tok *token.Token, args ...any) string {
return "trying to return value from " + emph("try") + " expression"
Expand Down

0 comments on commit 90b5223

Please sign in to comment.