提交 563e6d04 编写于 作者: chai2010's avatar chai2010

打印 ast 的 map 时保序

上级 24da3673
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"io" "io"
"os" "os"
"reflect" "reflect"
"sort"
"wa-lang.org/wa/internal/token" "wa-lang.org/wa/internal/token"
) )
...@@ -147,6 +148,30 @@ func (p *printer) print(x reflect.Value) { ...@@ -147,6 +148,30 @@ func (p *printer) print(x reflect.Value) {
return 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() { switch x.Kind() {
case reflect.Interface: case reflect.Interface:
p.print(x.Elem()) p.print(x.Elem())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册