diff --git a/lang/ast/ast.go b/lang/ast/ast.go index cc4f4069..a613c876 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -400,8 +400,8 @@ const MaxTypeExprDepth = 63 // // Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or // "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be -// refined as "foo[LHS:MHS]". LHS and MHS are Expr's, possibly nil. For -// example, the MHS for "u32[:4096]" is nil. +// refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For +// example, the LHS for "u32[..4095]" is nil. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } @@ -413,17 +413,17 @@ func (n *TypeExpr) PackageOrDecorator() t.ID { return n.id0 } func (n *TypeExpr) Name() t.ID { return n.id1 } func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() } func (n *TypeExpr) Bounds() [2]*Expr { return [2]*Expr{n.lhs.Expr(), n.mhs.Expr()} } -func (n *TypeExpr) InclMin() *Expr { return n.lhs.Expr() } -func (n *TypeExpr) ExclMax() *Expr { return n.mhs.Expr() } +func (n *TypeExpr) Min() *Expr { return n.lhs.Expr() } +func (n *TypeExpr) Max() *Expr { return n.mhs.Expr() } func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() } -func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthInclMin *Expr, exclMax *Expr, inner *TypeExpr) *TypeExpr { +func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inner *TypeExpr) *TypeExpr { return &TypeExpr{ kind: KTypeExpr, id0: pkgOrDec, id1: name, - lhs: arrayLengthInclMin.Node(), - mhs: exclMax.Node(), + lhs: arrayLengthMin.Node(), + mhs: max.Node(), rhs: inner.Node(), } } diff --git a/lang/ast/string.go b/lang/ast/string.go index a7621a73..1ac7935e 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -145,7 +145,7 @@ func (n *TypeExpr) String(m *t.IDMap) string { if n == nil { return "" } - if n.PackageOrDecorator() == 0 && n.InclMin() == nil && n.ExclMax() == nil { + if n.PackageOrDecorator() == 0 && n.Min() == nil && n.Max() == nil { return m.ByID(n.Name()) } return string(n.appendString(nil, m, 0)) @@ -176,11 +176,11 @@ func (n *TypeExpr) appendString(buf []byte, m *t.IDMap, depth uint32) []byte { buf = append(buf, '.') buf = append(buf, m.ByID(n.Name())...) } - if n.InclMin() != nil || n.ExclMax() != nil { + if n.Min() != nil || n.Max() != nil { buf = append(buf, '[') - buf = n.InclMin().appendString(buf, m, false, 0) - buf = append(buf, ':') - buf = n.ExclMax().appendString(buf, m, false, 0) + buf = n.Min().appendString(buf, m, false, 0) + buf = append(buf, ".."...) + buf = n.Max().appendString(buf, m, false, 0) buf = append(buf, ']') } return buf diff --git a/lang/ast/string_test.go b/lang/ast/string_test.go index cb0699d1..5be9b0bd 100644 --- a/lang/ast/string_test.go +++ b/lang/ast/string_test.go @@ -50,13 +50,13 @@ func TestString(t *testing.T) { "x as u32", "x as T", "x as T", - "x as T[i:]", - "x as T[:j]", - "x as T[i:j]", + "x as T[i..]", + "x as T[..j]", + "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 [8 + (2 * N)] ptr [4] ptr pkg.T[i..j]", } idMap := &token.IDMap{} diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 6369c12d..9f959191 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -348,14 +348,14 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.idMap)) } if n.IsRefined() { - if x := n.InclMin(); x != nil { + if x := n.Min(); x != nil { if cv := x.ConstValue(); cv != nil && b[0].Cmp(cv) < 0 { b[0] = cv } } - if x := n.ExclMax(); x != nil { + if x := n.Max(); x != nil { if cv := x.ConstValue(); cv != nil && b[1].Cmp(cv) > 0 { - b[1] = sub1(cv) + b[1] = cv } } } diff --git a/lang/check/check_test.go b/lang/check/check_test.go index 1ac26500..9b5973b4 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -53,7 +53,7 @@ func TestCheck(t *testing.T) { func foo.bar()() { var x u8 var y i32 = +2 - var z u64[:123] + var z u64[..123] var a[4] u8 var b bool @@ -66,7 +66,7 @@ func TestCheck(t *testing.T) { y = x as i32 var p i32 - var q i32[0:8] + var q i32[0..8] assert true @@ -129,11 +129,11 @@ func TestCheck(t *testing.T) { {"in", "in"}, {"out", "out"}, {"p", "i32"}, - {"q", "i32[0:8]"}, + {"q", "i32[0..8]"}, {"this", "ptr foo"}, {"x", "u8"}, {"y", "i32"}, - {"z", "u64[:123]"}, + {"z", "u64[..123]"}, } if !reflect.DeepEqual(got, want) { t.Fatalf("\ngot %v\nwant %v", got, want) diff --git a/lang/check/type.go b/lang/check/type.go index 13fd5c92..7074eac7 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -524,7 +524,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error { } break } - if n.InclMin() != nil || n.ExclMax() != nil { + if n.Min() != nil || n.Max() != nil { // TODO: reject. } // TODO: see if name refers to a struct type. diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 0f342418..4eaff317 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -287,7 +287,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { lhs, mhs := (*a.Expr)(nil), (*a.Expr)(nil) if p.peek1().Key() == t.KeyOpenBracket { - _, lhs, mhs, err = p.parseRangeOrIndex(false) + _, lhs, mhs, err = p.parseBracket(t.IDDotDot) if err != nil { return nil, err } @@ -296,17 +296,18 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { return a.NewTypeExpr(pkg, name, lhs, mhs, nil), nil } -// parseRangeOrIndex parses "[i:j]", "[i:]", "[:j]" and "[:]". If allowIndex, -// it also parses "[x]". The returned op is t.IDColon for a range and +// parseBracket parses "[i:j]", "[i:]", "[:j]" and "[:]". A double dot replaces +// the colon if sep is t.IDDotDot instead of t.IDColon. If sep is t.IDColon, it +// also parses "[x]". The returned op is sep for a range or refinement and // t.IDOpenBracket for an index. -func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, ei *a.Expr, ej *a.Expr, err error) { +func (p *parser) parseBracket(sep t.ID) (op t.ID, ei *a.Expr, ej *a.Expr, err error) { if x := p.peek1().Key(); x != t.KeyOpenBracket { got := p.m.ByKey(x) return 0, nil, nil, fmt.Errorf(`parse: expected "[", got %q at %s:%d`, got, p.filename, p.line()) } p.src = p.src[1:] - if p.peek1().Key() != t.KeyColon { + if p.peek1() != sep { ei, err = p.parseExpr() if err != nil { return 0, nil, nil, err @@ -314,20 +315,21 @@ func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, ei *a.Expr, ej *a. } switch x := p.peek1().Key(); { - case x == t.KeyColon: + case x == sep.Key(): p.src = p.src[1:] - case x == t.KeyCloseBracket && allowIndex: + case x == t.KeyCloseBracket && sep.Key() == t.KeyColon: p.src = p.src[1:] return t.IDOpenBracket, nil, ei, nil default: - expected := `":"` - if allowIndex { - expected = `":" or "]"` + extra := `` + if sep.Key() == t.KeyColon { + extra = ` or "]"` } got := p.m.ByKey(x) - return 0, nil, nil, fmt.Errorf(`parse: expected %s, got %q at %s:%d`, expected, got, p.filename, p.line()) + return 0, nil, nil, fmt.Errorf(`parse: expected %q%s, got %q at %s:%d`, + sep.String(p.m), extra, got, p.filename, p.line()) } if p.peek1().Key() != t.KeyCloseBracket { @@ -343,7 +345,7 @@ func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, ei *a.Expr, ej *a. } p.src = p.src[1:] - return t.IDColon, ei, ej, nil + return sep, ei, ej, nil } func (p *parser) parseBlock() ([]*a.Node, error) { @@ -678,7 +680,7 @@ func (p *parser) parseOperand() (*a.Expr, error) { lhs = a.NewExpr(flags, t.IDOpenParen, 0, lhs.Node(), nil, nil, args) case t.KeyOpenBracket: - id0, mhs, rhs, err := p.parseRangeOrIndex(true) + id0, mhs, rhs, err := p.parseBracket(t.IDColon) if err != nil { return nil, err } diff --git a/lang/token/list.go b/lang/token/list.go index b280c97d..5453c2db 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -137,6 +137,7 @@ const ( KeyCloseCurly = Key(IDCloseCurly >> KeyShift) KeyDot = Key(IDDot >> KeyShift) + KeyDotDot = Key(IDDotDot >> KeyShift) KeyComma = Key(IDComma >> KeyShift) KeyQuestion = Key(IDQuestion >> KeyShift) KeyColon = Key(IDColon >> KeyShift) @@ -261,10 +262,11 @@ const ( IDCloseCurly = ID(0x15<