59 lines
2.0 KiB
Scala
59 lines
2.0 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)
|
|
case Error.UndefinedIdentifier(ident, identType) =>
|
|
println(
|
|
s"Undefined ${identType.toString.toLowerCase()} ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
|
|
)
|
|
highlight(ident)
|
|
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(ident: ast.Ident)(using errorContent: String): Unit = {
|
|
val lines = errorContent.split("\n")
|
|
|
|
val preLine = if (ident.getPosition.line > 1) lines(ident.getPosition.line - 2) else ""
|
|
val midLine = lines(ident.getPosition.line - 1)
|
|
val postLine = lines(ident.getPosition.line)
|
|
val linePointer = " " * (ident.getPosition.column) + ("^" * (ident.v.length)) + "\n"
|
|
|
|
println(
|
|
s">$preLine\n>$midLine\n$linePointer>$postLine"
|
|
)
|
|
|
|
// var lines: Iterator[String] = errorContent.linesIterator
|
|
|
|
// lines = lines.drop(Math.max(0, ident.getPosition.line - 2))
|
|
// println(s">${lines.next()}")
|
|
// if (lines.hasNext)
|
|
// println(s">${lines.next()}")
|
|
// println(" " * (ident.getPosition.column) + ("^" * (ident.v.length)))
|
|
// if(lines.hasNext)
|
|
// println(s">${lines.next()}")
|
|
}
|