From d33db6dae0b23a83aabda5e5e8ddd1dff5f29a5f Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 5 Apr 2018 22:21:08 +1000 Subject: [PATCH] Introduce "array[N] T" and "slice T" type syntax --- doc/changelog.md | 1 + doc/wuffs-the-language.md | 6 ++--- lang/ast/ast.go | 6 ++--- lang/ast/string.go | 4 +-- lang/ast/string_test.go | 4 +-- lang/builtin/builtin.go | 26 ++++++++++---------- lang/check/check_test.go | 4 +-- lang/parse/parse.go | 42 +++++++++++++++++++++----------- lang/token/list.go | 6 ++--- std/crc32/decode_crc32.wuffs | 4 +-- std/deflate/codec_consts.wuffs | 4 +-- std/deflate/decode_deflate.wuffs | 20 +++++++-------- std/gif/decode_gif.wuffs | 12 ++++----- std/gif/decode_lzw.wuffs | 8 +++--- std/zlib/decode_zlib.wuffs | 4 +-- 15 files changed, 83 insertions(+), 68 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index e1aac1f8..e324ce60 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -13,6 +13,7 @@ - Let the `std/zlib` decoder ignore checksums. - Renamed `std/flate` to `std/deflate`. - Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc. +- Renamed `[N] T` and `[] T` types to `array[N] T` and `slice T`. - Supported animated (not just single frame) GIFs. - Marked the `std/gif` LZW decoder as private. - Marked some internal status codes as private. diff --git a/doc/wuffs-the-language.md b/doc/wuffs-the-language.md index ea6a7702..f459c1a5 100644 --- a/doc/wuffs-the-language.md +++ b/doc/wuffs-the-language.md @@ -78,9 +78,9 @@ Converting an expression `x` to the type `T` is written as `x as T`. ## Types -Types read from left to right: `ptr [100] u32` is a pointer to a 100-element -array of unsigned 32-bit integers. `ptr` here means a non-null pointer. Use -`nptr` for a nullable pointer type. +Types read from left to right: `ptr array [100] u32` is a pointer to a +100-element array of unsigned 32-bit integers. `ptr` here means a non-null +pointer. Use `nptr` for a nullable pointer type. Integer types can also be refined: `var x u32[10..20]` defines a variable x that is stored as 4 bytes (32 bits) and can be combined arithmetically (e.g. diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 953ba53e..066e5940 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -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" diff --git a/lang/ast/string.go b/lang/ast/string.go index 5514fcb8..e6514ef6 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -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 "...) diff --git a/lang/ast/string_test.go b/lang/ast/string_test.go index 4038b090..e5424236 100644 --- a/lang/ast/string_test.go +++ b/lang/ast/string_test.go @@ -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{} diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 034e2d8e..84f64eb0 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -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)", } diff --git a/lang/check/check_test.go b/lang/check/check_test.go index 90f476c1..e8368958 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -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"}, diff --git a/lang/parse/parse.go b/lang/parse/parse.go index fef081f9..1738f793 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -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() diff --git a/lang/token/list.go b/lang/token/list.go index 214a6e07..8a048fbc 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -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<= 0x8000 { // If written is longer than the ringbuffer, we can ignore the @@ -456,7 +456,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. // - counts[4] = 1 // - counts[5] = 2 // - all other counts elements are 0. - var counts[16] u16[..320] + var counts array[16] u16[..320] var i u32 = in.n_codes0 while i < in.n_codes1 { assert i < 320 via "a < b: a < c; c <= b"(c:in.n_codes1) @@ -504,7 +504,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. // - offsets[6] = 10 // - all other offsets elements are 10. // - n_symbols = 10 - var offsets[16] u16[..320] + var offsets array[16] u16[..320] var n_symbols u32[..320] i = 1 while i <= 15 { @@ -541,7 +541,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..320], n_codes1 u32[.. // - offsets[3] = 7, formerly 0 // - offsets[4] = 8, formerly 7 // - offsets[5] = 10, formerly 8 - var symbols[320] u16[..319] + var symbols array[320] u16[..319] i = in.n_codes0 while i < in.n_codes1, inv n_symbols <= 288, diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 41a9c9f9..2f0c6f61 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -53,8 +53,8 @@ pub struct decoder?( // gct and lct are the Global / Local Color Tables: 256 (R, G, B) entries. // // TODO: 4 byte per pixel RGBA or BGRA instead of 3 bpp RGB? - gct[3 * 256] u8, - lct[3 * 256] u8, + gct array[3 * 256] u8, + lct array[3 * 256] u8, lzw lzw_decoder, ) @@ -122,7 +122,7 @@ pub func decoder.decode_frame?(dst io_writer, src io_reader)() { // // See the spec section 17 "Header" on page 7. pri func decoder.decode_header?(src io_reader)() { - var c[6] u8 + var c array[6] u8 var i u32 while i < 6 { c[i] = in.src.read_u8?() @@ -138,7 +138,7 @@ pri func decoder.decode_header?(src io_reader)() { // // See the spec section 18 "Logical Screen Descriptor" on page 8. pri func decoder.decode_lsd?(src io_reader)() { - var c[7] u8 + var c array[7] u8 var i u32 while i < 7 { c[i] = in.src.read_u8?() @@ -196,12 +196,12 @@ pri func decoder.skip_blocks?(src io_reader)() { } // animexts1dot0 is "ANIMEXTS1.0" as bytes. -pri const animexts1dot0[11] u8 = $( +pri const animexts1dot0 array[11] u8 = $( 0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30, ) // netscape2dot0 is "NETSCAPE2.0" as bytes. -pri const netscape2dot0[11] u8 = $( +pri const netscape2dot0 array[11] u8 = $( 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30, ) diff --git a/std/gif/decode_lzw.wuffs b/std/gif/decode_lzw.wuffs index 7569d41d..b1a1af61 100644 --- a/std/gif/decode_lzw.wuffs +++ b/std/gif/decode_lzw.wuffs @@ -19,9 +19,9 @@ pub error "LZW prefix chain is cyclical" pri struct lzw_decoder?( literal_width u32[2..8] = 8, - stack[4096] u8, - suffixes[4096] u8, - prefixes[4096] u16[..4095], + stack array[4096] u8, + suffixes array[4096] u8, + prefixes array[4096] u16[..4095], ) pri func lzw_decoder.set_literal_width!(lw u32[2..8] = 8)() { @@ -109,7 +109,7 @@ pri func lzw_decoder.decode?(dst io_writer, src io_reader)() { inv n_bits < 8, inv c < 256, { - var expansion[] u8 = this.stack[s:] + var expansion slice u8 = this.stack[s:] var n_copied u64 = in.dst.copy_from_slice(s:expansion) if n_copied == expansion.length() { break diff --git a/std/zlib/decode_zlib.wuffs b/std/zlib/decode_zlib.wuffs index f7e7c0fe..8d70b1fb 100644 --- a/std/zlib/decode_zlib.wuffs +++ b/std/zlib/decode_zlib.wuffs @@ -73,7 +73,7 @@ pri struct adler32?( ) // TODO: add a ! as this function is impure. -pri func adler32.update(x[] u8)(checksum u32) { +pri func adler32.update(x slice u8)(checksum u32) { // The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in // RFC 1950. @@ -81,7 +81,7 @@ pri func adler32.update(x[] u8)(checksum u32) { var s2 u32 = this.state.high_bits(n:16) while in.x.length() > 0 { - var remaining[] u8 + var remaining slice u8 if in.x.length() > 5552 { remaining = in.x[5552:] in.x = in.x[:5552]