带括号的消息优先级以及如何检查它?
Message precedence with parenthesis and how to check for it?
在 AST 中,我正在查看 RBMessageNode,我想检查该节点的优先级是否与标准不同。注意到 startWithParenthesis 和 stopWithParenthesis,但它们似乎没有产生我预期的结果。
aNode startWithParenthesis ifTrue: [ ... do whatever ... ].
有办法吗?
根据 Leandro 的建议,needsParanthesis
选择器可用于此目的:
aNode needsParenthesis ifFalse: [ ... do something ... ].
抓得好!
这里有趣的是原始条件之间的等价性
- 检测优先级与标准不同的节点
以及解析器需要符合 Smalltalk 语法的那个
- 括号的必要性.
作为一种好的做法,当人们发现这些巧合时,最好通过添加第二个选择器来表达其他含义,从而使它们明确化。鉴于在这种情况下这些是测试方法,我们有两个选择:
阴性测试:
subvertsPrecedence
^self needsParenthesis
阳性检测:
hasStandardPrecedence
^self needsParenthesis not
我们应该实施能更好地表达我们意图的那个。如果我们决定实现其中两个,最好 re-writte 第二个
阳性检测:
hasStandardPrecedence
^self subvertsPrecedence not
为了更清楚和对方的关系
在 AST 中,我正在查看 RBMessageNode,我想检查该节点的优先级是否与标准不同。注意到 startWithParenthesis 和 stopWithParenthesis,但它们似乎没有产生我预期的结果。
aNode startWithParenthesis ifTrue: [ ... do whatever ... ].
有办法吗?
根据 Leandro 的建议,needsParanthesis
选择器可用于此目的:
aNode needsParenthesis ifFalse: [ ... do something ... ].
抓得好!
这里有趣的是原始条件之间的等价性
- 检测优先级与标准不同的节点
以及解析器需要符合 Smalltalk 语法的那个
- 括号的必要性.
作为一种好的做法,当人们发现这些巧合时,最好通过添加第二个选择器来表达其他含义,从而使它们明确化。鉴于在这种情况下这些是测试方法,我们有两个选择:
阴性测试:
subvertsPrecedence
^self needsParenthesis
阳性检测:
hasStandardPrecedence
^self needsParenthesis not
我们应该实施能更好地表达我们意图的那个。如果我们决定实现其中两个,最好 re-writte 第二个
阳性检测:
hasStandardPrecedence
^self subvertsPrecedence not
为了更清楚和对方的关系