helper for clean expression building

This commit is contained in:
2021-01-16 19:54:00 -07:00
parent a473536d17
commit 9cc9407db6
2 changed files with 24 additions and 1 deletions

View File

@@ -328,4 +328,20 @@ class Helpers {
};
return def.withPosOf(exp);
}
// Return convenient functions for succinctly making new ReaderExps that link back to an original exp's
// position in source code
public static function expBuilder(posRef:ReaderExp) {
return {
call: (func:ReaderExp, args:Array<ReaderExp>) -> CallExp(func, args).withPosOf(posRef),
list: (exps:Array<ReaderExp>) -> ListExp(exps).withPosOf(posRef),
str: (s:String) -> StrExp(s).withPosOf(posRef),
symbol: (name:String) -> Symbol(name).withPosOf(posRef),
raw: (code:String) -> RawHaxe(code).withPosOf(posRef),
typed: (path:String, exp:ReaderExp) -> TypedExp(path, exp).withPosOf(posRef),
meta: (m:String, exp:ReaderExp) -> MetaExp(m, exp).withPosOf(posRef),
field: (f:String, exp:ReaderExp) -> FieldExp(f, exp).withPosOf(posRef),
keyValue: (key:ReaderExp, value:ReaderExp) -> KeyValueExp(key, value).withPosOf(posRef),
};
}
}

View File

@@ -22,7 +22,14 @@ class Macros {
function destructiveVersion(op:String, assignOp:String) {
macros[assignOp] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k) -> {
wholeExp.checkNumArgs(2, null, '($assignOp [var] [v1] [values...])');
CallExp(Symbol("set").withPosOf(wholeExp), [exps[0], CallExp(Symbol(op).withPosOf(wholeExp), exps).withPosOf(wholeExp)]).withPosOf(wholeExp);
var b = wholeExp.expBuilder();
b.call(
b.symbol("set"), [
exps[0],
b.call(
b.symbol(op),
exps)
]);
};
}