added as keyword for case. close #29
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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])};
|
||||
};
|
||||
}
|
||||
|
@@ -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 []
|
||||
|
Reference in New Issue
Block a user