From 81e02e7db6aa15bb475b6326ac010ff78f2f39b5 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 15 Apr 2018 15:15:56 +1000 Subject: [PATCH] Remove token ID/Key distinction, part 7 of N --- lang/ast/string.go | 150 +++++++++++++++++++++++---------------------- lang/token/list.go | 9 ++- 2 files changed, 81 insertions(+), 78 deletions(-) diff --git a/lang/ast/string.go b/lang/ast/string.go index 4437abe1..a12a5332 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -35,80 +35,12 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32) } depth++ - if n != nil { + if n == nil { + return buf + } + + if n.id0.IsXOp() { switch { - default: - switch n.id0.Key() { - case t.KeyError, t.KeyStatus, t.KeySuspension: - switch n.id0.Key() { - case t.KeyError: - buf = append(buf, "error "...) - case t.KeyStatus: - buf = append(buf, "status "...) - case t.KeySuspension: - buf = append(buf, "suspension "...) - } - if n.id1 != 0 { - buf = append(buf, tm.ByID(n.id1)...) - buf = append(buf, '.') - } - fallthrough - - case 0: - buf = append(buf, tm.ByID(n.id2)...) - - case t.KeyTry: - buf = append(buf, "try "...) - fallthrough - - case t.KeyOpenParen: - buf = n.lhs.Expr().appendStr(buf, tm, true, depth) - if n.flags&FlagsSuspendible != 0 { - buf = append(buf, '?') - } else if n.flags&FlagsImpure != 0 { - buf = append(buf, '!') - } - buf = append(buf, '(') - for i, o := range n.list0 { - if i != 0 { - buf = append(buf, ", "...) - } - buf = append(buf, tm.ByID(o.Arg().Name())...) - buf = append(buf, ':') - buf = o.Arg().Value().appendStr(buf, tm, false, depth) - } - buf = append(buf, ')') - - case t.KeyOpenBracket: - buf = n.lhs.Expr().appendStr(buf, tm, true, depth) - buf = append(buf, '[') - buf = n.rhs.Expr().appendStr(buf, tm, false, depth) - buf = append(buf, ']') - - case t.KeyColon: - buf = n.lhs.Expr().appendStr(buf, tm, true, depth) - buf = append(buf, '[') - buf = n.mhs.Expr().appendStr(buf, tm, false, depth) - buf = append(buf, ':') - buf = n.rhs.Expr().appendStr(buf, tm, false, depth) - buf = append(buf, ']') - - case t.KeyDot: - buf = n.lhs.Expr().appendStr(buf, tm, true, depth) - buf = append(buf, '.') - buf = append(buf, tm.ByID(n.id2)...) - - case t.KeyDollar: - buf = append(buf, "$("...) - for i, o := range n.list0 { - if i != 0 { - buf = append(buf, ", "...) - } - buf = o.Expr().appendStr(buf, tm, false, depth) - } - buf = append(buf, ')') - } - case n.id0.IsXUnaryOp(): buf = append(buf, opStrings[0xFF&n.id0.Key()]...) buf = n.rhs.Expr().appendStr(buf, tm, true, depth) @@ -143,6 +75,78 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32) buf = append(buf, ')') } } + + } else { + switch n.id0.Key() { + case t.KeyError, t.KeyStatus, t.KeySuspension: + switch n.id0.Key() { + case t.KeyError: + buf = append(buf, "error "...) + case t.KeyStatus: + buf = append(buf, "status "...) + case t.KeySuspension: + buf = append(buf, "suspension "...) + } + if n.id1 != 0 { + buf = append(buf, tm.ByID(n.id1)...) + buf = append(buf, '.') + } + fallthrough + + case 0: + buf = append(buf, tm.ByID(n.id2)...) + + case t.KeyTry: + buf = append(buf, "try "...) + fallthrough + + case t.KeyOpenParen: + buf = n.lhs.Expr().appendStr(buf, tm, true, depth) + if n.flags&FlagsSuspendible != 0 { + buf = append(buf, '?') + } else if n.flags&FlagsImpure != 0 { + buf = append(buf, '!') + } + buf = append(buf, '(') + for i, o := range n.list0 { + if i != 0 { + buf = append(buf, ", "...) + } + buf = append(buf, tm.ByID(o.Arg().Name())...) + buf = append(buf, ':') + buf = o.Arg().Value().appendStr(buf, tm, false, depth) + } + buf = append(buf, ')') + + case t.KeyOpenBracket: + buf = n.lhs.Expr().appendStr(buf, tm, true, depth) + buf = append(buf, '[') + buf = n.rhs.Expr().appendStr(buf, tm, false, depth) + buf = append(buf, ']') + + case t.KeyColon: + buf = n.lhs.Expr().appendStr(buf, tm, true, depth) + buf = append(buf, '[') + buf = n.mhs.Expr().appendStr(buf, tm, false, depth) + buf = append(buf, ':') + buf = n.rhs.Expr().appendStr(buf, tm, false, depth) + buf = append(buf, ']') + + case t.KeyDot: + buf = n.lhs.Expr().appendStr(buf, tm, true, depth) + buf = append(buf, '.') + buf = append(buf, tm.ByID(n.id2)...) + + case t.KeyDollar: + buf = append(buf, "$("...) + for i, o := range n.list0 { + if i != 0 { + buf = append(buf, ", "...) + } + buf = o.Expr().appendStr(buf, tm, false, depth) + } + buf = append(buf, ')') + } } return buf diff --git a/lang/token/list.go b/lang/token/list.go index 93387406..0e0f49e7 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -40,8 +40,6 @@ const ( // Key is the high 16 bits of an ID. It is the map key for a Map. type Key uint32 -func (k Key) isXOp() bool { return minXOpKey <= k && k <= maxXOpKey } - const ( FlagsBits = 16 FlagsMask = 1<