From 3c9b8172578177fe5d2137db942c488cdfd343d7 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 16 Oct 2008 14:25:23 -0700 Subject: [PATCH] snapshot: - typeguards, var decls, several printing bug fixed - now fully idempotent on many files (which are accepted by 6g afterwards) - still some detail issues R=r OCL=17310 CL=17310 --- usr/gri/pretty/Makefile | 11 ------ usr/gri/pretty/parser.go | 19 +++++----- usr/gri/pretty/printer.go | 77 +++++++++++++++++++++++---------------- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/usr/gri/pretty/Makefile b/usr/gri/pretty/Makefile index ae49902ea1..fe99249c82 100644 --- a/usr/gri/pretty/Makefile +++ b/usr/gri/pretty/Makefile @@ -58,17 +58,6 @@ testnoisy: pretty pretty $(GOROOT)/usr/r/*/*.go echo "DONE" -# These tests don't work yet -testfull: pretty - pretty *.go - pretty ../gosrc/*.go - pretty $(GOROOT)/test/*.go - pretty $(GOROOT)/src/pkg/*.go - pretty $(GOROOT)/src/lib/*.go - pretty $(GOROOT)/src/lib/*/*.go - pretty $(GOROOT)/usr/r/*/*.go - echo "DONE" - install: pretty cp pretty $(HOME)/bin/pretty diff --git a/usr/gri/pretty/parser.go b/usr/gri/pretty/parser.go index 53145b462b..23dbf55e29 100644 --- a/usr/gri/pretty/parser.go +++ b/usr/gri/pretty/parser.go @@ -139,7 +139,7 @@ func (P *Parser) ParseIdentList() *Node.Expr { pos := P.pos; P.Next(); y := P.ParseIdentList(); - x := Node.NewExpr(pos, Scanner.COMMA, x, y); + x = Node.NewExpr(pos, Scanner.COMMA, x, y); } P.Ecart(); @@ -369,14 +369,13 @@ func (P *Parser) ParseFunctionType() *Node.Type { } -func (P *Parser) ParseMethodDecl() *Node.Decl { +func (P *Parser) ParseMethodSpec(list *Node.List) { P.Trace("MethodDecl"); - P.ParseIdent(); - P.ParseFunctionType(); + list.Add(P.ParseIdent()); + list.Add(Node.NewTypeExpr(P.ParseFunctionType())); P.Ecart(); - return nil; } @@ -387,8 +386,9 @@ func (P *Parser) ParseInterfaceType() *Node.Type { P.Expect(Scanner.INTERFACE); if P.tok == Scanner.LBRACE { P.Next(); + t.list = Node.NewList(); for P.tok == Scanner.IDENT { - P.ParseMethodDecl(); + P.ParseMethodSpec(t.list); if P.tok != Scanner.RBRACE { P.Expect(Scanner.SEMICOLON); } @@ -591,16 +591,15 @@ func (P *Parser) ParseOperand() *Node.Expr { func (P *Parser) ParseSelectorOrTypeGuard(x *Node.Expr) *Node.Expr { P.Trace("SelectorOrTypeGuard"); - pos := P.pos; + x = Node.NewExpr(P.pos, Scanner.PERIOD, x, nil); P.Expect(Scanner.PERIOD); if P.tok == Scanner.IDENT { - y := P.ParseIdent(); - x = Node.NewExpr(pos, Scanner.PERIOD, x, y); + x.y = P.ParseIdent(); } else { P.Expect(Scanner.LPAREN); - P.ParseType(); + x.t = P.ParseType(); P.Expect(Scanner.RPAREN); } diff --git a/usr/gri/pretty/printer.go b/usr/gri/pretty/printer.go index 7259514017..9ec7594fb6 100644 --- a/usr/gri/pretty/printer.go +++ b/usr/gri/pretty/printer.go @@ -90,6 +90,7 @@ func (P *Printer) Parameters(pos int, list *Node.List) { func (P *Printer) Fields(list *Node.List) { + P.OpenScope(" {"); var prev int; for i, n := 0, list.len(); i < n; i++ { x := list.at(i).(*Node.Expr); @@ -107,6 +108,7 @@ func (P *Printer) Fields(list *Node.List) { prev = x.tok; } P.newl = 1; + P.CloseScope("}"); } @@ -128,12 +130,11 @@ func (P *Printer) Type(t *Node.Type) { P.String(0, "]"); P.Type(t.elt); - case Scanner.STRUCT: - P.String(t.pos, "struct"); + case Scanner.STRUCT, Scanner.INTERFACE: + P.Token(t.pos, t.tok); if t.list != nil { - P.OpenScope(" {"); + P.Blank(); P.Fields(t.list); - P.CloseScope("}"); } case Scanner.MAP: @@ -152,19 +153,6 @@ func (P *Printer) Type(t *Node.Type) { P.String(t.pos, m); P.Type(t.elt); - case Scanner.INTERFACE: - P.String(t.pos, "interface"); - if t.list != nil { - P.OpenScope(" {"); - /* - for i := 0; i < x.methods.len(); i++ { - P.Print(x.methods.at(i)); - P.newl, P.semi = true, true; - } - */ - P.CloseScope("}"); - } - case Scanner.MUL: P.String(t.pos, "*"); P.Type(t.elt); @@ -192,40 +180,54 @@ func (P *Printer) Expr1(x *Node.Expr, prec1 int) { switch x.tok { case Scanner.TYPE: + // type expr P.Type(x.t); case Scanner.IDENT, Scanner.INT, Scanner.STRING, Scanner.FLOAT: + // literal P.String(x.pos, x.s); case Scanner.COMMA: + // list P.Expr1(x.x, 0); P.String(x.pos, ", "); P.Expr1(x.y, 0); case Scanner.PERIOD: + // selector or type guard P.Expr1(x.x, 8); // 8 == highest precedence P.String(x.pos, "."); - P.Expr1(x.y, 8); + if x.y != nil { + P.Expr1(x.y, 8); + } else { + P.String(0, "("); + P.Type(x.t); + P.String(0, ")"); + } case Scanner.LBRACK: + // index P.Expr1(x.x, 8); P.String(x.pos, "["); P.Expr1(x.y, 0); P.String(0, "]"); case Scanner.LPAREN: + // call P.Expr1(x.x, 8); P.String(x.pos, "("); P.Expr1(x.y, 0); P.String(0, ")"); case Scanner.LBRACE: + // composite P.Expr1(x.x, 8); P.String(x.pos, "{"); P.Expr1(x.y, 0); P.String(0, "}"); default: + // unary and binary expressions if x.x == nil { // unary expression P.Token(x.pos, x.tok); @@ -281,21 +283,30 @@ func (P *Printer) Block(list *Node.List, indent bool) { func (P *Printer) ControlClause(s *Node.Stat) { - if s.init != nil { + has_post := s.tok == Scanner.FOR && s.post != nil; // post also used by "if" + if s.init == nil && !has_post { + // no semicolons required + if s.expr != nil { + P.Blank(); + P.Expr(s.expr); + } + } else { + // all semicolons required P.Blank(); - P.Stat(s.init); - P.semi = true; - } - if s.expr != nil { - P.Blank(); - P.Expr(s.expr); - P.semi = false; - } - if s.tok == Scanner.FOR && s.post != nil { + if s.init != nil { + P.Stat(s.init); + } P.semi = true; P.Blank(); - P.Stat(s.post); - P.semi = false; + if s.expr != nil { + P.Expr(s.expr); + } + if has_post { + P.semi = true; + P.Blank(); + P.Stat(s.post); + P.semi = false + } } P.Blank(); } @@ -375,9 +386,11 @@ func (P *Printer) Stat(s *Node.Stat) { P.Expr(s.expr); } P.String(0, ":"); - P.OpenScope(""); + P.indent++; + P.newl = 1; P.StatementList(s.block); - P.CloseScope(""); + P.indent--; + P.newl = 1; case Scanner.GO, Scanner.RETURN, Scanner.FALLTHROUGH, Scanner.BREAK, Scanner.CONTINUE, Scanner.GOTO: P.Token(s.pos, s.tok);