From: Robert Griesemer Date: Tue, 13 Jan 2009 01:44:10 +0000 (-0800) Subject: - removed an unnecessary field from AST.Expr nodes X-Git-Tag: weekly.2009-11-06~2389 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ba556a881811ed6b619037783fe9f4b5dc3c142f;p=gostls13.git - removed an unnecessary field from AST.Expr nodes R=r OCL=22601 CL=22601 --- diff --git a/usr/gri/pretty/ast.go b/usr/gri/pretty/ast.go index 0f0dd6a1f5..8247a6268d 100644 --- a/usr/gri/pretty/ast.go +++ b/usr/gri/pretty/ast.go @@ -170,9 +170,6 @@ export type Expr struct { Node; x, y *Expr; // binary (x, y) and unary (y) expressions obj *Object; - - // TODO this one should go as well - t *Type; // type expressions, function literal types } @@ -198,9 +195,10 @@ export func NewExpr(pos, tok int, x, y *Expr) *Expr { } -export func NewLit(pos, tok int, obj *Object) *Expr { +// TODO probably don't need the tok parameter eventually +export func NewLit(tok int, obj *Object) *Expr { e := new(Expr); - e.pos, e.tok, e.obj = pos, tok, obj; + e.pos, e.tok, e.obj = obj.pos, tok, obj; return e; } @@ -302,6 +300,7 @@ export type Type struct { key *Type; // receiver type or map key elt *Type; // array, map, channel or pointer element type, function result type list *array.Array; end int; // struct fields, interface methods, function parameters + scope *Scope; // struct fields, methods } @@ -340,10 +339,10 @@ func (t *Type) nfields() int { // requires complete Type.pos access -export func NewTypeExpr(t *Type) *Expr { - e := new(Expr); - e.pos, e.tok, e.t = t.pos, Scanner.TYPE, t; - return e; +export func NewTypeExpr(typ *Type) *Expr { + obj := NewObject(typ.pos, TYPE, ""); + obj.typ = typ; + return NewLit(Scanner.TYPE, obj); } diff --git a/usr/gri/pretty/parser.go b/usr/gri/pretty/parser.go index 6b1bb43398..eaadf105a3 100644 --- a/usr/gri/pretty/parser.go +++ b/usr/gri/pretty/parser.go @@ -195,7 +195,7 @@ func (P *Parser) Declare(p *AST.Expr, kind int) { func ExprType(x *AST.Expr) *AST.Type { var t *AST.Type; if x.tok == Scanner.TYPE { - t = x.t; + t = x.obj.typ; } else if x.tok == Scanner.IDENT { // assume a type name t = AST.NewType(x.pos, AST.TYPENAME); @@ -213,7 +213,7 @@ func (P *Parser) NoType(x *AST.Expr) *AST.Expr { if x != nil && x.tok == Scanner.TYPE { P.Error(x.pos, "expected expression, found type"); val := AST.NewObject(x.pos, AST.NONE, "0"); - x = AST.NewLit(x.pos, Scanner.INT, val); + x = AST.NewLit(Scanner.INT, val); } return x; } @@ -248,7 +248,8 @@ func (P *Parser) ParseIdent(scope *AST.Scope) *AST.Expr { } else { assert(obj.kind != AST.NONE); } - x = AST.NewLit(P.pos, Scanner.IDENT, obj); + x = AST.NewLit(Scanner.IDENT, obj); + x.pos = P.pos; // override obj.pos (incorrect if object was looked up!) if P.verbose { P.PrintIndent(); print("Ident = \"", P.val, "\"\n"); @@ -382,10 +383,7 @@ func (P *Parser) ParseChannelType() *AST.Type { } -// TODO: The code below (ParseVarDecl, ParseVarDeclList) is all too -// complicated. There must be a better way to do this. - -func (P *Parser) ParseVarDecl(expect_ident bool) *AST.Type { +func (P *Parser) ParseVar(expect_ident bool) *AST.Type { t := AST.BadType; if expect_ident { x := P.ParseIdent(nil); @@ -401,13 +399,14 @@ func (P *Parser) ParseVarDecl(expect_ident bool) *AST.Type { } -func (P *Parser) ParseVarDeclList(list *array.Array, ellipsis_ok bool) { - P.Trace("VarDeclList"); +func (P *Parser) ParseVarList(list *array.Array, ellipsis_ok bool) { + P.Trace("VarList"); - // parse a list of types + // assume a list of types + // (a list of identifiers looks like a list of type names) i0 := list.Len(); for { - list.Push(P.ParseVarDecl(ellipsis_ok /* param list */ && i0 > 0)); + list.Push(P.ParseVar(ellipsis_ok /* param list */ && i0 > 0)); if P.tok == Scanner.COMMA { P.Next(); } else { @@ -415,6 +414,7 @@ func (P *Parser) ParseVarDeclList(list *array.Array, ellipsis_ok bool) { } } + // if we had a list of identifiers, it must be followed by a type typ := P.TryType(); if typ == nil && P.tok == Scanner.ELLIPSIS { typ = AST.NewType(P.pos, AST.ELLIPSIS); @@ -460,10 +460,10 @@ func (P *Parser) ParseParameterList(ellipsis_ok bool) *array.Array { P.Trace("ParameterList"); list := array.New(0); - P.ParseVarDeclList(list, ellipsis_ok); + P.ParseVarList(list, ellipsis_ok); for P.tok == Scanner.COMMA { P.Next(); - P.ParseVarDeclList(list, ellipsis_ok); + P.ParseVarList(list, ellipsis_ok); } P.Ecart(); @@ -623,7 +623,7 @@ func (P *Parser) ParseStructType() *AST.Type { t.list = array.New(0); for P.tok != Scanner.RBRACE && P.tok != Scanner.EOF { - P.ParseVarDeclList(t.list, false); + P.ParseVarList(t.list, false); if P.tok == Scanner.STRING { // ParseOperand takes care of string concatenation t.list.Push(P.ParseOperand()); @@ -758,9 +758,9 @@ func (P *Parser) ParseFunctionLit() *AST.Expr { P.Trace("FunctionLit"); val := AST.NewObject(P.pos, AST.NONE, ""); - x := AST.NewLit(P.pos, Scanner.FUNC, val); + x := AST.NewLit(Scanner.FUNC, val); P.Expect(Scanner.FUNC); - x.t = P.ParseFunctionType(); + val.typ = P.ParseFunctionType(); P.expr_lev++; P.scope_lev++; val.block, val.end = P.ParseBlock(); @@ -813,7 +813,7 @@ func (P *Parser) ParseOperand() *AST.Expr { case Scanner.INT, Scanner.FLOAT, Scanner.STRING: val := AST.NewObject(P.pos, AST.NONE, P.val); - x = AST.NewLit(P.pos, P.tok, val); + x = AST.NewLit(P.tok, val); P.Next(); if x.tok == Scanner.STRING { // TODO should remember the list instead of @@ -852,7 +852,7 @@ func (P *Parser) ParseSelectorOrTypeGuard(x *AST.Expr) *AST.Expr { } else { P.Expect(Scanner.LPAREN); - x.t = P.ParseType(); + x.y = AST.NewTypeExpr(P.ParseType()); P.Expect(Scanner.RPAREN); } @@ -966,7 +966,8 @@ func (P *Parser) ParseCompositeLit(t *AST.Type) *AST.Expr { P.Trace("CompositeLit"); x := P.NewExpr(P.pos, Scanner.LBRACE, nil, nil); - x.t = t; + x.obj = AST.NewObject(t.pos, AST.TYPE, ""); + x.obj.typ = t; P.Expect(Scanner.LBRACE); if P.tok != Scanner.RBRACE { x.y = P.ParseCompositeElements(); @@ -1022,7 +1023,7 @@ func (P *Parser) ParseUnaryExpr() *AST.Expr { if tok == Scanner.MUL && y.tok == Scanner.TYPE { // pointer type t := AST.NewType(pos, AST.POINTER); - t.elt = y.t; + t.elt = y.obj.typ; x = AST.NewTypeExpr(t); } else { x = P.NewExpr(pos, tok, nil, y); @@ -1480,7 +1481,7 @@ func (P *Parser) ParseImportSpec(pos int) *AST.Decl { if P.tok == Scanner.STRING { // TODO eventually the scanner should strip the quotes val := AST.NewObject(P.pos, AST.NONE, P.val); - d.val = AST.NewLit(P.pos, Scanner.STRING, val); + d.val = AST.NewLit(Scanner.STRING, val); P.Next(); } else { P.Expect(Scanner.STRING); // use Expect() error handling diff --git a/usr/gri/pretty/printer.go b/usr/gri/pretty/printer.go index 8da1a847b5..5beff8a0a6 100644 --- a/usr/gri/pretty/printer.go +++ b/usr/gri/pretty/printer.go @@ -547,7 +547,7 @@ func (P *Printer) Expr1(x *AST.Expr, prec1 int) { switch x.tok { case Scanner.TYPE: // type expr - P.Type(x.t); + P.Type(x.obj.typ); case Scanner.IDENT: P.HtmlIdentifier(x); @@ -559,7 +559,7 @@ func (P *Printer) Expr1(x *AST.Expr, prec1 int) { case Scanner.FUNC: // function literal P.String(x.pos, "func"); - P.Type(x.t); + P.Type(x.obj.typ); P.Block(0, x.obj.block, x.obj.end, true); P.newlines = 0; @@ -576,12 +576,12 @@ func (P *Printer) Expr1(x *AST.Expr, prec1 int) { // selector or type guard P.Expr1(x.x, Scanner.HighestPrec); P.String(x.pos, "."); - if x.y != nil { - P.Expr1(x.y, Scanner.HighestPrec); - } else { + if x.y.tok == Scanner.TYPE { P.String(0, "("); - P.Type(x.t); + P.Expr(x.y); P.String(0, ")"); + } else { + P.Expr1(x.y, Scanner.HighestPrec); } case Scanner.LBRACK: @@ -599,8 +599,8 @@ func (P *Printer) Expr1(x *AST.Expr, prec1 int) { P.String(0, ")"); case Scanner.LBRACE: - // composite - P.Type(x.t); + // composite literal + P.Type(x.obj.typ); P.String(x.pos, "{"); P.Expr(x.y); P.String(0, "}");