Example #1
0
File: debug.go Project: 8l/leaf
func (self *Package) Print() {
	// just for debugging
	p := prt.Stdout()
	p.Indent = "    "

	p.Println("package", self.path)
	p.ShiftIn()

	for _, t := range self.types {
		name := t.Name()
		obj := t.Obj()
		switch t := obj.(type) {
		case *types.Named:
			p.Printf("type %s %s", name, t.Type)
		case types.Basic:
			p.Printf("type %s <%s>", name, t.String())
		case *types.Pointer:
			assert(t.Type == nil)
			p.Printf("type %s", name)
		default:
			panic(reflect.TypeOf(obj))
		}
	}

	for _, f := range self.funcs {
		name := f.Name()
		obj := f.Obj()
		switch f := obj.(type) {
		case *Func:
			p.Printf("func %s%s", name, f.t.SigString())
			if f.code != nil {
				p.ShiftIn()
				printInsts(p, f.code)
				p.ShiftOut()
			}
		default:
			panic("bug")
		}
	}

	if len(self.files) > 0 {
		p.Print("<files>")
		p.ShiftIn()
		for _, f := range self.files {
			p.Print(f.name)
		}
		p.ShiftOut()
	}

	p.ShiftOut()
	p.Println()
}
Example #2
0
File: main.go Project: 8l/leaf
func mainParse(args []string) {
	fset := flag.NewFlagSet("asm8-parse", flag.ExitOnError)
	astFlag := fset.Bool("ast", false, "print AST instead of token tree")
	fset.Parse(args)

	files := fset.Args()

	if len(files) != 1 {
		fmt.Fprintln(os.Stderr, "expects one single input file.")
		os.Exit(1)
	}

	onError := func(e error) { fmt.Fprintln(os.Stderr, e) }

	f := files[0]
	fin, e := os.Open(f)
	if e != nil {
		onError(e)
		os.Exit(1)
		return
	}

	parser := parser.New(fin, f)
	res, errs := parser.Parse()

	if *astFlag {
		if res != nil {
			p := prt.Stdout()
			p.Indent = "    "
			ast.Print(p, res)
		}
	} else {
		parser.PrintTree(os.Stdout)
	}

	for _, e := range errs {
		onError(e)
	}

	if len(errs) > 0 {
		os.Exit(1)
		return
	}
}