diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 6e1fb3ba..25527431 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -409,7 +409,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare b.writes("wuffs_base__slice_u8__subslice_ij(") } - lhsIsArray := lhs.MType().Decorator().Key() == t.KeyOpenBracket + lhsIsArray := lhs.MType().IsArrayType() if lhsIsArray { // TODO: don't assume that the slice is a slice of u8. b.writes("((wuffs_base__slice_u8){.ptr=") @@ -556,7 +556,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var const maxNumPointers = 16 x := n - for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() { + for ; x != nil && x.IsArrayType(); x = x.Inner() { } numPointers, innermost := 0, x @@ -617,7 +617,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var b.writes(varName) x = n - for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() { + for ; x != nil && x.IsArrayType(); x = x.Inner() { b.writeb('[') b.writes(x.ArrayLength().ConstValue().String()) b.writeb(']') diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 5a8c682e..95eb70fe 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -275,7 +275,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { return err } } - if n.XType().Decorator().Key() == t.KeyOpenBracket { + if n.XType().IsArrayType() { if n.Value() != nil { // TODO: something like: // cv := n.XType().ArrayLength().ConstValue() diff --git a/cmd/wuffs-c/internal/cgen/var.go b/cmd/wuffs-c/internal/cgen/var.go index d9d1a149..e329971f 100644 --- a/cmd/wuffs-c/internal/cgen/var.go +++ b/cmd/wuffs-c/internal/cgen/var.go @@ -312,7 +312,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp rhs = cTypeNames[key] } } - case t.KeyColon: + case t.KeySlice: // TODO: don't assume that the slice is a slice of u8. rhs = "wuffs_base__slice_u8" } @@ -362,7 +362,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool, initBoolTyp case 0: b.printf("%s = %s;\n", lhs, rhs) return nil - case t.KeyOpenBracket: + case t.KeyArray: inner := typ.Inner() if inner.Decorator() != 0 { break diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 7ab512b8..953ba53e 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -623,7 +623,7 @@ const MaxTypeExprDepth = 63 // TypeExpr is a type expression, such as "u32", "u32[..8]", "pkg.foo", "ptr // T", "[8] T" or "[] T": -// - ID0: <0|IDPtr|IDOpenBracket|IDColon|IDOpenParen> +// - ID0: <0|IDPtr|IDArray|IDSlice|IDOpenParen> // - ID1: <0|pkg> // - ID2: <0|type name> // - LHS: @@ -632,9 +632,9 @@ const MaxTypeExprDepth = 63 // // An IDPtr ID0 means "ptr RHS". RHS is the inner type. // -// An IDOpenBracket ID0 means "[LHS] RHS". RHS is the inner type. +// An IDArray ID0 means "[LHS] RHS". RHS is the inner type. // -// An IDColon ID0 means "[] RHS". RHS is the inner type. +// An IDSlice ID0 means "[] 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" @@ -648,7 +648,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 "u32[..4095]" is nil. // -// TODO: struct types, list types, nptr vs ptr. +// TODO: struct types, list types, nptr vs ptr, table types. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } @@ -691,11 +691,15 @@ func (n *TypeExpr) IsNumTypeOrIdeal() bool { } func (n *TypeExpr) IsRefined() bool { - return n.id0.Key() != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) + return n.id0.Key() != t.KeyArray && (n.lhs != nil || n.mhs != nil) +} + +func (n *TypeExpr) IsArrayType() bool { + return n.id0.Key() == t.KeyArray } func (n *TypeExpr) IsSliceType() bool { - return n.id0.Key() == t.KeyColon + return n.id0.Key() == t.KeySlice } func (n *TypeExpr) IsUnsignedInteger() bool { @@ -711,7 +715,7 @@ func (n *TypeExpr) HasPointers() bool { case t.KeyIOReader, t.KeyIOWriter: return true } - case t.KeyPtr, t.KeyNptr, t.KeyColon: + case t.KeyPtr, t.KeyNptr, t.KeySlice: return true } } diff --git a/lang/ast/eq.go b/lang/ast/eq.go index 5fd6c913..e22a48c6 100644 --- a/lang/ast/eq.go +++ b/lang/ast/eq.go @@ -105,7 +105,7 @@ func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool) bool { if n.id0 != o.id0 || n.id1 != o.id1 || n.id2 != o.id2 { return false } - if n.id0.Key() == t.KeyOpenBracket || !ignoreRefinements { + if n.IsArrayType() || !ignoreRefinements { if !n.lhs.Expr().Eq(o.lhs.Expr()) || !n.mhs.Expr().Eq(o.mhs.Expr()) { return false } diff --git a/lang/ast/string.go b/lang/ast/string.go index 44c3daa3..5514fcb8 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -211,12 +211,12 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte { case t.KeyPtr: buf = append(buf, "ptr "...) return n.Inner().appendStr(buf, tm, depth) - case t.KeyOpenBracket: + case t.KeyArray: buf = append(buf, '[') buf = n.ArrayLength().appendStr(buf, tm, false, 0) buf = append(buf, "] "...) return n.Inner().appendStr(buf, tm, depth) - case t.KeyColon: + case t.KeySlice: buf = append(buf, "[] "...) return n.Inner().appendStr(buf, tm, depth) case t.KeyOpenParen: diff --git a/lang/check/bounds.go b/lang/check/bounds.go index f8af8612..7f1b90db 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -439,9 +439,10 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error { case t.KeyPtr: // TODO: handle. return nil - case t.KeyOpenBracket: + case t.KeyArray: // TODO: handle. return nil + // TODO: t.KeySlice? } _, _, err := q.bcheckExpr(lhs, 0) @@ -890,7 +891,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } lengthExpr := (*a.Expr)(nil) - if lTyp := lhs.MType(); lTyp.Decorator().Key() == t.KeyOpenBracket { + if lTyp := lhs.MType(); lTyp.IsArrayType() { lengthExpr = lTyp.ArrayLength() } else { lengthExpr = makeSliceLengthExpr(lhs) @@ -912,7 +913,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } lengthExpr := (*a.Expr)(nil) - if lTyp := lhs.MType(); lTyp.Decorator().Key() == t.KeyOpenBracket { + if lTyp := lhs.MType(); lTyp.IsArrayType() { lengthExpr = lTyp.ArrayLength() } else { lengthExpr = makeSliceLengthExpr(lhs) @@ -1187,7 +1188,7 @@ func (q *checker) bcheckTypeExpr(typ *a.TypeExpr) (*big.Int, *big.Int, error) { switch typ.Decorator().Key() { // TODO: case t.KeyOpenParen. - case t.KeyPtr, t.KeyOpenBracket, t.KeyColon: + case t.KeyPtr, t.KeyArray, t.KeySlice: return nil, nil, nil } diff --git a/lang/check/check.go b/lang/check/check.go index d7bf521b..c0b5ffda 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -332,7 +332,7 @@ func (c *Checker) checkConst(node *a.Node) error { nLists := 0 typ := n.XType() - for typ.Decorator().Key() == t.KeyOpenBracket { + for typ.IsArrayType() { if nLists == a.MaxTypeExprDepth { return fmt.Errorf("check: type expression recursion depth too large") } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index b9d34c96..647d7658 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -48,7 +48,7 @@ var ( typeExprIOWriter = a.NewTypeExpr(0, 0, t.IDIOWriter, nil, nil, nil) typeExprImageConfig = a.NewTypeExpr(0, 0, t.IDImageConfig, nil, nil, nil) - typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, 0, nil, nil, typeExprU8) + typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8) // TODO: delete this. typeExprPlaceholder = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil) @@ -139,8 +139,7 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { lTyp := typ.Receiver() lQID := lTyp.QID() qqid := t.QQID{lQID[0], lQID[1], typ.FuncName()} - if lTyp.Decorator().Key() == t.KeyColon { - // lTyp is a slice. + if lTyp.IsSliceType() { qqid[0] = 0 qqid[1] = t.IDDiamond if f, err := c.builtInSliceFunc(qqid); err != nil { diff --git a/lang/check/type.go b/lang/check/type.go index ba9431c2..6bf2eadd 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -465,7 +465,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return err } lTyp := lhs.MType() - if key := lTyp.Decorator().Key(); key != t.KeyOpenBracket && key != t.KeyColon { + if key := lTyp.Decorator().Key(); key != t.KeyArray && key != t.KeySlice { return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array or slice type", n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) } @@ -510,9 +510,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { default: return fmt.Errorf("check: %s is a slice expression but %s has type %s, not an array or slice type", n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) - case t.KeyOpenBracket: - n.SetMType(a.NewTypeExpr(t.IDColon, 0, 0, nil, nil, lTyp.Inner())) - case t.KeyColon: + case t.KeyArray: + n.SetMType(a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, lTyp.Inner())) + case t.KeySlice: n.SetMType(lTyp) } return nil @@ -689,8 +689,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { lQID := lTyp.QID() qqid := t.QQID{lQID[0], lQID[1], n.Ident()} - if key := lTyp.Decorator().Key(); key == t.KeyColon { - // lTyp is a slice. + if lTyp.IsSliceType() { qqid[0] = 0 qqid[1] = t.IDDiamond if f, err := q.c.builtInSliceFunc(qqid); err != nil { @@ -700,7 +699,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { } n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.Ident(), lTyp.Node(), nil, nil)) return nil - } else if key != 0 { + } else if lTyp.Decorator().Key() != 0 { return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm)) } @@ -1056,7 +1055,7 @@ swtch: } return fmt.Errorf("check: %q is not a type", typ.Str(q.tm)) - case t.KeyOpenBracket: + case t.KeyArray: aLen := typ.ArrayLength() if err := q.tcheckExpr(aLen, 0); err != nil { return err @@ -1067,7 +1066,7 @@ swtch: fallthrough // TODO: also check t.KeyNptr? Where else should we look for nptr? - case t.KeyPtr, t.KeyColon: + case t.KeyPtr, t.KeySlice: if err := q.tcheckTypeExpr(typ.Inner(), depth); err != nil { return err } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 472737ae..fef081f9 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -389,9 +389,9 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { if p.peek1().Key() == t.KeyOpenBracket { p.src = p.src[1:] - decorator, lhs := t.IDColon, (*a.Expr)(nil) + decorator, lhs := t.IDSlice, (*a.Expr)(nil) if p.peek1().Key() != t.KeyCloseBracket { - decorator = t.IDOpenBracket + decorator = t.IDArray var err error lhs, err = p.parseExpr() if err != nil { diff --git a/lang/token/list.go b/lang/token/list.go index c0f158b0..214a6e07 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -162,7 +162,8 @@ func (t Token) IsXAssociativeOp() bool { return t.ID.Key().isXOp() && t.ID.IsAss // - [0x20, 0x2F] are squiggly assignments, such as "=" and "+=". // - [0x30, 0x4F] are operators, such as "+", "==" and "not". // - [0x50, 0x6F] are keywords, such as "if" and "return". -// - [0x70, 0x77] are literals, such as "false" and "true". +// - [0x70, 0x73] are type modifiers, such as "ptr" and "slice". +// - [0x74, 0x77] are literals, such as "false" and "true". // - [0x78, 0xCF] are identifiers, such as "bool", "u32" and "read_u8". // - [0xD0, 0xFF] are disambiguation forms, e.g. unary "+" vs binary "+". // @@ -238,7 +239,6 @@ const ( // TODO: sort these by name, when the list has stabilized. KeyFunc = Key(IDFunc >> KeyShift) - KeyPtr = Key(IDPtr >> KeyShift) KeyAssert = Key(IDAssert >> KeyShift) KeyWhile = Key(IDWhile >> KeyShift) KeyIf = Key(IDIf >> KeyShift) @@ -249,7 +249,6 @@ const ( KeyStruct = Key(IDStruct >> KeyShift) KeyUse = Key(IDUse >> KeyShift) KeyVar = Key(IDVar >> KeyShift) - KeyNptr = Key(IDNptr >> KeyShift) KeyPre = Key(IDPre >> KeyShift) KeyInv = Key(IDInv >> KeyShift) KeyPost = Key(IDPost >> KeyShift) @@ -264,6 +263,11 @@ const ( KeyIterate = Key(IDIterate >> KeyShift) KeyYield = Key(IDYield >> KeyShift) + KeyArray = Key(IDArray >> KeyShift) + KeyNptr = Key(IDNptr >> KeyShift) + KeyPtr = Key(IDPtr >> KeyShift) + KeySlice = Key(IDSlice >> KeyShift) + KeyFalse = Key(IDFalse >> KeyShift) KeyTrue = Key(IDTrue >> KeyShift) KeyZero = Key(IDZero >> KeyShift) @@ -427,35 +431,38 @@ const ( // TODO: sort these by name, when the list has stabilized. IDFunc = ID(0x50<