From 6e06d1f9f190b4223f6eff17b1134978f8a9a32d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 18 May 2018 13:15:39 +1000 Subject: [PATCH] Rename IDDiamond to IDDagger1 and IDDagger2 --- lang/builtin/builtin.go | 28 ++++++++++++++------------- lang/check/check.go | 2 +- lang/check/resolve.go | 13 ++++++++----- lang/check/type.go | 33 +++++++++++++++++++------------ lang/token/list.go | 43 ++++++++++++++++++++++++----------------- 5 files changed, 70 insertions(+), 49 deletions(-) diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 4fff6381..0f1de180 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -157,25 +157,27 @@ var Funcs = []string{ "status.is_suspension()(ret bool)", } -// The "T" types here are placeholders for generic "slice U" or "table U" -// types. After tokenizing (but before parsing) these XxxFunc strings (e.g. in -// the lang/check package), replace the "T" receiver type with the "◊" diamond, -// to avoid collision with a user-defined "T" type. +// The "T1" and "T2" types here are placeholders for generic "slice T" or +// "table T" types. After tokenizing (but before parsing) these XxxFunc strings +// (e.g. in the lang/check package), replace "T1" and "T2" with "†" or "‡" +// daggers, to avoid collision with a user-defined "T1" or "T2" type. const ( - GenericOldName = t.IDCapitalT - GenericNewName = t.IDDiamond + GenericOldName1 = t.IDT1 + GenericOldName2 = t.IDT2 + GenericNewName1 = t.IDDagger1 + GenericNewName2 = t.IDDagger2 ) var SliceFuncs = []string{ - "T.copy_from_slice(s T)(ret u64)", - "T.length()(ret u64)", - "T.prefix(up_to u64)(ret T)", - "T.suffix(up_to u64)(ret T)", + "T1.copy_from_slice(s T1)(ret u64)", + "T1.length()(ret u64)", + "T1.prefix(up_to u64)(ret T1)", + "T1.suffix(up_to u64)(ret T1)", } var TableFuncs = []string{ - "T.height()(ret u64)", - "T.stride()(ret u64)", - "T.width()(ret u64)", + "T2.height()(ret u64)", + "T2.stride()(ret u64)", + "T2.width()(ret u64)", } diff --git a/lang/check/check.go b/lang/check/check.go index c0023fe5..50995b15 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -499,7 +499,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error { if qqid[0] == t.IDBase { // No need to populate c.funcs and c.localVars for built-in funcs. In // any case, the remaining type checking code in this function doesn't - // handle the base.◊ diamond type. + // handle the base.† dagger type. return nil } if other, ok := c.funcs[qqid]; ok { diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 4009c0f2..057aa313 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -34,7 +34,8 @@ var ( // typeExprFoo is an *ast.Node MType (implicit type). var ( - typeExprGeneric = a.NewTypeExpr(0, t.IDBase, t.IDDiamond, nil, nil, nil) + typeExprGeneric1 = a.NewTypeExpr(0, t.IDBase, t.IDDagger1, nil, nil, nil) + typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil) typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDDoubleZ, nil, nil, nil) typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil) typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDPilcrow, nil, nil, nil) @@ -128,8 +129,10 @@ func (c *Checker) parseBuiltInFuncs(ss []string, generic bool) (map[t.QQID]*a.Fu } if generic { for i := range tokens { - if tokens[i].ID == builtin.GenericOldName { - tokens[i].ID = builtin.GenericNewName + if tokens[i].ID == builtin.GenericOldName1 { + tokens[i].ID = builtin.GenericNewName1 + } else if tokens[i].ID == builtin.GenericOldName2 { + tokens[i].ID = builtin.GenericNewName2 } } } @@ -161,7 +164,7 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { qqid := t.QQID{lQID[0], lQID[1], typ.FuncName()} if lTyp.IsSliceType() { qqid[0] = t.IDBase - qqid[1] = t.IDDiamond + qqid[1] = t.IDDagger1 if f, err := c.builtInSliceFunc(qqid); err != nil { return nil, err } else if f != nil { @@ -170,7 +173,7 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { } else if lTyp.IsTableType() { qqid[0] = t.IDBase - qqid[1] = t.IDDiamond + qqid[1] = t.IDDagger2 if f, err := c.builtInTableFunc(qqid); err != nil { return nil, err } else if f != nil { diff --git a/lang/check/type.go b/lang/check/type.go index dc3c4ae6..e98b915e 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -321,7 +321,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error { ) } -func (q *checker) tcheckArg(n *a.Arg, inField *a.Field, genericType *a.TypeExpr, depth uint32) error { +func (q *checker) tcheckArg(n *a.Arg, inField *a.Field, genericType1 *a.TypeExpr, genericType2 *a.TypeExpr, depth uint32) error { if err := q.tcheckExpr(n.Value(), depth); err != nil { return err } @@ -336,8 +336,10 @@ func (q *checker) tcheckArg(n *a.Arg, inField *a.Field, genericType *a.TypeExpr, return fmt.Errorf("check: argument name: got %q, want %q", n.Name().Str(q.tm), inField.Name().Str(q.tm)) } inFieldTyp := inField.XType() - if genericType != nil && inFieldTyp.Eq(typeExprGeneric) { - inFieldTyp = genericType + if genericType1 != nil && inFieldTyp.Eq(typeExprGeneric1) { + inFieldTyp = genericType1 + } else if genericType2 != nil && inFieldTyp.Eq(typeExprGeneric2) { + inFieldTyp = genericType2 } if err := q.tcheckEq(inField.Name(), nil, inFieldTyp, n.Value(), n.Value().MType()); err != nil { return err @@ -551,9 +553,14 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error { n.Str(q.tm), ne, f.QQID().Str(q.tm), fe) } - genericType := (*a.TypeExpr)(nil) - if f.Receiver() == (t.QID{t.IDBase, t.IDDiamond}) { - genericType = lhs.MType().Receiver() + genericType1 := (*a.TypeExpr)(nil) + genericType2 := (*a.TypeExpr)(nil) + switch f.Receiver() { + case t.QID{t.IDBase, t.IDDagger1}: + genericType1 = lhs.MType().Receiver() + case t.QID{t.IDBase, t.IDDagger2}: + genericType1 = lhs.MType().Receiver() // TODO: convert table T to slice T. + genericType2 = lhs.MType().Receiver() } // Check that the func's in type matches the arguments. @@ -565,7 +572,7 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error { for i, o := range n.Args() { // TODO: inline tcheckArg here, after removing the special-cased hacks // in tcheckExprOther. - if err := q.tcheckArg(o.Arg(), inFields[i].Field(), genericType, depth); err != nil { + if err := q.tcheckArg(o.Arg(), inFields[i].Field(), genericType1, genericType2, depth); err != nil { return err } } @@ -587,8 +594,10 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error { n.SetMType(typeExprEmptyStruct) case 1: oTyp := outFields[0].Field().XType() - if genericType != nil && oTyp.Eq(typeExprGeneric) { - n.SetMType(genericType) + if genericType1 != nil && oTyp.Eq(typeExprGeneric1) { + n.SetMType(genericType1) + } else if genericType2 != nil && oTyp.Eq(typeExprGeneric2) { + n.SetMType(genericType2) } else { n.SetMType(oTyp) } @@ -608,7 +617,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { if lTyp.IsSliceType() { qqid[0] = t.IDBase - qqid[1] = t.IDDiamond + qqid[1] = t.IDDagger1 if f, err := q.c.builtInSliceFunc(qqid); err != nil { return err } else if f == nil { @@ -619,7 +628,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { } else if lTyp.IsTableType() { qqid[0] = t.IDBase - qqid[1] = t.IDDiamond + qqid[1] = t.IDDagger2 if f, err := q.c.builtInTableFunc(qqid); err != nil { return err } else if f == nil { @@ -976,7 +985,7 @@ swtch: // TODO: reject. You can only refine numeric types. } if qid[0] == t.IDBase { - if _, ok := builtInTypeMap[qid[1]]; ok || qid[1] == t.IDDiamond { + if _, ok := builtInTypeMap[qid[1]]; ok || qid[1] == t.IDDagger1 || qid[1] == t.IDDagger2 { break swtch } } diff --git a/lang/token/list.go b/lang/token/list.go index e2aee8a0..345245a4 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -397,15 +397,19 @@ const ( IDEmptyStruct = ID(0x100) IDBool = ID(0x101) - IDStatus = ID(0x102) - IDIOReader = ID(0x103) - IDIOWriter = ID(0x104) - IDCapitalT = ID(0x105) - IDImageBuffer = ID(0x106) - IDImageConfig = ID(0x107) - IDPilcrow = ID(0x10D) - IDDiamond = ID(0x10E) + IDImageBuffer = ID(0x102) + IDImageConfig = ID(0x103) + IDIOReader = ID(0x104) + IDIOWriter = ID(0x105) + IDStatus = ID(0x106) + + IDT1 = ID(0x10A) + IDT2 = ID(0x10B) + IDDagger1 = ID(0x10C) + IDDagger2 = ID(0x10D) + + IDPilcrow = ID(0x10E) // It is important that IDDoubleZ is right next to the IDI8..IDU64 block. // See the ID.IsNumTypeOrIdeal method. @@ -588,28 +592,31 @@ var builtInsByID = [nBuiltInIDs]string{ IDEmptyStruct: "empty_struct", IDBool: "bool", - IDStatus: "status", - IDIOReader: "io_reader", - IDIOWriter: "io_writer", - IDCapitalT: "T", + IDImageBuffer: "image_buffer", IDImageConfig: "image_config", + IDIOReader: "io_reader", + IDIOWriter: "io_writer", + IDStatus: "status", - // The next few IDs are never returned by the tokenizer, as it rejects - // non-ASCII input. The string representations "¶", "◊" and "ℤ" are + // Some of the next few IDs are never returned by the tokenizer, as it + // rejects non-ASCII input. The string representations "¶", "ℤ" etc. are // specifically non-ASCII so that no user-defined (non built-in) identifier // will conflict with them. + // IDDaggerN is used by the type checker as a dummy-valued built-in ID to + // represent a generic type. + IDT1: "T1", + IDT2: "T2", + IDDagger1: "†", // U+2020 DAGGER + IDDagger2: "‡", // U+2021 DOUBLE DAGGER + // IDPilcrow is used by the type checker to build a placeholder MType for // AST nodes that aren't expression nodes, such as struct definition nodes // and statement nodes. Its presence means that the non-expression node is // type checked. IDPilcrow: "¶", // U+00B6 PILCROW SIGN - // IDDiamond is used by the type checker as a dummy-valued built-in ID to - // represent a generic type. - IDDiamond: "◊", // U+25C7 WHITE DIAMOND - // IDDoubleZ is used by the type checker as a dummy-valued built-in ID to // represent an ideal integer type (in mathematical terms, the integer ring // ℤ), as opposed to a realized integer type whose range is restricted. For