indexOf macro

This commit is contained in:
2021-11-17 16:03:07 -07:00
parent 3196f45d4f
commit 6bb03cc468
2 changed files with 15 additions and 2 deletions

View File

@@ -1076,6 +1076,19 @@ class Macros {
].concat(body))]);
};
function indexOfMacro(last:Bool, wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) {
var funcName = if (last) "lastIndexOf" else "indexOf";
wholeExp.checkNumArgs(2, 3, '($funcName <list or string> <element or substring> <?startingIndex>)');
var b = wholeExp.expBuilder();
return b.callSymbol("case", [
b.callField(funcName, exps.shift(), exps),
b.callSymbol("-1", [b.symbol("haxe.ds.Option.None")]),
b.callSymbol("other", [b.callSymbol("haxe.ds.Option.Some", [b.symbol("other")])])
]);
}
macros["indexOf"] = indexOfMacro.bind(false);
macros["lastIndexOf"] = indexOfMacro.bind(true);
return macros;
}

View File

@@ -399,8 +399,8 @@
(Assert.equals "you" (dictGet myMap "hey"))
(Assert.equals "me" (dictGet myMap "found"))
(doFor =>key value myMap
(Assert.isTrue (<= 0 (.indexOf ["hey" "found"] key)))
(Assert.isTrue (<= 0 (.indexOf ["you" "me"] value))))
(assertLet [(Some _) (indexOf ["hey" "found"] key)] 0)
(assertLet [(Some _) (indexOf ["you" "me"] value)] 0))
// Map destructuring:
(let [[=>"hey" v1 =>"found" v2] myMap]