refactor: consistent error handling in Main.scala
This commit is contained in:
@@ -3,6 +3,9 @@ package wacc
|
||||
import wacc.ast.Position
|
||||
import wacc.types._
|
||||
|
||||
private val SYNTAX_ERROR = 100
|
||||
private val SEMANTIC_ERROR = 200
|
||||
|
||||
/** Error types for semantic errors
|
||||
*/
|
||||
enum Error {
|
||||
@@ -14,6 +17,15 @@ enum Error {
|
||||
case SemanticError(pos: Position, msg: String)
|
||||
case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String)
|
||||
case InternalError(pos: Position, msg: String)
|
||||
|
||||
case SyntaxError(msg: String)
|
||||
}
|
||||
|
||||
extension (e: Error) {
|
||||
def exitCode: Int = e match {
|
||||
case Error.SyntaxError(_) => SYNTAX_ERROR
|
||||
case _ => SEMANTIC_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
/** Function to handle printing the details of a given semantic error
|
||||
@@ -25,7 +37,6 @@ enum Error {
|
||||
*/
|
||||
def formatError(error: Error)(using errorContent: String): String = {
|
||||
val sb = new StringBuilder()
|
||||
sb.append("Semantic error:\n")
|
||||
|
||||
/** Function to format the position of an error
|
||||
*
|
||||
@@ -55,6 +66,13 @@ def formatError(error: Error)(using errorContent: String): String = {
|
||||
)
|
||||
}
|
||||
|
||||
error match {
|
||||
case Error.SyntaxError(_) =>
|
||||
sb.append("Syntax error:\n")
|
||||
case _ =>
|
||||
sb.append("Semantic error:\n")
|
||||
}
|
||||
|
||||
error match {
|
||||
case Error.DuplicateDeclaration(ident) =>
|
||||
formatPosition(ident.pos)
|
||||
@@ -87,6 +105,9 @@ def formatError(error: Error)(using errorContent: String): String = {
|
||||
formatPosition(pos)
|
||||
sb.append(s"Internal error: $msg")
|
||||
formatHighlight(pos, 1)
|
||||
case Error.SyntaxError(msg) =>
|
||||
sb.append(msg)
|
||||
sb.append("\n")
|
||||
}
|
||||
|
||||
sb.toString()
|
||||
|
||||
Reference in New Issue
Block a user