diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index c68db9ad..6b650cd6 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -455,21 +455,22 @@ func cOpName(x t.ID) string { } var cOpNames = [...]string{ - t.IDEq: " = ", - t.IDPlusEq: " += ", - t.IDMinusEq: " -= ", - t.IDStarEq: " *= ", - t.IDSlashEq: " /= ", - t.IDShiftLEq: " <<= ", - t.IDShiftREq: " >>= ", - t.IDAmpEq: " &= ", - t.IDPipeEq: " |= ", - t.IDHatEq: " ^= ", - t.IDPercentEq: " %= ", - t.IDTildeModPlusEq: " += ", - t.IDTildeModMinusEq: " -= ", - t.IDTildeSatPlusEq: noSuchCOperator, - t.IDTildeSatMinusEq: noSuchCOperator, + t.IDEq: " = ", + t.IDPlusEq: " += ", + t.IDMinusEq: " -= ", + t.IDStarEq: " *= ", + t.IDSlashEq: " /= ", + t.IDShiftLEq: " <<= ", + t.IDShiftREq: " >>= ", + t.IDAmpEq: " &= ", + t.IDPipeEq: " |= ", + t.IDHatEq: " ^= ", + t.IDPercentEq: " %= ", + t.IDTildeModShiftLEq: " <<= ", + t.IDTildeModPlusEq: " += ", + t.IDTildeModMinusEq: " -= ", + t.IDTildeSatPlusEq: noSuchCOperator, + t.IDTildeSatMinusEq: noSuchCOperator, t.IDXUnaryPlus: " + ", t.IDXUnaryMinus: " - ", @@ -477,29 +478,30 @@ var cOpNames = [...]string{ t.IDXUnaryRef: " & ", t.IDXUnaryDeref: " * ", - t.IDXBinaryPlus: " + ", - t.IDXBinaryMinus: " - ", - t.IDXBinaryStar: " * ", - t.IDXBinarySlash: " / ", - t.IDXBinaryShiftL: " << ", - t.IDXBinaryShiftR: " >> ", - t.IDXBinaryAmp: " & ", - t.IDXBinaryPipe: " | ", - t.IDXBinaryHat: " ^ ", - t.IDXBinaryPercent: " % ", - t.IDXBinaryTildeModPlus: " + ", - t.IDXBinaryTildeModMinus: " - ", - t.IDXBinaryTildeSatPlus: noSuchCOperator, - t.IDXBinaryTildeSatMinus: noSuchCOperator, - t.IDXBinaryNotEq: " != ", - t.IDXBinaryLessThan: " < ", - t.IDXBinaryLessEq: " <= ", - t.IDXBinaryEqEq: " == ", - t.IDXBinaryGreaterEq: " >= ", - t.IDXBinaryGreaterThan: " > ", - t.IDXBinaryAnd: " && ", - t.IDXBinaryOr: " || ", - t.IDXBinaryAs: noSuchCOperator, + t.IDXBinaryPlus: " + ", + t.IDXBinaryMinus: " - ", + t.IDXBinaryStar: " * ", + t.IDXBinarySlash: " / ", + t.IDXBinaryShiftL: " << ", + t.IDXBinaryShiftR: " >> ", + t.IDXBinaryAmp: " & ", + t.IDXBinaryPipe: " | ", + t.IDXBinaryHat: " ^ ", + t.IDXBinaryPercent: " % ", + t.IDXBinaryTildeModShiftL: " << ", + t.IDXBinaryTildeModPlus: " + ", + t.IDXBinaryTildeModMinus: " - ", + t.IDXBinaryTildeSatPlus: noSuchCOperator, + t.IDXBinaryTildeSatMinus: noSuchCOperator, + t.IDXBinaryNotEq: " != ", + t.IDXBinaryLessThan: " < ", + t.IDXBinaryLessEq: " <= ", + t.IDXBinaryEqEq: " == ", + t.IDXBinaryGreaterEq: " >= ", + t.IDXBinaryGreaterThan: " > ", + t.IDXBinaryAnd: " && ", + t.IDXBinaryOr: " || ", + t.IDXBinaryAs: noSuchCOperator, t.IDXAssociativePlus: " + ", t.IDXAssociativeStar: " * ", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 5edac35e..07c7a0db 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1078,7 +1078,7 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax } return zero, big.NewInt(0).Sub(rMax, one), nil - case t.IDXBinaryShiftL: + case t.IDXBinaryShiftL, t.IDXBinaryTildeModShiftL: if lMin.Sign() < 0 { return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm)) } @@ -1088,7 +1088,15 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax if rMax.Cmp(ffff) > 0 { return nil, nil, fmt.Errorf("check: shift %q out of range", rhs.Str(q.tm)) } - return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil + nMin := big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())) + nMax := big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())) + if op == t.IDXBinaryTildeModShiftL { + if qid := lhs.MType().QID(); qid[0] == t.IDBase { + b := numTypeBounds[qid[1]] + nMax = min(nMax, b[1]) + } + } + return nMin, nMax, nil case t.IDXBinaryShiftR: if lMin.Sign() < 0 { diff --git a/lang/check/type.go b/lang/check/type.go index e019d6d2..ca1fc832 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -298,7 +298,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error { } switch n.Operator() { - case t.IDShiftLEq, t.IDShiftREq: + case t.IDShiftLEq, t.IDShiftREq, t.IDTildeModShiftLEq: if !rTyp.IsNumTypeOrIdeal() { return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type", n.Operator().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) @@ -768,7 +768,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { lTyp.Str(q.tm), rTyp.Str(q.tm), ) } - case t.IDXBinaryShiftL, t.IDXBinaryShiftR: + case t.IDXBinaryShiftL, t.IDXBinaryShiftR, t.IDXBinaryTildeModShiftL: if lTyp.IsIdeal() && !rTyp.IsIdeal() { return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+ "cannot shift an ideal number by a non-ideal number", @@ -859,9 +859,6 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm)) } return big.NewInt(0).Mod(l, r), nil - case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus, - t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: - return nil, fmt.Errorf("check: cannot apply tilde-operators to ideal numbers") case t.IDXBinaryNotEq: return btoi(l.Cmp(r) != 0), nil case t.IDXBinaryLessThan: @@ -878,6 +875,10 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big. return btoi((l.Sign() != 0) && (r.Sign() != 0)), nil case t.IDXBinaryOr: return btoi((l.Sign() != 0) || (r.Sign() != 0)), nil + case t.IDXBinaryTildeModShiftL, + t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus, + t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: + return nil, fmt.Errorf("check: cannot apply tilde-operators to ideal numbers") } return nil, fmt.Errorf("check: unrecognized token (0x%02X) for evalConstValueBinaryOp", n.Operator()) } diff --git a/lang/token/list.go b/lang/token/list.go index 5f90e3e7..798c3eca 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -223,20 +223,21 @@ const ( minAssign = 0x10 maxAssign = 0x2F - IDPlusEq = ID(0x10) - IDMinusEq = ID(0x11) - IDStarEq = ID(0x12) - IDSlashEq = ID(0x13) - IDShiftLEq = ID(0x14) - IDShiftREq = ID(0x15) - IDAmpEq = ID(0x16) - IDPipeEq = ID(0x17) - IDHatEq = ID(0x18) - IDPercentEq = ID(0x19) - IDTildeModPlusEq = ID(0x1B) - IDTildeModMinusEq = ID(0x1C) - IDTildeSatPlusEq = ID(0x1D) - IDTildeSatMinusEq = ID(0x1E) + IDPlusEq = ID(0x10) + IDMinusEq = ID(0x11) + IDStarEq = ID(0x12) + IDSlashEq = ID(0x13) + IDShiftLEq = ID(0x14) + IDShiftREq = ID(0x15) + IDAmpEq = ID(0x16) + IDPipeEq = ID(0x17) + IDHatEq = ID(0x18) + IDPercentEq = ID(0x19) + IDTildeModShiftLEq = ID(0x1A) + IDTildeModPlusEq = ID(0x1B) + IDTildeModMinusEq = ID(0x1C) + IDTildeSatPlusEq = ID(0x1D) + IDTildeSatMinusEq = ID(0x1E) IDEq = ID(0x2E) IDEqColon = ID(0x2F) @@ -250,20 +251,21 @@ const ( maxXOp = 0x7F maxOp = 0x7F - IDPlus = ID(0x30) - IDMinus = ID(0x31) - IDStar = ID(0x32) - IDSlash = ID(0x33) - IDShiftL = ID(0x34) - IDShiftR = ID(0x35) - IDAmp = ID(0x36) - IDPipe = ID(0x37) - IDHat = ID(0x38) - IDPercent = ID(0x39) - IDTildeModPlus = ID(0x3B) - IDTildeModMinus = ID(0x3C) - IDTildeSatPlus = ID(0x3D) - IDTildeSatMinus = ID(0x3E) + IDPlus = ID(0x30) + IDMinus = ID(0x31) + IDStar = ID(0x32) + IDSlash = ID(0x33) + IDShiftL = ID(0x34) + IDShiftR = ID(0x35) + IDAmp = ID(0x36) + IDPipe = ID(0x37) + IDHat = ID(0x38) + IDPercent = ID(0x39) + IDTildeModShiftL = ID(0x3A) + IDTildeModPlus = ID(0x3B) + IDTildeModMinus = ID(0x3C) + IDTildeSatPlus = ID(0x3D) + IDTildeSatMinus = ID(0x3E) IDNotEq = ID(0x40) IDLessThan = ID(0x41) @@ -290,29 +292,30 @@ const ( IDXUnaryRef = ID(0x53) IDXUnaryDeref = ID(0x54) - IDXBinaryPlus = ID(0x58) - IDXBinaryMinus = ID(0x59) - IDXBinaryStar = ID(0x5A) - IDXBinarySlash = ID(0x5B) - IDXBinaryShiftL = ID(0x5C) - IDXBinaryShiftR = ID(0x5D) - IDXBinaryAmp = ID(0x5E) - IDXBinaryPipe = ID(0x5F) - IDXBinaryHat = ID(0x60) - IDXBinaryPercent = ID(0x61) - IDXBinaryTildeModPlus = ID(0x63) - IDXBinaryTildeModMinus = ID(0x64) - IDXBinaryTildeSatPlus = ID(0x65) - IDXBinaryTildeSatMinus = ID(0x66) - IDXBinaryNotEq = ID(0x67) - IDXBinaryLessThan = ID(0x68) - IDXBinaryLessEq = ID(0x69) - IDXBinaryEqEq = ID(0x6A) - IDXBinaryGreaterEq = ID(0x6B) - IDXBinaryGreaterThan = ID(0x6C) - IDXBinaryAnd = ID(0x6D) - IDXBinaryOr = ID(0x6E) - IDXBinaryAs = ID(0x6F) + IDXBinaryPlus = ID(0x58) + IDXBinaryMinus = ID(0x59) + IDXBinaryStar = ID(0x5A) + IDXBinarySlash = ID(0x5B) + IDXBinaryShiftL = ID(0x5C) + IDXBinaryShiftR = ID(0x5D) + IDXBinaryAmp = ID(0x5E) + IDXBinaryPipe = ID(0x5F) + IDXBinaryHat = ID(0x60) + IDXBinaryPercent = ID(0x61) + IDXBinaryTildeModShiftL = ID(0x62) + IDXBinaryTildeModPlus = ID(0x63) + IDXBinaryTildeModMinus = ID(0x64) + IDXBinaryTildeSatPlus = ID(0x65) + IDXBinaryTildeSatMinus = ID(0x66) + IDXBinaryNotEq = ID(0x67) + IDXBinaryLessThan = ID(0x68) + IDXBinaryLessEq = ID(0x69) + IDXBinaryEqEq = ID(0x6A) + IDXBinaryGreaterEq = ID(0x6B) + IDXBinaryGreaterThan = ID(0x6C) + IDXBinaryAnd = ID(0x6D) + IDXBinaryOr = ID(0x6E) + IDXBinaryAs = ID(0x6F) IDXAssociativePlus = ID(0x70) IDXAssociativeStar = ID(0x71) @@ -530,38 +533,40 @@ var builtInsByID = [nBuiltInIDs]string{ IDSemicolon: ";", IDDollar: "$", - IDPlusEq: "+=", - IDMinusEq: "-=", - IDStarEq: "*=", - IDSlashEq: "/=", - IDShiftLEq: "<<=", - IDShiftREq: ">>=", - IDAmpEq: "&=", - IDPipeEq: "|=", - IDHatEq: "^=", - IDPercentEq: "%=", - IDTildeModPlusEq: "~mod+=", - IDTildeModMinusEq: "~mod-=", - IDTildeSatPlusEq: "~sat+=", - IDTildeSatMinusEq: "~sat-=", + IDPlusEq: "+=", + IDMinusEq: "-=", + IDStarEq: "*=", + IDSlashEq: "/=", + IDShiftLEq: "<<=", + IDShiftREq: ">>=", + IDAmpEq: "&=", + IDPipeEq: "|=", + IDHatEq: "^=", + IDPercentEq: "%=", + IDTildeModShiftLEq: "~mod<<=", + IDTildeModPlusEq: "~mod+=", + IDTildeModMinusEq: "~mod-=", + IDTildeSatPlusEq: "~sat+=", + IDTildeSatMinusEq: "~sat-=", IDEq: "=", IDEqColon: "=:", - IDPlus: "+", - IDMinus: "-", - IDStar: "*", - IDSlash: "/", - IDShiftL: "<<", - IDShiftR: ">>", - IDAmp: "&", - IDPipe: "|", - IDHat: "^", - IDPercent: "%", - IDTildeModPlus: "~mod+", - IDTildeModMinus: "~mod-", - IDTildeSatPlus: "~sat+", - IDTildeSatMinus: "~sat-", + IDPlus: "+", + IDMinus: "-", + IDStar: "*", + IDSlash: "/", + IDShiftL: "<<", + IDShiftR: ">>", + IDAmp: "&", + IDPipe: "|", + IDHat: "^", + IDPercent: "%", + IDTildeModShiftL: "~mod<<", + IDTildeModPlus: "~mod+", + IDTildeModMinus: "~mod-", + IDTildeSatPlus: "~sat+", + IDTildeSatMinus: "~sat-", IDNotEq: "!=", IDLessThan: "<", @@ -845,6 +850,8 @@ var lexers = [256][]suffixLexer{ {"", IDGreaterThan}, }, '~': { + {"mod<<=", IDTildeModShiftLEq}, + {"mod<<", IDTildeModShiftL}, {"mod+=", IDTildeModPlusEq}, {"mod+", IDTildeModPlus}, {"mod-=", IDTildeModMinusEq}, @@ -863,29 +870,30 @@ var ambiguousForms = [nBuiltInSymbolicIDs]ID{ IDXUnaryRef: IDRef, IDXUnaryDeref: IDDeref, - IDXBinaryPlus: IDPlus, - IDXBinaryMinus: IDMinus, - IDXBinaryStar: IDStar, - IDXBinarySlash: IDSlash, - IDXBinaryShiftL: IDShiftL, - IDXBinaryShiftR: IDShiftR, - IDXBinaryAmp: IDAmp, - IDXBinaryPipe: IDPipe, - IDXBinaryHat: IDHat, - IDXBinaryPercent: IDPercent, - IDXBinaryTildeModPlus: IDTildeModPlus, - IDXBinaryTildeModMinus: IDTildeModMinus, - IDXBinaryTildeSatPlus: IDTildeSatPlus, - IDXBinaryTildeSatMinus: IDTildeSatMinus, - IDXBinaryNotEq: IDNotEq, - IDXBinaryLessThan: IDLessThan, - IDXBinaryLessEq: IDLessEq, - IDXBinaryEqEq: IDEqEq, - IDXBinaryGreaterEq: IDGreaterEq, - IDXBinaryGreaterThan: IDGreaterThan, - IDXBinaryAnd: IDAnd, - IDXBinaryOr: IDOr, - IDXBinaryAs: IDAs, + IDXBinaryPlus: IDPlus, + IDXBinaryMinus: IDMinus, + IDXBinaryStar: IDStar, + IDXBinarySlash: IDSlash, + IDXBinaryShiftL: IDShiftL, + IDXBinaryShiftR: IDShiftR, + IDXBinaryAmp: IDAmp, + IDXBinaryPipe: IDPipe, + IDXBinaryHat: IDHat, + IDXBinaryPercent: IDPercent, + IDXBinaryTildeModShiftL: IDTildeModShiftL, + IDXBinaryTildeModPlus: IDTildeModPlus, + IDXBinaryTildeModMinus: IDTildeModMinus, + IDXBinaryTildeSatPlus: IDTildeSatPlus, + IDXBinaryTildeSatMinus: IDTildeSatMinus, + IDXBinaryNotEq: IDNotEq, + IDXBinaryLessThan: IDLessThan, + IDXBinaryLessEq: IDLessEq, + IDXBinaryEqEq: IDEqEq, + IDXBinaryGreaterEq: IDGreaterEq, + IDXBinaryGreaterThan: IDGreaterThan, + IDXBinaryAnd: IDAnd, + IDXBinaryOr: IDOr, + IDXBinaryAs: IDAs, IDXAssociativePlus: IDPlus, IDXAssociativeStar: IDStar, @@ -931,35 +939,37 @@ var unaryForms = [nBuiltInSymbolicIDs]ID{ } var binaryForms = [nBuiltInSymbolicIDs]ID{ - IDPlusEq: IDXBinaryPlus, - IDMinusEq: IDXBinaryMinus, - IDStarEq: IDXBinaryStar, - IDSlashEq: IDXBinarySlash, - IDShiftLEq: IDXBinaryShiftL, - IDShiftREq: IDXBinaryShiftR, - IDAmpEq: IDXBinaryAmp, - IDPipeEq: IDXBinaryPipe, - IDHatEq: IDXBinaryHat, - IDPercentEq: IDXBinaryPercent, - IDTildeModPlusEq: IDXBinaryTildeModPlus, - IDTildeModMinusEq: IDXBinaryTildeModMinus, - IDTildeSatPlusEq: IDXBinaryTildeSatPlus, - IDTildeSatMinusEq: IDXBinaryTildeSatMinus, + IDPlusEq: IDXBinaryPlus, + IDMinusEq: IDXBinaryMinus, + IDStarEq: IDXBinaryStar, + IDSlashEq: IDXBinarySlash, + IDShiftLEq: IDXBinaryShiftL, + IDShiftREq: IDXBinaryShiftR, + IDAmpEq: IDXBinaryAmp, + IDPipeEq: IDXBinaryPipe, + IDHatEq: IDXBinaryHat, + IDPercentEq: IDXBinaryPercent, + IDTildeModShiftLEq: IDXBinaryTildeModShiftL, + IDTildeModPlusEq: IDXBinaryTildeModPlus, + IDTildeModMinusEq: IDXBinaryTildeModMinus, + IDTildeSatPlusEq: IDXBinaryTildeSatPlus, + IDTildeSatMinusEq: IDXBinaryTildeSatMinus, - IDPlus: IDXBinaryPlus, - IDMinus: IDXBinaryMinus, - IDStar: IDXBinaryStar, - IDSlash: IDXBinarySlash, - IDShiftL: IDXBinaryShiftL, - IDShiftR: IDXBinaryShiftR, - IDAmp: IDXBinaryAmp, - IDPipe: IDXBinaryPipe, - IDHat: IDXBinaryHat, - IDPercent: IDXBinaryPercent, - IDTildeModPlus: IDXBinaryTildeModPlus, - IDTildeModMinus: IDXBinaryTildeModMinus, - IDTildeSatPlus: IDXBinaryTildeSatPlus, - IDTildeSatMinus: IDXBinaryTildeSatMinus, + IDPlus: IDXBinaryPlus, + IDMinus: IDXBinaryMinus, + IDStar: IDXBinaryStar, + IDSlash: IDXBinarySlash, + IDShiftL: IDXBinaryShiftL, + IDShiftR: IDXBinaryShiftR, + IDAmp: IDXBinaryAmp, + IDPipe: IDXBinaryPipe, + IDHat: IDXBinaryHat, + IDPercent: IDXBinaryPercent, + IDTildeModShiftL: IDXBinaryTildeModShiftL, + IDTildeModPlus: IDXBinaryTildeModPlus, + IDTildeModMinus: IDXBinaryTildeModMinus, + IDTildeSatPlus: IDXBinaryTildeSatPlus, + IDTildeSatMinus: IDXBinaryTildeSatMinus, IDNotEq: IDXBinaryNotEq, IDLessThan: IDXBinaryLessThan,