Update facts after "x -= y".

This commit is contained in:
Nigel Tao committed 2017-06-02 12:21:28 +10:00
1 parent 80a39bfccb
commit ee27dffa9e
4 files changed
+26 -9

No files matched your search

+23
View File
@@ -112,6 +112,29 @@ func refine(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
return nil, nil
}
// simplify returns a simplified form of n. For example, (x - x) becomes 0.
func simplify(n *a.Expr) *a.Expr {
// TODO: be rigorous about this, not ad hoc.
switch op, lhs, rhs := parseBinaryOp(n); op.Key() {
case t.KeyXBinaryPlus:
// TODO: constant folding, so ((x + 1) + 1) becomes (x + 2).
case t.KeyXBinaryMinus:
if lhs.Eq(rhs) {
return zeroExpr
}
if lOp, lLHS, lRHS := parseBinaryOp(lhs); lOp.Key() == t.KeyXBinaryPlus {
if lLHS.Eq(rhs) {
return lRHS
}
if lRHS.Eq(rhs) {
return lLHS
}
}
}
return n
}
func argValue(m *t.IDMap, args []*a.Node, name string) *a.Expr {
if x := m.ByName(name); x != 0 {
for _, a := range args {
+3 -4
View File
@@ -247,11 +247,10 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
return nil
}
switch op.Key() {
case t.KeyPlusEq:
// TODO: fold constants, so that ((x + 1) + 1) becomes (x + 2)?
oRHS := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryPlus, 0, xRHS.Node(), nil, rhs.Node(), nil)
case t.KeyPlusEq, t.KeyMinusEq:
oRHS := a.NewExpr(a.FlagsTypeChecked, op.BinaryForm(), 0, xRHS.Node(), nil, rhs.Node(), nil)
oRHS.SetMType(xRHS.MType())
o := a.NewExpr(a.FlagsTypeChecked, xOp, 0, xLHS.Node(), nil, oRHS.Node(), nil)
o := a.NewExpr(a.FlagsTypeChecked, xOp, 0, xLHS.Node(), nil, simplify(oRHS).Node(), nil)
o.SetMType(x.MType())
return o
}