1
0
mirror of https://github.com/golang/go synced 2024-11-14 07:50:21 -07:00

- converted expr representation of ast into a new representation

using interfaces properly => much cleaner code
- converted tracing code to use 'defer' statement
- next steps: convert rest of ast as well

R=r
OCL=24277
CL=24277
This commit is contained in:
Robert Griesemer 2009-02-03 17:44:01 -08:00
parent f9cc900ae8
commit c048ee21ad
4 changed files with 792 additions and 600 deletions

View File

@ -17,8 +17,7 @@ type (
Type struct; Type struct;
Block struct; Block struct;
Lit struct; Expr interface;
Expr struct;
Stat struct; Stat struct;
Decl struct; Decl struct;
) )
@ -128,26 +127,6 @@ type Node struct {
} }
// ----------------------------------------------------------------------------
// Literals
type Lit struct {
Node;
// Identifiers
Obj *Object;
// Constant literals
// Type literals
Len *Expr; // array length
Dir int; // channel direction
Key *Type; // receiver or map key type
Elt *Type; // array, map, channel, pointer element, or function result type
List *array.Array; End int; // struct fields, interface methods, function parameters
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Scopes // Scopes
@ -218,87 +197,6 @@ func (scope *Scope) Print() {
} }
// ----------------------------------------------------------------------------
// Blocks
//
// Syntactic constructs of the form:
//
// "{" StatementList "}"
// ":" StatementList
type Block struct {
Node;
List *array.Array;
End int; // position of closing "}" if present
}
func NewBlock(pos, tok int) *Block {
assert(tok == Scanner.LBRACE || tok == Scanner.COLON);
b := new(Block);
b.Pos, b.Tok, b.List = pos, tok, array.New(0);
return b;
}
// ----------------------------------------------------------------------------
// Expressions
type Expr struct {
Node;
X, Y *Expr; // binary (X, Y) and unary (Y) expressions
Obj *Object; // identifiers, literals
Typ *Type;
}
// Length of a comma-separated expression list.
func (x *Expr) Len() int {
if x == nil {
return 0;
}
n := 1;
for ; x.Tok == Scanner.COMMA; x = x.Y {
n++;
}
return n;
}
// The i'th expression in a comma-separated expression list.
func (x *Expr) At(i int) *Expr {
for j := 0; j < i; j++ {
assert(x.Tok == Scanner.COMMA);
x = x.Y;
}
if x.Tok == Scanner.COMMA {
x = x.X;
}
return x;
}
func NewExpr(pos, tok int, x, y *Expr) *Expr {
if x != nil && x.Tok == Scanner.TYPE || y != nil && y.Tok == Scanner.TYPE {
panic("no type expression allowed");
}
e := new(Expr);
e.Pos, e.Tok, e.X, e.Y = pos, tok, x, y;
return e;
}
// TODO probably don't need the tok parameter eventually
func NewLit(tok int, obj *Object) *Expr {
e := new(Expr);
e.Pos, e.Tok, e.Obj, e.Typ = obj.Pos, tok, obj, obj.Typ;
return e;
}
var BadExpr = NewExpr(0, Scanner.ILLEGAL, nil, nil);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Types // Types
@ -388,7 +286,7 @@ type Type struct {
// syntactic components // syntactic components
Pos int; // source position (< 0 if unknown position) Pos int; // source position (< 0 if unknown position)
Expr *Expr; // type name, array length Expr Expr; // type name, array length
Mode int; // channel mode Mode int; // channel mode
Key *Type; // receiver type or map key Key *Type; // receiver type or map key
Elt *Type; // type name type, array, map, channel or pointer element type, function result type Elt *Type; // type name type, array, map, channel or pointer element type, function result type
@ -411,13 +309,200 @@ func NewType(pos, form int) *Type {
} }
func (typ* Type) String() string {
if typ != nil {
return
"Type(" +
FormStr(typ.Form) +
")";
}
return "nil";
}
var BadType = NewType(0, Scanner.ILLEGAL);
// ----------------------------------------------------------------------------
// Blocks
//
// Syntactic constructs of the form:
//
// "{" StatementList "}"
// ":" StatementList
type Block struct {
Node;
List *array.Array;
End int; // position of closing "}" if present
}
func NewBlock(pos, tok int) *Block {
assert(tok == Scanner.LBRACE || tok == Scanner.COLON);
b := new(Block);
b.Pos, b.Tok, b.List = pos, tok, array.New(0);
return b;
}
// ----------------------------------------------------------------------------
// Expressions
type (
Visitor interface;
Expr interface {
Pos() int;
Visit(v Visitor);
};
BadExpr struct {
Pos_ int;
};
Ident struct {
Pos_ int;
Obj *Object;
};
BinaryExpr struct {
Pos_, Tok int;
X, Y Expr;
};
UnaryExpr struct {
Pos_, Tok int;
X Expr;
};
BasicLit struct {
Pos_, Tok int;
Val string
};
FunctionLit struct {
Pos_ int; // position of "func"
Typ *Type;
Body *Block;
};
CompositeLit struct {
Pos_ int; // position of "{"
Typ *Type;
Elts Expr;
};
TypeLit struct {
Typ *Type;
};
Selector struct {
Pos_ int; // position of "."
X Expr;
Sel *Ident;
};
TypeGuard struct {
Pos_ int; // position of "."
X Expr;
Typ *Type;
};
Index struct {
Pos_ int; // position of "["
X, I Expr;
};
Call struct {
Pos_ int; // position of "("
F, Args Expr
};
)
type Visitor interface {
DoBadExpr(x *BadExpr);
DoIdent(x *Ident);
DoBinaryExpr(x *BinaryExpr);
DoUnaryExpr(x *UnaryExpr);
DoBasicLit(x *BasicLit);
DoFunctionLit(x *FunctionLit);
DoCompositeLit(x *CompositeLit);
DoTypeLit(x *TypeLit);
DoSelector(x *Selector);
DoTypeGuard(x *TypeGuard);
DoIndex(x *Index);
DoCall(x *Call);
}
func (x *BadExpr) Pos() int { return x.Pos_; }
func (x *Ident) Pos() int { return x.Pos_; }
func (x *BinaryExpr) Pos() int { return x.Pos_; }
func (x *UnaryExpr) Pos() int { return x.Pos_; }
func (x *BasicLit) Pos() int { return x.Pos_; }
func (x *FunctionLit) Pos() int { return x.Pos_; }
func (x *CompositeLit) Pos() int { return x.Pos_; }
func (x *TypeLit) Pos() int { return x.Typ.Pos; }
func (x *Selector) Pos() int { return x.Pos_; }
func (x *TypeGuard) Pos() int { return x.Pos_; }
func (x *Index) Pos() int { return x.Pos_; }
func (x *Call) Pos() int { return x.Pos_; }
func (x *BadExpr) Visit(v Visitor) { v.DoBadExpr(x); }
func (x *Ident) Visit(v Visitor) { v.DoIdent(x); }
func (x *BinaryExpr) Visit(v Visitor) { v.DoBinaryExpr(x); }
func (x *UnaryExpr) Visit(v Visitor) { v.DoUnaryExpr(x); }
func (x *BasicLit) Visit(v Visitor) { v.DoBasicLit(x); }
func (x *FunctionLit) Visit(v Visitor) { v.DoFunctionLit(x); }
func (x *CompositeLit) Visit(v Visitor) { v.DoCompositeLit(x); }
func (x *TypeLit) Visit(v Visitor) { v.DoTypeLit(x); }
func (x *Selector) Visit(v Visitor) { v.DoSelector(x); }
func (x *TypeGuard) Visit(v Visitor) { v.DoTypeGuard(x); }
func (x *Index) Visit(v Visitor) { v.DoIndex(x); }
func (x *Call) Visit(v Visitor) { v.DoCall(x); }
// Length of a comma-separated expression list.
func ExprLen(x Expr) int {
if x == nil {
return 0;
}
n := 1;
for {
if p, ok := x.(*BinaryExpr); ok && p.Tok == Scanner.COMMA {
n++;
x = p.Y;
} else {
break;
}
}
return n;
}
func ExprAt(x Expr, i int) Expr {
for j := 0; j < i; j++ {
assert(x.(*BinaryExpr).Tok == Scanner.COMMA);
x = x.(*BinaryExpr).Y;
}
if t, is_binary := x.(*BinaryExpr); is_binary && t.Tok == Scanner.COMMA {
x = t.X;
}
return x;
}
func (t *Type) Nfields() int { func (t *Type) Nfields() int {
if t.List == nil { if t.List == nil {
return 0; return 0;
} }
nx, nt := 0, 0; nx, nt := 0, 0;
for i, n := 0, t.List.Len(); i < n; i++ { for i, n := 0, t.List.Len(); i < n; i++ {
if t.List.At(i).(*Expr).Tok == Scanner.TYPE { if dummy, ok := t.List.At(i).(*TypeLit); ok {
nt++; nt++;
} else { } else {
nx++; nx++;
@ -430,51 +515,13 @@ func (t *Type) Nfields() int {
} }
func (typ* Type) String() string {
if typ != nil {
return
"Type(" +
FormStr(typ.Form) +
")";
}
return "nil";
}
// requires complete Type.Pos access
func NewTypeExpr(typ *Type) *Expr {
e := new(Expr);
e.Pos, e.Tok, e.Typ = typ.Pos, Scanner.TYPE, typ;
return e;
}
// requires complete Type.String access
func (x *Expr) String() string {
if x != nil {
return
"Expr(" +
Scanner.TokenString(x.Tok) + ", " +
x.X.String() + ", " +
x.Y.String() + ", " +
x.Obj.String() + ", " +
x.Typ.String() +
")";
}
return "nil";
}
var BadType = NewType(0, Scanner.ILLEGAL);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Statements // Statements
type Stat struct { type Stat struct {
Node; Node;
Init, Post *Stat; Init, Post *Stat;
Expr *Expr; Expr Expr;
Body *Block; // composite statement body Body *Block; // composite statement body
Decl *Decl; // declaration statement Decl *Decl; // declaration statement
} }
@ -495,9 +542,10 @@ var BadStat = NewStat(0, Scanner.ILLEGAL);
type Decl struct { type Decl struct {
Node; Node;
Ident *Expr; // nil for ()-style declarations Ident Expr; // nil for ()-style declarations
Typ *Type; Typ *Type;
Val *Expr; Val Expr;
Body *Block;
// list of *Decl for ()-style declarations // list of *Decl for ()-style declarations
List *array.Array; End int; List *array.Array; End int;
} }
@ -531,7 +579,7 @@ func NewComment(pos int, text string) *Comment {
type Program struct { type Program struct {
Pos int; // tok is Scanner.PACKAGE Pos int; // tok is Scanner.PACKAGE
Ident *Expr; Ident Expr;
Decls *array.Array; Decls *array.Array;
Comments *array.Array; Comments *array.Array;
} }

View File

@ -158,6 +158,8 @@ func fileExists(name string) bool {
func addDeps(globalset map [string] bool, wset *array.Array, src_file string, flags *Flags) { func addDeps(globalset map [string] bool, wset *array.Array, src_file string, flags *Flags) {
panic();
/*
dummy, found := globalset[src_file]; dummy, found := globalset[src_file];
if !found { if !found {
globalset[src_file] = true; globalset[src_file] = true;
@ -198,6 +200,7 @@ func addDeps(globalset map [string] bool, wset *array.Array, src_file string, fl
print("\n\n"); print("\n\n");
} }
} }
*/
} }

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,26 @@ var (
) )
// ----------------------------------------------------------------------------
// Elementary support
func unimplemented() {
panic("unimplemented");
}
func unreachable() {
panic("unreachable");
}
func assert(pred bool) {
if !pred {
panic("assertion failed");
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Printer // Printer
@ -76,6 +96,9 @@ type Printer struct {
// semantic state // semantic state
state int; // current semantic state state int; // current semantic state
laststate int; // state for last string laststate int; // state for last string
// expression precedence
prec int;
} }
@ -104,6 +127,9 @@ func (P *Printer) Init(text io.Write, comments *array.Array) {
P.NextComment(); P.NextComment();
// formatting parameters & semantic state initialized correctly by default // formatting parameters & semantic state initialized correctly by default
// expression precedence
P.prec = Scanner.LowestPrec;
} }
@ -384,24 +410,21 @@ func (P *Printer) HtmlEpilogue() {
} }
func (P *Printer) HtmlIdentifier(x *AST.Expr) { func (P *Printer) HtmlIdentifier(x *AST.Ident) {
if x.Tok != Scanner.IDENT {
panic();
}
obj := x.Obj; obj := x.Obj;
if *html && obj.Kind != AST.NONE { if *html && obj.Kind != AST.NONE {
// depending on whether we have a declaration or use, generate different html // depending on whether we have a declaration or use, generate different html
// - no need to htmlEscape ident // - no need to htmlEscape ident
id := Utils.IntToString(obj.Id, 10); id := Utils.IntToString(obj.Id, 10);
if x.Pos == obj.Pos { if x.Pos() == obj.Pos {
// probably the declaration of x // probably the declaration of x
P.TaggedString(x.Pos, `<a name="id` + id + `">`, obj.Ident, `</a>`); P.TaggedString(x.Pos(), `<a name="id` + id + `">`, obj.Ident, `</a>`);
} else { } else {
// probably not the declaration of x // probably not the declaration of x
P.TaggedString(x.Pos, `<a href="#id` + id + `">`, obj.Ident, `</a>`); P.TaggedString(x.Pos(), `<a href="#id` + id + `">`, obj.Ident, `</a>`);
} }
} else { } else {
P.String(x.Pos, obj.Ident); P.String(x.Pos(), obj.Ident);
} }
} }
@ -409,32 +432,58 @@ func (P *Printer) HtmlIdentifier(x *AST.Expr) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Types // Types
func (P *Printer) Type(t *AST.Type, full_function_type bool) int 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) Expr1(x *AST.Expr, prec1 int, full_function_type bool)
func (P *Printer) Parameters(pos int, list *array.Array) { func (P *Printer) Parameters(pos int, list *array.Array) {
P.String(pos, "("); P.String(pos, "(");
if list != nil { if list != nil {
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).(*AST.Expr); x := list.At(i).(AST.Expr);
tok := Scanner.TYPE;
if dummy, is_ident := x.(*AST.Ident); is_ident {
tok = Scanner.IDENT;
}
if i > 0 { if i > 0 {
if prev == x.Tok || prev == Scanner.TYPE { if prev == tok || prev == Scanner.TYPE {
P.separator = comma; P.separator = comma;
} else { } else {
P.separator = blank; P.separator = blank;
} }
} }
P.Expr(x); P.Expr(x);
prev = x.Tok; prev = tok;
} }
} }
P.String(0, ")"); P.String(0, ")");
} }
func (P *Printer) Fields(list *array.Array, end int, full_function_type bool) { // Returns the separator (semicolon or none) required if
// the type is terminating a declaration or statement.
func (P *Printer) Signature(t *AST.Type) int {
assert(t.Form == AST.FUNCTION);
separator := none;
P.Parameters(t.Pos, t.List);
if t.Elt != nil {
P.separator = blank;
list := t.Elt.List;
dummy, is_type := list.At(0).(*AST.TypeLit);
if list.Len() > 1 || is_type && dummy.Typ.Form == AST.FUNCTION {
// single, anonymous result types which are functions must
// be parenthesized as well
P.Parameters(0, list);
} else {
// single, anonymous result type
separator = P.Type(list.At(0).(*AST.TypeLit).Typ);
}
}
return separator;
}
func (P *Printer) Fields(list *array.Array, end int, in_interface bool) {
P.state = opening_scope; P.state = opening_scope;
P.String(0, "{"); P.String(0, "{");
@ -442,19 +491,29 @@ func (P *Printer) Fields(list *array.Array, end int, full_function_type bool) {
P.newlines = 1; P.newlines = 1;
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).(*AST.Expr); x := list.At(i).(AST.Expr);
tok := Scanner.TYPE;
if dummy, is_ident := x.(*AST.Ident); is_ident {
tok = Scanner.IDENT;
} else if dummy, is_lit := x.(*AST.BasicLit); is_lit && dummy.Tok == Scanner.STRING {
tok = Scanner.STRING;
}
if i > 0 { if i > 0 {
if prev == Scanner.TYPE && x.Tok != Scanner.STRING || prev == Scanner.STRING { if prev == Scanner.TYPE && tok != Scanner.STRING || prev == Scanner.STRING {
P.separator = semicolon; P.separator = semicolon;
P.newlines = 1; P.newlines = 1;
} else if prev == x.Tok { } else if prev == tok {
P.separator = comma; P.separator = comma;
} else { } else {
P.separator = tab; P.separator = tab;
} }
} }
P.Expr1(x, Scanner.LowestPrec, full_function_type); if in_interface && tok == Scanner.TYPE {
prev = x.Tok; P.Signature(x.(*AST.TypeLit).Typ);
} else {
P.Expr(x);
}
prev = tok;
} }
P.newlines = 1; P.newlines = 1;
} }
@ -466,7 +525,7 @@ func (P *Printer) Fields(list *array.Array, end int, full_function_type bool) {
// Returns the separator (semicolon or none) required if // Returns the separator (semicolon or none) required if
// the type is terminating a declaration or statement. // the type is terminating a declaration or statement.
func (P *Printer) Type(t *AST.Type, full_function_type bool) int { func (P *Printer) Type(t *AST.Type) int {
separator := semicolon; separator := semicolon;
switch t.Form { switch t.Form {
@ -479,7 +538,7 @@ func (P *Printer) Type(t *AST.Type, full_function_type bool) int {
P.Expr(t.Expr); P.Expr(t.Expr);
} }
P.String(0, "]"); P.String(0, "]");
separator = P.Type(t.Elt, true); separator = P.Type(t.Elt);
case AST.STRUCT, AST.INTERFACE: case AST.STRUCT, AST.INTERFACE:
switch t.Form { switch t.Form {
@ -488,15 +547,15 @@ func (P *Printer) Type(t *AST.Type, full_function_type bool) int {
} }
if t.List != nil { if t.List != nil {
P.separator = blank; P.separator = blank;
P.Fields(t.List, t.End, t.Form == AST.STRUCT); P.Fields(t.List, t.End, t.Form == AST.INTERFACE);
} }
separator = none; separator = none;
case AST.MAP: case AST.MAP:
P.String(t.Pos, "map ["); P.String(t.Pos, "map [");
P.Type(t.Key, true); P.Type(t.Key);
P.String(0, "]"); P.String(0, "]");
separator = P.Type(t.Elt, true); separator = P.Type(t.Elt);
case AST.CHANNEL: case AST.CHANNEL:
var m string; var m string;
@ -506,29 +565,15 @@ func (P *Printer) Type(t *AST.Type, full_function_type bool) int {
case AST.SEND: m = "chan <- "; case AST.SEND: m = "chan <- ";
} }
P.String(t.Pos, m); P.String(t.Pos, m);
separator = P.Type(t.Elt, true); separator = P.Type(t.Elt);
case AST.POINTER: case AST.POINTER:
P.String(t.Pos, "*"); P.String(t.Pos, "*");
separator = P.Type(t.Elt, true); separator = P.Type(t.Elt);
case AST.FUNCTION: case AST.FUNCTION:
if full_function_type {
P.Token(0, Scanner.FUNC); P.Token(0, Scanner.FUNC);
} separator = P.Signature(t);
P.Parameters(t.Pos, t.List);
if t.Elt != nil {
P.separator = blank;
list := t.Elt.List;
if list.Len() > 1 || list.At(0).(*AST.Expr).Typ.Form == AST.FUNCTION {
// single, anonymous result types which are functions must
// be parenthesized as well
P.Parameters(0, list);
} else {
// single, anonymous result type
P.Expr(list.At(0).(*AST.Expr));
}
}
case AST.ELLIPSIS: case AST.ELLIPSIS:
P.String(t.Pos, "..."); P.String(t.Pos, "...");
@ -545,105 +590,133 @@ func (P *Printer) Type(t *AST.Type, full_function_type bool) int {
// Expressions // Expressions
func (P *Printer) Block(b *AST.Block, indent bool); func (P *Printer) Block(b *AST.Block, indent bool);
func (P *Printer) Expr1(x AST.Expr, prec1 int);
func (P *Printer) Expr1(x *AST.Expr, prec1 int, full_function_type bool) {
if x == nil {
return; // empty expression list
}
switch x.Tok { func (P *Printer) DoBadExpr(x *AST.BadExpr) {
case Scanner.TYPE: P.String(0, "BadExpr");
// type expr }
P.Type(x.Typ, full_function_type);
case Scanner.IDENT:
func (P *Printer) DoIdent(x *AST.Ident) {
P.HtmlIdentifier(x); P.HtmlIdentifier(x);
}
case Scanner.INT, Scanner.STRING, Scanner.FLOAT:
// literal
P.String(x.Pos, x.Obj.Ident);
case Scanner.FUNC: func (P *Printer) DoBinaryExpr(x *AST.BinaryExpr) {
// function literal if x.Tok == Scanner.COMMA {
P.String(x.Pos, "func");
P.Type(x.Obj.Typ, false);
P.Block(x.Obj.Body, true);
P.newlines = 0;
case Scanner.COMMA:
// list
// (don't use binary expression printing because of different spacing) // (don't use binary expression printing because of different spacing)
P.Expr(x.X); P.Expr(x.X);
P.String(x.Pos, ","); P.String(x.Pos(), ",");
P.separator = blank; P.separator = blank;
P.state = inside_list; P.state = inside_list;
P.Expr(x.Y); P.Expr(x.Y);
case Scanner.PERIOD:
// selector or type guard
P.Expr1(x.X, Scanner.HighestPrec, true);
P.String(x.Pos, ".");
if x.Y.Tok == Scanner.TYPE {
P.String(0, "(");
P.Expr(x.Y);
P.String(0, ")");
} else { } else {
P.Expr1(x.Y, Scanner.HighestPrec, true); prec := Scanner.Precedence(x.Tok);
} if prec < P.prec {
case Scanner.LBRACK:
// index
P.Expr1(x.X, Scanner.HighestPrec, true);
P.String(x.Pos, "[");
P.Expr1(x.Y, 0, true);
P.String(0, "]");
case Scanner.LPAREN:
// call
P.Expr1(x.X, Scanner.HighestPrec, true);
P.String(x.Pos, "(");
P.Expr(x.Y);
P.String(0, ")");
case Scanner.LBRACE:
// composite literal
P.Type(x.Obj.Typ, true);
P.String(x.Pos, "{");
P.Expr(x.Y);
P.String(0, "}");
default:
// unary and binary expressions including ":" for pairs
prec := Scanner.UnaryPrec;
if x.X != nil {
prec = Scanner.Precedence(x.Tok);
}
if prec < prec1 {
P.String(0, "("); P.String(0, "(");
} }
if x.X == nil { P.Expr1(x.X, prec);
// unary expression
P.Token(x.Pos, x.Tok);
if x.Tok == Scanner.RANGE {
P.separator = blank; P.separator = blank;
} P.Token(x.Pos(), x.Tok);
} else {
// binary expression
P.Expr1(x.X, prec, true);
P.separator = blank; P.separator = blank;
P.Token(x.Pos, x.Tok); P.Expr1(x.Y, prec);
P.separator = blank; if prec < P.prec {
}
P.Expr1(x.Y, prec, true);
if prec < prec1 {
P.String(0, ")"); P.String(0, ")");
} }
} }
} }
func (P *Printer) Expr(x *AST.Expr) { func (P *Printer) DoUnaryExpr(x *AST.UnaryExpr) {
P.Expr1(x, Scanner.LowestPrec, true); prec := Scanner.UnaryPrec;
if prec < P.prec {
P.String(0, "(");
}
P.Token(x.Pos(), x.Tok);
if x.Tok == Scanner.RANGE {
P.separator = blank;
}
P.Expr1(x.X, prec);
if prec < P.prec {
P.String(0, ")");
}
}
func (P *Printer) DoBasicLit(x *AST.BasicLit) {
P.String(x.Pos(), x.Val);
}
func (P *Printer) DoTypeLit(x *AST.TypeLit) {
P.Type(x.Typ);
}
func (P *Printer) DoFunctionLit(x *AST.FunctionLit) {
P.String(x.Pos(), "func");
P.Signature(x.Typ);
P.separator = blank;
P.Block(x.Body, true);
P.newlines = 0;
}
func (P *Printer) DoCompositeLit(x *AST.CompositeLit) {
P.Type(x.Typ);
P.String(x.Pos(), "{");
P.Expr(x.Elts);
P.String(0, "}");
}
func (P *Printer) DoSelector(x *AST.Selector) {
P.Expr1(x.X, Scanner.HighestPrec);
P.String(x.Pos(), ".");
P.Expr1(x.Sel, Scanner.HighestPrec);
}
func (P *Printer) DoTypeGuard(x *AST.TypeGuard) {
P.Expr1(x.X, Scanner.HighestPrec);
P.String(x.Pos(), ".");
P.String(0, "(");
P.Type(x.Typ);
P.String(0, ")");
}
func (P *Printer) DoIndex(x *AST.Index) {
P.Expr1(x.X, Scanner.HighestPrec);
P.String(x.Pos(), "[");
P.Expr1(x.I, 0);
P.String(0, "]");
}
func (P *Printer) DoCall(x *AST.Call) {
P.Expr1(x.F, Scanner.HighestPrec);
P.String(x.Pos(), "(");
P.Expr(x.Args);
P.String(0, ")");
}
func (P *Printer) Expr1(x AST.Expr, prec1 int) {
if x == nil {
return; // empty expression list
}
saved_prec := P.prec;
P.prec = prec1;
x.Visit(P);
P.prec = saved_prec;
}
func (P *Printer) Expr(x AST.Expr) {
P.Expr1(x, Scanner.LowestPrec);
} }
@ -834,7 +907,7 @@ func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
if d.Ident != nil { if d.Ident != nil {
P.Expr(d.Ident); P.Expr(d.Ident);
} else { } else {
P.String(d.Val.Pos, ""); // flush pending ';' separator/newlines P.String(d.Val.Pos(), ""); // flush pending ';' separator/newlines
} }
P.separator = tab; P.separator = tab;
P.Expr(d.Val); P.Expr(d.Val);
@ -843,13 +916,13 @@ func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
case Scanner.TYPE: case Scanner.TYPE:
P.Expr(d.Ident); P.Expr(d.Ident);
P.separator = blank; // TODO switch to tab? (but indentation problem with structs) P.separator = blank; // TODO switch to tab? (but indentation problem with structs)
P.separator = P.Type(d.Typ, true); P.separator = P.Type(d.Typ);
case Scanner.CONST, Scanner.VAR: case Scanner.CONST, Scanner.VAR:
P.Expr(d.Ident); P.Expr(d.Ident);
if d.Typ != nil { if d.Typ != nil {
P.separator = blank; // TODO switch to tab? (indentation problem with structs) P.separator = blank; // TODO switch to tab? (indentation problem with structs)
P.separator = P.Type(d.Typ, true); P.separator = P.Type(d.Typ);
} }
if d.Val != nil { if d.Val != nil {
P.separator = tab; P.separator = tab;
@ -866,10 +939,10 @@ func (P *Printer) Declaration(d *AST.Decl, parenthesized bool) {
P.separator = blank; P.separator = blank;
} }
P.Expr(d.Ident); P.Expr(d.Ident);
P.separator = P.Type(d.Typ, false); P.separator = P.Signature(d.Typ);
if d.Val != nil { if d.Body != nil {
P.separator = blank; P.separator = blank;
P.Block(d.Val.Obj.Body, true); P.Block(d.Body, true);
} }
default: default:
@ -910,7 +983,7 @@ func Print(prog *AST.Program) {
P.Init(text, prog.Comments); P.Init(text, prog.Comments);
// TODO would be better to make the name of the src file be the title // TODO would be better to make the name of the src file be the title
P.HtmlPrologue("package " + prog.Ident.Obj.Ident); P.HtmlPrologue("package " + prog.Ident.(*AST.Ident).Obj.Ident);
P.Program(prog); P.Program(prog);
P.HtmlEpilogue(); P.HtmlEpilogue();