Add a slice.length method.

This commit is contained in:
Nigel Tao committed 2017-09-22 12:37:07 +10:00
1 parent fda696bf95
commit 51f2b183a2
7 files changed
+57 -6

No files matched your search

+15
View File
@@ -144,6 +144,21 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
}
return nil
}
if isLength(g.tm, n) {
if pp == parenthesesMandatory {
b.writeb('(')
}
b.writes("(uint64_t)(")
x := n.LHS().Expr().LHS().Expr()
if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(".len)")
if pp == parenthesesMandatory {
b.writeb(')')
}
return nil
}
// TODO.
case t.KeyOpenBracket:
+8 -5
View File
@@ -196,11 +196,6 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
b.writeb('0')
}
b.writes(";\n")
// TODO: remove this.
if n.XType().Decorator().Key() == t.KeyColon {
b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", vPrefix, n.Name().String(g.tm))
}
}
return nil
@@ -782,3 +777,11 @@ func isDecode(tm *t.Map, n *a.Expr) bool {
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
}
func isLength(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() || len(n.Args()) != 0 {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLength
}
+2 -1
View File
@@ -588,7 +588,8 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
if (v_z > 0) {
v_written = ((puffs_base_slice_u8){.ptr = a_dst.buf->ptr,
.len = b_wptr_dst - a_dst.buf->ptr});
PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(v_written);
if (((uint64_t)(v_written.len)) >= 32768) {
}
}
status = v_z;
goto suspend;
+3
View File
@@ -715,6 +715,9 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
}
return nil, nil, nil
}
if isLength(q.tm, n) {
break
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
case t.KeyOpenBracket:
+24
View File
@@ -442,6 +442,22 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(typeExprPlaceholder) // HACK.
return nil
}
// TODO: delete this hack that only matches "foo.length(etc)".
if isLength(q.tm, n) {
foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil {
return err
}
n.LHS().SetTypeChecked()
n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK.
for _, o := range n.Args() {
if err := q.tcheckArg(o.Arg(), depth); err != nil {
return err
}
}
n.SetMType(typeExprU64)
return nil
}
case t.KeyOpenBracket:
// n is an index.
@@ -599,6 +615,14 @@ func isDecode(tm *t.Map, n *a.Expr) bool {
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
}
func isLength(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() || len(n.Args()) != 0 {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLength
}
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
lhs := n.LHS().Expr()
if err := q.tcheckExpr(lhs, depth); err != nil {
+3
View File
@@ -270,6 +270,7 @@ const (
KeyIsOK = Key(IDIsOK >> KeyShift)
KeyIsSuspension = Key(IDIsSuspension >> KeyShift)
KeySlice = Key(IDSlice >> KeyShift)
KeyLength = Key(IDLength >> KeyShift)
KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift)
KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift)
@@ -444,6 +445,7 @@ const (
IDIsOK = ID(0xC6<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsSuspension = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSlice = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDLength = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
)
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
@@ -640,6 +642,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyIsOK: {"is_ok", IDIsOK},
KeyIsSuspension: {"is_suspension", IDIsSuspension},
KeySlice: {"slice", IDSlice},
KeyLength: {"length", IDLength},
}
var builtInsByName = map[string]ID{}
+2
View File
@@ -99,6 +99,8 @@ pub func decoder.decode?(dst writer1, src reader1)() {
// 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.slice()
if written.length() >= (32 * 1024) {
}
}
return z
}