mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Bounds check "x + 1".
This commit is contained in:
1 parent
a8c304236d
commit
05a217e8a3
2 files changed
+108
-26
No files matched your search
+102
-24
@@ -82,25 +82,9 @@ func (c *typeChecker) bcheckAssignment(lTyp *a.TypeExpr, rhs *a.Expr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
bounds := [2]*big.Int{}
|
||||
if key := lTyp.Name().Key(); key < t.Key(len(numTypeBounds)) {
|
||||
bounds = numTypeBounds[key]
|
||||
}
|
||||
if bounds[0] == nil || bounds[1] == nil {
|
||||
return fmt.Errorf("check: internal error: unknown bounds for %q", lTyp.String(c.idMap))
|
||||
}
|
||||
|
||||
if lTyp.IsRefined() {
|
||||
if x := lTyp.InclMin(); x != nil {
|
||||
if cv := x.ConstValue(); cv != nil && bounds[0].Cmp(cv) < 0 {
|
||||
bounds[0] = cv
|
||||
}
|
||||
}
|
||||
if x := lTyp.ExclMax(); x != nil {
|
||||
if cv := x.ConstValue(); cv != nil && bounds[1].Cmp(cv) > 0 {
|
||||
bounds[1] = big.NewInt(0).Sub(cv, one)
|
||||
}
|
||||
}
|
||||
lBounds0, lBounds1, err := c.bcheckTypeExpr(lTyp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cv := zero
|
||||
@@ -108,13 +92,107 @@ func (c *typeChecker) bcheckAssignment(lTyp *a.TypeExpr, rhs *a.Expr) error {
|
||||
cv = rhs.ConstValue()
|
||||
}
|
||||
if cv != nil {
|
||||
if cv.Cmp(bounds[0]) >= 0 && cv.Cmp(bounds[1]) <= 0 {
|
||||
if cv.Cmp(lBounds0) >= 0 && cv.Cmp(lBounds1) <= 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: cannot prove constant %v within bounds [%v..%v]",
|
||||
cv, bounds[0], bounds[1])
|
||||
return fmt.Errorf("check: constant %v is not within bounds [%v..%v]", cv, lBounds0, lBounds1)
|
||||
}
|
||||
|
||||
return fmt.Errorf("check: cannot prove expression %q within bounds [%v..%v]",
|
||||
rhs.String(c.idMap), bounds[0], bounds[1])
|
||||
rBounds0, rBounds1, err := c.bcheckExpr(rhs, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rBounds0.Cmp(lBounds0) >= 0 && rBounds1.Cmp(lBounds1) <= 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
|
||||
rhs.String(c.idMap), rBounds0, rBounds1, lBounds0, lBounds1)
|
||||
}
|
||||
|
||||
func (c *typeChecker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
|
||||
if depth > a.MaxExprDepth {
|
||||
return nil, nil, fmt.Errorf("check: expression recursion depth too large")
|
||||
}
|
||||
depth++
|
||||
|
||||
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
||||
case 0:
|
||||
if cv := n.ConstValue(); cv != nil {
|
||||
return cv, cv, nil
|
||||
}
|
||||
return c.bcheckTypeExpr(n.MType())
|
||||
case t.FlagsUnaryOp:
|
||||
return c.bcheckExprUnaryOp(n, depth)
|
||||
case t.FlagsBinaryOp:
|
||||
return c.bcheckExprBinaryOp(n, depth)
|
||||
case t.FlagsAssociativeOp:
|
||||
return c.bcheckExprAssociativeOp(n, depth)
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
|
||||
}
|
||||
|
||||
func (c *typeChecker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key())
|
||||
}
|
||||
|
||||
func (c *typeChecker) bcheckExprBinaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
|
||||
l0, l1, err := c.bcheckExpr(n.LHS().Expr(), depth)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
r0, r1, err := c.bcheckExpr(n.RHS().Expr(), depth)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
switch n.ID0() {
|
||||
case t.IDXBinaryPlus:
|
||||
return big.NewInt(0).Add(l0, r0), big.NewInt(0).Add(l1, r1), nil
|
||||
case t.IDXBinaryMinus:
|
||||
case t.IDXBinaryStar:
|
||||
case t.IDXBinarySlash:
|
||||
case t.IDXBinaryShiftL:
|
||||
case t.IDXBinaryShiftR:
|
||||
case t.IDXBinaryAmp:
|
||||
case t.IDXBinaryAmpHat:
|
||||
case t.IDXBinaryPipe:
|
||||
case t.IDXBinaryHat:
|
||||
case t.IDXBinaryNotEq:
|
||||
case t.IDXBinaryLessThan:
|
||||
case t.IDXBinaryLessEq:
|
||||
case t.IDXBinaryEqEq:
|
||||
case t.IDXBinaryGreaterEq:
|
||||
case t.IDXBinaryGreaterThan:
|
||||
case t.IDXBinaryAnd:
|
||||
case t.IDXBinaryOr:
|
||||
case t.IDXBinaryAs:
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", n.ID0().Key())
|
||||
}
|
||||
|
||||
func (c *typeChecker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprAssociativeOp", n.ID0().Key())
|
||||
}
|
||||
|
||||
func (c *typeChecker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
|
||||
b := [2]*big.Int{}
|
||||
if key := n.Name().Key(); key < t.Key(len(numTypeBounds)) {
|
||||
b = numTypeBounds[key]
|
||||
}
|
||||
if b[0] == nil || b[1] == nil {
|
||||
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(c.idMap))
|
||||
}
|
||||
if n.IsRefined() {
|
||||
if x := n.InclMin(); x != nil {
|
||||
if cv := x.ConstValue(); cv != nil && b[0].Cmp(cv) < 0 {
|
||||
b[0] = cv
|
||||
}
|
||||
}
|
||||
if x := n.ExclMax(); x != nil {
|
||||
if cv := x.ConstValue(); cv != nil && b[1].Cmp(cv) > 0 {
|
||||
b[1] = big.NewInt(0).Sub(cv, one)
|
||||
}
|
||||
}
|
||||
}
|
||||
return b[0], b[1], nil
|
||||
}
|
||||
@@ -10,11 +10,15 @@ struct lzw_decoder?(
|
||||
|
||||
func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() {
|
||||
var bits u32
|
||||
var nBits u32
|
||||
var n_bits u32
|
||||
|
||||
// TODO: set clear_code, end_code, etc.
|
||||
|
||||
var width u32[:13] = 12
|
||||
var width u32[:13] = this.literal_width + 1
|
||||
|
||||
// TODO: implement.
|
||||
|
||||
while n_bits < width {
|
||||
// TODO.
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user