1
0
mirror of https://github.com/golang/go synced 2024-11-13 17:30:24 -07:00

- fixed a bug with import printing (missing separator between alias and string)

- rewrote declaration printing - was unreadable before
- no semicolons after closing "}" for types

R=r
OCL=20379
CL=20379
This commit is contained in:
Robert Griesemer 2008-12-03 15:47:30 -08:00
parent dfa5893d4f
commit 2dba9a66e2
2 changed files with 63 additions and 38 deletions

View File

@ -320,7 +320,7 @@ func (P *Printer) Error(pos int, tok int, msg string) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Types // Types
func (P *Printer) Type(t *AST.Type) func (P *Printer) Type(t *AST.Type) int
func (P *Printer) Expr(x *AST.Expr) func (P *Printer) Expr(x *AST.Expr)
func (P *Printer) Parameters(pos int, list *array.Array) { func (P *Printer) Parameters(pos int, list *array.Array) {
@ -374,7 +374,11 @@ func (P *Printer) Fields(list *array.Array, end int) {
} }
func (P *Printer) Type(t *AST.Type) { // Returns the separator (semicolon or none) required if
// the type is terminating a declaration or statement.
func (P *Printer) Type(t *AST.Type) int {
separator := semicolon;
switch t.tok { switch t.tok {
case Scanner.IDENT: case Scanner.IDENT:
P.Expr(t.expr); P.Expr(t.expr);
@ -385,7 +389,7 @@ func (P *Printer) Type(t *AST.Type) {
P.Expr(t.expr); P.Expr(t.expr);
} }
P.String(0, "]"); P.String(0, "]");
P.Type(t.elt); separator = P.Type(t.elt);
case Scanner.STRUCT, Scanner.INTERFACE: case Scanner.STRUCT, Scanner.INTERFACE:
P.Token(t.pos, t.tok); P.Token(t.pos, t.tok);
@ -393,12 +397,13 @@ func (P *Printer) Type(t *AST.Type) {
P.separator = blank; P.separator = blank;
P.Fields(t.list, t.end); P.Fields(t.list, t.end);
} }
separator = none;
case Scanner.MAP: case Scanner.MAP:
P.String(t.pos, "map ["); P.String(t.pos, "map [");
P.Type(t.key); P.Type(t.key);
P.String(0, "]"); P.String(0, "]");
P.Type(t.elt); separator = P.Type(t.elt);
case Scanner.CHAN: case Scanner.CHAN:
var m string; var m string;
@ -408,11 +413,11 @@ func (P *Printer) Type(t *AST.Type) {
case AST.SEND: m = "chan <- "; case AST.SEND: m = "chan <- ";
} }
P.String(t.pos, m); P.String(t.pos, m);
P.Type(t.elt); separator = P.Type(t.elt);
case Scanner.MUL: case Scanner.MUL:
P.String(t.pos, "*"); P.String(t.pos, "*");
P.Type(t.elt); separator = P.Type(t.elt);
case Scanner.LPAREN: case Scanner.LPAREN:
P.Parameters(t.pos, t.list); P.Parameters(t.pos, t.list);
@ -433,6 +438,8 @@ func (P *Printer) Type(t *AST.Type) {
default: default:
P.Error(t.pos, t.tok, "type"); P.Error(t.pos, t.tok, "type");
} }
return separator;
} }
@ -685,8 +692,6 @@ func (P *Printer) Stat(s *AST.Stat) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Declarations // Declarations
// TODO This code is unreadable! Clean up AST and rewrite this.
func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) { func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
if !parenthesized { if !parenthesized {
if d.exported { if d.exported {
@ -698,6 +703,7 @@ func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
} }
if d.tok != Scanner.FUNC && d.list != nil { if d.tok != Scanner.FUNC && d.list != nil {
// group of parenthesized declarations
P.state = opening_scope; P.state = opening_scope;
P.String(0, "("); P.String(0, "(");
if d.list.Len() > 0 { if d.list.Len() > 0 {
@ -712,43 +718,56 @@ func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
P.String(d.end, ")"); P.String(d.end, ")");
} else { } else {
if d.tok == Scanner.FUNC && d.typ.key != nil { // single declaration
P.Parameters(0, d.typ.key.list); switch d.tok {
P.separator = blank; case Scanner.IMPORT:
} if d.ident != nil {
P.Expr(d.ident);
P.Expr(d.ident); } else {
P.String(d.val.pos, ""); // flush pending ';' separator/newlines
if d.typ != nil {
if d.tok != Scanner.FUNC {
// TODO would like to change this to a tab separator
// but currently this causes trouble when the type is
// a struct/interface (fields are indented wrongly)
P.separator = blank;
} }
P.Type(d.typ);
P.separator = tab; P.separator = tab;
} P.Expr(d.val);
P.separator = semicolon;
if d.val != nil { case Scanner.EXPORT:
if d.tok != Scanner.IMPORT { P.Expr(d.ident);
P.separator = semicolon;
case Scanner.TYPE:
P.Expr(d.ident);
P.separator = blank; // TODO switch to tab? (but indentation problem with structs)
P.separator = P.Type(d.typ);
case Scanner.CONST, Scanner.VAR:
P.Expr(d.ident);
if d.typ != nil {
P.separator = blank; // TODO switch to tab? (indentation problem with structs)
P.separator = P.Type(d.typ);
}
if d.val != nil {
P.separator = tab; P.separator = tab;
P.String(0, "="); P.String(0, "=");
P.separator = blank; P.separator = blank;
P.Expr(d.val);
} }
P.Expr(d.val);
}
if d.list != nil {
if d.tok != Scanner.FUNC {
panic("must be a func declaration");
}
P.separator = blank;
P.Block(0, d.list, d.end, true);
}
if d.tok != Scanner.TYPE {
P.separator = semicolon; P.separator = semicolon;
case Scanner.FUNC:
if d.typ.key != nil {
// method: print receiver
P.Parameters(0, d.typ.key.list);
P.separator = blank;
}
P.Expr(d.ident);
P.separator = P.Type(d.typ);
if d.list != nil {
P.separator = blank;
P.Block(0, d.list, d.end, true);
}
default:
P.Error(d.pos, d.tok, "decl");
} }
} }
@ -787,7 +806,7 @@ export func Print(prog *AST.Program) {
P.Program(prog); P.Program(prog);
// flush // flush
P.String(0, ""); P.String(0, ""); // flush pending separator/newlines
err := P.writer.Flush(); err := P.writer.Flush();
if err != nil { if err != nil {
panic("print error - exiting"); panic("print error - exiting");

View File

@ -60,6 +60,12 @@ func f1(tag int) {
} }
func f2(tag int) {
type T1 struct {}
var x T
}
func main() { func main() {
// the prologue // the prologue
for i := 0; i <= 10 /* limit */; i++ { for i := 0; i <= 10 /* limit */; i++ {