通过SSA的解释执行窥探Golang编译之一角Syntax Tree,AST),涉及的包: • go/token 词法单元定义 • go/scanner 词法扫描 • go/ast 抽象语法树定义 • go/parser 语法解析 Golang 编译流程简介 源代码 抽象语法树 抽象语法树 (带语义信息) 静态单赋值 目标代码 1 + 2 * 3 *ast.BinaryExpr (+) *ast.BasicLit BasicLit 1 : INT *ast.BinaryExpr (*) *ast.BasicLit 2 : INT * ast.BasicLit 3 : INT Golang 编译流程简介 01-AST 源代码 抽象语法树 抽象语法树 (带语义信息) 静态单赋值 目标代码 package main import ( "go/ast" "go/parser" "go/token" AllErrors) ast.Print(nil, f) } *ast.File *ast.GenDecl (var) *ast.Ident “answer” *ast.BinaryExpr (+) *ast.BasicLit 40 : INT *ast.BasicLit 2 : INT *ast.FuncDecl *ast.Ident “main” *ast.BlockStmt0 码力 | 30 页 | 1.50 MB | 1 年前3
 Golang Manual By AstaXie-20120522implements formatted I/O with functions analogous to C's printf and scanf. go ast Package ast declares the types used to represent syntax trees for Go packages. build Package source code documentation from a Go AST. parser Package parser implements a parser for Go source files. printer Package printer implements printing of AST nodes. scanner Package BSD license. Terms of Service | Privacy Policy Directory /src/pkg/go Name Synopsis ast Package ast declares the types used to represent syntax trees for Go packages. build Package build0 码力 | 6205 页 | 12.83 MB | 1 年前3
 2.1 gofmt 的文化演变1:3999/gofmt-cn.slide#1 10/34 处理源代码 使用`go/scanner`, `go/parser`及其相关的库 给每一个go文件生成一个抽象语法树 每一个语法结构都有相应的AST节点 // Syntax of an if statement. IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt Else Stmt // else branch; or nil } AST节点有(选择性的)位置信息。 4/21/2015 gofmt 的文化演变 http://127.0.0.1:3999/gofmt-cn.slide#1 11/34 基本的格式化 遍历AST然后打印每个节点 case *ast.IfStmt: p.print(token.IF) p.controlClause(false nil { p.print(blank, token.ELSE, blank) switch s.Else.(type) { case *ast.BlockStmt, *ast.IfStmt: p.stmt(s.Else, nextIsRBrace) default: p.print(token0 码力 | 34 页 | 9.97 MB | 1 年前3
 The Way To Go - 2012.....................................................................28 Fig 3.1: LiteIDE and its AST-view .................................................................................33 Fig 3.2: GoClipse methods within packages, program types, variables, functions and methods; function signatures. (18) An AST-view (abstract syntax tree) of a project/package code. (19) Built-in go tools, such as: go fmt, go for editing, compiling and running Go programs and -projects. Contains an abstract syntax tree (AST) viewer of the source code and a built-in make tool: http://code.google.com/p/golangide/0 码力 | 629 页 | 4.85 MB | 1 年前3
 Go Compile Time Instrumentationand manipulation - All tools needed for source level manipulation are part of standard libraryGo AST TraversalInstrumentationInstrgen first instrumentation approach ! Problematic from context propagation0 码力 | 27 页 | 1.74 MB | 6 月前3
 Go 入门指南(The way to Go)拥有断点、检查变量值、单步执行、逐过程执行标识库中代码的能力。 16. 能够方便的存取最近使用过的文件或项目。 17. 拥有对包、类型、变量、函数和方法的智能代码补全的功能。 18. 能够对项目或包中的代码建立抽象语法树视图(AST-view)。 19. 内置 Go 的相关工具。 20. 能够方便完整地查阅 Go 文档。 21. 能够方便地在不同的 Go 环境之间切换。 22. 能够导出不同格式的代码文件,如:PDF,HTML0 码力 | 380 页 | 2.97 MB | 1 年前3
 Go 入门指南(The way to Go)拥有断点、检查变量值、单步执行、逐过程执行标识库中代码的能力。 16. 能够方便的存取最近使用过的文件或项目。 17. 拥有对包、类型、变量、函数和方法的智能代码补全的功能。 18. 能够对项目或包中的代码建立抽象语法树视图(AST-view)。 19. 内置 Go 的相关工具。 20. 能够方便完整地查阅 Go 文档。 21. 能够方便地在不同的 Go 环境之间切换。 22. 能够导出不同格式的代码文件,如:PDF,HTML0 码力 | 466 页 | 4.44 MB | 1 年前3
共 7 条
- 1
 













