diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 60131947..29b2c2f1 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -188,8 +188,28 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil case t.KeyColon: - // n is a slice. - // TODO. + // n is a slice. + lhs := n.LHS().Expr() + mhs := n.MHS().Expr() + rhs := n.RHS().Expr() + if mhs == nil && rhs == nil { + if lhs.MType().Decorator().Key() == t.KeyOpenBracket { + // Slice of an array. + // TODO: don't assume that the slice is a slice of u8. + b.writes("((puffs_base_slice_u8){.ptr = ") + if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil { + return err + } + b.printf(", .len=%s})", lhs.MType().ArrayLength().ConstValue()) + } else { + // Slice of a slice. + if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil { + return err + } + } + return nil + } + return fmt.Errorf("TODO: writeExprOther for a non-trivial slice") case t.KeyDot: lhs := n.LHS().Expr() diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index ab158d6d..69311f99 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -61,12 +61,10 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { // TODO: delete this hack that only matches "foo.copy_from(etc)". if isThatMethod(g.tm, n, t.KeyCopyFrom, 1) { b.writes("puffs_base_slice_u8_copy_from(") - // TODO: don't hard-code this.history (a [32768]u8) as the - // receiver. In any case, the receiver should probably be a slice - // instead of an array. - b.writes("((puffs_base_slice_u8){") - b.writes(".ptr=self->private_impl.f_history,") - b.writes(".len=sizeof(self->private_impl.f_history)})") + receiver := n.LHS().Expr().LHS().Expr() + if err := g.writeExpr(b, receiver, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } b.writeb(',') a := n.Args()[0].Arg().Value() if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index b034d46f..b52c7b0d 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -650,9 +650,8 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, if (((uint64_t)(v_written.len)) >= 32768) { v_written = puffs_base_slice_u8_suffix(v_written, 32768); puffs_base_slice_u8_copy_from( - ((puffs_base_slice_u8){ - .ptr = self->private_impl.f_history, - .len = sizeof(self->private_impl.f_history)}), + ((puffs_base_slice_u8){.ptr = self->private_impl.f_history, + .len = 32768}), v_written); self->private_impl.f_history_index = 32768; } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 7f10dec9..0c0c89a8 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -761,8 +761,12 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } case t.KeyColon: - // TODO. - return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key()) + mhs := n.MHS().Expr() + rhs := n.RHS().Expr() + if mhs == nil && rhs == nil { + return nil, nil, nil + } + return nil, nil, fmt.Errorf("TODO: bcheckExprOther for a non-trivial slice") case t.KeyDot: // TODO: delete this hack that only matches "in". diff --git a/lang/check/type.go b/lang/check/type.go index 135ddfc0..c2300c9a 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -487,7 +487,41 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyColon: // n is a slice. - // TODO. + if mhs := n.MHS().Expr(); mhs != nil { + if err := q.tcheckExpr(mhs, depth); err != nil { + return err + } + mTyp := mhs.MType() + if !mTyp.IsNumTypeOrIdeal() { + return fmt.Errorf("check: %s is a slice expression but %s has type %s, not a numeric type", + n.String(q.tm), mhs.String(q.tm), mTyp.String(q.tm)) + } + } + if rhs := n.RHS().Expr(); rhs != nil { + if err := q.tcheckExpr(rhs, depth); err != nil { + return err + } + rTyp := rhs.MType() + if !rTyp.IsNumTypeOrIdeal() { + return fmt.Errorf("check: %s is a slice expression but %s has type %s, not a numeric type", + n.String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) + } + } + lhs := n.LHS().Expr() + if err := q.tcheckExpr(lhs, depth); err != nil { + return err + } + lTyp := lhs.MType() + switch lTyp.Decorator().Key() { + default: + return fmt.Errorf("check: %s is a slice expression but %s has type %s, not an array or slice type", + n.String(q.tm), lhs.String(q.tm), lTyp.String(q.tm)) + case t.KeyOpenBracket: + n.SetMType(a.NewTypeExpr(t.IDColon, 0, nil, nil, lTyp.Inner())) + case t.KeyColon: + n.SetMType(lTyp) + } + return nil case t.KeyDot: return q.tcheckDot(n, depth) diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 07ffc979..57f2120d 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -114,8 +114,7 @@ pub func decoder.decode?(dst writer1, src reader1)() { var written[] u8 = in.dst.slice() if written.length() >= (32 * 1024) { written = written.suffix(up_to:32 * 1024) - // TODO: change this.history to this.history[:]. - this.history.copy_from(s:written) + this.history[:].copy_from(s:written) this.history_index = 32 * 1024 } else { // TODO: fill part of the history.