mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +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
+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")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user