Infix format for Nemerle macro

Let's say I need a very special multiplication operator. It can be implemented in the following macro:

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

And I can use it as

def val = 2 <<! 3

And his job.

But what I really want is some kind of "English" -like operator for DSL Im, developing now:

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

and if I try to use it as

def val = 2 multiply 3

the compiler does not work with the "expected"; error

What is the problem? How can I implement this macro in infix style?

+3
source share
2 answers

Directly from the source code of the compiler:

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 ]>
  }

You need to sprinkle with OperatorAttributes and it will work. Btw, OperatorAttribute is defined as follows:

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

, , :) , , assemply, :

namespace TestMacroLib
{
  [assembly: Nemerle.Internal.OperatorAttribute ("TestMacroLib", "multiply", false, 160, 161)]
  public macro multiply(op1, op2)
  {
    <[ ( $op1 * $op2 ) ]>
  }
}
+1

Source: https://habr.com/ru/post/1698545/


All Articles