withEvalOnce macro using implicit quasiquote. Close #26

This commit is contained in:
2022-06-27 18:09:43 +00:00
parent db7a3a02f5
commit 82f05a274c
2 changed files with 23 additions and 2 deletions

View File

@@ -369,6 +369,10 @@ class Helpers {
FieldExp(field, removeTypeAnnotations(innerExp));
case KeyValueExp(keyExp, valueExp):
KeyValueExp(removeTypeAnnotations(keyExp), removeTypeAnnotations(valueExp));
case Unquote(innerExp):
Unquote(removeTypeAnnotations(innerExp));
case UnquoteList(innerExp):
UnquoteList(removeTypeAnnotations(innerExp));
case None:
None;
default:
@@ -606,7 +610,11 @@ class Helpers {
}
function float(v:Float) {
return _symbol(Std.string(v));
}
}
function let(bindings:Array<ReaderExp>, body:Array<ReaderExp>) {
return callSymbol("let", [list(bindings)].concat(body));
}
return {
call: call,
callSymbol: callSymbol,
@@ -625,7 +633,7 @@ class Helpers {
field: field,
keyValue: (key:ReaderExp, value:ReaderExp) -> KeyValueExp(key, value).withPosOf(posRef),
begin: (exps:Array<ReaderExp>) -> callSymbol("begin", exps),
let: (bindings:Array<ReaderExp>, body:Array<ReaderExp>) -> callSymbol("let", [list(bindings)].concat(body)),
let: let,
objectWith: objectWith,
throwKissError: (reason:String) -> {
callSymbol("throw", [

View File

@@ -1231,6 +1231,19 @@ class Macros {
typedCallMacro("intersect", "intersect", "Array<Array<Dynamic>>");
typedCallMacro("concat", "concat", "Array<Dynamic>");
k.doc("withEvalOnce", 2, null, "(withEvalOnce [<symbols...>] <body...>)");
macros["withEvalOnce"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();
var symbols = exps[0].argList("withEvalOnce");
var body = exps.slice(1);
var bindings = [];
for (symbol in symbols) {
bindings.push(symbol);
bindings.push(Unquote(symbol).withPosOf(symbol));
}
return Quasiquote(b.let(bindings, body)).withPosOf(wholeExp);
};
return macros;
}