Use "T[..inclMax]" instead of "T[:exclMax]".

This commit is contained in:
Nigel Tao committed 2017-05-20 15:11:30 +10:00
1 parent d3eba13223
commit 9184e2682e
9 files changed
+53 -44

No files matched your search

+7 -7
View File
@@ -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(),
}
}
+5 -5
View File
@@ -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
+4 -4
View File
@@ -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{}
+3 -3
View File
@@ -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
}
}
}
+4 -4
View File
@@ -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)
+1 -1
View File
@@ -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.
+15 -13
View File
@@ -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
}
+12 -5
View File
@@ -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<<KeyShift | FlagsClose | FlagsImplicitSemicolon)
IDDot = ID(0x20<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDComma = ID(0x21<<KeyShift | FlagsTightLeft)
IDQuestion = ID(0x22<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDColon = ID(0x23<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDSemicolon = ID(0x24<<KeyShift | FlagsTightLeft)
IDDotDot = ID(0x21<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDComma = ID(0x22<<KeyShift | FlagsTightLeft)
IDQuestion = ID(0x23<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDColon = ID(0x24<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDSemicolon = ID(0x25<<KeyShift | FlagsTightLeft)
IDEq = ID(0x30<<KeyShift | FlagsAssign)
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
@@ -393,6 +395,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyCloseCurly: {"}", IDCloseCurly},
KeyDot: {".", IDDot},
KeyDotDot: {"..", IDDotDot},
KeyComma: {",", IDComma},
KeyQuestion: {"?", IDQuestion},
KeyColon: {":", IDColon},
@@ -491,7 +494,6 @@ var squiggles = [256]ID{
'{': IDOpenCurly,
'}': IDCloseCurly,
'.': IDDot,
',': IDComma,
'?': IDQuestion,
':': IDColon,
@@ -500,6 +502,7 @@ var squiggles = [256]ID{
// 1<<KeyShift is a non-zero ID with zero Flags. It is an invalid ID,
// signifying that lookup should continue in the lexers array below.
'.': 1 << KeyShift,
'!': 1 << KeyShift,
'&': 1 << KeyShift,
'|': 1 << KeyShift,
@@ -524,6 +527,10 @@ type suffixLexer struct {
// The order of the []suffixLexer elements matters. The first match wins. Since
// we want to lex greedily, longer suffixes should be earlier in the slice.
var lexers = [256][]suffixLexer{
'.': {
{".", IDDotDot},
{"", IDDot},
},
'!': {
{"=", IDNotEq},
},
+2 -2
View File
@@ -2,7 +2,7 @@ use "std/io"
struct lzw_decoder?(
// TODO: default values??
literal_width u32[2:9],
literal_width u32[2..8],
stack[4096] u8,
suffixes[4096] u8,
prefixes[4096] u16,
@@ -14,7 +14,7 @@ func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() {
// TODO: set clear_code, end_code, etc.
var width u32[:13] = this.literal_width + 1
var width u32[..12] = this.literal_width + 1
// TODO: implement.