diff --git a/src/main/scala/millfork/compiler/mos/MosStatementCompiler.scala b/src/main/scala/millfork/compiler/mos/MosStatementCompiler.scala index 2faa8575..ad557b0a 100644 --- a/src/main/scala/millfork/compiler/mos/MosStatementCompiler.scala +++ b/src/main/scala/millfork/compiler/mos/MosStatementCompiler.scala @@ -198,11 +198,15 @@ object MosStatementCompiler extends AbstractStatementCompiler[AssemblyLine] { stackPointerFixBeforeReturn(ctx) ++ List(AssemblyLine.discardAF(), AssemblyLine.discardXF(), AssemblyLine.discardYF()) ++ returnInstructions case 1 => - ErrorReporting.warn("Returning without a value", ctx.options, statement.position) + if (statement.position.isDefined){ + ErrorReporting.warn("Returning without a value", ctx.options, statement.position) + } stackPointerFixBeforeReturn(ctx) ++ List(AssemblyLine.discardXF(), AssemblyLine.discardYF()) ++ returnInstructions case 2 => - ErrorReporting.warn("Returning without a value", ctx.options, statement.position) + if (statement.position.isDefined){ + ErrorReporting.warn("Returning without a value", ctx.options, statement.position) + } stackPointerFixBeforeReturn(ctx) ++ List(AssemblyLine.discardYF()) ++ returnInstructions } diff --git a/src/main/scala/millfork/error/ErrorReporting.scala b/src/main/scala/millfork/error/ErrorReporting.scala index 0662821b..664463a5 100644 --- a/src/main/scala/millfork/error/ErrorReporting.scala +++ b/src/main/scala/millfork/error/ErrorReporting.scala @@ -3,28 +3,32 @@ package millfork.error import millfork.{CompilationFlag, CompilationOptions} import millfork.node.Position +import scala.collection.mutable + object ErrorReporting { var verbosity = 0 var hasErrors = false - private var sourceLines: Option[IndexedSeq[String]] = None + private var sourceLines: mutable.Map[String, IndexedSeq[String]] = mutable.Map() - private def printErrorContext(pos: Option[Position]): Unit = { - if (sourceLines.isDefined && pos.isDefined) { - val line = sourceLines.get.apply(pos.get.line - 1) - val column = pos.get.column - 1 - val margin = " " - print(margin) - println(line) - print(margin) - print(" " * column) - println("^") + private def printErrorContext(pos: Option[Position]): Unit = synchronized { + pos.foreach { position => + sourceLines.get(position.moduleName).foreach { lines => + val line = lines.apply(pos.get.line - 1) + val column = pos.get.column - 1 + val margin = " " + print(margin) + println(line) + print(margin) + print(" " * column) + println("^") + } } } - def f(position: Option[Position]): String = position.fold("")(p => s"(${p.line}:${p.column}) ") + def f(position: Option[Position]): String = position.fold("")(p => s"(${p.moduleName}:${p.line}:${p.column}) ") def info(msg: String, position: Option[Position] = None): Unit = { if (verbosity < 0) return @@ -94,13 +98,18 @@ object ErrorReporting { } } - def clearErrors(): Unit = { + def clearErrors(): Unit = synchronized { hasErrors = false - sourceLines = None + sourceLines.clear() } - def setSource(source: Option[IndexedSeq[String]]): Unit = { - sourceLines = source + def setSource(source: Option[IndexedSeq[String]]): Unit = synchronized { + sourceLines.clear() + source.foreach(sourceLines("") = _) + } + + def addSource(filename: String, lines: IndexedSeq[String]): Unit = synchronized { + sourceLines(filename) = lines } } \ No newline at end of file diff --git a/src/main/scala/millfork/node/Node.scala b/src/main/scala/millfork/node/Node.scala index 90b769db..a032952a 100644 --- a/src/main/scala/millfork/node/Node.scala +++ b/src/main/scala/millfork/node/Node.scala @@ -4,7 +4,7 @@ import millfork.assembly.mos.{AddrMode, Opcode} import millfork.assembly.z80.{ZOpcode, ZRegisters} import millfork.env.{Constant, ParamPassingConvention, Type} -case class Position(filename: String, line: Int, column: Int, cursor: Int) +case class Position(moduleName: String, line: Int, column: Int, cursor: Int) sealed trait Node { var position: Option[Position] = None diff --git a/src/main/scala/millfork/parser/AbstractSourceLoadingQueue.scala b/src/main/scala/millfork/parser/AbstractSourceLoadingQueue.scala index 4905375f..56c29ca7 100644 --- a/src/main/scala/millfork/parser/AbstractSourceLoadingQueue.scala +++ b/src/main/scala/millfork/parser/AbstractSourceLoadingQueue.scala @@ -59,7 +59,9 @@ abstract class AbstractSourceLoadingQueue[T](val initialFilenames: List[String], val path = Paths.get(filename) val parentDir = path.toFile.getAbsoluteFile.getParent val (src, featureConstants) = Preprocessor(options, Files.readAllLines(path, StandardCharsets.UTF_8).toIndexedSeq) - val parser = createParser(filename, src, parentDir, featureConstants) + val shortFileName = path.getFileName.toString + val parser = createParser(shortFileName, src, parentDir, featureConstants) + ErrorReporting.addSource(shortFileName, src.lines.toIndexedSeq) parser.toAst match { case Success(prog, _) => parsedModules.synchronized { diff --git a/src/main/scala/millfork/parser/MfParser.scala b/src/main/scala/millfork/parser/MfParser.scala index 53a0abc9..16b3b54d 100644 --- a/src/main/scala/millfork/parser/MfParser.scala +++ b/src/main/scala/millfork/parser/MfParser.scala @@ -13,11 +13,11 @@ import millfork.{CompilationFlag, CompilationOptions, SeparatedList} /** * @author Karol Stasiak */ -abstract class MfParser[T](filename: String, input: String, currentDirectory: String, options: CompilationOptions, featureConstants: Map[String, Long]) { +abstract class MfParser[T](fileId: String, input: String, currentDirectory: String, options: CompilationOptions, featureConstants: Map[String, Long]) { import MfParser._ - var lastPosition = Position(filename, 1, 1, 0) + var lastPosition = Position(fileId, 1, 1, 0) var lastLabel = "" def toAst: Parsed[Program] = program.parse(input + "\n\n\n") @@ -33,7 +33,7 @@ abstract class MfParser[T](filename: String, input: String, currentDirectory: St } val columnNumber = i - lineStarts(lineNumber) lineNumber += 1 - val newPosition = Position(filename, lineNumber, columnNumber, i) + val newPosition = Position(fileId, lineNumber, columnNumber, i) if (newPosition.cursor > lastPosition.cursor) { lastPosition = newPosition lastLabel = label