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