YaK:: golang : Parser & Abstract Syntax Tree [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

golang: Parser & Abstract Syntax Tree

package main

import "flag"
import "go/ast"
import "go/parser"
import _ "go/printer"
import "go/token"
import "go/types"
import "os"

const FOO = 23

var BAR int = FOO

type TYPE int

type Worm struct {
        wormlen int64
}
type Apple struct {
        Worm
        color    string
        rotation complex128
}
type WormLener interface {
        WormLen() int64
}

func (p *Worm) WormLen() int64 { return p.wormlen }

func main() {
        flag.Parse()

        aaa := new(Apple)
        xxx := aaa.WormLen()
        var www WormLener = aaa
        println(www.(*Apple).rotation, xxx)

        fset := token.NewFileSet()
        files := make(map[string]*ast.File)

        for _, filename := range flag.Args() {

                tree, err := parser.ParseFile(fset, filename, nil, 0)
                if err != nil {
                        panic(err)
                }

                /*
                   println("#####################################")
                   //_, err = ast.Fprint(os.Stdout, fset, tree, nil)

                   println("#####################################")
                   //if err = printer.Fprint(os.Stdout, fset, tree); err != nil { panic(err) }

                   println("#####################################")
                   println("## Universe");
                   for k, v := range types.Universe.Objects {
                     println("k=", k, "  v=", v)
                   }

                   println("## Unsafe");
                   for k, v := range types.Unsafe.Objects {
                     println("k=", k, "  v=", v)
                   }
                */
                println("#####################################")
                importer := types.GcImporter
                univ := types.Universe

                files[filename] = tree
                pkg, err := ast.NewPackage(fset, files, importer, univ)
                if err != nil {
                        panic(err)
                }
                _ = pkg

                n, err := ast.Fprint(os.Stdout, fset, tree, nil)
                println("n=", n)
        }

        for name, tree := range files {
                println("#####################################")
                println("#######################")
                println("#######################   ", name)
                println("#######################")
                n, err := ast.Fprint(os.Stdout, fset, tree, nil)
                if err != nil {
                        panic(err)
                }
                println("n=", n)
                println("")
        }
}

Output:

6g ast.go && 6l ast.6 &&

./6.out ast.go | grep -v 'ast[.]go:' | grep -v '}$' | grep -v ": -$" | grep -v " nil$"

ast.txt

(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2011-05-06)       [Login]
(No back references.)