我想创建两个使用相同匹配表达式的宏,但是通过它们的句法类别(即action或expression)进行区分。 例如,我想表达
var x := obj.foo(); // value-returning
...
obj.foo(); // not value-returning
使用以下宏定义:
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
};
这有可能吗?我知道我可以使用&#34;计算&#34;在void上下文中调用一个返回值的方法,但它不是很好。
答案 0 :(得分:2)
您不需要询问宏在哪种情况下匹配,因为解释器会为您处理。这是一个带有函数的示例,该函数在表达式上下文中返回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";
};
解释器将根据查看some_function
的上下文知道要扩展哪一个:
extend sys {
run() is also {
some_function;
if some_function {
out("here I was used in expression context");
}
};
};
您可以轻松修改这些宏以使对象调用方法。