key-value for loops
This commit is contained in:
@@ -183,15 +183,23 @@ class SpecialForms {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function forExpr(formName:String, wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) {
|
function forExpr(formName:String, wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) {
|
||||||
wholeExp.checkNumArgs(3, null, '($formName [varName] [list] [body...])');
|
wholeExp.checkNumArgs(3, null, '($formName [varName or [varNames...] or =>keyName valueName] [list] [body...])');
|
||||||
var uniqueVarName = "_" + Uuid.v4().toShort();
|
var uniqueVarName = "_" + Uuid.v4().toShort();
|
||||||
var namesExp = args[0];
|
var namesExp = args[0];
|
||||||
var listExp = args[1];
|
var listExp = args[1];
|
||||||
var bodyExps = args.slice(2);
|
var bodyExps = args.slice(2);
|
||||||
bodyExps.insert(0, CallExp(Symbol("deflocal").withPosOf(args[2]), [namesExp, Symbol(uniqueVarName).withPosOf(args[2])]).withPosOf(args[2]));
|
|
||||||
|
var loopVarExpr:Expr = switch (namesExp.def) {
|
||||||
|
case KeyValueExp(_, _): k.convert(namesExp);
|
||||||
|
default: {
|
||||||
|
bodyExps.insert(0,
|
||||||
|
CallExp(Symbol("deflocal").withPosOf(args[2]), [namesExp, Symbol(uniqueVarName).withPosOf(args[2])]).withPosOf(args[2]));
|
||||||
|
macro $i{uniqueVarName};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var body = CallExp(Symbol("begin").withPosOf(args[2]), bodyExps).withPosOf(args[2]);
|
var body = CallExp(Symbol("begin").withPosOf(args[2]), bodyExps).withPosOf(args[2]);
|
||||||
return EFor(EBinop(OpIn, EConst(CIdent(uniqueVarName)).withMacroPosOf(wholeExp), k.convert(listExp)).withMacroPosOf(wholeExp),
|
return EFor(EBinop(OpIn, loopVarExpr, k.convert(listExp)).withMacroPosOf(wholeExp), k.convert(body)).withMacroPosOf(wholeExp);
|
||||||
k.convert(body)).withMacroPosOf(wholeExp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
map["doFor"] = forExpr.bind("doFor");
|
map["doFor"] = forExpr.bind("doFor");
|
||||||
|
|||||||
@@ -346,4 +346,6 @@
|
|||||||
=>"found" "me"])
|
=>"found" "me"])
|
||||||
(Assert.equals "you" (dict-get myMap "hey"))
|
(Assert.equals "you" (dict-get myMap "hey"))
|
||||||
(Assert.equals "me" (dict-get myMap "found"))
|
(Assert.equals "me" (dict-get myMap "found"))
|
||||||
)
|
(doFor =>key value myMap
|
||||||
|
(Assert.isTrue (<= 0 (.indexOf ["hey" "found"] key)))
|
||||||
|
(Assert.isTrue (<= 0 (.indexOf ["you" "me"] value)))))
|
||||||
Reference in New Issue
Block a user