From 339673ae82711e3230977d6341b3176d562dbdc0 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 16 Dec 2017 17:23:49 +1100 Subject: [PATCH] Add built-in slice functions --- cmd/wuffs-c/internal/cgen/expr.go | 4 +- cmd/wuffs-c/internal/cgen/statement.go | 2 +- lang/ast/ast.go | 4 ++ lang/builtin/builtin.go | 16 +++++ lang/check/check.go | 5 +- lang/check/resolve.go | 89 ++++++++++++++++++-------- lang/check/type.go | 19 ++++-- lang/token/list.go | 26 +++++--- 8 files changed, 120 insertions(+), 45 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 3f7d7f9a..8efd8463 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -365,7 +365,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil { return err } - if lTyp := n.LHS().Expr().MType(); lTyp.Decorator().Key() == t.KeyColon { + if lTyp := n.LHS().Expr().MType(); lTyp.IsSliceType() { // TODO: don't assume that the slice is a slice of u8. b.writes(".ptr") } @@ -521,7 +521,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var // TODO: fix this, allow slices of all types, not just of u8's. Also allow // arrays of slices, slices of pointers, etc. - if n.Decorator().Key() == t.KeyColon { + if n.IsSliceType() { o := n.Inner() if o.Decorator() == 0 && o.Name().Key() == t.KeyU8 && !o.IsRefined() { b.writes("wuffs_base__slice_u8") diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index d8fe44bf..c976898c 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -295,7 +295,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { if err := g.writeExpr(b, v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { return err } - } else if n.XType().Decorator().Key() == t.KeyColon { + } else if n.XType().IsSliceType() { // TODO: don't assume that the slice is a slice of u8. b.printf("((wuffs_base__slice_u8){})") } else { diff --git a/lang/ast/ast.go b/lang/ast/ast.go index a33d5b0b..fa5b8ea5 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -617,6 +617,10 @@ func (n *TypeExpr) IsRefined() bool { return n.id0.Key() != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) } +func (n *TypeExpr) IsSliceType() bool { + return n.id0.Key() == t.KeyColon +} + func (n *TypeExpr) IsUnsignedInteger() bool { return n.id0 == 0 && (n.id1.Key() == t.KeyU8 || n.id1.Key() == t.KeyU16 || n.id1.Key() == t.KeyU32 || n.id1.Key() == t.KeyU64) // TODO: t.KeyUsize? diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 0853e937..3697053a 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -126,3 +126,19 @@ var Funcs = []string{ "writer1.mark()()", "writer1.since_mark()(ret[] u8)", } + +const ( + GenericReplaceFrom = t.IDCapitalT + GenericReplaceTo = t.IDDiamond +) + +var SliceFuncs = []string{ + // The "T" types here are generic placeholders for every "[] etc" slice + // type. When parsing these strings (e.g. in the lang/check package), "T" + // will be replaced by the "◊" diamond to denote a generic slice method, to + // avoid any possible ambiguity with a user-defined, non-generic "T" type. + "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)", +} diff --git a/lang/check/check.go b/lang/check/check.go index 11f07055..8485f345 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -168,8 +168,9 @@ type Checker struct { statuses map[t.ID]Status structs map[t.ID]Struct - builtInFuncs map[t.QID]*a.Func - unsortedStructs []*a.Struct + builtInFuncs map[t.QID]*a.Func + builtInSliceFuncs map[t.QID]*a.Func + unsortedStructs []*a.Struct } func (c *Checker) PackageID() uint32 { return c.packageID } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 4844d45b..c85dc406 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -70,39 +70,65 @@ var builtInTypeMap = TypeMap{ func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) { if c.builtInFuncs == nil { - c.builtInFuncs = map[t.QID]*a.Func{} - - buf := []byte(nil) - opts := parse.Options{ - AllowBuiltIns: true, - } - for _, s := range builtin.Funcs { - buf = buf[:0] - buf = append(buf, "pub func "...) - buf = append(buf, s...) - buf = append(buf, "{}\n"...) - - const filename = "builtin.wuffs" - tokens, _, err := t.Tokenize(c.tm, filename, buf) - if err != nil { - return nil, err - } - file, err := parse.Parse(c.tm, filename, tokens, &opts) - if err != nil { - return nil, err - } - - tlds := file.TopLevelDecls() - if len(tlds) != 1 || tlds[0].Kind() != a.KFunc { - return nil, fmt.Errorf("parsing %c: got %d top level decls, want %d", s, len(tlds), 1) - } - f := tlds[0].Func() - c.builtInFuncs[f.QID()] = f + err := error(nil) + c.builtInFuncs, err = parseBuiltInFuncs(c.tm, builtin.Funcs, false) + if err != nil { + return nil, err } } return c.builtInFuncs[qid], nil } +func (c *Checker) builtInSliceFunc(qid t.QID) (*a.Func, error) { + if c.builtInSliceFuncs == nil { + err := error(nil) + c.builtInSliceFuncs, err = parseBuiltInFuncs(c.tm, builtin.SliceFuncs, true) + if err != nil { + return nil, err + } + } + return c.builtInSliceFuncs[qid], nil +} + +func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QID]*a.Func, error) { + m := map[t.QID]*a.Func{} + buf := []byte(nil) + opts := parse.Options{ + AllowBuiltIns: true, + } + for _, s := range ss { + buf = buf[:0] + buf = append(buf, "pub func "...) + buf = append(buf, s...) + buf = append(buf, "{}\n"...) + + const filename = "builtin.wuffs" + tokens, _, err := t.Tokenize(tm, filename, buf) + if err != nil { + return nil, fmt.Errorf("check: could not tokenize built-in funcs: %v", err) + } + if generic { + for i := range tokens { + if tokens[i].ID == builtin.GenericReplaceFrom { + tokens[i].ID = builtin.GenericReplaceTo + } + } + } + file, err := parse.Parse(tm, filename, tokens, &opts) + if err != nil { + return nil, fmt.Errorf("check: could not parse built-in funcs: %v", err) + } + + tlds := file.TopLevelDecls() + if len(tlds) != 1 || tlds[0].Kind() != a.KFunc { + return nil, fmt.Errorf("parsing %c: got %d top level decls, want %d", s, len(tlds), 1) + } + f := tlds[0].Func() + m[f.QID()] = f + } + return m, nil +} + func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { // TODO: look up funcs in used packages. if typ.Decorator().Key() != t.KeyOpenParen { @@ -117,5 +143,12 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { if f := c.funcs[qid].Func; f != nil { return f, nil } + if typ.Receiver().IsSliceType() { + if f, err := c.builtInSliceFunc(t.QID{t.IDDiamond, qid[1]}); err != nil { + return nil, err + } else if f != nil { + return f, nil + } + } return nil, fmt.Errorf("check: resolveFunc cannot look up %q", typ.Str(c.tm)) } diff --git a/lang/check/type.go b/lang/check/type.go index 54f41daa..d2d6f309 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -182,7 +182,7 @@ func (q *checker) tcheckStatement(n *a.Node) error { return fmt.Errorf("check: iterate variable %q, of type %q, does not have a pointer type", n.Name().Str(q.tm), lTyp.Str(q.tm)) } - if rTyp.Decorator().Key() != t.KeyColon { + if !rTyp.IsSliceType() { return fmt.Errorf("check: iterate range %q, of type %q, does not have a slice type", value.Str(q.tm), rTyp.Str(q.tm)) } @@ -464,9 +464,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { } return nil } - // TODO: delete this hack that only matches "foo.length(etc)". - if isThatMethod(q.tm, n, t.KeyCopyFromSlice, 1) || isThatMethod(q.tm, n, t.KeyLength, 0) || - isThatMethod(q.tm, n, t.KeyAvailable, 0) { + // TODO: delete this hack that only matches "foo.copy_from_slice(etc)". + if isThatMethod(q.tm, n, t.KeyCopyFromSlice, 1) { foo := n.LHS().Expr().LHS().Expr() if err := q.tcheckExpr(foo, depth); err != nil { return err @@ -594,6 +593,7 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error { if err != nil { return err } + // TODO: check if f is generic (its receiver is t.IDDiamond). // Check that the func's in type matches the arguments. inFields := f.In().Fields() @@ -704,6 +704,17 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { lTyp := lhs.MType().Pointee() if lTyp.Decorator() != 0 { + if lTyp.IsSliceType() { + f, err := q.c.builtInSliceFunc(t.QID{t.IDDiamond, n.ID1()}) + if err != nil { + return err + } + if f == nil { + return fmt.Errorf("check: no slice method %q", n.ID1().Str(q.tm)) + } + n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil)) + return nil + } // TODO. return fmt.Errorf("check: unsupported decorator for tcheckDot") } diff --git a/lang/token/list.go b/lang/token/list.go index 9e31872c..f31d7adb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -158,6 +158,7 @@ const ( KeyInvalid = Key(0) KeyDoubleZ = Key(IDDoubleZ >> KeyShift) + KeyDiamond = Key(IDDiamond >> KeyShift) KeyOpenParen = Key(IDOpenParen >> KeyShift) KeyCloseParen = Key(IDCloseParen >> KeyShift) @@ -253,6 +254,7 @@ const ( KeyThis = Key(IDThis >> KeyShift) KeyIn = Key(IDIn >> KeyShift) KeyOut = Key(IDOut >> KeyShift) + KeyCapitalT = Key(IDCapitalT >> KeyShift) KeyI8 = Key(IDI8 >> KeyShift) KeyI16 = Key(IDI16 >> KeyShift) @@ -347,6 +349,7 @@ const ( IDInvalid = ID(0) IDDoubleZ = ID(0x01<