(contains) macro

This commit is contained in:
2022-02-03 14:29:57 -07:00
parent cafea12bd6
commit 5f4351bd43
4 changed files with 20 additions and 1 deletions

View File

@@ -594,6 +594,7 @@ class Helpers {
callField: (fieldName:String, callOn:ReaderExp, args:Array<ReaderExp>) -> call(field(fieldName, callOn), args),
print: (arg:ReaderExp) -> CallExp(Symbol("print").withPosOf(posRef), [arg]).withPosOf(posRef),
the: (type:ReaderExp, value:ReaderExp) -> callSymbol("the", [type, value]),
not: (exp:ReaderExp) -> callSymbol("not", [exp]),
list: list,
str: str,
symbol: _symbol,

View File

@@ -1136,6 +1136,13 @@ class Macros {
macros["indexOf"] = indexOfMacro.bind(false);
macros["lastIndexOf"] = indexOfMacro.bind(true);
// contains is a macro so it can be called on either an Array or a String
macros["contains"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(2, 2, '(contains <string or list> <snippet or element>)');
var b = wholeExp.expBuilder();
return b.not(b.callSymbol("=", [b.symbol("-1"), b.callField("indexOf", exps[0], [exps[1]])]));
}
// Under the hood, quoted expressions are just Kiss strings for a KissInterp
macros["quote"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(1, 1, '(quote <exp>)');

View File

@@ -331,6 +331,10 @@ class BasicTestCase extends Test {
function testCaseOnNull() {
_testCaseOnNull();
}
function testContains() {
_testContains();
}
}
class BasicObject {

View File

@@ -598,4 +598,11 @@
(function _testCaseOnNull []
(Assert.equals 5 (case null (v 10) (null 5)))
(Assert.equals 5 (case null (v 10) (null 5) (otherwise 6)))
(Assert.equals 5 (case null (v 10) (otherwise 5))))
(Assert.equals 5 (case null (v 10) (otherwise 5))))
(function _testContains []
(assert (contains "abc" "b"))
(assert !(contains "abc" "z"))
(assert (contains [1 2 3] 1))
(assert !(contains [1 2 3] 5))
(Assert.pass))