Commit 73be5d82 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: use printer in vconv

Change-Id: Ib30ed686448c4c0a5777cdf1d505ea06eb8b2a47
Reviewed-on: https://go-review.googlesource.com/27917Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent dc2a0d59
...@@ -338,6 +338,8 @@ func jconv(n *Node, flag FmtFlag) string { ...@@ -338,6 +338,8 @@ func jconv(n *Node, flag FmtFlag) string {
// Fmt "%V": Values // Fmt "%V": Values
func vconv(v Val, flag FmtFlag) string { func vconv(v Val, flag FmtFlag) string {
var p printer
switch u := v.U.(type) { switch u := v.U.(type) {
case *Mpint: case *Mpint:
if !u.Rune { if !u.Rune {
...@@ -347,17 +349,19 @@ func vconv(v Val, flag FmtFlag) string { ...@@ -347,17 +349,19 @@ func vconv(v Val, flag FmtFlag) string {
return bconv(u, 0) return bconv(u, 0)
} }
x := u.Int64() switch x := u.Int64(); {
if ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'' { case ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'':
return fmt.Sprintf("'%c'", int(x)) p.f("'%c'", int(x))
}
if 0 <= x && x < 1<<16 { case 0 <= x && x < 1<<16:
return fmt.Sprintf("'\\u%04x'", uint(int(x))) p.f("'\\u%04x'", uint(int(x)))
}
if 0 <= x && x <= utf8.MaxRune { case 0 <= x && x <= utf8.MaxRune:
return fmt.Sprintf("'\\U%08x'", uint64(x)) p.f("'\\U%08x'", uint64(x))
default:
p.f("('\\x00' + %v)", u)
} }
return fmt.Sprintf("('\\x00' + %v)", u)
case *Mpflt: case *Mpflt:
if flag&FmtSharp != 0 { if flag&FmtSharp != 0 {
...@@ -366,19 +370,22 @@ func vconv(v Val, flag FmtFlag) string { ...@@ -366,19 +370,22 @@ func vconv(v Val, flag FmtFlag) string {
return fconv(u, FmtSharp) return fconv(u, FmtSharp)
case *Mpcplx: case *Mpcplx:
if flag&FmtSharp != 0 { switch {
return fmt.Sprintf("(%v+%vi)", &u.Real, &u.Imag) case flag&FmtSharp != 0:
} p.f("(%v+%vi)", &u.Real, &u.Imag)
if v.U.(*Mpcplx).Real.CmpFloat64(0) == 0 {
return fmt.Sprintf("%vi", fconv(&u.Imag, FmtSharp)) case v.U.(*Mpcplx).Real.CmpFloat64(0) == 0:
} p.f("%vi", fconv(&u.Imag, FmtSharp))
if v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0 {
case v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0:
return fconv(&u.Real, FmtSharp) return fconv(&u.Real, FmtSharp)
case v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0:
p.f("(%v%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
default:
p.f("(%v+%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
} }
if v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0 {
return fmt.Sprintf("(%v%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
}
return fmt.Sprintf("(%v+%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
case string: case string:
return strconv.Quote(u) return strconv.Quote(u)
...@@ -391,9 +398,12 @@ func vconv(v Val, flag FmtFlag) string { ...@@ -391,9 +398,12 @@ func vconv(v Val, flag FmtFlag) string {
case *NilVal: case *NilVal:
return "nil" return "nil"
default:
p.f("<ctype=%d>", v.Ctype())
} }
return fmt.Sprintf("<ctype=%d>", v.Ctype()) return p.String()
} }
/* /*
......
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