说我需要一些非常特殊的乘法运算符。它可以在以下宏中实现:

macro @<<!(op1, op2)
{
    <[ ( $op1 * $op2 ) ]>
}

我可以像

一样使用它
def val = 2 <<! 3

及其工作。

但我真正想要的是现在正在开发的DSL Im的一些'英语'操作员:

macro @multiply(op1, op2)
{
    <[ ( $op1 * $op2 ) ]>
}

如果我尝试使用它

def val = 2 multiply 3

编译器因'expected;'而失败错误

有什么问题?如何实现这个中缀格式宏?

有帮助吗?

解决方案

直接来自编译器源代码:

namespace Nemerle.English
{
  [assembly: Nemerle.Internal.OperatorAttribute ("Nemerle.English", "and", false, 160, 161)]
  [assembly: Nemerle.Internal.OperatorAttribute ("Nemerle.English", "or", false, 150, 151)]
  [assembly: Nemerle.Internal.OperatorAttribute ("Nemerle.English", "not", true, 181, 180)]  

  macro @and (e1, e2) {
    <[ $e1 && $e2 ]>
  }

  macro @or (e1, e2) {
    <[ $e1 || $e2 ]>
  }

  macro @not (e) {
    <[ ! $e ]>
  }

你需要在其周围撒上OperatorAttributes并且它会起作用。顺便说一下,OperatorAttribute的定义如下:

public class OperatorAttribute : NemerleAttribute
{
  public mutable env : string;
  public mutable name : string;
  public mutable IsUnary : bool;
  public mutable left : int;
  public mutable right : int;
}

其他提示

通常,我发现答案早于社区回复:) 因此,解决方案是简单地使用特殊的程序集级别属性,该属性将宏指定为二元运算符:

namespace TestMacroLib
{
  [assembly: Nemerle.Internal.OperatorAttribute ("TestMacroLib", "multiply", false, 160, 161)]
  public macro multiply(op1, op2)
  {
    <[ ( $op1 * $op2 ) ]>
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top