mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 16:47:28 +01:00
Add ~sat+ and ~sat-
This commit is contained in:
1 parent
6fb1abc739
commit
6d167b7424
7 files changed
+162
-49
No files matched your search
@@ -171,6 +171,8 @@ var opStrings = [256]string{
|
||||
t.IDXBinaryHat: " ^ ",
|
||||
t.IDXBinaryTildeModPlus: " ~mod+ ",
|
||||
t.IDXBinaryTildeModMinus: " ~mod- ",
|
||||
t.IDXBinaryTildeSatPlus: " ~sat+ ",
|
||||
t.IDXBinaryTildeSatMinus: " ~sat- ",
|
||||
t.IDXBinaryNotEq: " != ",
|
||||
t.IDXBinaryLessThan: " < ",
|
||||
t.IDXBinaryLessEq: " <= ",
|
||||
|
||||
+55
-20
@@ -1003,6 +1003,32 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token (0x%X) for bcheckExprUnaryOp", n.Operator())
|
||||
}
|
||||
|
||||
func (q *checker) bcheckExprXBinaryPlus(lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, rMin *big.Int, rMax *big.Int) (*big.Int, *big.Int, error) {
|
||||
return big.NewInt(0).Add(lMin, rMin), big.NewInt(0).Add(lMax, rMax), nil
|
||||
}
|
||||
|
||||
func (q *checker) bcheckExprXBinaryMinus(lhs *a.Expr, lMin *big.Int, lMax *big.Int, rhs *a.Expr, rMin *big.Int, rMax *big.Int) (*big.Int, *big.Int, error) {
|
||||
nMin := big.NewInt(0).Sub(lMin, rMax)
|
||||
nMax := big.NewInt(0).Sub(lMax, rMin)
|
||||
for _, x := range q.facts {
|
||||
xOp, xLHS, xRHS := parseBinaryOp(x)
|
||||
if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) {
|
||||
continue
|
||||
}
|
||||
switch xOp {
|
||||
case t.IDXBinaryLessThan:
|
||||
nMax = min(nMax, minusOne)
|
||||
case t.IDXBinaryLessEq:
|
||||
nMax = min(nMax, zero)
|
||||
case t.IDXBinaryGreaterEq:
|
||||
nMin = max(nMin, zero)
|
||||
case t.IDXBinaryGreaterThan:
|
||||
nMin = max(nMin, one)
|
||||
}
|
||||
}
|
||||
return nMin, nMax, nil
|
||||
}
|
||||
|
||||
func (q *checker) bcheckExprBinaryOp(op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
|
||||
lMin, lMax, err := q.bcheckExpr(lhs, depth)
|
||||
if err != nil {
|
||||
@@ -1019,28 +1045,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax
|
||||
|
||||
switch op {
|
||||
case t.IDXBinaryPlus:
|
||||
return big.NewInt(0).Add(lMin, rMin), big.NewInt(0).Add(lMax, rMax), nil
|
||||
return q.bcheckExprXBinaryPlus(lhs, lMin, lMax, rhs, rMin, rMax)
|
||||
|
||||
case t.IDXBinaryMinus:
|
||||
nMin := big.NewInt(0).Sub(lMin, rMax)
|
||||
nMax := big.NewInt(0).Sub(lMax, rMin)
|
||||
for _, x := range q.facts {
|
||||
xOp, xLHS, xRHS := parseBinaryOp(x)
|
||||
if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) {
|
||||
continue
|
||||
}
|
||||
switch xOp {
|
||||
case t.IDXBinaryLessThan:
|
||||
nMax = min(nMax, minusOne)
|
||||
case t.IDXBinaryLessEq:
|
||||
nMax = min(nMax, zero)
|
||||
case t.IDXBinaryGreaterEq:
|
||||
nMin = max(nMin, zero)
|
||||
case t.IDXBinaryGreaterThan:
|
||||
nMin = max(nMin, one)
|
||||
}
|
||||
}
|
||||
return nMin, nMax, nil
|
||||
return q.bcheckExprXBinaryMinus(lhs, lMin, lMax, rhs, rMin, rMax)
|
||||
|
||||
case t.IDXBinaryStar:
|
||||
// TODO: handle multiplication by negative numbers. Note that this
|
||||
@@ -1132,6 +1140,33 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lMin *big.Int, lMax
|
||||
return b[0], b[1], nil
|
||||
}
|
||||
|
||||
case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
|
||||
typ := lhs.MType()
|
||||
if typ.IsIdeal() {
|
||||
typ = rhs.MType()
|
||||
}
|
||||
if qid := typ.QID(); qid[0] == t.IDBase {
|
||||
b := numTypeBounds[qid[1]]
|
||||
|
||||
nFunc := (*checker).bcheckExprXBinaryPlus
|
||||
if op != t.IDXBinaryTildeSatPlus {
|
||||
nFunc = (*checker).bcheckExprXBinaryMinus
|
||||
}
|
||||
nMin, nMax, err := nFunc(q, lhs, lMin, lMax, rhs, rMin, rMax)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if op == t.IDXBinaryTildeSatPlus {
|
||||
nMin = min(nMin, b[1])
|
||||
nMax = min(nMax, b[1])
|
||||
} else {
|
||||
nMin = max(nMin, b[0])
|
||||
nMax = max(nMax, b[0])
|
||||
}
|
||||
return nMin, nMax, nil
|
||||
}
|
||||
|
||||
case t.IDXBinaryNotEq, t.IDXBinaryLessThan, t.IDXBinaryLessEq, t.IDXBinaryEqEq,
|
||||
t.IDXBinaryGreaterEq, t.IDXBinaryGreaterThan, t.IDXBinaryAnd, t.IDXBinaryOr:
|
||||
return zero, one, nil
|
||||
|
||||
+5
-3
@@ -282,7 +282,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
|
||||
n.Operator().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
|
||||
}
|
||||
return nil
|
||||
case t.IDTildeModPlusEq, t.IDTildeModMinusEq:
|
||||
case t.IDTildeModPlusEq, t.IDTildeModMinusEq, t.IDTildeSatPlusEq, t.IDTildeSatMinusEq:
|
||||
if !lTyp.IsUnsignedInteger() {
|
||||
return fmt.Errorf("check: assignment %q: %q, of type %q, does not have unsigned integer type",
|
||||
n.Operator().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
|
||||
@@ -810,7 +810,8 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
|
||||
switch op {
|
||||
case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus:
|
||||
case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus,
|
||||
t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
|
||||
typ := lTyp
|
||||
if typ.IsIdeal() {
|
||||
typ = rTyp
|
||||
@@ -890,7 +891,8 @@ 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:
|
||||
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
|
||||
|
||||
+34
-14
@@ -209,6 +209,8 @@ const (
|
||||
IDPercentEq = ID(0x1B)
|
||||
IDTildeModPlusEq = ID(0x1C)
|
||||
IDTildeModMinusEq = ID(0x1D)
|
||||
IDTildeSatPlusEq = ID(0x1E)
|
||||
IDTildeSatMinusEq = ID(0x1F)
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -232,6 +234,8 @@ const (
|
||||
IDPercent = ID(0x2B)
|
||||
IDTildeModPlus = ID(0x2C)
|
||||
IDTildeModMinus = ID(0x2D)
|
||||
IDTildeSatPlus = ID(0x2E)
|
||||
IDTildeSatMinus = ID(0x2F)
|
||||
|
||||
IDNotEq = ID(0x30)
|
||||
IDLessThan = ID(0x31)
|
||||
@@ -268,17 +272,19 @@ const (
|
||||
IDXBinaryPipe = ID(0x50)
|
||||
IDXBinaryHat = ID(0x51)
|
||||
IDXBinaryPercent = ID(0x52)
|
||||
IDXBinaryNotEq = ID(0x53)
|
||||
IDXBinaryLessThan = ID(0x54)
|
||||
IDXBinaryLessEq = ID(0x55)
|
||||
IDXBinaryEqEq = ID(0x56)
|
||||
IDXBinaryGreaterEq = ID(0x57)
|
||||
IDXBinaryGreaterThan = ID(0x58)
|
||||
IDXBinaryAnd = ID(0x59)
|
||||
IDXBinaryOr = ID(0x5A)
|
||||
IDXBinaryAs = ID(0x5B)
|
||||
IDXBinaryTildeModPlus = ID(0x5C)
|
||||
IDXBinaryTildeModMinus = ID(0x5D)
|
||||
IDXBinaryTildeModPlus = ID(0x53)
|
||||
IDXBinaryTildeModMinus = ID(0x54)
|
||||
IDXBinaryTildeSatPlus = ID(0x55)
|
||||
IDXBinaryTildeSatMinus = ID(0x56)
|
||||
IDXBinaryNotEq = ID(0x57)
|
||||
IDXBinaryLessThan = ID(0x58)
|
||||
IDXBinaryLessEq = ID(0x59)
|
||||
IDXBinaryEqEq = ID(0x5A)
|
||||
IDXBinaryGreaterEq = ID(0x5B)
|
||||
IDXBinaryGreaterThan = ID(0x5C)
|
||||
IDXBinaryAnd = ID(0x5D)
|
||||
IDXBinaryOr = ID(0x5E)
|
||||
IDXBinaryAs = ID(0x5F)
|
||||
|
||||
IDXAssociativePlus = ID(0x60)
|
||||
IDXAssociativeStar = ID(0x61)
|
||||
@@ -444,6 +450,8 @@ var builtInsByID = [nBuiltInIDs]string{
|
||||
IDPercentEq: "%=",
|
||||
IDTildeModPlusEq: "~mod+=",
|
||||
IDTildeModMinusEq: "~mod-=",
|
||||
IDTildeSatPlusEq: "~sat+=",
|
||||
IDTildeSatMinusEq: "~sat-=",
|
||||
|
||||
IDPlus: "+",
|
||||
IDMinus: "-",
|
||||
@@ -458,6 +466,8 @@ var builtInsByID = [nBuiltInIDs]string{
|
||||
IDPercent: "%",
|
||||
IDTildeModPlus: "~mod+",
|
||||
IDTildeModMinus: "~mod-",
|
||||
IDTildeSatPlus: "~sat+",
|
||||
IDTildeSatMinus: "~sat-",
|
||||
|
||||
IDNotEq: "!=",
|
||||
IDLessThan: "<",
|
||||
@@ -684,6 +694,10 @@ var lexers = [nBuiltInIDs][]suffixLexer{
|
||||
{"mod+", IDTildeModPlus},
|
||||
{"mod-=", IDTildeModMinusEq},
|
||||
{"mod-", IDTildeModMinus},
|
||||
{"sat+=", IDTildeSatPlusEq},
|
||||
{"sat+", IDTildeSatPlus},
|
||||
{"sat-=", IDTildeSatMinusEq},
|
||||
{"sat-", IDTildeSatMinus},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -705,6 +719,10 @@ var ambiguousForms = [nBuiltInIDs]ID{
|
||||
IDXBinaryPipe: IDPipe,
|
||||
IDXBinaryHat: IDHat,
|
||||
IDXBinaryPercent: IDPercent,
|
||||
IDXBinaryTildeModPlus: IDTildeModPlus,
|
||||
IDXBinaryTildeModMinus: IDTildeModMinus,
|
||||
IDXBinaryTildeSatPlus: IDTildeSatPlus,
|
||||
IDXBinaryTildeSatMinus: IDTildeSatMinus,
|
||||
IDXBinaryNotEq: IDNotEq,
|
||||
IDXBinaryLessThan: IDLessThan,
|
||||
IDXBinaryLessEq: IDLessEq,
|
||||
@@ -714,8 +732,6 @@ var ambiguousForms = [nBuiltInIDs]ID{
|
||||
IDXBinaryAnd: IDAnd,
|
||||
IDXBinaryOr: IDOr,
|
||||
IDXBinaryAs: IDAs,
|
||||
IDXBinaryTildeModPlus: IDTildeModPlus,
|
||||
IDXBinaryTildeModMinus: IDTildeModMinus,
|
||||
|
||||
IDXAssociativePlus: IDPlus,
|
||||
IDXAssociativeStar: IDStar,
|
||||
@@ -774,6 +790,8 @@ var binaryForms = [nBuiltInIDs]ID{
|
||||
IDPercentEq: IDXBinaryPercent,
|
||||
IDTildeModPlusEq: IDXBinaryTildeModPlus,
|
||||
IDTildeModMinusEq: IDXBinaryTildeModMinus,
|
||||
IDTildeSatPlusEq: IDXBinaryTildeSatPlus,
|
||||
IDTildeSatMinusEq: IDXBinaryTildeSatMinus,
|
||||
|
||||
IDPlus: IDXBinaryPlus,
|
||||
IDMinus: IDXBinaryMinus,
|
||||
@@ -788,6 +806,8 @@ var binaryForms = [nBuiltInIDs]ID{
|
||||
IDPercent: IDXBinaryPercent,
|
||||
IDTildeModPlus: IDXBinaryTildeModPlus,
|
||||
IDTildeModMinus: IDXBinaryTildeModMinus,
|
||||
IDTildeSatPlus: IDXBinaryTildeSatPlus,
|
||||
IDTildeSatMinus: IDXBinaryTildeSatMinus,
|
||||
|
||||
IDNotEq: IDXBinaryNotEq,
|
||||
IDLessThan: IDXBinaryLessThan,
|
||||
@@ -806,7 +826,7 @@ var associativeForms = [nBuiltInIDs]ID{
|
||||
IDAmp: IDXAssociativeAmp,
|
||||
IDPipe: IDXAssociativePipe,
|
||||
IDHat: IDXAssociativeHat,
|
||||
// TODO: IDTildeModPlus, IDTildeModMinus?
|
||||
// TODO: IDTildeModPlus, IDTildeSatPlus?
|
||||
IDAnd: IDXAssociativeAnd,
|
||||
IDOr: IDXAssociativeOr,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user