mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Add built-in slice functions
This commit is contained in:
1 parent
333b11dc65
commit
339673ae82
8 files changed
+120
-45
No files matched your search
@@ -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")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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)",
|
||||
}
|
||||
+3
-2
@@ -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 }
|
||||
|
||||
+61
-28
@@ -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))
|
||||
}
|
||||
+15
-4
@@ -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")
|
||||
}
|
||||
|
||||
+18
-8
@@ -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<<KeyShift | FlagsOther)
|
||||
IDDiamond = ID(0x02<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
|
||||
IDOpenParen = ID(0x10<<KeyShift | FlagsOpen | FlagsTightRight)
|
||||
IDCloseParen = ID(0x11<<KeyShift | FlagsClose | FlagsTightLeft | FlagsImplicitSemicolon)
|
||||
@@ -442,6 +445,7 @@ const (
|
||||
IDThis = ID(0x79<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDIn = ID(0x7A<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDOut = ID(0x7B<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDCapitalT = ID(0x7C<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
|
||||
IDI8 = ID(0x80<<KeyShift | FlagsIdent | FlagsImplicitSemicolon | FlagsNumType)
|
||||
IDI16 = ID(0x81<<KeyShift | FlagsIdent | FlagsImplicitSemicolon | FlagsNumType)
|
||||
@@ -543,17 +547,22 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
name string
|
||||
id ID
|
||||
}{
|
||||
// KeyDoubleZ (and IDDoubleZ) are never returned by the tokenizer, as the
|
||||
// tokenizer rejects non-ASCII input. It is used by the type checker as a
|
||||
// dummy-valued built-in Key to represent an ideal integer type (in
|
||||
// mathematical terms, the integer ring ℤ), as opposed to a realized
|
||||
// integer type whose range is restricted. For example, the u16 type is
|
||||
// restricted to [0x0000, 0xFFFF].
|
||||
// KeyDoubleZ and KeyDiamond (and IDDoubleZ and IDDiamond) are never
|
||||
// returned by the tokenizer, as the tokenizer rejects non-ASCII input.
|
||||
//
|
||||
// The string representation "ℤ" is specifically non-ASCII so that no
|
||||
// user-defined (non built-in) identifier will conflict with this.
|
||||
// The string representations "ℤ" and "◊" are specifically non-ASCII so
|
||||
// that no user-defined (non built-in) identifier will conflict with them.
|
||||
|
||||
// KeyDoubleZ is used by the type checker as a dummy-valued built-in Key to
|
||||
// represent an ideal integer type (in mathematical terms, the integer ring
|
||||
// ℤ), as opposed to a realized integer type whose range is restricted. For
|
||||
// example, the u16 type is restricted to [0x0000, 0xFFFF].
|
||||
KeyDoubleZ: {"ℤ", IDDoubleZ}, // U+2124 DOUBLE-STRUCK CAPITAL Z
|
||||
|
||||
// KeyDiamond is used by the type checker as a dummy-valued built-in Key to
|
||||
// represent a generic type.
|
||||
KeyDiamond: {"◊", IDDiamond}, // U+25C7 WHITE DIAMOND
|
||||
|
||||
KeyOpenParen: {"(", IDOpenParen},
|
||||
KeyCloseParen: {")", IDCloseParen},
|
||||
KeyOpenBracket: {"[", IDOpenBracket},
|
||||
@@ -647,6 +656,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyThis: {"this", IDThis},
|
||||
KeyIn: {"in", IDIn},
|
||||
KeyOut: {"out", IDOut},
|
||||
KeyCapitalT: {"T", IDCapitalT},
|
||||
|
||||
// Change MaxIntBits if a future update adds an i128 or u128 type.
|
||||
|
||||
|
||||
Reference in new issue
Block a user