added as keyword for case. close #29

This commit is contained in:
2021-07-22 18:20:28 -06:00
parent a15bfe7da3
commit 9dd3016091
3 changed files with 13 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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<ReaderExp>, k:KissState):Expr {
wholeExp.checkNumArgs(2, 2, "(as [name] [pattern])");
return macro ${k.convert(args[0])} = ${k.convert(args[1])};
};
}

View File

@@ -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 []