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

+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
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 {
+6 -5
View File
@@ -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())
}