diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 3346a7d3..75756542 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -510,18 +510,19 @@ var cTypeNames = [...]string{ } var cOpNames = [256]string{ - t.KeyEq: " = ", - t.KeyPlusEq: " += ", - t.KeyMinusEq: " -= ", - t.KeyStarEq: " *= ", - t.KeySlashEq: " /= ", - t.KeyShiftLEq: " <<= ", - t.KeyShiftREq: " >>= ", - t.KeyAmpEq: " &= ", - t.KeyAmpHatEq: " no_such_amp_hat_C_operator ", - t.KeyPipeEq: " |= ", - t.KeyHatEq: " ^= ", - t.KeyPercentEq: " %= ", + t.KeyEq: " = ", + t.KeyPlusEq: " += ", + t.KeyMinusEq: " -= ", + t.KeyStarEq: " *= ", + t.KeySlashEq: " /= ", + t.KeyShiftLEq: " <<= ", + t.KeyShiftREq: " >>= ", + t.KeyAmpEq: " &= ", + t.KeyAmpHatEq: " no_such_amp_hat_C_operator ", + t.KeyPipeEq: " |= ", + t.KeyHatEq: " ^= ", + t.KeyPercentEq: " %= ", + t.KeyTildePlusEq: " += ", t.KeyXUnaryPlus: "+", t.KeyXUnaryMinus: "-", @@ -547,6 +548,7 @@ var cOpNames = [256]string{ t.KeyXBinaryAnd: " && ", t.KeyXBinaryOr: " || ", t.KeyXBinaryAs: " no_such_as_C_operator ", + t.KeyXBinaryTildePlus: " + ", t.KeyXAssociativePlus: " + ", t.KeyXAssociativeStar: " * ", diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 11e641bc..8be87d2a 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -1493,7 +1493,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman( } if (v_n_copied > 0) { v_hlen -= v_n_copied; - v_hdist = ((v_hdist + (v_n_copied & 32767)) & 32767); + v_hdist = ((v_hdist + v_n_copied) & 32767); if (v_hdist == 0) { goto label_5_break; } diff --git a/lang/ast/ast.go b/lang/ast/ast.go index d6c81678..2d7c1636 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -524,6 +524,11 @@ func (n *TypeExpr) IsRefined() bool { return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) } +func (n *TypeExpr) IsUnsignedInteger() bool { + return n.id0 == 0 && (n.id1.Key() == t.KeyU8 || n.id1.Key() == t.KeyU16 || + n.id1.Key() == t.KeyU32 || n.id1.Key() == t.KeyU64) // TODO: t.KeyUsize? +} + func (n *TypeExpr) HasPointers() bool { for ; n != nil; n = n.Inner() { switch n.id0.Key() { diff --git a/lang/ast/string.go b/lang/ast/string.go index 0d9d3380..f6f182cd 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -158,6 +158,7 @@ var opStrings = [256]string{ t.KeyXBinaryAnd: " and ", t.KeyXBinaryOr: " or ", t.KeyXBinaryAs: " as ", + t.KeyXBinaryTildePlus: " ~+ ", t.KeyXAssociativePlus: " + ", t.KeyXAssociativeStar: " * ", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 2d9ff64b..9cc3dda5 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -997,6 +997,14 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax case t.KeyXBinaryAs: // Unreachable, as this is checked by the caller. + + case t.KeyXBinaryTildePlus: + typ := lhs.MType() + if typ.IsIdeal() { + typ = rhs.MType() + } + b := numTypeBounds[typ.Name().Key()] + return b[0], b[1], nil } return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op) } diff --git a/lang/check/type.go b/lang/check/type.go index 569f8374..50f46b8a 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -262,6 +262,11 @@ func (q *checker) tcheckAssign(n *a.Assign) error { } return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type", n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) + case t.KeyTildePlusEq: + if !lTyp.IsUnsignedInteger() { + return fmt.Errorf("check: assignment %q: %q, of type %q, does not have unsigned integer type", + n.Operator().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm)) + } } if rTyp.IsIdeal() || lTyp.EqIgnoringRefinements(rTyp) { @@ -826,6 +831,28 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { } } + switch op.Key() { + case t.KeyXBinaryTildePlus: + typ := lTyp + if typ.IsIdeal() { + typ = rTyp + if typ.IsIdeal() { + return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have non-ideal types", + op.AmbiguousForm().String(q.tm), + lhs.String(q.tm), rhs.String(q.tm), + lTyp.String(q.tm), rTyp.String(q.tm), + ) + } + } + if !typ.IsUnsignedInteger() { + return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have unsigned integer types", + op.AmbiguousForm().String(q.tm), + lhs.String(q.tm), rhs.String(q.tm), + lTyp.String(q.tm), rTyp.String(q.tm), + ) + } + } + if lcv, rcv := lhs.ConstValue(), rhs.ConstValue(); lcv != nil && rcv != nil { ncv, err := evalConstValueBinaryOp(q.tm, n, lcv, rcv) if err != nil { @@ -901,6 +928,8 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. return btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)), nil case t.KeyXBinaryOr: return btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)), nil + case t.KeyXBinaryTildePlus: + return nil, fmt.Errorf("check: cannot apply ~+ operator to ideal numbers") } return nil, fmt.Errorf("check: unrecognized token.Key (0x%02X) for evalConstValueBinaryOp", n.ID0().Key()) } diff --git a/lang/token/list.go b/lang/token/list.go index 67ef928e..de894bfc 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -173,30 +173,32 @@ const ( KeySemicolon = Key(IDSemicolon >> KeyShift) KeyDollar = Key(IDDollar >> KeyShift) - KeyEq = Key(IDEq >> KeyShift) - KeyPlusEq = Key(IDPlusEq >> KeyShift) - KeyMinusEq = Key(IDMinusEq >> KeyShift) - KeyStarEq = Key(IDStarEq >> KeyShift) - KeySlashEq = Key(IDSlashEq >> KeyShift) - KeyShiftLEq = Key(IDShiftLEq >> KeyShift) - KeyShiftREq = Key(IDShiftREq >> KeyShift) - KeyAmpEq = Key(IDAmpEq >> KeyShift) - KeyAmpHatEq = Key(IDAmpHatEq >> KeyShift) - KeyPipeEq = Key(IDPipeEq >> KeyShift) - KeyHatEq = Key(IDHatEq >> KeyShift) - KeyPercentEq = Key(IDPercentEq >> KeyShift) + KeyEq = Key(IDEq >> KeyShift) + KeyPlusEq = Key(IDPlusEq >> KeyShift) + KeyMinusEq = Key(IDMinusEq >> KeyShift) + KeyStarEq = Key(IDStarEq >> KeyShift) + KeySlashEq = Key(IDSlashEq >> KeyShift) + KeyShiftLEq = Key(IDShiftLEq >> KeyShift) + KeyShiftREq = Key(IDShiftREq >> KeyShift) + KeyAmpEq = Key(IDAmpEq >> KeyShift) + KeyAmpHatEq = Key(IDAmpHatEq >> KeyShift) + KeyPipeEq = Key(IDPipeEq >> KeyShift) + KeyHatEq = Key(IDHatEq >> KeyShift) + KeyPercentEq = Key(IDPercentEq >> KeyShift) + KeyTildePlusEq = Key(IDTildePlusEq >> KeyShift) - KeyPlus = Key(IDPlus >> KeyShift) - KeyMinus = Key(IDMinus >> KeyShift) - KeyStar = Key(IDStar >> KeyShift) - KeySlash = Key(IDSlash >> KeyShift) - KeyShiftL = Key(IDShiftL >> KeyShift) - KeyShiftR = Key(IDShiftR >> KeyShift) - KeyAmp = Key(IDAmp >> KeyShift) - KeyAmpHat = Key(IDAmpHat >> KeyShift) - KeyPipe = Key(IDPipe >> KeyShift) - KeyHat = Key(IDHat >> KeyShift) - KeyPercent = Key(IDPercent >> KeyShift) + KeyPlus = Key(IDPlus >> KeyShift) + KeyMinus = Key(IDMinus >> KeyShift) + KeyStar = Key(IDStar >> KeyShift) + KeySlash = Key(IDSlash >> KeyShift) + KeyShiftL = Key(IDShiftL >> KeyShift) + KeyShiftR = Key(IDShiftR >> KeyShift) + KeyAmp = Key(IDAmp >> KeyShift) + KeyAmpHat = Key(IDAmpHat >> KeyShift) + KeyPipe = Key(IDPipe >> KeyShift) + KeyHat = Key(IDHat >> KeyShift) + KeyPercent = Key(IDPercent >> KeyShift) + KeyTildePlus = Key(IDTildePlus >> KeyShift) KeyNotEq = Key(IDNotEq >> KeyShift) KeyLessThan = Key(IDLessThan >> KeyShift) @@ -320,6 +322,7 @@ const ( KeyXBinaryAnd = Key(IDXBinaryAnd >> KeyShift) KeyXBinaryOr = Key(IDXBinaryOr >> KeyShift) KeyXBinaryAs = Key(IDXBinaryAs >> KeyShift) + KeyXBinaryTildePlus = Key(IDXBinaryTildePlus >> KeyShift) KeyXAssociativePlus = Key(IDXAssociativePlus >> KeyShift) KeyXAssociativeStar = Key(IDXAssociativeStar >> KeyShift) @@ -351,30 +354,32 @@ const ( IDSemicolon = ID(0x26<>=", IDShiftREq}, - KeyAmpEq: {"&=", IDAmpEq}, - KeyAmpHatEq: {"&^=", IDAmpHatEq}, - KeyPipeEq: {"|=", IDPipeEq}, - KeyHatEq: {"^=", IDHatEq}, - KeyPercentEq: {"%=", IDPercentEq}, + KeyEq: {"=", IDEq}, + KeyPlusEq: {"+=", IDPlusEq}, + KeyMinusEq: {"-=", IDMinusEq}, + KeyStarEq: {"*=", IDStarEq}, + KeySlashEq: {"/=", IDSlashEq}, + KeyShiftLEq: {"<<=", IDShiftLEq}, + KeyShiftREq: {">>=", IDShiftREq}, + KeyAmpEq: {"&=", IDAmpEq}, + KeyAmpHatEq: {"&^=", IDAmpHatEq}, + KeyPipeEq: {"|=", IDPipeEq}, + KeyHatEq: {"^=", IDHatEq}, + KeyPercentEq: {"%=", IDPercentEq}, + KeyTildePlusEq: {"~+=", IDTildePlusEq}, - KeyPlus: {"+", IDPlus}, - KeyMinus: {"-", IDMinus}, - KeyStar: {"*", IDStar}, - KeySlash: {"/", IDSlash}, - KeyShiftL: {"<<", IDShiftL}, - KeyShiftR: {">>", IDShiftR}, - KeyAmp: {"&", IDAmp}, - KeyAmpHat: {"&^", IDAmpHat}, - KeyPipe: {"|", IDPipe}, - KeyHat: {"^", IDHat}, - KeyPercent: {"%", IDPercent}, - // TODO: tilde-ops such as ~+ for ignoring overflow. Swift defines overflow - // ops for +, - and *. Swift syntax uses a leading &. Puffs uses a leading - // ~ as it is less ambiguous with the &^ operator (and any potential use of - // & as address-of) and ~ is otherwise unused. + KeyPlus: {"+", IDPlus}, + KeyMinus: {"-", IDMinus}, + KeyStar: {"*", IDStar}, + KeySlash: {"/", IDSlash}, + KeyShiftL: {"<<", IDShiftL}, + KeyShiftR: {">>", IDShiftR}, + KeyAmp: {"&", IDAmp}, + KeyAmpHat: {"&^", IDAmpHat}, + KeyPipe: {"|", IDPipe}, + KeyHat: {"^", IDHat}, + KeyPercent: {"%", IDPercent}, + KeyTildePlus: {"~+", IDPercent}, KeyNotEq: {"!=", IDNotEq}, KeyLessThan: {"<", IDLessThan}, @@ -770,6 +774,10 @@ var lexers = [256][]suffixLexer{ {"=", IDGreaterEq}, {"", IDGreaterThan}, }, + '~': { + {"+=", IDTildePlusEq}, + {"+", IDTildePlus}, + }, } var ambiguousForms = [256]ID{ @@ -797,6 +805,7 @@ var ambiguousForms = [256]ID{ KeyXBinaryAnd: IDAnd, KeyXBinaryOr: IDOr, KeyXBinaryAs: IDAs, + KeyXBinaryTildePlus: IDTildePlus, KeyXAssociativePlus: IDPlus, KeyXAssociativeStar: IDStar, @@ -814,17 +823,18 @@ var unaryForms = [256]ID{ } var binaryForms = [256]ID{ - KeyPlusEq: IDXBinaryPlus, - KeyMinusEq: IDXBinaryMinus, - KeyStarEq: IDXBinaryStar, - KeySlashEq: IDXBinarySlash, - KeyShiftLEq: IDXBinaryShiftL, - KeyShiftREq: IDXBinaryShiftR, - KeyAmpEq: IDXBinaryAmp, - KeyAmpHatEq: IDXBinaryAmpHat, - KeyPipeEq: IDXBinaryPipe, - KeyHatEq: IDXBinaryHat, - KeyPercentEq: IDXBinaryPercent, + KeyPlusEq: IDXBinaryPlus, + KeyMinusEq: IDXBinaryMinus, + KeyStarEq: IDXBinaryStar, + KeySlashEq: IDXBinarySlash, + KeyShiftLEq: IDXBinaryShiftL, + KeyShiftREq: IDXBinaryShiftR, + KeyAmpEq: IDXBinaryAmp, + KeyAmpHatEq: IDXBinaryAmpHat, + KeyPipeEq: IDXBinaryPipe, + KeyHatEq: IDXBinaryHat, + KeyPercentEq: IDXBinaryPercent, + KeyTildePlusEq: IDXBinaryTildePlus, KeyPlus: IDXBinaryPlus, KeyMinus: IDXBinaryMinus, @@ -846,6 +856,7 @@ var binaryForms = [256]ID{ KeyAnd: IDXBinaryAnd, KeyOr: IDXBinaryOr, KeyAs: IDXBinaryAs, + KeyTildePlus: IDXBinaryTildePlus, } var associativeForms = [256]ID{ diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 08426e0e..1ea8a8e8 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -390,8 +390,7 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() { } if n_copied > 0 { hlen -= n_copied - // TODO: a "~+" tilde-operator that ignores overflow. - hdist = (hdist + (n_copied & 0x7FFF)) & 0x7FFF + hdist = (hdist ~+ n_copied) & 0x7FFF if hdist == 0 { // Wrap around the this.history ringbuffer. break