1
0
mirror of https://github.com/golang/go synced 2024-11-12 10:20:27 -07:00

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
This commit is contained in:
Robert Griesemer 2008-10-16 14:25:23 -07:00
parent 493a9d2f42
commit 3c9b817257
3 changed files with 54 additions and 53 deletions

View File

@ -58,17 +58,6 @@ testnoisy: pretty
pretty $(GOROOT)/usr/r/*/*.go pretty $(GOROOT)/usr/r/*/*.go
echo "DONE" 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 install: pretty
cp pretty $(HOME)/bin/pretty cp pretty $(HOME)/bin/pretty

View File

@ -139,7 +139,7 @@ func (P *Parser) ParseIdentList() *Node.Expr {
pos := P.pos; pos := P.pos;
P.Next(); P.Next();
y := P.ParseIdentList(); y := P.ParseIdentList();
x := Node.NewExpr(pos, Scanner.COMMA, x, y); x = Node.NewExpr(pos, Scanner.COMMA, x, y);
} }
P.Ecart(); 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.Trace("MethodDecl");
P.ParseIdent(); list.Add(P.ParseIdent());
P.ParseFunctionType(); list.Add(Node.NewTypeExpr(P.ParseFunctionType()));
P.Ecart(); P.Ecart();
return nil;
} }
@ -387,8 +386,9 @@ func (P *Parser) ParseInterfaceType() *Node.Type {
P.Expect(Scanner.INTERFACE); P.Expect(Scanner.INTERFACE);
if P.tok == Scanner.LBRACE { if P.tok == Scanner.LBRACE {
P.Next(); P.Next();
t.list = Node.NewList();
for P.tok == Scanner.IDENT { for P.tok == Scanner.IDENT {
P.ParseMethodDecl(); P.ParseMethodSpec(t.list);
if P.tok != Scanner.RBRACE { if P.tok != Scanner.RBRACE {
P.Expect(Scanner.SEMICOLON); P.Expect(Scanner.SEMICOLON);
} }
@ -591,16 +591,15 @@ func (P *Parser) ParseOperand() *Node.Expr {
func (P *Parser) ParseSelectorOrTypeGuard(x *Node.Expr) *Node.Expr { func (P *Parser) ParseSelectorOrTypeGuard(x *Node.Expr) *Node.Expr {
P.Trace("SelectorOrTypeGuard"); P.Trace("SelectorOrTypeGuard");
pos := P.pos; x = Node.NewExpr(P.pos, Scanner.PERIOD, x, nil);
P.Expect(Scanner.PERIOD); P.Expect(Scanner.PERIOD);
if P.tok == Scanner.IDENT { if P.tok == Scanner.IDENT {
y := P.ParseIdent(); x.y = P.ParseIdent();
x = Node.NewExpr(pos, Scanner.PERIOD, x, y);
} else { } else {
P.Expect(Scanner.LPAREN); P.Expect(Scanner.LPAREN);
P.ParseType(); x.t = P.ParseType();
P.Expect(Scanner.RPAREN); P.Expect(Scanner.RPAREN);
} }

View File

@ -90,6 +90,7 @@ func (P *Printer) Parameters(pos int, list *Node.List) {
func (P *Printer) Fields(list *Node.List) { func (P *Printer) Fields(list *Node.List) {
P.OpenScope(" {");
var prev int; var prev int;
for i, n := 0, list.len(); i < n; i++ { for i, n := 0, list.len(); i < n; i++ {
x := list.at(i).(*Node.Expr); x := list.at(i).(*Node.Expr);
@ -107,6 +108,7 @@ func (P *Printer) Fields(list *Node.List) {
prev = x.tok; prev = x.tok;
} }
P.newl = 1; P.newl = 1;
P.CloseScope("}");
} }
@ -128,12 +130,11 @@ func (P *Printer) Type(t *Node.Type) {
P.String(0, "]"); P.String(0, "]");
P.Type(t.elt); P.Type(t.elt);
case Scanner.STRUCT: case Scanner.STRUCT, Scanner.INTERFACE:
P.String(t.pos, "struct"); P.Token(t.pos, t.tok);
if t.list != nil { if t.list != nil {
P.OpenScope(" {"); P.Blank();
P.Fields(t.list); P.Fields(t.list);
P.CloseScope("}");
} }
case Scanner.MAP: case Scanner.MAP:
@ -152,19 +153,6 @@ func (P *Printer) Type(t *Node.Type) {
P.String(t.pos, m); P.String(t.pos, m);
P.Type(t.elt); 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: case Scanner.MUL:
P.String(t.pos, "*"); P.String(t.pos, "*");
P.Type(t.elt); P.Type(t.elt);
@ -192,40 +180,54 @@ func (P *Printer) Expr1(x *Node.Expr, prec1 int) {
switch x.tok { switch x.tok {
case Scanner.TYPE: case Scanner.TYPE:
// type expr
P.Type(x.t); P.Type(x.t);
case Scanner.IDENT, Scanner.INT, Scanner.STRING, Scanner.FLOAT: case Scanner.IDENT, Scanner.INT, Scanner.STRING, Scanner.FLOAT:
// literal
P.String(x.pos, x.s); P.String(x.pos, x.s);
case Scanner.COMMA: case Scanner.COMMA:
// list
P.Expr1(x.x, 0); P.Expr1(x.x, 0);
P.String(x.pos, ", "); P.String(x.pos, ", ");
P.Expr1(x.y, 0); P.Expr1(x.y, 0);
case Scanner.PERIOD: case Scanner.PERIOD:
// selector or type guard
P.Expr1(x.x, 8); // 8 == highest precedence P.Expr1(x.x, 8); // 8 == highest precedence
P.String(x.pos, "."); 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: case Scanner.LBRACK:
// index
P.Expr1(x.x, 8); P.Expr1(x.x, 8);
P.String(x.pos, "["); P.String(x.pos, "[");
P.Expr1(x.y, 0); P.Expr1(x.y, 0);
P.String(0, "]"); P.String(0, "]");
case Scanner.LPAREN: case Scanner.LPAREN:
// call
P.Expr1(x.x, 8); P.Expr1(x.x, 8);
P.String(x.pos, "("); P.String(x.pos, "(");
P.Expr1(x.y, 0); P.Expr1(x.y, 0);
P.String(0, ")"); P.String(0, ")");
case Scanner.LBRACE: case Scanner.LBRACE:
// composite
P.Expr1(x.x, 8); P.Expr1(x.x, 8);
P.String(x.pos, "{"); P.String(x.pos, "{");
P.Expr1(x.y, 0); P.Expr1(x.y, 0);
P.String(0, "}"); P.String(0, "}");
default: default:
// unary and binary expressions
if x.x == nil { if x.x == nil {
// unary expression // unary expression
P.Token(x.pos, x.tok); 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) { 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.Blank();
P.Stat(s.init); if s.init != nil {
P.semi = true; P.Stat(s.init);
} }
if s.expr != nil {
P.Blank();
P.Expr(s.expr);
P.semi = false;
}
if s.tok == Scanner.FOR && s.post != nil {
P.semi = true; P.semi = true;
P.Blank(); P.Blank();
P.Stat(s.post); if s.expr != nil {
P.semi = false; P.Expr(s.expr);
}
if has_post {
P.semi = true;
P.Blank();
P.Stat(s.post);
P.semi = false
}
} }
P.Blank(); P.Blank();
} }
@ -375,9 +386,11 @@ func (P *Printer) Stat(s *Node.Stat) {
P.Expr(s.expr); P.Expr(s.expr);
} }
P.String(0, ":"); P.String(0, ":");
P.OpenScope(""); P.indent++;
P.newl = 1;
P.StatementList(s.block); P.StatementList(s.block);
P.CloseScope(""); P.indent--;
P.newl = 1;
case Scanner.GO, Scanner.RETURN, Scanner.FALLTHROUGH, Scanner.BREAK, Scanner.CONTINUE, Scanner.GOTO: case Scanner.GO, Scanner.RETURN, Scanner.FALLTHROUGH, Scanner.BREAK, Scanner.CONTINUE, Scanner.GOTO:
P.Token(s.pos, s.tok); P.Token(s.pos, s.tok);