From c479907bffe5f592a668f08d4a89e670988fc30c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 20 Apr 2018 17:16:23 +1000 Subject: [PATCH] Remove typeExprPlaceholder --- lang/builtin/builtin.go | 2 ++ lang/check/resolve.go | 15 ++++++--------- lang/check/type.go | 16 +++++++--------- lang/token/list.go | 14 ++++++++------ 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 84f64eb0..a2efd011 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -69,6 +69,8 @@ var Types = []string{ "u16", "u32", "u64", + + "empty_struct", "bool", "status", "io_reader", diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 4bbb41be..c233a3dd 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -38,10 +38,12 @@ var ( typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDDoubleZ, nil, nil, nil) typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil) - typeExprU8 = a.NewTypeExpr(0, t.IDBase, t.IDU8, nil, nil, nil) - typeExprU16 = a.NewTypeExpr(0, t.IDBase, t.IDU16, nil, nil, nil) - typeExprU32 = a.NewTypeExpr(0, t.IDBase, t.IDU32, nil, nil, nil) - typeExprU64 = a.NewTypeExpr(0, t.IDBase, t.IDU64, nil, nil, nil) + typeExprU8 = a.NewTypeExpr(0, t.IDBase, t.IDU8, nil, nil, nil) + typeExprU16 = a.NewTypeExpr(0, t.IDBase, t.IDU16, nil, nil, nil) + typeExprU32 = a.NewTypeExpr(0, t.IDBase, t.IDU32, nil, nil, nil) + typeExprU64 = a.NewTypeExpr(0, t.IDBase, t.IDU64, nil, nil, nil) + + typeExprEmptyStruct = a.NewTypeExpr(0, t.IDBase, t.IDEmptyStruct, nil, nil, nil) typeExprBool = a.NewTypeExpr(0, t.IDBase, t.IDBool, nil, nil, nil) typeExprStatus = a.NewTypeExpr(0, t.IDBase, t.IDStatus, nil, nil, nil) typeExprIOReader = a.NewTypeExpr(0, t.IDBase, t.IDIOReader, nil, nil, nil) @@ -49,11 +51,6 @@ var ( typeExprImageConfig = a.NewTypeExpr(0, t.IDBase, t.IDImageConfig, nil, nil, nil) typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8) - - // TODO: delete this. - typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDU8, nil, nil, nil) - typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDBase, t.IDU16, nil, nil, nil) - typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDBase, t.IDU32, nil, nil, nil) ) // typeMap maps from variable names (as token IDs) to types. diff --git a/lang/check/type.go b/lang/check/type.go index 1895b7a5..735da274 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -429,24 +429,23 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { // require a TypeExpr being able to express function and method types. // TODO: delete this hack that only matches "foo.decode(etc)". - if isThatMethod(q.tm, n, q.tm.ByName("decode"), 3) { + if idDecode := q.tm.ByName("decode"); isThatMethod(q.tm, n, idDecode, 3) { foo := n.LHS().Expr().LHS().Expr() if err := q.tcheckExpr(foo, depth); err != nil { return err } n.LHS().SetTypeChecked() - n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK. + n.LHS().Expr().SetMType(a.NewTypeExpr(t.IDFunc, 0, idDecode, foo.MType().Node(), nil, nil)) for _, o := range n.Args() { if err := q.tcheckArg(o.Arg(), nil, nil, depth); err != nil { return err } } - if n.Operator() == t.IDTry { - // TODO: be more principled about inferring "try etc"'s type. - n.SetMType(typeExprStatus) - } else { - n.SetMType(typeExprPlaceholder) // HACK. + if n.Operator() != t.IDTry { + return fmt.Errorf("TODO: foo.decode(etc) outside of a try statement") } + // TODO: be more principled about inferring "try etc"'s type. + n.SetMType(typeExprStatus) return nil } @@ -600,8 +599,7 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error { // *ast.Struct, to an *ast.TypeExpr we can pass to n.SetMType. return fmt.Errorf("TODO: translate an *ast.Struct to an *ast.TypeExpr") case 0: - // TODO: use a "unit", "void" or "empty struct" type. - n.SetMType(typeExprPlaceholder) + n.SetMType(typeExprEmptyStruct) case 1: oTyp := outFields[0].Field().XType() if genericType != nil && oTyp.Eq(typeExprGeneric) { diff --git a/lang/token/list.go b/lang/token/list.go index c8b627c2..7148aebb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -366,11 +366,12 @@ const ( IDSLICE = ID(0xA6) IDBase = ID(0xA7) - IDBool = ID(0xA8) - IDIOReader = ID(0xA9) - IDIOWriter = ID(0xAA) - IDStatus = ID(0xAB) - IDImageConfig = ID(0xAC) + IDEmptyStruct = ID(0xA8) + IDBool = ID(0xA9) + IDStatus = ID(0xAA) + IDIOReader = ID(0xAB) + IDIOWriter = ID(0xAC) + IDImageConfig = ID(0xAD) IDMark = ID(0xB0) IDReadU8 = ID(0xB1) @@ -534,10 +535,11 @@ var builtInsByID = [nBuiltInIDs]string{ IDSLICE: "SLICE", IDBase: "base", + IDEmptyStruct: "empty_struct", IDBool: "bool", + IDStatus: "status", IDIOReader: "io_reader", IDIOWriter: "io_writer", - IDStatus: "status", IDImageConfig: "image_config", IDMark: "mark",