打印 ast 的 map 时保序
This commit is contained in:
parent
24da36739c
commit
563e6d040a
|
|
@ -11,6 +11,7 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"wa-lang.org/wa/internal/token"
|
||||
)
|
||||
|
|
@ -147,6 +148,30 @@ func (p *printer) print(x reflect.Value) {
|
|||
return
|
||||
}
|
||||
|
||||
// 特别处理, key 有序输出
|
||||
if m, ok := x.Interface().(map[string]*Object); ok {
|
||||
p.printf("%s (len = %d) {", x.Type(), x.Len())
|
||||
if len(m) > 0 {
|
||||
var keys = make([]string, 0, len(m))
|
||||
for key := range m {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
p.indent++
|
||||
p.printf("\n")
|
||||
for _, key := range keys {
|
||||
p.print(reflect.ValueOf(key))
|
||||
p.printf(": ")
|
||||
p.print(reflect.ValueOf(m[key]))
|
||||
p.printf("\n")
|
||||
}
|
||||
p.indent--
|
||||
}
|
||||
p.printf("}")
|
||||
return
|
||||
}
|
||||
|
||||
switch x.Kind() {
|
||||
case reflect.Interface:
|
||||
p.print(x.Elem())
|
||||
|
|
|
|||
Loading…
Reference in New Issue