简体   繁体   English

定义为计算宏的语法类别(意识)?

[英]Syntactic category (awareness) of define-as-computed macro?

I want to create two macros that use the same match expression but are distinguished by their syntactic category, ie action or expression. 我想创建两个使用相同匹配表达式但通过其语法类别(即动作或表达式)区分的宏。 For example I want to express 例如我想表达

var x := obj.foo(); // value-returning
...
obj.foo(); // not value-returning

Using the following macro definitions: 使用以下宏定义:

define <my_macro_void_call'action> "<obj'exp>.foo" as computed {
   <if in expression context>
     reject_match()
   <else>
     do_something
};

define <my_macro_call'expr> "<obj'exp>.foo" as computed {
   <if in action context>
     reject_match()
   <else>
     do_something
};

Is this possible at all? 这有可能吗? I know that I can use "compute" to call a value-returning method in a void context, but it is not as nice. 我知道我可以在无效的上下文中使用“计算”​​来调用返回值的方法,但这并不是很好。

You don't need to ask in what context the macro was matched, because the interpreter will handle that for you. 您不需要询问在哪个上下文中匹配了宏,因为解释器将为您处理该宏。 Here's an example with a function that either returns TRUE in expression context or prints out something in action context: 这是一个带有在表达式上下文中返回TRUE或在操作上下文中打印出某些内容的函数的示例:

define <my_macro_void_call'action> "some_function" as computed {
  print "matched in action context";
  result ="out(\"here I am in action context\");";
};

define <my_macro_call'exp> "some_function" as computed {
  print "matched in expression context";
  result = "TRUE";
};

The interpreter will know which one to expand depending on the context where some_function is seen in: 解释器将根据在其中看到some_function的上下文来知道要扩展哪一个:

extend sys {
  run() is also {
    some_function;

    if some_function {
      out("here I was used in expression context");
    }
  };
};

You can easily modify these macros to take an object to call a method on. 您可以轻松地修改这些宏以采用对象来调用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM