我在哪里可以了解如何为 Scala 宏构建 AST?

编程入门 行业动态 更新时间:2024-10-11 23:24:49
本文介绍了我在哪里可以了解如何为 Scala 宏构建 AST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在哪里可以学习如何构建 Scala 的宏生成的 AST?

Scaladoc 没有我想的那么有用.例如:

abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.TreeApply 节点的工厂方法.

但是我如何确定 Apply 节点是什么?我在哪里可以找到 AST 中的节点类型列表,以及它们如何组合在一起?

解决方案

没有很多关于编译器内部的文档可用,但是可用的东西应该足够开始了.

,其中包含 AST.

如果需要了解特定源代码片段在内部是如何表示的,可以使用 reify:

scala>导入反射.runtime.universe._导入反射.runtime.universe._斯卡拉>showRaw(reify{val i = 0}.tree)res8: String = Block(List(ValDef(Modifiers(), newTermName("i"), TypeTree(),文字(常量(0)))),文字(常量(())))

Where I can learn how to construct the AST's that Scala's macros generate?

The Scaladoc isn't as helpful as I'd like. For example:

abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.Tree A factory method for Apply nodes.

But how do I figure out what an Apply node is? Where can I find a list of the node types in AST's, and how they fit together?

解决方案

There isn't a lot of documentation for the internals of the compiler available, but the things that are available should be enough to get started.

Mirko Stocker, has written his Master Thesis about Scala Refactoring. In Appendix D (p. 95) he describes the architecture of the AST. It includes also a graphical overview:

Another way to find information about the AST is to look directly into the sources of reflect.internal.Trees, which contains the AST.

If one needs to find out how a specific source code snippet is represented internally there is reify:

scala> import reflect.runtime.universe._ import reflect.runtime.universe._ scala> showRaw(reify{val i = 0}.tree) res8: String = Block(List(ValDef(Modifiers(), newTermName("i"), TypeTree(), Literal(Constant(0)))), Literal(Constant(())))

更多推荐

我在哪里可以了解如何为 Scala 宏构建 AST?

本文发布于:2023-06-05 11:04:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/518326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宏构   我在   何为   Scala   AST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!