用LeMP生成字符串

时间:2018-07-13 02:03:08

标签: c# lemp ec#

我正在尝试使用LeMP为C ++库生成一些C#绑定,作为此过程的一部分,我需要生成一个字符串,该字符串将LeMP宏中的一些参数组合在一起,以用于DllImport EntryPoint值。看一下文档,似乎concatId和stringify的组合应该可以完成这项工作,但是我无法使其正常工作。这是所讨论代码的稍微简化的版本:

define TypedIndexer2D($CONTAINER_TYPE, $T1, $T2)
{
    replace(MethodName => concatId(Buffer, $CONTAINER_TYPE, GetExpr_, $T1, $T2));
    replace(CFunction => concatId(buffer_, $CONTAINER_TYPE, _getexpr__, $T1, $T2));

    [DllImport(Constants.LibName, EntryPoint = CFunction)]
public static extern IntPtr MethodName(IntPtr obj, IntPtr x, IntPtr y);
}

TypedIndexer2D(Int, Var, Var);

这会发出以下信息:

[DllImport(Constants.LibName, EntryPoint = buffer_Int_getexpr__VarVar)] 
public static extern IntPtr BufferIntGetExpr_VarVar(IntPtr obj, IntPtr x, IntPtr y);

但是,我需要这个:

[DllImport(Constants.LibName, EntryPoint = "buffer_Int_getexpr__VarVar")] 
public static extern IntPtr BufferIntGetExpr_VarVar(IntPtr obj, IntPtr x, IntPtr y);

(请注意引用的EntryPoint)。

我曾经以为会是这样:

replace(CFunction => stringify(concatId(buffer_, $CONTAINER_TYPE, _getexpr__, $T1, $T2)));

然而,它仅发出以下内容:

[DllImport(Constants.LibName, EntryPoint = "concatId(buffer_, Int, _getexpr__, Var, Var)")]

如何说服LeMP在这里生成所需的字符串?谢谢!

1 个答案:

答案 0 :(得分:0)

答案确实是在stringify的输出上运行concatId,但这有一个窍门。

困难是由执行顺序引起的。宏通常以“由内而外”的方式运行,首先是最外面的宏,这与“由内而外”的普通功能相反。因此

stringify(concatId(Tea, ring, Cot, ton));

产生"concatId(Tea, ring, Cot, ton)"。还没有一种超级优雅的方法来反转顺序-在自定义define宏中,您可以使用[ProcessChildrenBefore]属性,但这不能让您修改{{1}的现有行为}。这是一种有效的技术:

stringify

与普通replacePP(xx => concatId(Tea, ring, Cot, ton)) { stringify(xx); } // Output: "TearingCotton"; 相比,replace预处理匹配和替换表达式,因此replacePP发生在concatId之前。将此解决方案应用于您的stringify,我们得到

TypedIndexer2D