diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index ce48a83b..bee0f9e7 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -1118,7 +1118,7 @@ func (g *gen) writeExpr(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, d return err } case t.FlagsAssociativeOp: - if err := g.writeExprAssociativeOp(n, rp, depth); err != nil { + if err := g.writeExprAssociativeOp(n, rp, pp, depth); err != nil { return err } default: diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 940f8b02..cd1c3cbf 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -704,7 +704,10 @@ func (g *gen) writeExprAs(lhs *a.Expr, rhs *a.TypeExpr, rp replacementPolicy, de return nil } -func (g *gen) writeExprAssociativeOp(n *a.Expr, rp replacementPolicy, depth uint32) error { +func (g *gen) writeExprAssociativeOp(n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { + if pp == parenthesesMandatory { + g.writeb('(') + } opName := cOpNames[0xFF&n.ID0().Key()] for i, o := range n.Args() { if i != 0 { @@ -714,6 +717,9 @@ func (g *gen) writeExprAssociativeOp(n *a.Expr, rp replacementPolicy, depth uint return err } } + if pp == parenthesesMandatory { + g.writeb(')') + } return nil } diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index bdf25035..421ab304 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -1143,8 +1143,8 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, v_reversed_code = (((uint32_t)(puffs_flate_reverse8[v_code >> 1])) | ((v_code & 1) << 8)); v_reversed_code >>= (9 - v_cl); - v_value = ((((uint32_t)(self->private_impl.f_symbols[v_i])) << 8) | v_cl); - v_value |= 1073741824; + v_value = (1073741824 | + (((uint32_t)(self->private_impl.f_symbols[v_i])) << 8) | v_cl); v_high_bits = (((uint32_t)(1)) << v_max_cl); v_delta = (((uint32_t)(1)) << v_cl); while (v_high_bits >= v_delta) { diff --git a/lang/check/bounds.go b/lang/check/bounds.go index dc94141f..44ff3438 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -360,7 +360,7 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error { } rMin, rMax, err = q.bcheckExpr(rhs, 0) } else { - rMin, rMax, err = q.bcheckExprBinaryOp(lhs, op.BinaryForm().Key(), rhs, 0) + rMin, rMax, err = q.bcheckExprBinaryOp(op.BinaryForm().Key(), lhs, rhs, 0) } if err != nil { return err @@ -629,7 +629,7 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (*big.Int, *big.Int, erro if n.ID0().Key() == t.KeyXBinaryAs { return q.bcheckExpr(n.LHS().Expr(), depth) } - return q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth) + return q.bcheckExprBinaryOp(n.ID0().Key(), n.LHS().Expr(), n.RHS().Expr(), depth) case t.FlagsAssociativeOp: return q.bcheckExprAssociativeOp(n, depth) } @@ -778,11 +778,15 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key()) } -func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) { +func (q *checker) bcheckExprBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) { lMin, lMax, err := q.bcheckExpr(lhs, depth) if err != nil { return nil, nil, err } + return q.bcheckExprBinaryOp1(op, lhs, lMin, lMax, rhs, depth) +} + +func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) { rMin, rMax, err := q.bcheckExpr(rhs, depth) if err != nil { return nil, nil, err @@ -897,25 +901,30 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u } func (q *checker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { - switch n.ID0().Key() { - // TODO. - case t.KeyXAssociativePlus: - case t.KeyXAssociativeStar: - case t.KeyXAssociativeAmp: - case t.KeyXAssociativePipe: - case t.KeyXAssociativeHat: - - case t.KeyXAssociativeAnd, t.KeyXAssociativeOr: - for _, o := range n.Args() { - o := o.Expr() - if _, _, err := q.bcheckExpr(o, depth); err != nil { - return nil, nil, err - } - } - return zero, one, nil + op := n.ID0().AmbiguousForm().BinaryForm().Key() + if op == 0 { + return nil, nil, fmt.Errorf( + "check: unrecognized token.Key (0x%X) for bcheckExprAssociativeOp", n.ID0().Key()) } - - return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprAssociativeOp", n.ID0().Key()) + args := n.Args() + if len(args) < 1 { + return nil, nil, fmt.Errorf("check: associative op has no arguments") + } + lMin, lMax, err := q.bcheckExpr(args[0].Expr(), depth) + if err != nil { + return nil, nil, err + } + for i, o := range args { + if i == 0 { + continue + } + lhs := a.NewExpr(n.Node().Raw().Flags(), n.ID0(), n.ID1(), n.LHS(), n.MHS(), n.RHS(), args[:i]) + lMin, lMax, err = q.bcheckExprBinaryOp1(op, lhs, lMin, lMax, o.Expr(), depth) + if err != nil { + return nil, nil, err + } + } + return lMin, lMax, nil } func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { diff --git a/lang/check/type.go b/lang/check/type.go index 4015324a..c7bc4187 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -795,12 +795,40 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error { switch n.ID0().Key() { - // TODO. - case t.KeyXAssociativePlus: - case t.KeyXAssociativeStar: - case t.KeyXAssociativeAmp: - case t.KeyXAssociativePipe: - case t.KeyXAssociativeHat: + case t.KeyXAssociativePlus, t.KeyXAssociativeStar, + t.KeyXAssociativeAmp, t.KeyXAssociativePipe, t.KeyXAssociativeHat: + + expr, typ := (*a.Expr)(nil), (*a.TypeExpr)(nil) + for _, o := range n.Args() { + o := o.Expr() + if err := q.tcheckExpr(o, depth); err != nil { + return err + } + oTyp := o.MType() + if oTyp.IsIdeal() { + continue + } + if !oTyp.IsNumType() { + return fmt.Errorf("check: associative %q: %q, of type %q, does not have a numeric type", + n.ID0().AmbiguousForm().String(q.tm), o.String(q.tm), oTyp.String(q.tm)) + } + if typ == nil { + expr, typ = o, oTyp.Unrefined() + continue + } + if !typ.EqIgnoringRefinements(oTyp) { + return fmt.Errorf("check: associative %q: %q and %q, of types %q and %q, "+ + "do not have compatible types", + n.ID0().AmbiguousForm().String(q.tm), + expr.String(q.tm), o.String(q.tm), + expr.MType().String(q.tm), o.MType().String(q.tm)) + } + } + if typ == nil { + typ = typeExprIdeal + } + n.SetMType(typ) + return nil case t.KeyXAssociativeAnd, t.KeyXAssociativeOr: for _, o := range n.Args() { diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 33108e36..fe72a652 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -501,8 +501,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes u32[..289])() { reversed_code >>= 9 - cl // TODO: don't assume that the value is a literal. - var value u32 = ((this.symbols[i] as u32) << 8) | cl - value |= 0x40000000 + var value u32 = 0x40000000 | ((this.symbols[i] as u32) << 8) | cl // The table uses max_cl bits, but reversed_code only has cl bits. We // duplicate the key-value pair across all possible values of the high