From 6656b6e5e9100c02e666f3ecd4d18bb09690c171 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 16 Apr 2018 08:19:54 +1000 Subject: [PATCH] Tighten some more ast.TypeExpr methods --- lang/ast/ast.go | 16 +++++++++------- lang/token/list.go | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index f2cf61c2..a458e12e 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -670,11 +670,11 @@ func (n *TypeExpr) Pointee() *TypeExpr { } func (n *TypeExpr) IsBool() bool { - return n.id0 == 0 && n.id2 == t.IDBool + return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDBool } func (n *TypeExpr) IsIdeal() bool { - return n.id0 == 0 && n.id2 == t.IDDoubleZ + return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDDoubleZ } func (n *TypeExpr) IsNumType() bool { @@ -682,7 +682,7 @@ func (n *TypeExpr) IsNumType() bool { } func (n *TypeExpr) IsNumTypeOrIdeal() bool { - return n.id0 == 0 && n.id1 == t.IDBase && (n.id2.IsNumType() || n.id2 == t.IDDoubleZ) + return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumTypeOrIdeal() } func (n *TypeExpr) IsRefined() bool { @@ -698,7 +698,7 @@ func (n *TypeExpr) IsSliceType() bool { } func (n *TypeExpr) IsUnsignedInteger() bool { - return n.id0 == 0 && + return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDU8 || n.id2 == t.IDU16 || n.id2 == t.IDU32 || n.id2 == t.IDU64) } @@ -706,9 +706,11 @@ func (n *TypeExpr) HasPointers() bool { for ; n != nil; n = n.Inner() { switch n.id0 { case 0: - switch n.id2 { - case t.IDIOReader, t.IDIOWriter: - return true + if n.id1 == t.IDBase { + switch n.id2 { + case t.IDIOReader, t.IDIOWriter: + return true + } } case t.IDPtr, t.IDNptr, t.IDSlice: return true diff --git a/lang/token/list.go b/lang/token/list.go index d4e0ae9d..c8b627c2 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -104,8 +104,9 @@ func (x ID) IsClose() bool { return x < ID(len(isClose)) && isClose[x] } func (x ID) IsTightLeft() bool { return x < ID(len(isTightLeft)) && isTightLeft[x] } func (x ID) IsTightRight() bool { return x < ID(len(isTightRight)) && isTightRight[x] } -func (x ID) IsAssign() bool { return minAssign <= x && x <= maxAssign } -func (x ID) IsNumType() bool { return minNumType <= x && x <= maxNumType } +func (x ID) IsAssign() bool { return minAssign <= x && x <= maxAssign } +func (x ID) IsNumType() bool { return minNumType <= x && x <= maxNumType } +func (x ID) IsNumTypeOrIdeal() bool { return minNumTypeOrIdeal <= x && x <= maxNumTypeOrIdeal } func (x ID) IsImplicitSemicolon(m *Map) bool { return x.IsLiteral(m) || x.IsIdent(m) || @@ -336,10 +337,12 @@ const ( ) const ( - minBuiltInIdent = 0x98 - minNumType = 0x98 - maxNumType = 0x9F - maxBuiltInIdent = 0xFF + minBuiltInIdent = 0x98 + minNumTypeOrIdeal = 0x98 + minNumType = 0x98 + maxNumType = 0x9F + maxNumTypeOrIdeal = 0xA0 + maxBuiltInIdent = 0xFF IDI8 = ID(0x98) IDI16 = ID(0x99) @@ -350,7 +353,10 @@ const ( IDU32 = ID(0x9E) IDU64 = ID(0x9F) + // It is important that IDDoubleZ is right next to the IDI8..IDU64 block. + // See the ID.IsNumTypeOrIdeal method. IDDoubleZ = ID(0xA0) + IDDiamond = ID(0xA1) IDUnderscore = ID(0xA2)