From 9dd30160912cd13e340d1b6e05b83cc60ef2f5cf Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 22 Jul 2021 18:20:28 -0600 Subject: [PATCH] added as keyword for case. close #29 --- src/kiss/Kiss.hx | 1 + src/kiss/SpecialForms.hx | 5 +++++ src/test/cases/BasicTestCase.kiss | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/src/kiss/Kiss.hx b/src/kiss/Kiss.hx index 14c56b3..496b0dc 100644 --- a/src/kiss/Kiss.hx +++ b/src/kiss/Kiss.hx @@ -284,6 +284,7 @@ class Kiss { var copy = withoutListWrapping(k); copy.macros.remove("or"); copy.specialForms["or"] = SpecialForms.caseOr; + copy.specialForms["as"] = SpecialForms.caseAs; return copy; } diff --git a/src/kiss/SpecialForms.hx b/src/kiss/SpecialForms.hx index c187e10..333388b 100644 --- a/src/kiss/SpecialForms.hx +++ b/src/kiss/SpecialForms.hx @@ -371,4 +371,9 @@ class SpecialForms { macro ${k.convert(args[0])} | ${caseOr(wholeExp, args.slice(1), k)}; }; }; + + public static function caseAs(wholeExp:ReaderExp, args:Array, k:KissState):Expr { + wholeExp.checkNumArgs(2, 2, "(as [name] [pattern])"); + return macro ${k.convert(args[0])} = ${k.convert(args[1])}; + }; } diff --git a/src/test/cases/BasicTestCase.kiss b/src/test/cases/BasicTestCase.kiss index 961269f..562c0fb 100644 --- a/src/test/cases/BasicTestCase.kiss +++ b/src/test/cases/BasicTestCase.kiss @@ -360,6 +360,13 @@ (case (Some 5) ((Some (or 6 5 4)) (Assert.pass)) + (otherwise (Assert.fail))) + // In Haxe, name = Pattern can be used in switch/case to match values: https://haxe.org/manual/lf-pattern-matching-variable-capture.html + // In Kiss, the syntax for this is (as name pattern) + (case (Some (Some 5)) + ((Some (as inner (Some v))) + (Assert.equals 5 v) + (Assert.isTrue (Type.enumEq (Some 5) inner))) (otherwise (Assert.fail)))) (defun _testMaps []