// PrintTo prints the level using the indent printer func (lvl *Level) PrintTo(p prt.Iface) { p.Printf("+ %s:", lvl.Name) p.ShiftIn() for _, node := range lvl.Nodes { level, isLevel := node.(*Level) if isLevel { level.PrintTo(p) } else { p.Printf("- %s", node) } } p.ShiftOut() }
func Print(p prt.Iface, n Node) { if n == nil { p.Printf("! nil") return } switch n := n.(type) { case *Program: p.Printf("+ src: %s", n.Filename) p.ShiftIn() for _, d := range n.Decls { Print(p, d) } p.ShiftOut() case *Func: p.Printf("+ func: %s", n.Name) if len(n.Args) > 0 { p.Printf(" args:") p.ShiftIn() for _, a := range n.Args { Print(p, a) } p.ShiftOut() } if n.Ret != nil { p.Printf(" ret: %s", n.Ret) } Print(p, n.Block) case *Block: p.Print(" {") p.ShiftIn() for _, s := range n.Stmts { Print(p, s) } p.ShiftOut(" }") case *EmptyStmt: p.Print("+ <empty-stmt>") case *ExprStmt: p.Print("+ <expr-stmt>:") p.ShiftIn() Print(p, n.Expr) p.ShiftOut() case *CallExpr: p.Print("+ <call-expr>") p.Print(" func:") p.ShiftIn() Print(p, n.Func) p.ShiftOut() if len(n.Args) > 0 { p.Print(" args:") p.ShiftIn() for _, a := range n.Args { Print(p, a) } p.ShiftOut() } case *Operand: p.Printf("+ <operand>: %s", n.Token) default: p.Printf("? %s: %s", reflect.TypeOf(n), n) } }
// Print prints the AST with a printer. func Print(p prt.Iface, n interface{}) { if n == nil { p.Print("! nil") return } switch n := n.(type) { case *Program: p.Printf("+ prog: %s", n.Filename) p.ShiftIn() for _, d := range n.Decls { Print(p, d) } p.ShiftOut() case *Func: p.Printf("+ func: %s", n.Name) Print(p, n.Block) case *Block: p.ShiftIn() for _, line := range n.Lines { Print(p, line) } p.ShiftOut() case *Line: if n.Label != nil { p.Printf("%s:", n.Label.Name) } if n.Inst != nil { p.Print(" ", n.Inst.String()) } case *Var: p.Print(varString(n)) default: p.Printf("? %s: %s", reflect.TypeOf(n), n) } }