feat: functional single-threaded imports

This commit is contained in:
2025-03-13 22:24:41 +00:00
parent ee54a1201c
commit 6e592e7d9b
10 changed files with 166 additions and 155 deletions

View File

@@ -36,7 +36,7 @@ extension (e: Error) {
* @param errorContent
* Contents of the file to generate code snippets
*/
def formatError(error: Error)(using errorContent: String): String = {
def formatError(error: Error): String = {
val sb = new StringBuilder()
/** Format the file of an error
@@ -66,7 +66,7 @@ def formatError(error: Error)(using errorContent: String): String = {
* Size(in chars) of section to highlight
*/
def formatHighlight(pos: Position, size: Int): Unit = {
val lines = errorContent.split("\n")
val lines = os.read(os.Path(pos.file.getCanonicalPath)).split("\n")
val preLine = if (pos.line > 1) lines(pos.line - 2) else ""
val midLine = lines(pos.line - 1)
val postLine = if (pos.line < lines.size) lines(pos.line) else ""
@@ -87,38 +87,38 @@ def formatError(error: Error)(using errorContent: String): String = {
error match {
case Error.DuplicateDeclaration(ident) =>
formatPosition(ident.pos)
sb.append(s"Duplicate declaration of identifier ${ident.v}")
sb.append(s"Duplicate declaration of identifier ${ident.v}\n")
formatHighlight(ident.pos, ident.v.length)
case Error.UndeclaredVariable(ident) =>
formatPosition(ident.pos)
sb.append(s"Undeclared variable ${ident.v}")
sb.append(s"Undeclared variable ${ident.v}\n")
formatHighlight(ident.pos, ident.v.length)
case Error.UndefinedFunction(ident) =>
formatPosition(ident.pos)
sb.append(s"Undefined function ${ident.v}")
sb.append(s"Undefined function ${ident.v}\n")
formatHighlight(ident.pos, ident.v.length)
case Error.FunctionParamsMismatch(id, expected, got, funcType) =>
formatPosition(id.pos)
sb.append(s"Function expects $expected parameters, got $got")
sb.append(s"Function expects $expected parameters, got $got\n")
sb.append(
s"(function ${id.v} has type (${funcType.params.mkString(", ")}) -> ${funcType.returnType})"
s"(function ${id.v} has type (${funcType.params.mkString(", ")}) -> ${funcType.returnType})\n"
)
formatHighlight(id.pos, 1)
case Error.TypeMismatch(pos, expected, got, msg) =>
formatPosition(pos)
sb.append(s"Type mismatch: $msg\nExpected: $expected\nGot: $got")
sb.append(s"Type mismatch: $msg\nExpected: $expected\nGot: $got\n")
formatHighlight(pos, 1)
case Error.SemanticError(pos, msg) =>
formatPosition(pos)
sb.append(msg)
sb.append(msg + "\n")
formatHighlight(pos, 1)
case wacc.Error.InternalError(pos, msg) =>
formatPosition(pos)
sb.append(s"Internal error: $msg")
sb.append(s"Internal error: $msg\n")
formatHighlight(pos, 1)
case Error.SyntaxError(file, msg) =>
formatFile(file)
sb.append(msg)
sb.append(msg + "\n")
sb.append("\n")
}