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

+1
View File
@@ -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.
+3 -3
View File
@@ -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.
+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.
+2 -2
View File
@@ -24,7 +24,7 @@ pub struct ieee?(
// TODO: should it be ? instead of ! because it's public, and might need a way
// to signal "initializer not called"? Should the return type, in the generated
// C code, be "struct{ uint32_t checksum; wuffs_crc32__status status }"?
pub func ieee.update(x[] u8)(checksum u32) {
pub func ieee.update(x slice u8)(checksum u32) {
var s u32 = 0xFFFFFFFF ^ this.state
iterate.1 (p ptr u8:in.x) {
@@ -37,7 +37,7 @@ pub func ieee.update(x[] u8)(checksum u32) {
// The table below was created by script/print-crc32-magic-numbers.go.
pri const ieee_table[256] u32 = $(
pri const ieee_table array[256] u32 = $(
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
+2 -2
View File
@@ -13,12 +13,12 @@
// limitations under the License.
// code_order is defined in the RFC section 3.2.7.
pri const code_order[19] u8[..18] = $(
pri const code_order array[19] u8[..18] = $(
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
)
// reverse8 reverses the bits in a byte.
pri const reverse8[256] u8 = $(
pri const reverse8 array[256] u8 = $(
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17
+10 -10
View File
@@ -41,14 +41,14 @@ pri error "internal error: inconsistent n_bits"
//
// Some trailing elements are 0x08000000. Bit 27 indicates an invalid value.
pri const lcode_magic_numbers[32] u32 = $(
pri const lcode_magic_numbers array[32] u32 = $(
0x40000300, 0x40000400, 0x40000500, 0x40000600, 0x40000700, 0x40000800, 0x40000900, 0x40000A00,
0x40000B10, 0x40000D10, 0x40000F10, 0x40001110, 0x40001320, 0x40001720, 0x40001B20, 0x40001F20,
0x40002330, 0x40002B30, 0x40003330, 0x40003B30, 0x40004340, 0x40005340, 0x40006340, 0x40007340,
0x40008350, 0x4000A350, 0x4000C350, 0x4000E350, 0x40010200, 0x08000000, 0x08000000, 0x08000000,
)
pri const dcode_magic_numbers[32] u32 = $(
pri const dcode_magic_numbers array[32] u32 = $(
0x40000000, 0x40000100, 0x40000200, 0x40000300, 0x40000410, 0x40000610, 0x40000820, 0x40000C20,
0x40001030, 0x40001830, 0x40002040, 0x40003040, 0x40004050, 0x40006050, 0x40008060, 0x4000C060,
0x40010070, 0x40018070, 0x40020080, 0x40030080, 0x40040090, 0x40060090, 0x400800A0, 0x400C00A0,
@@ -93,8 +93,8 @@ pub struct decoder?(
//
// TODO: replace the magic "big enough" 1234 with something more
// principled, perhaps discovered via an exhaustive search.
huffs[2][1234] u32,
n_huffs_bits[2] u32[..9],
huffs array[2] array[1234] u32,
n_huffs_bits array[2] u32[..9],
// history holds up to the last 32KiB of decoded output, if the decoding
// was incomplete (e.g. due to a short read or write). RFC 1951 (DEFLATE)
@@ -107,13 +107,13 @@ pub struct decoder?(
//
// history[history_index & 0x7FFF] is where the next byte of decoded output
// will be written.
history[0x8000] u8, // 32 KiB.
history array[0x8000] u8, // 32 KiB.
history_index u32,
// code_lengths is used to pass out-of-band data to init_huff.
//
// code_lengths[in.n_codes0 + i] holds the number of bits in the i'th code.
code_lengths[320] u8[..15],
code_lengths array[320] u8[..15],
// end_of_block is whether decode_huffman_xxx saw an end-of-block code.
//
@@ -131,7 +131,7 @@ pub func decoder.decode?(dst io_writer, src io_reader)() {
// TODO: should "since_mark" be "since_mark!", as the return value lets
// you modify the state of in.dst, so future mutations (via the slice)
// can change the veracity of any in.dst assertions?
var written[] u8 = in.dst.since_mark()
var written slice u8 = in.dst.since_mark()
// Append written, the decoded output, to the history ringbuffer.
if written.length() >= 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,
+6 -6
View File
@@ -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,
)
+4 -4
View File
@@ -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
+2 -2
View File
@@ -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]