[英]How to pass an Array of Expr (expressions) to Haxe Macro?
我基本上试图通过为所有条件提供一个公共结果语句来创建一个自动生成if/else if
链的宏。
这是我到目前为止所尝试的(修改代码仅作为示例):
import haxe.macro.Expr;
class LazyUtils {
public macro static function tryUntilFalse( xBool:Expr, xConds:Array<Expr> ) {
var con1, con2, con3, con4, con5;
/*
* Here's a switch to handle specific # of conditions, because for-loops
* don't seem to be allowed here (at least in the ways I've tried so far).
*
* If you know how to use for-loop for this, PLEASE do tell!
*/
switch(xConds.length) {
case 1: {
con1 = conds[0];
return macro {
if (!$con1) $xBool;
}
}
case 2: {
con1 = conds[0];
con2 = conds[1];
return macro {
if (!$con1) $xBool;
else if (!$con2) $xBool;
}
}
case 3: {
con1 = conds[0];
con2 = conds[1];
con3 = conds[2];
return macro {
if (!$con1) $xBool;
else if (!$con2) $xBool;
else if (!$con3) $xBool;
}
}
// ... so on and so forth
}
return macro { trace("Unhandled length of conditions :("); };
}
}
然后,在理论上它可以这样使用:
class Main {
static function main() {
var isOK = true;
LazyUtils.tryUntilFalse( isOK = false, [
doSomething(),
doSomethingElse(), //Returns false, so should stop here.
doFinalThing()
]);
}
static function doSomething():Bool {
// ???
return true;
}
static function doSomethingElse():Bool {
// ???
return false;
}
static function doFinalThing():Bool {
return true;
}
}
哪个应该生成这个条件树:
if (!doSomething()) isOK = false;
else if (!doSomethingElse()) isOK = false;
else if (!doFinalThing()) isOK = false;
或者,我想它可以输出这个:
if(!doSomething() || !doSomethingElse() || !doFinalThing()) isOK = false;
现在回过头来看,这是真的 - 编写一个完整的宏来生成更容易以原始格式输入的代码可能没什么意义。
但是为了学习宏,有没有人知道是否可以像我在上面的代码示例中尝试的那样在Array<Expr>
传递多个表达式?
您可能无法使xConds
参数的行为与您预期的一样,因为具有Array<Expr>
类型的表达式宏的最后一个参数隐式地是一个rest参数 。 这意味着您最终得到一个包含单个EArrayDecl
表达式的数组。 这可以通过省略[]
。
关于生成if
- else
-chain - 让我们来看看EIf
:
/**
An `if(econd) eif` or `if(econd) eif else eelse` expression.
**/
EIf( econd : Expr, eif : Expr, eelse : Null<Expr> );
链可以被看作是一个单向链表-的eelse
如果第一个EIf
要引用下一个EIf
等等,直到我们有停止eelse = null
最后EIf
。 所以我们想为你的例子生成这个(伪代码):
EIf(doSomething(), isOk = false, /* else */
EIf(doSomethingElse, isOk = false, /* else */
EIf(doFinalThing(), isOk = false, null)
)
)
递归适用于此。
通常,使用reification比使用像我这样的原始表达式更方便,但是我不确定在动态生成这样的表达式时前者是否真的可行。
import haxe.macro.Context;
import haxe.macro.Expr;
class LazyUtils {
public macro static function tryUntilFalse(setBool:Expr, conditions:Array<Expr>):Expr {
return generateIfChain(setBool, conditions);
}
private static function generateIfChain(eif:Expr, conditions:Array<Expr>):Expr {
// get the next condition
var condition = conditions.shift();
if (condition == null) {
return null; // no more conditions
}
// recurse deeper to generate the next if
var nextIf = generateIfChain(eif, conditions);
return {
expr: EIf(condition, eif, nextIf),
pos: Context.currentPos()
};
}
}
和Main.hx
(大部分没有变化):
class Main {
static function main() {
var isOK = true;
LazyUtils.tryUntilFalse(isOK = false,
!doSomething(),
!doSomethingElse(), //Returns false, so should stop here.
!doFinalThing()
);
}
static function doSomething():Bool {
trace("doSomething");
return true;
}
static function doSomethingElse():Bool {
trace("doSomethingElse");
return false;
}
static function doFinalThing():Bool {
trace("doFinalThing");
return true;
}
}
为了简单起见,我将函数调用参数反转为!
在调用站点而不是在宏中处理它。
您可以使用-D dump=pretty
来生成AST转储并检查正在生成的代码。 这是结果:
if ((! Main.doSomething()))isOK = false else if ((! Main.doSomethingElse()))isOK = false else if ((! Main.doFinalThing()))isOK = false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.