]> Cypherpunks repositories - gostls13.git/commitdiff
- removed an unnecessary field from AST.Expr nodes
authorRobert Griesemer <gri@golang.org>
Tue, 13 Jan 2009 01:44:10 +0000 (17:44 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 13 Jan 2009 01:44:10 +0000 (17:44 -0800)
R=r
OCL=22601
CL=22601

usr/gri/pretty/ast.go
usr/gri/pretty/parser.go
usr/gri/pretty/printer.go

index 0f0dd6a1f542ca9ac1e8501d69852a75c1872df1..8247a6268d45dac0d45bb88f74bf6f0ba9c56021 100644 (file)
@@ -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);
 }
 
 
index 6b1bb43398b10b4559f40b3acd72d87502aca4d0..eaadf105a330bf39e858a59fef2fef28bbe0806d 100644 (file)
@@ -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
index 8da1a847b5fcf814f9ce2ef3421e33b4973894c0..5beff8a0a6b4c953dbdb8fdbee223ccda919bdea 100644 (file)
@@ -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, "}");