From 563e6d040a0f677bcbb2e8b3de50374773652a2b Mon Sep 17 00:00:00 2001 From: chai2010 Date: Sat, 29 Apr 2023 07:35:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=20ast=20=E7=9A=84=20map=20?= =?UTF-8?q?=E6=97=B6=E4=BF=9D=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/ast/print.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/internal/ast/print.go b/internal/ast/print.go index 1e84d28..a028b3a 100644 --- a/internal/ast/print.go +++ b/internal/ast/print.go @@ -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())