Slightly simplify Repr.

This commit is contained in:
Qi Xiao 2016-02-19 20:31:54 +01:00
parent 2413edcc09
commit 2cdbf67147
12 changed files with 30 additions and 19 deletions

View File

@ -155,7 +155,7 @@ func (c EvalCaller) Call(ed *Editor) {
}()
go func() {
for v := range chanOut {
ed.notify("[bound fn value] %s", v.Repr(-1))
ed.notify("[bound fn value] %s", v.Repr(eval.NoPretty))
}
wg.Done()
}()

View File

@ -80,7 +80,7 @@ func (bt BindingTable) IndexSet(idx, v eval.Value) {
case eval.String:
builtin, ok := builtinMap[string(v)]
if !ok {
throw(fmt.Errorf("no builtin named %s", v.Repr(-1)))
throw(fmt.Errorf("no builtin named %s", v.Repr(eval.NoPretty)))
}
f = builtin
case eval.CallerValue:

View File

@ -361,7 +361,7 @@ func (cp *compiler) singleVariable(n *parse.Indexing, msg string) VariableOp {
// Now this must be an IndexSetter.
indexSetter, ok := value.(IndexSetter)
if !ok {
ec.errorf( /* from p to */ indexBegins[n-1], "cannot be indexed for setting (value is %s, type %s)", value.Repr(-1), value.Kind())
ec.errorf( /* from p to */ indexBegins[n-1], "cannot be indexed for setting (value is %s, type %s)", value.Repr(NoPretty), value.Kind())
}
// XXX Duplicate code.
indicies := indexOps[n-1](ec)

View File

@ -48,7 +48,7 @@ func (me multiError) Repr(indent int) string {
// TODO Make a more generalized ListReprBuilder and use it here.
b := new(bytes.Buffer)
b.WriteString("?(multi-error")
elemIndent := IncIndent(indent, len("?(multi-error "))
elemIndent := indent + len("?(multi-error ")
for _, e := range me.errors {
if indent > 0 {
b.WriteString("\n" + strings.Repeat(" ", elemIndent))

View File

@ -36,9 +36,8 @@ func (List) Kind() string {
func (l List) Repr(indent int) string {
var b ListReprBuilder
b.Indent = indent
elemIndent := IncIndent(indent, 1)
for _, v := range *l.inner {
b.WriteElem(v.Repr(elemIndent))
b.WriteElem(v.Repr(indent + 1))
}
return b.String()
}
@ -103,12 +102,12 @@ func (b *ListReprBuilder) WriteElem(v string) {
if b.buf.Len() == 0 {
b.buf.WriteByte('[')
}
if b.Indent > 0 {
if b.Indent >= 0 {
// Pretty printing.
//
// Add a newline and indent+1 spaces, so that the
// starting & lines up with the first pair.
b.buf.WriteString("\n" + strings.Repeat(" ", IncIndent(b.Indent, 1)))
b.buf.WriteString("\n" + strings.Repeat(" ", b.Indent+1))
} else if b.buf.Len() > 1 {
b.buf.WriteByte(' ')
}

View File

@ -24,9 +24,8 @@ func (Map) Kind() string {
func (m Map) Repr(indent int) string {
var builder MapReprBuilder
builder.Indent = indent
kvIndent := IncIndent(indent, 1)
for k, v := range *m.inner {
builder.WritePair(k.Repr(kvIndent), v.Repr(kvIndent))
builder.WritePair(k.Repr(indent+1), v.Repr(indent+1))
}
return builder.String()
}
@ -38,7 +37,7 @@ func (m Map) Len() int {
func (m Map) IndexOne(idx Value) Value {
v, ok := (*m.inner)[idx]
if !ok {
throw(errors.New("no such key: " + idx.Repr(-1)))
throw(errors.New("no such key: " + idx.Repr(NoPretty)))
}
return v
}

View File

@ -37,7 +37,7 @@ func (m *muster) zerothMustStr() String {
s, ok := v.(String)
if !ok {
m.ec.errorf(m.p, "%s must be a string; got %s (type %s)",
m.what, v.Repr(-1), v.Kind())
m.what, v.Repr(NoPretty), v.Kind())
}
return s
}

View File

@ -43,12 +43,12 @@ func resolve(s string, ec *EvalCtx) Caller {
}
// ToString converts a Value to String. When the Value type implements
// String(), it is used. Otherwise Repr(-1) is used.
// String(), it is used. Otherwise Repr(NoPretty) is used.
func ToString(v Value) string {
if s, ok := v.(Stringer); ok {
return s.String()
}
return v.Repr(-1)
return v.Repr(NoPretty)
}
func quote(s string) string {

View File

@ -29,9 +29,8 @@ func (*Struct) Kind() string {
func (s *Struct) Repr(indent int) string {
var builder MapReprBuilder
builder.Indent = indent
fieldIndent := IncIndent(indent, 1)
for i, name := range s.FieldNames {
builder.WritePair(parse.Quote(name), s.Fields[i].Get().Repr(fieldIndent))
builder.WritePair(parse.Quote(name), s.Fields[i].Get().Repr(indent+1))
}
return builder.String()
}
@ -58,6 +57,6 @@ func (s *Struct) index(idx Value) Variable {
return s.Fields[i]
}
}
throw(fmt.Errorf("no such field: %s", index.Repr(-1)))
throw(fmt.Errorf("no such field: %s", index.Repr(NoPretty)))
panic("unreachable")
}

View File

@ -5,11 +5,17 @@ import (
"fmt"
"math/big"
"reflect"
"github.com/elves/elvish/util"
)
// Definitions for Value interfaces, some simple Value types and some common
// Value helpers.
var (
NoPretty = util.MinInt
)
// Value is an elvish value.
type Value interface {
Kinder
@ -243,7 +249,7 @@ func ToRat(v Value) (Rat, error) {
r := big.Rat{}
_, err := fmt.Sscanln(string(v), &r)
if err != nil {
return Rat{}, fmt.Errorf("%s cannot be parsed as rat", v.Repr(-1))
return Rat{}, fmt.Errorf("%s cannot be parsed as rat", v.Repr(NoPretty))
}
return Rat{&r}, nil
default:

View File

@ -32,7 +32,7 @@ var reprTests = []struct {
func TestRepr(t *testing.T) {
for _, test := range reprTests {
repr := test.v.Repr(-1)
repr := test.v.Repr(NoPretty)
if repr != test.want {
t.Errorf("Repr = %s, want %s", repr, test.want)
}

8
util/limits.go Normal file
View File

@ -0,0 +1,8 @@
package util
const (
MaxUint = ^uint(0)
MinUint = 0
MaxInt = int(MaxUint >> 1)
MinInt = -MaxInt - 1
)