diff --git a/src/main/wacc/Error.scala b/src/main/wacc/Error.scala index 9d60b2a..a3a5f1e 100644 --- a/src/main/wacc/Error.scala +++ b/src/main/wacc/Error.scala @@ -17,19 +17,31 @@ def printError(error: Error)(using errorContent: String): Unit = { println("Semantic error:") error match { case Error.DuplicateDeclaration(ident) => + printPosition(ident.getPosition) println( - s"Duplicate declaration of identifier ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}" + s"Duplicate declaration of identifier ${ident.v}" ) highlight(ident.getPosition, ident.v.length) case Error.UndefinedIdentifier(ident, identType) => + printPosition(ident.getPosition) println( - s"Undefined ${identType.toString.toLowerCase()} ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}" + s"Undefined ${identType.toString.toLowerCase()} ${ident.v}" ) highlight(ident.getPosition, ident.v.length) case Error.FunctionParamsMismatch(ident, expected, got) => + printPosition(ident.getPosition) println(s"Function ${ident.v} expects $expected parameters, got $got") + highlight(ident.getPosition, ident.v.length) case Error.TypeMismatch(expected, got) => println(s"Type mismatch: expected $expected, got $got") + case Error.SemanticError(pos, msg) => + printPosition(pos) + println(s"$msg at line: ${pos.line} column: ${pos.column}") + highlight(pos, 1) + case wacc.Error.InternalError(pos, msg) => + printPosition(pos) + println(s"Internal error: $msg") + highlight(pos, 1) } } @@ -39,20 +51,14 @@ def highlight(pos: Position, size: Int)(using errorContent: String): Unit = { val preLine = if (pos.line > 1) lines(pos.line - 2) else "" val midLine = lines(pos.line - 1) - val postLine = lines(pos.line) - val linePointer = " " * (pos.column) + ("^" * (size)) + "\n" + val postLine = if (pos.line < lines.size) lines(pos.line) else "" + val linePointer = " " * (pos.column + 2) + ("^" * (size)) + "\n" println( - s">$preLine\n>$midLine\n$linePointer>$postLine" + 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()}") +} + +def printPosition(pos: Position): Unit = { + println(s"(line ${pos.line}, column ${pos.column}):") }