diff --git a/src/kiss/Helpers.hx b/src/kiss/Helpers.hx index b3e862f..2d4de08 100644 --- a/src/kiss/Helpers.hx +++ b/src/kiss/Helpers.hx @@ -214,11 +214,17 @@ class Helpers { function makeSwitchPattern(patternExp:ReaderExp):Array { return switch (patternExp.def) { case CallExp({pos: _, def: Symbol("when")}, whenExps): - patternExp.checkNumArgs(2, 2, "(when [guard] [pattern])"); + patternExp.checkNumArgs(2, 2, "(when )"); if (guard != null) - throw CompileError.fromExp(caseExp, "case pattern can only have one `when` guard"); + throw CompileError.fromExp(caseExp, "case pattern can only have one `when` or `unless` guard"); guard = macro Prelude.truthy(${k.convert(whenExps[0])}); makeSwitchPattern(whenExps[1]); + case CallExp({pos: _, def: Symbol("unless")}, whenExps): + patternExp.checkNumArgs(2, 2, "(unless )"); + if (guard != null) + throw CompileError.fromExp(caseExp, "case pattern can only have one `when` or `unless` guard"); + guard = macro !Prelude.truthy(${k.convert(whenExps[0])}); + makeSwitchPattern(whenExps[1]); case ListEatingExp(exps) if (exps.length == 0): throw CompileError.fromExp(patternExp, "list-eating pattern should not be empty"); case ListEatingExp(exps): diff --git a/src/test/cases/BasicTestCase.kiss b/src/test/cases/BasicTestCase.kiss index 3ddcdd6..dc92e53 100644 --- a/src/test/cases/BasicTestCase.kiss +++ b/src/test/cases/BasicTestCase.kiss @@ -369,6 +369,10 @@ ((when false (or 5 6)) (Assert.fail)) ((when true (or 7 8 9)) (Assert.fail)) (otherwise (Assert.pass))) + (case 5 + ((unless true (or 5 6)) (Assert.fail)) + ((unless false (or 7 8 9)) (Assert.fail)) + (otherwise (Assert.pass))) // In Haxe, // `switch (Some(true)) { case Some(true | false): "a"; default: "b"; }` // returns "a", so nested use of `or` in case patterns should also be valid: