Nemerle宏的中缀格式 [英] Infix format for Nemerle macro
本文介绍了Nemerle宏的中缀格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我需要一些非常特殊的乘法运算符.可以在以下宏中实现:
Say I need some very special multiplication operator. It may be implemented in following macro:
macro @<<!(op1, op2)
{
<[ ( $op1 * $op2 ) ]>
}
我可以像使用它
def val = 2 <<! 3
及其工作.
但是我真正想要的是现在正在开发的DSL Im的一些类似于英语"的运算符:
But what I really want is some 'english'-like operator for the DSL Im developing now:
macro @multiply(op1, op2)
{
<[ ( $op1 * $op2 ) ]>
}
如果我尝试使用它
def val = 2 multiply 3
编译器失败,显示"expected;"错误
compiler fails with 'expected ;' error
出什么问题了?如何实现此infix格式的宏?
What is the problem? How can I implement this infix-format macro?
推荐答案
直接来自编译器源代码:
Straight from the compiler source code:
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定义如下:
You need to sprinkle OperatorAttributes around 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;
}
这篇关于Nemerle宏的中缀格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文