Add a ~mod<< operator

This commit is contained in:
Nigel Tao committed 2018-06-11 10:27:35 +10:00
1 parent 9260f66dd3
commit bc702b4c2b
4 files changed
+196 -175

No files matched your search

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