Add pkg to ast Expr nodes

This commit is contained in:
Nigel Tao committed 2017-12-29 13:28:31 +11:00
1 parent 8b4ba30a36
commit 139bcfa7f1
5 files changed
+25 -7

No files matched your search

+6 -4
View File
@@ -140,7 +140,7 @@ type Node struct {
// Assert keyword reason . Assert
// Assign operator . . Assign
// Const . pkg name Const
// Expr operator . literal/ident Expr
// Expr operator pkg literal/ident Expr
// Field . . name Field
// File . . . File
// Func funcName receiverPkg receiverName Func
@@ -238,7 +238,7 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error {
return n.Node().Walk(func(o *Node) error {
switch o.Kind() {
case KConst, KFunc, KStatus, KStruct, KTypeExpr:
case KConst, KExpr, KFunc, KStatus, KStruct, KTypeExpr:
if o.id1 != 0 {
return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`,
pkg.Str(tm), o.id1.Str(tm))
@@ -268,6 +268,7 @@ const MaxExprDepth = 255
// - FlagsCallImpure is "f(x)" vs "f!(x)"
// - FlagsCallSuspendible is "f(x)" vs "f?(x)", it implies FlagsCallImpure
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot>
// - ID1: <0|pkg> (for statuses)
// - ID2: <0|literal|ident>
// - LHS: <nil|Expr>
// - MHS: <nil|Expr>
@@ -297,8 +298,8 @@ const MaxExprDepth = 255
//
// For lists, like "$(0, 1, 2)", ID0 is IDDollar.
//
// For statuses, like `error "foo"` and `"suspension "bar"`, ID0 is the keyword
// and ID1 is the message.
// For statuses, like `error "foo"` and `suspension bar."baz"`, ID0 is the
// keyword, ID1 is the package and ID2 is the message.
type Expr Node
func (n *Expr) Node() *Node { return (*Node)(n) }
@@ -313,6 +314,7 @@ func (n *Expr) BoundsCheckOptimized() bool { return n.flags&FlagsBoundsCheckOpti
func (n *Expr) ConstValue() *big.Int { return n.constValue }
func (n *Expr) MType() *TypeExpr { return n.mType }
func (n *Expr) Operator() t.ID { return n.id0 }
func (n *Expr) StatusQID() t.QID { return t.QID{n.id1, n.id2} }
func (n *Expr) Ident() t.ID { return n.id2 }
func (n *Expr) LHS() *Node { return n.lhs }
func (n *Expr) MHS() *Node { return n.mhs }
+16 -1
View File
@@ -23,7 +23,7 @@ func (n *Expr) Str(tm *t.Map) string {
if n == nil {
return ""
}
if n.id0 == 0 {
if n.id0 == 0 && n.id1 == 0 {
return tm.ByID(n.id2)
}
return string(n.appendStr(nil, tm, false, 0))
@@ -39,6 +39,21 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0:
switch n.id0.Key() {
case t.KeyError, t.KeyStatus, t.KeySuspension:
switch n.id0.Key() {
case t.KeyError:
buf = append(buf, "error "...)
case t.KeyStatus:
buf = append(buf, "status "...)
case t.KeySuspension:
buf = append(buf, "suspension "...)
}
if n.id1 != 0 {
buf = append(buf, tm.ByID(n.id1)...)
buf = append(buf, '.')
}
fallthrough
case 0:
buf = append(buf, tm.ByID(n.id2)...)