【问题标题】:Generating Strings with LeMP使用 LeMP 生成字符串
【发布时间】:2018-07-16 17:19:28
【问题描述】:

我正在尝试使用 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);

(注意引用的入口点)。

我原以为会是这样的:

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

然而,这只是发出以下内容:

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

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

【问题讨论】:

    标签: c# lemp ec#


    【解决方案1】:

    答案确实是在concatId 的输出上运行stringify,但它有一个技巧。

    困难是由执行顺序引起的。宏通常运行“由内而外”,最外层的宏首先运行,与运行“由内向外”的普通函数相反。因此

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

    产生"concatId(Tea, ring, Cot, ton)"。目前还没有一种非常优雅的方式来反转顺序 - 在自定义 define 宏中,您可以使用 [ProcessChildrenBefore] 属性,但这不允许您修改 stringify 的现有行为。这是一种有效的技术:

    replacePP(xx => concatId(Tea, ring, Cot, ton)) { stringify(xx); }
    // Output: "TearingCotton";
    

    与普通的replace 相比,replacePP 预处理匹配和替换表达式,因此concatId 发生在stringify 之前。将此解决方案应用于您的TypedIndexer2D,我们得到

    define TypedIndexer2D($CONTAINER_TYPE, $T1, $T2)
    {
        replace(MethodName => concatId(Buffer, $CONTAINER_TYPE, GetExpr_, $T1, $T2));
        replacePP(CFunction => concatId(buffer_, $CONTAINER_TYPE, _getexpr__, $T1, $T2));
    
        [DllImport(Constants.LibName, EntryPoint = stringify(CFunction))]
        public static extern IntPtr MethodName(IntPtr obj, IntPtr x, IntPtr y);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2013-10-18
      • 2015-04-03
      • 2021-10-22
      相关资源
      最近更新 更多