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
}