42 lines
1.6 KiB
Scala
42 lines
1.6 KiB
Scala
package wacc
|
|
|
|
import wacc.ast.Position
|
|
import wacc.types._
|
|
|
|
enum Error {
|
|
case DuplicateDeclaration(ident: ast.Ident)
|
|
case UndefinedIdentifier(ident: ast.Ident, identType: renamer.IdentType)
|
|
|
|
case FunctionParamsMismatch(pos: Position, expected: Int, got: Int)
|
|
case SemanticError(pos: Position, msg: String)
|
|
case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String)
|
|
case InternalError(pos: Position, msg: String)
|
|
}
|
|
|
|
def printError(error: Error)(using errorContent: String): Unit = {
|
|
println("Semantic error:")
|
|
error match {
|
|
case Error.DuplicateDeclaration(ident) =>
|
|
println(
|
|
s"Duplicate declaration of identifier ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
|
|
)
|
|
highlight(ident.getPosition.line, ident.getPosition.column, ident.v.length)
|
|
case Error.UndefinedIdentifier(ident, identType) =>
|
|
println(
|
|
s"Undefined ${identType.toString.toLowerCase} ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
|
|
)
|
|
highlight(ident.getPosition.line, ident.getPosition.column, ident.v.length)
|
|
case Error.FunctionParamsMismatch(ident, expected, got) =>
|
|
println(s"Function ${ident.v} expects $expected parameters, got $got")
|
|
case Error.TypeMismatch(expected, got) =>
|
|
println(s"Type mismatch: expected $expected, got $got")
|
|
}
|
|
|
|
}
|
|
|
|
def highlight(line: Int, column: Int, size: Int)(using errorContent: String): Unit = {
|
|
val lines = errorContent.split("\n")
|
|
val linePointer = " " * (column) + ("^" * (size)) + "\n"
|
|
println(s">${lines(line - 2)}\n>${lines(line - 1)}\n$linePointer>${lines(line)}")
|
|
}
|