Add built-in table T methods

This commit is contained in:
Nigel Tao committed 2018-05-18 12:50:53 +10:00
1 parent eda326726a
commit e4320c3c6d
21 files changed
+194 -5

No files matched your search

+12 -4
View File
@@ -103,6 +103,8 @@ var Funcs = []string{
// ---- image_buffer
"image_buffer.plane(p u32[..3])(ret table u8)",
// ---- image_config
"image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, num_loops u32)()",
@@ -155,10 +157,10 @@ var Funcs = []string{
"status.is_suspension()(ret bool)",
}
// The "T" types here are placeholders for generic "slice U" types. After
// tokenizing (but before parsing) these SliceFunc 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 "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.
const (
GenericOldName = t.IDCapitalT
@@ -171,3 +173,9 @@ var SliceFuncs = []string{
"T.prefix(up_to u64)(ret T)",
"T.suffix(up_to u64)(ret T)",
}
var TableFuncs = []string{
"T.height()(ret u64)",
"T.stride()(ret u64)",
"T.width()(ret u64)",
}
+1
View File
@@ -169,6 +169,7 @@ type Checker struct {
builtInFuncs map[t.QQID]*a.Func
builtInSliceFuncs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
unsortedStructs []*a.Struct
}
+20
View File
@@ -98,6 +98,17 @@ func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) {
return c.builtInSliceFuncs[qqid], nil
}
func (c *Checker) builtInTableFunc(qqid t.QQID) (*a.Func, error) {
if c.builtInTableFuncs == nil {
err := error(nil)
c.builtInTableFuncs, err = c.parseBuiltInFuncs(builtin.TableFuncs, true)
if err != nil {
return nil, err
}
}
return c.builtInTableFuncs[qqid], nil
}
func (c *Checker) parseBuiltInFuncs(ss []string, generic bool) (map[t.QQID]*a.Func, error) {
m := map[t.QQID]*a.Func{}
buf := []byte(nil)
@@ -157,6 +168,15 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
return f, nil
}
} else if lTyp.IsTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDiamond
if f, err := c.builtInTableFunc(qqid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
} else {
if f, err := c.builtInFunc(qqid); err != nil {
return nil, err
+11
View File
@@ -617,6 +617,17 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.Node(), nil, nil))
return nil
} else if lTyp.IsTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDiamond
if f, err := q.c.builtInTableFunc(qqid); err != nil {
return err
} else if f == nil {
return fmt.Errorf("check: no table method %q", n.Ident().Str(q.tm))
}
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.Node(), nil, nil))
return nil
} else if lTyp.Decorator() != 0 {
return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm))
}
+6
View File
@@ -471,6 +471,9 @@ const (
IDMax = ID(0x213)
IDMin = ID(0x214)
IDSet = ID(0x215)
IDHeight = ID(0x216)
IDStride = ID(0x217)
IDWidth = ID(0x218)
)
var builtInsByID = [nBuiltInIDs]string{
@@ -671,6 +674,9 @@ var builtInsByID = [nBuiltInIDs]string{
IDMax: "max",
IDMin: "min",
IDSet: "set",
IDHeight: "height",
IDStride: "stride",
IDWidth: "width",
}
var builtInsByName = map[string]ID{}