mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Use "inv etc" not "assert etc" for invariants.
This commit is contained in:
1 parent
930ff53ff6
commit
47da01233b
5 files changed
+30
-22
No files matched your search
+2
-2
@@ -219,8 +219,8 @@ func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mh
|
||||
}
|
||||
}
|
||||
|
||||
// Assert is "assert RHS via ID1(args)", "pre etc" or "post etc":
|
||||
// - ID0: <IDAssert|IDPre|IDPost>
|
||||
// Assert is "assert RHS via ID1(args)", "pre etc", "inv etc" or "post etc":
|
||||
// - ID0: <IDAssert|IDPre|IDInv|IDPost>
|
||||
// - ID1: <string literal> reason
|
||||
// - RHS: <Expr>
|
||||
// - List0: <Arg> reason arguments
|
||||
|
||||
+10
-10
@@ -263,7 +263,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check the pre and assert conditions on entry.
|
||||
// Check the pre and inv conditions on entry.
|
||||
for _, o := range n.Asserts() {
|
||||
if o.Assert().Keyword().Key() == t.KeyPost {
|
||||
continue
|
||||
@@ -271,11 +271,11 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Check the post conditions on exit, assuming only the pre and assert
|
||||
// conditions and the inverted while condition.
|
||||
// Check the post conditions on exit, assuming only the pre and inv
|
||||
// (invariant) conditions and the inverted while condition.
|
||||
//
|
||||
// We don't need to check the assert conditions, even though we add them to
|
||||
// the facts after the while loop, since we have already proven each assert
|
||||
// We don't need to check the inv conditions, even though we add them to
|
||||
// the facts after the while loop, since we have already proven each inv
|
||||
// condition on entry, and below, proven them on each explicit continue and
|
||||
// on the implicit continue after the body.
|
||||
if cv := n.Condition().ConstValue(); cv != nil && cv.Cmp(one) == 0 {
|
||||
@@ -308,7 +308,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
||||
// We effectively have a "while false { etc }" loop. There's no need to
|
||||
// check the body.
|
||||
} else {
|
||||
// Assume the pre and assert conditions, and the while condition. Check
|
||||
// Assume the pre and inv conditions, and the while condition. Check
|
||||
// the body.
|
||||
q.facts = q.facts[:0]
|
||||
for _, o := range n.Asserts() {
|
||||
@@ -323,7 +323,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Check the pre and assert conditions on the implicit continue after the
|
||||
// Check the pre and inv conditions on the implicit continue after the
|
||||
// body.
|
||||
for _, o := range n.Asserts() {
|
||||
if o.Assert().Keyword().Key() == t.KeyPost {
|
||||
@@ -332,12 +332,12 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// TODO: check the pre and assert conditions for each continue.
|
||||
// TODO: check the pre and inv conditions for each continue.
|
||||
|
||||
// TODO: check the assert and post conditions for each break.
|
||||
// TODO: check the inv and post conditions for each break.
|
||||
}
|
||||
|
||||
// Assume the assert and post conditions.
|
||||
// Assume the inv and post conditions.
|
||||
q.facts = q.facts[:0]
|
||||
for _, o := range n.Asserts() {
|
||||
if o.Assert().Keyword().Key() == t.KeyPre {
|
||||
|
||||
@@ -71,6 +71,8 @@ func TestCheck(t *testing.T) {
|
||||
assert true
|
||||
|
||||
while p == q,
|
||||
pre true,
|
||||
inv true,
|
||||
post p != q,
|
||||
{
|
||||
}
|
||||
|
||||
+9
-6
@@ -395,25 +395,28 @@ func (p *parser) parseBlock() ([]*a.Node, error) {
|
||||
}
|
||||
|
||||
func (p *parser) assertsSorted(asserts []*a.Node) error {
|
||||
seenAssert, seenPost := false, false
|
||||
seenInv, seenPost := false, false
|
||||
for _, a := range asserts {
|
||||
switch a.Assert().Keyword().Key() {
|
||||
case t.KeyAssert:
|
||||
return fmt.Errorf(`parse: assertion chain cannot contain "assert", `+
|
||||
`only "pre", "inv" and "post" at %s:%d`, p.filename, p.line())
|
||||
case t.KeyPre:
|
||||
if seenPost || seenAssert {
|
||||
if seenPost || seenInv {
|
||||
break
|
||||
}
|
||||
continue
|
||||
case t.KeyAssert:
|
||||
case t.KeyInv:
|
||||
if seenPost {
|
||||
break
|
||||
}
|
||||
seenAssert = true
|
||||
seenInv = true
|
||||
continue
|
||||
default:
|
||||
seenPost = true
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf(`parse: assertion chain not in "pre", "assert", "post" order at %s:%d`,
|
||||
return fmt.Errorf(`parse: assertion chain not in "pre", "inv", "post" order at %s:%d`,
|
||||
p.filename, p.line())
|
||||
}
|
||||
return nil
|
||||
@@ -421,7 +424,7 @@ func (p *parser) assertsSorted(asserts []*a.Node) error {
|
||||
|
||||
func (p *parser) parseAssertNode() (*a.Node, error) {
|
||||
switch x := p.peek1(); x.Key() {
|
||||
case t.KeyAssert, t.KeyPre, t.KeyPost:
|
||||
case t.KeyAssert, t.KeyPre, t.KeyInv, t.KeyPost:
|
||||
p.src = p.src[1:]
|
||||
condition, err := p.parseExpr()
|
||||
if err != nil {
|
||||
|
||||
+7
-4
@@ -195,6 +195,7 @@ const (
|
||||
KeyVar = Key(IDVar >> KeyShift)
|
||||
KeyIdeal = Key(IDIdeal >> KeyShift)
|
||||
KeyPre = Key(IDPre >> KeyShift)
|
||||
KeyInv = Key(IDInv >> KeyShift)
|
||||
KeyPost = Key(IDPost >> KeyShift)
|
||||
KeyVia = Key(IDVia >> KeyShift)
|
||||
KeyPub = Key(IDPub >> KeyShift)
|
||||
@@ -323,10 +324,11 @@ const (
|
||||
IDVar = ID(0x6B<<KeyShift | FlagsOther)
|
||||
IDIdeal = ID(0x6C<<KeyShift | FlagsOther)
|
||||
IDPre = ID(0x6D<<KeyShift | FlagsOther)
|
||||
IDPost = ID(0x6E<<KeyShift | FlagsOther)
|
||||
IDVia = ID(0x6F<<KeyShift | FlagsOther)
|
||||
IDPub = ID(0x70<<KeyShift | FlagsOther)
|
||||
IDPri = ID(0x71<<KeyShift | FlagsOther)
|
||||
IDInv = ID(0x6E<<KeyShift | FlagsOther)
|
||||
IDPost = ID(0x6F<<KeyShift | FlagsOther)
|
||||
IDVia = ID(0x70<<KeyShift | FlagsOther)
|
||||
IDPub = ID(0x71<<KeyShift | FlagsOther)
|
||||
IDPri = ID(0x72<<KeyShift | FlagsOther)
|
||||
|
||||
IDFalse = ID(0x90<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||
IDTrue = ID(0x91<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||
@@ -457,6 +459,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyVar: {"var", IDVar},
|
||||
KeyIdeal: {"ideal", IDIdeal},
|
||||
KeyPre: {"pre", IDPre},
|
||||
KeyInv: {"inv", IDInv},
|
||||
KeyPost: {"post", IDPost},
|
||||
KeyVia: {"via", IDVia},
|
||||
KeyPub: {"pub", IDPub},
|
||||
|
||||
Reference in new issue
Block a user