diff --git a/lang/check/assert.go b/lang/check/assert.go new file mode 100644 index 00000000..35c26b50 --- /dev/null +++ b/lang/check/assert.go @@ -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 +} diff --git a/lang/check/bounds.go b/lang/check/bounds.go index a6f9e9ca..d8079624 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -36,6 +36,10 @@ func btoi(b bool) *big.Int { return zero } +func add1(i *big.Int) *big.Int { + return big.NewInt(0).Add(i, one) +} + func sub1(i *big.Int) *big.Int { return big.NewInt(0).Sub(i, one) } @@ -49,7 +53,8 @@ func (c *checker) bcheckStatement(n *a.Node) error { switch n.Kind() { case a.KAssert: - // TODO. + // TODO: check the assertion. + c.facts = append(c.facts, n.Assert().Condition()) case a.KAssign: o := n.Assign() @@ -60,20 +65,7 @@ func (c *checker) bcheckStatement(n *a.Node) error { return c.bcheckAssignment(nil, o.XType(), t.IDEq, o.Value()) case a.KWhile: - o := n.While() - cond := o.Condition() - if _, _, err := c.bcheckExpr(cond, 0); err != nil { - return err - } - for _, m := range o.Asserts() { - // TODO - _ = m - } - for _, m := range o.Body() { - if err := c.bcheckStatement(m); err != nil { - return err - } - } + return c.bcheckWhile(n.While()) case a.KIf: // TODO. @@ -92,6 +84,14 @@ func (c *checker) bcheckStatement(n *a.Node) error { } func (c *checker) bcheckAssignment(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error { + if err := c.bcheckAssignment1(lhs, lTyp, op, rhs); err != nil { + return err + } + // TODO: drop any facts involving lhs. + return nil +} + +func (c *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error { switch lTyp.PackageOrDecorator().Key() { case t.KeyPtr: // TODO: handle. @@ -142,6 +142,48 @@ func (c *checker) bcheckAssignment(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs * return nil } +func (c *checker) bcheckWhile(n *a.While) error { + if _, _, err := c.bcheckExpr(n.Condition(), 0); err != nil { + return err + } + + // Check the pre and assert conditions on entry. + for _, m := range n.Asserts() { + if m.Assert().Keyword().Key() == t.KeyPost { + continue + } + // TODO + } + + // Assume the pre and assert conditions, and the while condition. Check + // the body. + c.facts = c.facts[:0] + for _, m := range n.Asserts() { + if m.Assert().Keyword().Key() == t.KeyPost { + continue + } + c.facts = append(c.facts, m.Assert().Condition()) + } + c.facts = append(c.facts, n.Condition()) + for _, m := range n.Body() { + if err := c.bcheckStatement(m); err != nil { + return err + } + } + + // TODO: check the assert and post conditions on exit. + + // Assume the assert and post conditions. + c.facts = c.facts[:0] + for _, m := range n.Asserts() { + if m.Assert().Keyword().Key() == t.KeyPre { + continue + } + c.facts = append(c.facts, m.Assert().Condition()) + } + return nil +} + func (c *checker) 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") @@ -153,7 +195,24 @@ func (c *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { case 0: - return c.bcheckTypeExpr(n.MType()) + n0, n1, err := c.bcheckTypeExpr(n.MType()) + if err != nil { + return nil, nil, err + } + if n.ID0() == 0 && n.ID1().IsIdent() { + for _, f := range c.facts { + f0, f1 := binds(f, n.ID1()) + if f0 != nil && n0.Cmp(f0) < 0 { + n0 = f0 + } + if f1 != nil && n1.Cmp(f1) > 0 { + n1 = f1 + } + } + } else { + // TODO: propagate facts about e.g. "this.foo". + } + return n0, n1, nil case t.FlagsUnaryOp: return c.bcheckExprUnaryOp(n, depth) case t.FlagsBinaryOp: diff --git a/lang/check/check.go b/lang/check/check.go index c3d6e64e..e75f67b1 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -17,14 +17,29 @@ type Error struct { Line uint32 OtherFilename string OtherLine uint32 + + IDMap *t.IDMap + Facts []*a.Expr } func (e *Error) Error() string { + s := "" if e.OtherFilename != "" || e.OtherLine != 0 { - return fmt.Sprintf("%s at %s:%d and %s:%d", + s = fmt.Sprintf("%s at %s:%d and %s:%d", e.Err, e.Filename, e.Line, e.OtherFilename, e.OtherLine) + } else { + s = fmt.Sprintf("%s at %s:%d", e.Err, e.Filename, e.Line) } - return fmt.Sprintf("%s at %s:%d", e.Err, e.Filename, e.Line) + if e.IDMap == nil { + return s + } + b := append([]byte(s), ". Facts:\n"...) + for _, f := range e.Facts { + b = append(b, '\t') + b = append(b, f.String(e.IDMap)...) + b = append(b, '\n') + } + return string(b) } // TypeExprFoo is an *ast.Node MType (implicit type) that means that n is a @@ -284,6 +299,8 @@ func (c *Checker) checkFuncBody(n *a.Node) error { Err: err, Filename: q.errFilename, Line: q.errLine, + IDMap: c.idMap, + Facts: q.facts, } } } @@ -318,4 +335,6 @@ type checker struct { errFilename string errLine uint32 + + facts []*a.Expr } diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index c67e0df7..aab14899 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -19,7 +19,8 @@ func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() { // TODO: implement. while n_bits < width { + assert n_bits < 12 // TODO. - // n_bits += width + n_bits += 8 } }