我一直在查看 Roslyn 代码库并注意到它们有两种语法版本(一种内部版本和一种公共(public)版本)。通常这些似乎被称为“红色”节点和“绿色”节点。我想知道是否有人可以解释为什么有两个这样的语法版本。
最佳答案
来自 Persistence, Facades and Roslyn’s Red-Green Trees :
The “green” tree is immutable, persistent, has no parent references, is built “bottom-up”, and every node tracks its width but not its absolute position. When an edit happens we rebuild only the portions of the green tree that were affected by the edit, which is typically about O(log n) of the total parse nodes in the tree.
The “red” tree is an immutable facade that is built around the green tree; it is built “top-down” on demand and thrown away on every edit. It computes parent references by manufacturing them on demand as you descend through the tree from the top. It manufactures absolute positions by computing them from the widths, again, as you descend.
You, the consumer of the Roslyn API, only ever see the red tree; the green tree is an implementation detail. (And if you use the debugger to peer into the internal state of a parse node you’ll in fact see that there is a reference to another parse node in there of a different type; that’s the green tree node.)
Incidentally, these are called “red/green trees” because those were the whiteboard marker colours we used to draw the data structure in the design meeting. There’s no other meaning to the colours.
关于c# - 为什么 Roslyn 每种语言都有两个版本的语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266011/