From 5540482888fee154858b402ace0b758b6c62fbb0 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 21 Feb 2021 21:26:15 +0100 Subject: [PATCH] compiler error for duplicate when choice labels --- .../prog8/compiler/astprocessing/AstChecker.kt | 16 +++++++++++----- docs/source/todo.rst | 2 -- examples/test.p8 | 17 +++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 01aab09a7..91ce39ab8 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -1141,12 +1141,18 @@ internal class AstChecker(private val program: Program, val conditionType = whenStatement.condition.inferType(program).typeOrElse(DataType.STRUCT) if(conditionType !in IntegerDatatypes) errors.err("when condition must be an integer value", whenStatement.position) - val choiceValues = whenStatement.choiceValues(program) - val occurringValues = choiceValues.map {it.first} - val tally = choiceValues.associate { it.second to occurringValues.count { ov->it.first==ov} } - tally.filter { it.value>1 }.forEach { - errors.err("choice value occurs multiple times", it.key.position) + val tally = mutableSetOf() + for((choices, choiceNode) in whenStatement.choiceValues(program)) { + if(choices!=null) { + for (c in choices) { + if(c in tally) + errors.err("choice value already occurs earlier", choiceNode.position) + else + tally.add(c) + } + } } + if(whenStatement.choices.isEmpty()) errors.err("empty when statement", whenStatement.position) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 50a4c48b5..8d97c1f06 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,8 +2,6 @@ TODO ==== -- compiler error for double when labels - - add sound to the cx16 tehtriz - add const arrays and cost strings diff --git a/examples/test.p8 b/examples/test.p8 index 52697a5e8..ac0489f71 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,14 +6,19 @@ main { ; $1F9C0 - $1F9FF PSG registers - sub init(uword addr, ubyte length) { - @(addr+length) = $ea - } - sub start() { - init($4000, 0) - txt.print_uwhex(@($4000), true) + ubyte xx=33 + when xx { + 1 -> { + } + 2 -> { + } + else -> { + } + else -> { + } + } ; uword freq = 1181 ; cx16.vpoke(1, $f9c0, lsb(freq))