feat: implements error messages for DuplicateDeclaration and UndefinedIdentifier errors
This commit is contained in:
@@ -12,3 +12,30 @@ enum Error {
|
||||
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)}")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user