Commit 2dba9a66 authored by Robert Griesemer's avatar Robert Griesemer

- 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
parent dfa5893d
...@@ -320,7 +320,7 @@ func (P *Printer) Error(pos int, tok int, msg string) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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);
} else {
P.String(d.val.pos, ""); // flush pending ';' separator/newlines
} }
P.separator = tab;
P.Expr(d.val);
P.separator = semicolon;
case Scanner.EXPORT:
P.Expr(d.ident); 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 { if d.typ != nil {
if d.tok != Scanner.FUNC { P.separator = blank; // TODO switch to tab? (indentation problem with structs)
// TODO would like to change this to a tab separator P.separator = P.Type(d.typ);
// 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;
} }
if d.val != nil { if d.val != nil {
if d.tok != Scanner.IMPORT {
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);
} }
P.separator = semicolon;
if d.list != nil { case Scanner.FUNC:
if d.tok != Scanner.FUNC { if d.typ.key != nil {
panic("must be a func declaration"); // 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.separator = blank;
P.Block(0, d.list, d.end, true); P.Block(0, d.list, d.end, true);
} }
if d.tok != Scanner.TYPE { default:
P.separator = semicolon; P.Error(d.pos, d.tok, "decl");
} }
} }
...@@ -787,7 +806,7 @@ export func Print(prog *AST.Program) { ...@@ -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");
......
...@@ -60,6 +60,12 @@ func f1(tag int) { ...@@ -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++ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment