From 51f2b183a289d13db325fa632beac6f717500d13 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 22 Sep 2017 12:37:07 +1000 Subject: [PATCH] Add a slice.length method. --- cmd/puffs-c/internal/cgen/expr.go | 15 +++++++++++++++ cmd/puffs-c/internal/cgen/statement.go | 13 ++++++++----- gen/c/std/flate.c | 3 ++- lang/check/bounds.go | 3 +++ lang/check/type.go | 24 ++++++++++++++++++++++++ lang/token/list.go | 3 +++ std/flate/decode_flate.puffs | 2 ++ 7 files changed, 57 insertions(+), 6 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 8076d4ff..d3551df4 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -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: diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index c1818083..aeb9b996 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -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 +} diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 470bc047..35c692cd 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -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; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 8c6f1841..4f052088 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -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: diff --git a/lang/check/type.go b/lang/check/type.go index a292b786..578008ec 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -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 { diff --git a/lang/token/list.go b/lang/token/list.go index 1c2df852..a6b2fa50 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -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<= (32 * 1024) { + } } return z }