Add ~sat+ and ~sat-

This commit is contained in:
Nigel Tao committed 2018-04-26 11:13:19 +10:00
1 parent 6fb1abc739
commit 6d167b7424
7 files changed
+162 -49

No files matched your search

+58 -9
View File
@@ -462,27 +462,67 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
}
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
b.writes(cOpNames[0xFF&n.Operator()])
op := n.Operator()
opName := cOpNames[0xFF&op]
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
}
b.writes(opName)
return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth)
}
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
opName, tilde := "", false
op := n.Operator()
if op == t.IDXBinaryAs {
switch op {
case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
uType := ""
qid := n.MType().QID()
switch qid[1] {
case t.IDU8:
uType = "8"
case t.IDU16:
uType = "16"
case t.IDU32:
uType = "32"
case t.IDU64:
uType = "64"
}
if qid[0] != t.IDBase || uType == "" {
return fmt.Errorf("unsupported tilde-operator type %q", n.MType().Str(g.tm))
}
uOp := "add"
if op != t.IDXBinaryTildeSatPlus {
uOp = "sub"
}
b.printf("wuffs_base__u%s__sat_%s", uType, uOp)
opName, tilde = ",", true
case t.IDXBinaryAs:
return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
default:
opName = cOpNames[0xFF&op]
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
}
}
if pp == parenthesesMandatory {
if pp == parenthesesMandatory || tilde {
b.writeb('(')
}
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: does KeyXBinaryAmpHat need special consideration?
b.writes(cOpNames[0xFF&op])
b.writes(opName)
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
}
if pp == parenthesesMandatory {
if pp == parenthesesMandatory || tilde {
b.writeb(')')
}
return nil
@@ -504,10 +544,15 @@ func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacemen
}
func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
op := n.Operator()
opName := cOpNames[0xFF&op]
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
}
if pp == parenthesesMandatory {
b.writeb('(')
}
opName := cOpNames[0xFF&n.Operator()]
for i, o := range n.Args() {
if i != 0 {
b.writes(opName)
@@ -636,12 +681,14 @@ var cOpNames = [256]string{
t.IDShiftLEq: " <<= ",
t.IDShiftREq: " >>= ",
t.IDAmpEq: " &= ",
t.IDAmpHatEq: " no_such_amp_hat_C_operator ",
t.IDAmpHatEq: " no_such_C_operator ",
t.IDPipeEq: " |= ",
t.IDHatEq: " ^= ",
t.IDPercentEq: " %= ",
t.IDTildeModPlusEq: " += ",
t.IDTildeModMinusEq: " -= ",
t.IDTildeSatPlusEq: " no_such_C_operator ",
t.IDTildeSatMinusEq: " no_such_C_operator ",
t.IDXUnaryPlus: " + ",
t.IDXUnaryMinus: " - ",
@@ -656,12 +703,14 @@ var cOpNames = [256]string{
t.IDXBinaryShiftL: " << ",
t.IDXBinaryShiftR: " >> ",
t.IDXBinaryAmp: " & ",
t.IDXBinaryAmpHat: " no_such_amp_hat_C_operator ",
t.IDXBinaryAmpHat: " no_such_C_operator ",
t.IDXBinaryPipe: " | ",
t.IDXBinaryHat: " ^ ",
t.IDXBinaryPercent: " % ",
t.IDXBinaryTildeModPlus: " + ",
t.IDXBinaryTildeModMinus: " - ",
t.IDXBinaryTildeSatPlus: " no_such_C_operator ",
t.IDXBinaryTildeSatMinus: " no_such_C_operator ",
t.IDXBinaryNotEq: " != ",
t.IDXBinaryLessThan: " < ",
t.IDXBinaryLessEq: " <= ",
@@ -670,7 +719,7 @@ var cOpNames = [256]string{
t.IDXBinaryGreaterThan: " > ",
t.IDXBinaryAnd: " && ",
t.IDXBinaryOr: " || ",
t.IDXBinaryAs: " no_such_as_C_operator ",
t.IDXBinaryAs: " no_such_C_operator ",
t.IDXAssociativePlus: " + ",
t.IDXAssociativeStar: " * ",
+7 -2
View File
@@ -81,8 +81,13 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
if err := g.writeExpr(b, n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
// TODO: does KeyAmpHatEq need special consideration?
b.writes(cOpNames[0xFF&n.Operator()])
// TODO: `~sat+=`, `~sat-=` and `&^=` need special consideration.
op := n.Operator()
opName := cOpNames[0xFF&op]
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
}
b.writes(opName)
if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
+1 -1
View File
@@ -11,7 +11,7 @@
- Spun `std/zlib` out of `std/flate`.
- Let the `std/zlib` decoder ignore checksums.
- Renamed `std/flate` to `std/deflate`.
- Renamed `~+` to `~mod+`; add `~mod-`.
- Renamed `~+` to `~mod+`; added `~mod-`, `~sat+` and `~sat-`.
- Renamed `[N] T` and `[] T` types to `array[N] T` and `slice T`.
- Renamed `iterate.N` to `iterate[N]`.
- Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc.
+2
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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,
}