Generating Code From Scratch?

xuanbao · · 1001 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Could anyone steer me to some good examples of generating and printing Go code from scratch using go/ast and go/printer? I&#39;ve played around a bit with generating some files from built-from-scratch ASTs, and it seems like I can basically count on the printer to handle formatting by setting positions to the default...as long as there aren&#39;t any comments in the file. Once those come into play, it seems like there needs to be position data in the AST or else the printer just sticks the comments in unexpected places. The documentation seems to mostly assume that you&#39;re modifying parser output and doesn&#39;t get a lot into generating files from scratch, so I&#39;d love to look over some existing examples if there are any good ones out there</p> <hr/>**评论:**<br/><br/>mewkiz: <pre><p>Probably not the best place to start, but if you would like to see an example of an application using go/ast to generate Go code from scratch, have a look at ll2go (<a href="https://github.com/decomp/ll2go" rel="nofollow">https://github.com/decomp/ll2go</a>). It decompiles LLVM IR (a platform independent assembly) to Go source code.</p> <p>The ll2go tool is unfortunately not yet go-getable (this work is tracked by the issue <a href="https://github.com/decomp/decompilation/issues/167" rel="nofollow">https://github.com/decomp/decompilation/issues/167</a>). However, you may look at the code and get an idea for how ast.File&#39;s containing ast.FuncDecl&#39;s may be created and printed.</p></pre>bradfitz: <pre><p>It&#39;s really painful and go/ast isn&#39;t really designed for code generation. Don&#39;t be ashamed to just use print statements to write out your code. You can then format it with go/format. That&#39;s what the google-api-go-generator does. </p></pre>robertbieber: <pre><p>Ugh, the lost potential is so sad though :(</p></pre>bradfitz: <pre><p>Robert Griesemer (author of existing go/* packages) has plans or is working on a new go/* set of packages with a nicer API which would permit many more things like this easily.</p></pre>natefinch: <pre><p>That would be amazing. I have at least a couple projects percolating in my head that revolve around generating code from scratch.</p></pre>robertbieber: <pre><p>That would be so great. Is there perchance an issue out there somewhere I could follow to get updates on the progress?</p></pre>daveddev: <pre><p><a href="https://github.com/campoy/jsonenums" rel="nofollow">https://github.com/campoy/jsonenums</a></p> <p><a href="https://www.youtube.com/watch?v=YgnD27GFcyA" rel="nofollow">https://www.youtube.com/watch?v=YgnD27GFcyA</a> <a href="http://talks.golang.org/2015/json.slide#1" rel="nofollow">http://talks.golang.org/2015/json.slide#1</a></p></pre>klaaax: <pre><p>would really like to see a good tutorial about code generation, ast/token/parser packages in Go. It&#39;s often hard to tell what is what and how to generate go files the proper way.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1001 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传