mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 16:47:28 +01:00
Add fact tracking.
This commit is contained in:
1 parent
ea91f91466
commit
6da91a226c
4 files changed
+162
-19
No files matched your search
@@ -0,0 +1,64 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package check
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
a "github.com/google/puffs/lang/ast"
|
||||
t "github.com/google/puffs/lang/token"
|
||||
)
|
||||
|
||||
// binds returns fact's lower or upper bound for id.
|
||||
func binds(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
|
||||
op := fact.ID0()
|
||||
if !op.IsBinaryOp() || op.Key() == t.KeyAs {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
lhs := fact.LHS().Expr()
|
||||
rhs := fact.RHS().Expr()
|
||||
cv := (*big.Int)(nil)
|
||||
idLeft := lhs.ID0() == 0 && lhs.ID1() == id
|
||||
if idLeft {
|
||||
cv = rhs.ConstValue()
|
||||
} else {
|
||||
if rhs.ID0() != 0 || rhs.ID1() != id {
|
||||
return nil, nil
|
||||
}
|
||||
cv = lhs.ConstValue()
|
||||
}
|
||||
if cv == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch op.Key() {
|
||||
case t.KeyXBinaryLessThan:
|
||||
if idLeft {
|
||||
return nil, sub1(cv)
|
||||
} else {
|
||||
return add1(cv), nil
|
||||
}
|
||||
case t.KeyXBinaryLessEq:
|
||||
if idLeft {
|
||||
return nil, cv
|
||||
} else {
|
||||
return cv, nil
|
||||
}
|
||||
case t.KeyXBinaryGreaterEq:
|
||||
if idLeft {
|
||||
return cv, nil
|
||||
} else {
|
||||
return nil, cv
|
||||
}
|
||||
case t.KeyXBinaryGreaterThan:
|
||||
if idLeft {
|
||||
return add1(cv), nil
|
||||
} else {
|
||||
return nil, sub1(cv)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
Reference in new issue
Block a user