Introduce "array[N] T" and "slice T" type syntax

This commit is contained in:
Nigel Tao committed 2018-04-05 22:21:08 +10:00
1 parent 2b91e7861c
commit d33db6dae0
15 files changed
+83 -68

No files matched your search

+3 -3
View File
@@ -622,7 +622,7 @@ func NewJump(keyword t.ID, label t.ID) *Jump {
const MaxTypeExprDepth = 63
// TypeExpr is a type expression, such as "u32", "u32[..8]", "pkg.foo", "ptr
// T", "[8] T" or "[] T":
// T", "array[8] T" or "slice T":
// - ID0: <0|IDPtr|IDArray|IDSlice|IDOpenParen>
// - ID1: <0|pkg>
// - ID2: <0|type name>
@@ -632,9 +632,9 @@ const MaxTypeExprDepth = 63
//
// An IDPtr ID0 means "ptr RHS". RHS is the inner type.
//
// An IDArray ID0 means "[LHS] RHS". RHS is the inner type.
// An IDArray ID0 means "array[LHS] RHS". RHS is the inner type.
//
// An IDSlice ID0 means "[] RHS". RHS is the inner type.
// An IDSlice ID0 means "slice RHS". RHS is the inner type.
//
// An IDOpenParen ID0 means "LHS.ID2", a function or method type. LHS is the
// receiver type, which may be nil. If non-nil, it will be a pointee type: "T"
+2 -2
View File
@@ -212,12 +212,12 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
buf = append(buf, "ptr "...)
return n.Inner().appendStr(buf, tm, depth)
case t.KeyArray:
buf = append(buf, '[')
buf = append(buf, "array["...)
buf = n.ArrayLength().appendStr(buf, tm, false, 0)
buf = append(buf, "] "...)
return n.Inner().appendStr(buf, tm, depth)
case t.KeySlice:
buf = append(buf, "[] "...)
buf = append(buf, "slice "...)
return n.Inner().appendStr(buf, tm, depth)
case t.KeyOpenParen:
buf = append(buf, "func "...)
+2 -2
View File
@@ -67,8 +67,8 @@ func TestString(tt *testing.T) {
"x as T[i..j]",
"x as pkg.T",
"x as ptr T",
"x as [4] T",
"x as [8 + (2 * N)] ptr [4] ptr pkg.T[i..j]",
"x as array[4] T",
"x as array[8 + (2 * N)] ptr array[4] ptr pkg.T[i..j]",
}
tm := &t.Map{}
+13 -13
View File
@@ -104,7 +104,7 @@ var Funcs = []string{
"io_reader.is_marked()(ret bool)",
"io_reader.limit(l u64)(ret io_reader)",
"io_reader.mark()()",
"io_reader.since_mark()(ret[] u8)",
"io_reader.since_mark()(ret slice u8)",
"io_reader.skip32?(n u32)()",
"io_reader.skip64?(n u64)()",
"io_reader.unread_u8?()()",
@@ -120,28 +120,28 @@ var Funcs = []string{
"io_writer.available()(ret u64)",
"io_writer.copy_from_history32(distance u32, length u32)(ret u32)",
"io_writer.copy_from_reader32(r io_reader, length u32)(ret u32)",
"io_writer.copy_from_slice(s[] u8)(ret u64)",
"io_writer.copy_from_slice32(s[] u8, length u32)(ret u32)",
"io_writer.copy_from_slice(s slice u8)(ret u64)",
"io_writer.copy_from_slice32(s slice u8, length u32)(ret u32)",
"io_writer.is_marked()(ret bool)",
"io_writer.limit(l u64)(ret io_writer)",
"io_writer.mark()()",
"io_writer.since_mark()(ret[] u8)",
"io_writer.since_mark()(ret slice u8)",
"image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, num_loops u32)()",
}
const (
GenericReplaceFrom = t.IDCapitalT
GenericReplaceFrom = t.IDSLICE
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)",
// The "SLICE" types here are generic placeholders for every "slice T"
// type. After parsing these strings (e.g. in the lang/check package),
// replace "SLICE" with the "◊" diamond to denote a generic slice method,
// to avoid ambiguity with a user-defined, non-generic "SLICE" type.
"SLICE.copy_from_slice(s SLICE)(ret u64)",
"SLICE.length()(ret u64)",
"SLICE.prefix(up_to u64)(ret SLICE)",
"SLICE.suffix(up_to u64)(ret SLICE)",
}
+2 -2
View File
@@ -69,7 +69,7 @@ func TestCheck(tt *testing.T) {
var x u8
var y i32 = +2
var z u64[..123]
var a[4] u8
var a array[4] u8
var b bool
x = 0
@@ -147,7 +147,7 @@ func TestCheck(tt *testing.T) {
})
want := [][2]string{
{"a", "[4] u8"},
{"a", "array[4] u8"},
{"b", "bool"},
{"in", "in"},
{"out", "out"},
+28 -14
View File
@@ -387,27 +387,41 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
return a.NewTypeExpr(t.IDPtr, 0, 0, nil, nil, rhs), nil
}
if p.peek1().Key() == t.KeyOpenBracket {
decorator, arrayLength := t.ID(0), (*a.Expr)(nil)
switch p.peek1().Key() {
case t.KeyArray:
decorator = t.IDArray
p.src = p.src[1:]
decorator, lhs := t.IDSlice, (*a.Expr)(nil)
if p.peek1().Key() != t.KeyCloseBracket {
decorator = t.IDArray
var err error
lhs, err = p.parseExpr()
if err != nil {
return nil, err
}
if x := p.peek1().Key(); x != t.KeyCloseBracket {
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
}
if x := p.peek1().Key(); x != t.KeyOpenBracket {
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "[", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
var err error
arrayLength, err = p.parseExpr()
if err != nil {
return nil, err
}
if x := p.peek1().Key(); x != t.KeyCloseBracket {
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
case t.KeySlice:
decorator = t.IDSlice
p.src = p.src[1:]
}
if decorator != 0 {
rhs, err := p.parseTypeExpr()
if err != nil {
return nil, err
}
return a.NewTypeExpr(decorator, 0, 0, lhs.Node(), nil, rhs), nil
return a.NewTypeExpr(decorator, 0, 0, arrayLength.Node(), nil, rhs), nil
}
pkg, name, err := p.parseQualifiedIdent()
+3 -3
View File
@@ -276,7 +276,7 @@ const (
KeyThis = Key(IDThis >> KeyShift)
KeyIn = Key(IDIn >> KeyShift)
KeyOut = Key(IDOut >> KeyShift)
KeyCapitalT = Key(IDCapitalT >> KeyShift)
KeySLICE = Key(IDSLICE >> KeyShift)
KeyI8 = Key(IDI8 >> KeyShift)
KeyI16 = Key(IDI16 >> KeyShift)
@@ -468,7 +468,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)
IDSLICE = ID(0x7C<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDI8 = ID(0x80<<KeyShift | FlagsIdent | FlagsImplicitSemicolon | FlagsNumType)
IDI16 = ID(0x81<<KeyShift | FlagsIdent | FlagsImplicitSemicolon | FlagsNumType)
@@ -680,7 +680,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyThis: {"this", IDThis},
KeyIn: {"in", IDIn},
KeyOut: {"out", IDOut},
KeyCapitalT: {"T", IDCapitalT},
KeySLICE: {"SLICE", IDSLICE},
// Change MaxIntBits if a future update adds an i128 or u128 type.