在 F# 中转换抽象语法树 (AST)
我正在尝试为决策逻辑表设计 AST。我希望能够对代表我的 AST 的受歧视联合做的一件事是出于不同的原因对其部分进行变换。为了清楚起见,我将给您一个示例
决策逻辑表
@VAR = 10;Y;
上面的内容可以理解为有一个规则,并且条件 VAR = 10 通过 Y 条目进入该规则。
抽象语法树定义(本例已简化)
type expression =
| Value of double
| Variable of string
| Equality of expression * expression
type entry =
| Entry of string
type entries =
| Entries of entry list
type conditional =
| ConditionEntries of expression * entries
type condition
| Condition of expression * string
type rule =
| Rule of condition list
渲染(变换前)
ConditionEntries(
Equality(
Variable("VAR"),
Value(10.0)),
Entries(["Y"]))
渲染(变换后)
Rule(
Condition(
Equality(
Variable("VAR"),
Value(10.0)
),
Entry("Y")
)
)
现在我想做的是变换上面的树来扩展条目中表示的规则。我的想法是我可以使用递归函数和模式匹配来做到这一点,但我现在在理解它时遇到了一些麻烦。
我想本质上我想做的是每当我看到 ConditionEntries 节点时,我想为条件与条目组合的条目列表中的每个字符串发出一个新规则。这有什么意义吗?
预先感谢您的任何建议。
ps 我还没有尝试编译上面的例子,所以请原谅任何语法错误。
I am trying to design an AST for a decision logic table. One of the things I would like to be able to do with the discriminated union that represents my AST is transform parts of it for different reasons. For clarity I will give you an example
Decision Logic Table
@ VAR = 10 ;Y;
The above can be read as there is one rule and the condition VAR = 10 enters this rule with a Y entry.
Abstract Syntax Tree Definition (simplified for this example)
type expression =
| Value of double
| Variable of string
| Equality of expression * expression
type entry =
| Entry of string
type entries =
| Entries of entry list
type conditional =
| ConditionEntries of expression * entries
type condition
| Condition of expression * string
type rule =
| Rule of condition list
Rendered (before transform)
ConditionEntries(
Equality(
Variable("VAR"),
Value(10.0)),
Entries(["Y"]))
Rendered (after transform)
Rule(
Condition(
Equality(
Variable("VAR"),
Value(10.0)
),
Entry("Y")
)
)
Now what I would like to do is transform the above tree to expand the rules that are represented in the entries. My thinking was I could use a recursive function and pattern-matching to do this but I am having a little trouble wrapping my head around it right now.
I guess in essence what I am trying to do is whenever I see a ConditionEntries node, I want to emit a new Rule for every string in the Entries list where the Condition is combined with the Entry. Does that make any sense?
Thanks in advance for any advice.
p.s. I haven't quite tried to compile the above example, so please forgive any grammatical errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,基于你的 AST,它被严重破坏了,这里有一个
tranform
函数,它根据你想要的输入生成输出(尽管它不是递归的,只是使用List.map
具有一些模式匹配。表达式
是您唯一的递归类型,但看起来您不想递归地处理它?):Hmm, based on your AST, which is awfully broken up, here is a
tranform
function which produces the output from input you desire (though it's not recursive, just usesList.map
with some pattern matching.expression
is your only recursive type but it doesn't look like you want to process it recursively?):