mirror of
https://github.com/irmen/prog8.git
synced 2025-02-19 11:31:07 +00:00
reshuffle ErrorReporter
This commit is contained in:
parent
495a18805c
commit
1137da37c3
@ -274,7 +274,6 @@ class AsmGen(private val program: Program,
|
|||||||
&& variable.datatype != DataType.FLOAT
|
&& variable.datatype != DataType.FLOAT
|
||||||
&& options.zeropage != ZeropageType.DONTUSE) {
|
&& options.zeropage != ZeropageType.DONTUSE) {
|
||||||
try {
|
try {
|
||||||
val errors = ErrorReporter() // TODO why not just use this.errors? then we can clean up the visibility of ErrorReporter class again too
|
|
||||||
val address = zeropage.allocate(fullName, variable.datatype, null, errors)
|
val address = zeropage.allocate(fullName, variable.datatype, null, errors)
|
||||||
errors.report()
|
errors.report()
|
||||||
out("${variable.name} = $address\t; auto zp ${variable.datatype}")
|
out("${variable.name} = $address\t; auto zp ${variable.datatype}")
|
||||||
|
@ -21,6 +21,7 @@ import prog8.compilerinterface.*
|
|||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
import prog8tests.asmgen.helpers.DummyFunctions
|
import prog8tests.asmgen.helpers.DummyFunctions
|
||||||
import prog8tests.asmgen.helpers.DummyMemsizer
|
import prog8tests.asmgen.helpers.DummyMemsizer
|
||||||
|
import prog8tests.asmgen.helpers.ErrorReporterForTests
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
|
||||||
@ -79,7 +80,7 @@ locallabel:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createTestAsmGen(program: Program): AsmGen {
|
private fun createTestAsmGen(program: Program): AsmGen {
|
||||||
val errors = ErrorReporter()
|
val errors = ErrorReporterForTests()
|
||||||
val options = CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FULL, emptyList(), false, true, C64Target)
|
val options = CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.FULL, emptyList(), false, true, C64Target)
|
||||||
val zp = C64MachineDefinition.C64Zeropage(options)
|
val zp = C64MachineDefinition.C64Zeropage(options)
|
||||||
val asmgen = AsmGen(program, errors, zp, options, C64Target, Path.of(""))
|
val asmgen = AsmGen(program, errors, zp, options, C64Target, Path.of(""))
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
package prog8tests.asmgen
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import org.junit.jupiter.api.TestInstance
|
|
||||||
import kotlin.test.fail
|
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
||||||
class TestCodeGeneration {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Disabled("for future implementation")
|
|
||||||
fun dummy() {
|
|
||||||
fail("dummy")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
28
codeGeneration/test/helpers/ErrorReporterForTests.kt
Normal file
28
codeGeneration/test/helpers/ErrorReporterForTests.kt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package prog8tests.asmgen.helpers
|
||||||
|
|
||||||
|
import prog8.ast.base.Position
|
||||||
|
import prog8.compilerinterface.IErrorReporter
|
||||||
|
|
||||||
|
internal class ErrorReporterForTests(private val throwExceptionAtReportIfErrors: Boolean=true): IErrorReporter {
|
||||||
|
|
||||||
|
|
||||||
|
val errors = mutableListOf<String>()
|
||||||
|
val warnings = mutableListOf<String>()
|
||||||
|
|
||||||
|
override fun err(msg: String, position: Position) {
|
||||||
|
errors.add("${position.toClickableStr()} $msg")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun warn(msg: String, position: Position) {
|
||||||
|
warnings.add("${position.toClickableStr()} $msg")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun noErrors(): Boolean = errors.isEmpty()
|
||||||
|
|
||||||
|
override fun report() {
|
||||||
|
if(throwExceptionAtReportIfErrors)
|
||||||
|
finalizeNumErrors(errors.size, warnings.size)
|
||||||
|
errors.clear()
|
||||||
|
warnings.clear()
|
||||||
|
}
|
||||||
|
}
|
47
compiler/src/prog8/compiler/ErrorReporter.kt
Normal file
47
compiler/src/prog8/compiler/ErrorReporter.kt
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package prog8.compiler
|
||||||
|
|
||||||
|
import prog8.ast.base.Position
|
||||||
|
import prog8.compilerinterface.IErrorReporter
|
||||||
|
|
||||||
|
internal class ErrorReporter: IErrorReporter {
|
||||||
|
private enum class MessageSeverity {
|
||||||
|
WARNING,
|
||||||
|
ERROR
|
||||||
|
}
|
||||||
|
private class CompilerMessage(val severity: MessageSeverity, val message: String, val position: Position)
|
||||||
|
|
||||||
|
private val messages = mutableListOf<CompilerMessage>()
|
||||||
|
private val alreadyReportedMessages = mutableSetOf<String>()
|
||||||
|
|
||||||
|
override fun err(msg: String, position: Position) {
|
||||||
|
messages.add(CompilerMessage(MessageSeverity.ERROR, msg, position))
|
||||||
|
}
|
||||||
|
override fun warn(msg: String, position: Position) {
|
||||||
|
messages.add(CompilerMessage(MessageSeverity.WARNING, msg, position))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun report() {
|
||||||
|
var numErrors = 0
|
||||||
|
var numWarnings = 0
|
||||||
|
messages.forEach {
|
||||||
|
when(it.severity) {
|
||||||
|
MessageSeverity.ERROR -> System.err.print("\u001b[91m") // bright red
|
||||||
|
MessageSeverity.WARNING -> System.err.print("\u001b[93m") // bright yellow
|
||||||
|
}
|
||||||
|
val msg = "${it.position.toClickableStr()} ${it.severity} ${it.message}".trim()
|
||||||
|
if(msg !in alreadyReportedMessages) {
|
||||||
|
System.err.println(msg)
|
||||||
|
alreadyReportedMessages.add(msg)
|
||||||
|
when(it.severity) {
|
||||||
|
MessageSeverity.WARNING -> numWarnings++
|
||||||
|
MessageSeverity.ERROR -> numErrors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.err.print("\u001b[0m") // reset color
|
||||||
|
}
|
||||||
|
messages.clear()
|
||||||
|
finalizeNumErrors(numErrors, numWarnings)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun noErrors() = messages.none { it.severity==MessageSeverity.ERROR }
|
||||||
|
}
|
@ -6,9 +6,9 @@ import prog8.ast.internedStringsModuleName
|
|||||||
import prog8.compiler.determineCompilationOptions
|
import prog8.compiler.determineCompilationOptions
|
||||||
import prog8.compiler.parseImports
|
import prog8.compiler.parseImports
|
||||||
import prog8.compiler.target.C64Target
|
import prog8.compiler.target.C64Target
|
||||||
import prog8.compilerinterface.ErrorReporter
|
|
||||||
import prog8.compilerinterface.ZeropageType
|
import prog8.compilerinterface.ZeropageType
|
||||||
import prog8tests.ast.helpers.outputDir
|
import prog8tests.ast.helpers.outputDir
|
||||||
|
import prog8tests.helpers.ErrorReporterForTests
|
||||||
import prog8tests.helpers.assertSuccess
|
import prog8tests.helpers.assertSuccess
|
||||||
import prog8tests.helpers.compileText
|
import prog8tests.helpers.compileText
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
@ -70,7 +70,7 @@ main {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testModuleOrderAndCompilationOptionsCorrectWithJustImports() {
|
fun testModuleOrderAndCompilationOptionsCorrectWithJustImports() {
|
||||||
val errors = ErrorReporter()
|
val errors = ErrorReporterForTests()
|
||||||
val sourceText = """
|
val sourceText = """
|
||||||
%import textio
|
%import textio
|
||||||
%import floats
|
%import floats
|
||||||
|
@ -8,6 +8,7 @@ import prog8.compiler.target.Cx16Target
|
|||||||
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
|
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
|
||||||
import prog8.compiler.target.cx16.CX16MachineDefinition.CX16Zeropage
|
import prog8.compiler.target.cx16.CX16MachineDefinition.CX16Zeropage
|
||||||
import prog8.compilerinterface.*
|
import prog8.compilerinterface.*
|
||||||
|
import prog8tests.helpers.ErrorReporterForTests
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertFailsWith
|
import kotlin.test.assertFailsWith
|
||||||
import kotlin.test.assertFalse
|
import kotlin.test.assertFalse
|
||||||
@ -17,7 +18,7 @@ import kotlin.test.assertTrue
|
|||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class TestC64Zeropage {
|
class TestC64Zeropage {
|
||||||
|
|
||||||
private val errors = ErrorReporter()
|
private val errors = ErrorReporterForTests()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testNames() {
|
fun testNames() {
|
||||||
@ -216,7 +217,7 @@ class TestC64Zeropage {
|
|||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class TestCx16Zeropage {
|
class TestCx16Zeropage {
|
||||||
private val errors = ErrorReporter()
|
private val errors = ErrorReporterForTests()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testReservedLocations() {
|
fun testReservedLocations() {
|
||||||
|
@ -3,7 +3,7 @@ package prog8tests.helpers
|
|||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
import prog8.compilerinterface.IErrorReporter
|
import prog8.compilerinterface.IErrorReporter
|
||||||
|
|
||||||
class ErrorReporterForTests(private val throwExceptionAtReportIfErrors: Boolean=true): IErrorReporter {
|
internal class ErrorReporterForTests(private val throwExceptionAtReportIfErrors: Boolean=true): IErrorReporter {
|
||||||
|
|
||||||
|
|
||||||
val errors = mutableListOf<String>()
|
val errors = mutableListOf<String>()
|
||||||
|
@ -2,7 +2,6 @@ package prog8tests.helpers
|
|||||||
|
|
||||||
import prog8.compiler.CompilationResult
|
import prog8.compiler.CompilationResult
|
||||||
import prog8.compiler.compileProgram
|
import prog8.compiler.compileProgram
|
||||||
import prog8.compilerinterface.ErrorReporter
|
|
||||||
import prog8.compilerinterface.ICompilationTarget
|
import prog8.compilerinterface.ICompilationTarget
|
||||||
import prog8.compilerinterface.IErrorReporter
|
import prog8.compilerinterface.IErrorReporter
|
||||||
import prog8tests.ast.helpers.assumeReadableFile
|
import prog8tests.ast.helpers.assumeReadableFile
|
||||||
@ -46,7 +45,7 @@ internal fun compileFile(
|
|||||||
platform.name,
|
platform.name,
|
||||||
sourceDirs = listOf(),
|
sourceDirs = listOf(),
|
||||||
outputDir,
|
outputDir,
|
||||||
errors = errors ?: ErrorReporter()
|
errors = errors ?: ErrorReporterForTests()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,46 +15,3 @@ interface IErrorReporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class ErrorReporter: IErrorReporter {
|
|
||||||
private enum class MessageSeverity {
|
|
||||||
WARNING,
|
|
||||||
ERROR
|
|
||||||
}
|
|
||||||
private class CompilerMessage(val severity: MessageSeverity, val message: String, val position: Position)
|
|
||||||
|
|
||||||
private val messages = mutableListOf<CompilerMessage>()
|
|
||||||
private val alreadyReportedMessages = mutableSetOf<String>()
|
|
||||||
|
|
||||||
override fun err(msg: String, position: Position) {
|
|
||||||
messages.add(CompilerMessage(MessageSeverity.ERROR, msg, position))
|
|
||||||
}
|
|
||||||
override fun warn(msg: String, position: Position) {
|
|
||||||
messages.add(CompilerMessage(MessageSeverity.WARNING, msg, position))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun report() {
|
|
||||||
var numErrors = 0
|
|
||||||
var numWarnings = 0
|
|
||||||
messages.forEach {
|
|
||||||
when(it.severity) {
|
|
||||||
MessageSeverity.ERROR -> System.err.print("\u001b[91m") // bright red
|
|
||||||
MessageSeverity.WARNING -> System.err.print("\u001b[93m") // bright yellow
|
|
||||||
}
|
|
||||||
val msg = "${it.position.toClickableStr()} ${it.severity} ${it.message}".trim()
|
|
||||||
if(msg !in alreadyReportedMessages) {
|
|
||||||
System.err.println(msg)
|
|
||||||
alreadyReportedMessages.add(msg)
|
|
||||||
when(it.severity) {
|
|
||||||
MessageSeverity.WARNING -> numWarnings++
|
|
||||||
MessageSeverity.ERROR -> numErrors++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.err.print("\u001b[0m") // reset color
|
|
||||||
}
|
|
||||||
messages.clear()
|
|
||||||
finalizeNumErrors(numErrors, numWarnings)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun noErrors() = messages.none { it.severity==MessageSeverity.ERROR }
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user