diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 04a2efc0..96cd4b89 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -314,6 +314,17 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var } return fmt.Errorf("cannot convert Wuffs type %q to C", n.Str(g.tm)) } + if n.IsTableType() { + o := n.Inner() + if o.Decorator() == 0 && o.QID() == (t.QID{t.IDBase, t.IDU8}) && !o.IsRefined() { + b.writes("wuffs_base__table_u8") + b.writeb(' ') + b.writes(varNamePrefix) + b.writes(varName) + return nil + } + return fmt.Errorf("cannot convert Wuffs type %q to C", n.Str(g.tm)) + } // maxNumPointers is an arbitrary implementation restriction. const maxNumPointers = 16 diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 131e2bc2..1bc4c4a6 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -399,6 +399,9 @@ func (g *gen) writeStatementVar(b *buffer, n *a.Var, depth uint32) error { } else if nTyp.IsSliceType() { // TODO: don't assume that the slice is a slice of base.u8. b.printf("((wuffs_base__slice_u8){})") + } else if nTyp.IsTableType() { + // TODO: don't assume that the table is a table of base.u8. + b.printf("((wuffs_base__table_u8){})") } else if nTyp.IsIOType() { s := "reader" if nTyp.QID()[1] == t.IDIOWriter { diff --git a/cmd/wuffs-c/internal/cgen/var.go b/cmd/wuffs-c/internal/cgen/var.go index fec60708..54c62eec 100644 --- a/cmd/wuffs-c/internal/cgen/var.go +++ b/cmd/wuffs-c/internal/cgen/var.go @@ -285,6 +285,9 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp case t.IDSlice: // TODO: don't assume that the slice is a slice of base.u8. rhs = "wuffs_base__slice_u8" + case t.IDTable: + // TODO: don't assume that the table is a table of base.u8. + rhs = "wuffs_base__table_u8" } if rhs != "" { b.printf("%s = ((%s){});\n", local, rhs) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index f514d7d5..8ac9a8af 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -647,8 +647,8 @@ func NewJump(keyword t.ID, label t.ID) *Jump { const MaxTypeExprDepth = 63 // TypeExpr is a type expression, such as "base.u32", "base.u32[..8]", "foo", -// "pkg.bar", "ptr T", "array[8] T" or "slice T": -// - ID0: <0|IDPtr|IDArray|IDSlice|IDFunc> +// "pkg.bar", "ptr T", "array[8] T", "slice T" or "table T": +// - ID0: <0|IDArray|IDFunc|IDPtr|IDSlice|IDTable> // - ID1: <0|pkg> // - ID2: <0|type name> // - LHS: @@ -661,6 +661,8 @@ const MaxTypeExprDepth = 63 // // An IDSlice ID0 means "slice RHS". RHS is the inner type. // +// An IDTable ID0 means "table RHS". RHS is the inner type. +// // An IDFunc ID0 means "func ID2" or "func (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" instead of "ptr T", "ptr ptr T", etc. @@ -673,7 +675,7 @@ const MaxTypeExprDepth = 63 // Numeric types can be refined as "foo[LHS..MHS]". LHS and MHS are Expr's, // possibly nil. For example, the LHS for "base.u32[..4095]" is nil. // -// TODO: struct types, list types, nptr vs ptr, table types. +// TODO: struct types, list types, nptr vs ptr. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } @@ -731,6 +733,10 @@ func (n *TypeExpr) IsSliceType() bool { return n.id0 == t.IDSlice } +func (n *TypeExpr) IsTableType() bool { + return n.id0 == t.IDTable +} + func (n *TypeExpr) IsUnsignedInteger() bool { return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDU8 || n.id2 == t.IDU16 || n.id2 == t.IDU32 || n.id2 == t.IDU64) @@ -746,7 +752,7 @@ func (n *TypeExpr) HasPointers() bool { return true } } - case t.IDPtr, t.IDNptr, t.IDSlice: + case t.IDPtr, t.IDNptr, t.IDSlice, t.IDTable: return true } } diff --git a/lang/ast/string.go b/lang/ast/string.go index a55ba4d7..5c97ddb6 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -234,6 +234,9 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte { case t.IDSlice: buf = append(buf, "slice "...) return n.Inner().appendStr(buf, tm, depth) + case t.IDTable: + buf = append(buf, "table "...) + return n.Inner().appendStr(buf, tm, depth) case t.IDFunc: buf = append(buf, "func "...) if r := n.Receiver(); r != nil { diff --git a/lang/check/bounds.go b/lang/check/bounds.go index efa23cae..d21de527 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -463,7 +463,7 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error { case t.IDArray: // TODO: handle. return nil - // TODO: t.IDSlice? + // TODO: t.IDSlice, t.IDTable? } _, _, err := q.bcheckExpr(lhs, 0) @@ -1218,7 +1218,7 @@ func (q *checker) bcheckTypeExpr(typ *a.TypeExpr) (*big.Int, *big.Int, error) { switch typ.Decorator() { // TODO: case t.IDFunc. - case t.IDPtr, t.IDArray, t.IDSlice: + case t.IDPtr, t.IDArray, t.IDSlice, t.IDTable: return nil, nil, nil } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 31e85de8..6b7a2219 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -54,6 +54,7 @@ var ( typeExprStatus = a.NewTypeExpr(0, t.IDBase, t.IDStatus, nil, nil, nil) typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8) + typeExprTableU8 = a.NewTypeExpr(t.IDTable, 0, 0, nil, nil, typeExprU8) ) // typeMap maps from variable names (as token IDs) to types. diff --git a/lang/check/type.go b/lang/check/type.go index ab34651c..d21f1ab4 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -987,7 +987,7 @@ swtch: fallthrough // TODO: also check t.IDNptr? Where else should we look for nptr? - case t.IDPtr, t.IDSlice: + case t.IDPtr, t.IDSlice, t.IDTable: if err := q.tcheckTypeExpr(typ.Inner(), depth); err != nil { return err } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 5d92f586..c6d678a4 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -405,6 +405,10 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { case t.IDSlice: decorator = t.IDSlice p.src = p.src[1:] + + case t.IDTable: + decorator = t.IDTable + p.src = p.src[1:] } if decorator != 0 { diff --git a/lang/token/list.go b/lang/token/list.go index c3bf6a2e..24b9c6e0 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -185,9 +185,9 @@ type Token struct { // - [ 0x20, 0x3F] are operators, such as "+", "==" and "not". // - [ 0x40, 0x6F] are x-ops (disambiguation forms): unary vs binary "+". // - [ 0x70, 0x8F] are keywords, such as "if" and "return". -// - [ 0x90, 0x93] are type modifiers, such as "ptr" and "slice". -// - [ 0x94, 0x9F] are literals, such as "false" and "true". -// - [ 0xA0, 0xFF] are reserved. +// - [ 0x90, 0x9F] are type modifiers, such as "ptr" and "slice". +// - [ 0xA0, 0xAF] are literals, such as "false" and "true". +// - [ 0xB0, 0xFF] are reserved. // - [0x100, 0x3FF] are identifiers, such as "bool", "u32" and "read_u8". // // "Squiggly" means a sequence of non-alpha-numeric characters, such as "+" and @@ -356,32 +356,33 @@ const ( const ( minTypeModifier = 0x90 - maxTypeModifier = 0x93 + maxTypeModifier = 0x9F IDArray = ID(0x90) IDNptr = ID(0x91) IDPtr = ID(0x92) IDSlice = ID(0x93) + IDTable = ID(0x94) ) const ( - minBuiltInLiteral = 0x94 - minBuiltInNumLiteral = 0x96 - maxBuiltInNumLiteral = 0x9F - maxBuiltInLiteral = 0x9F + minBuiltInLiteral = 0xA0 + minBuiltInNumLiteral = 0xA2 + maxBuiltInNumLiteral = 0xAF + maxBuiltInLiteral = 0xAF - IDFalse = ID(0x94) - IDTrue = ID(0x95) - ID0 = ID(0x96) - ID1 = ID(0x97) - ID2 = ID(0x98) - ID4 = ID(0x99) - ID8 = ID(0x9A) - ID16 = ID(0x9B) - ID32 = ID(0x9C) - ID64 = ID(0x9D) - ID128 = ID(0x9E) - ID256 = ID(0x9F) + IDFalse = ID(0xA0) + IDTrue = ID(0xA1) + ID0 = ID(0xA2) + ID1 = ID(0xA3) + ID2 = ID(0xA4) + ID4 = ID(0xA5) + ID8 = ID(0xA6) + ID16 = ID(0xA7) + ID32 = ID(0xA8) + ID64 = ID(0xA9) + ID128 = ID(0xAA) + ID256 = ID(0xAB) ) const ( @@ -565,6 +566,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDNptr: "nptr", IDPtr: "ptr", IDSlice: "slice", + IDTable: "table", IDFalse: "false", IDTrue: "true",