mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Let an unbroken "while true" terminate a block
This commit is contained in:
1 parent
4a89913a9b
commit
e5c86ec191
2 files changed
+15
-8
No files matched your search
+10
-3
@@ -593,6 +593,11 @@ func (n *While) SetBody(body []*Node) { n.list2 = body }
|
|||||||
func (n *While) SetHasBreak() { n.flags |= FlagsHasBreak }
|
func (n *While) SetHasBreak() { n.flags |= FlagsHasBreak }
|
||||||
func (n *While) SetHasContinue() { n.flags |= FlagsHasContinue }
|
func (n *While) SetHasContinue() { n.flags |= FlagsHasContinue }
|
||||||
|
|
||||||
|
func (n *While) IsWhileTrue() bool {
|
||||||
|
condition := n.mhs.AsExpr()
|
||||||
|
return (condition.Operator() == 0) && (condition.Ident() == t.IDTrue)
|
||||||
|
}
|
||||||
|
|
||||||
func NewWhile(label t.ID, condition *Expr, asserts []*Node) *While {
|
func NewWhile(label t.ID, condition *Expr, asserts []*Node) *While {
|
||||||
return &While{
|
return &While{
|
||||||
kind: KWhile,
|
kind: KWhile,
|
||||||
@@ -1013,10 +1018,9 @@ func NewFile(filename string, topLevelDecls []*Node) *File {
|
|||||||
|
|
||||||
// Terminates returns whether a block of statements terminates. In other words,
|
// Terminates returns whether a block of statements terminates. In other words,
|
||||||
// whether the block is non-empty and its final statement is a "return",
|
// whether the block is non-empty and its final statement is a "return",
|
||||||
// "break", "continue" or an "if-else" chain where all branches terminate.
|
// "break", "continue", a "while true" that doesn't break or an "if-else" chain
|
||||||
|
// where all branches terminate.
|
||||||
func Terminates(body []*Node) bool {
|
func Terminates(body []*Node) bool {
|
||||||
// TODO: strengthen this to include "while" statements? For inspiration,
|
|
||||||
// the Go spec has https://golang.org/ref/spec#Terminating_statements
|
|
||||||
if len(body) == 0 {
|
if len(body) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -1041,6 +1045,9 @@ func Terminates(body []*Node) bool {
|
|||||||
return true
|
return true
|
||||||
case KRet:
|
case KRet:
|
||||||
return n.AsRet().Keyword() == t.IDReturn
|
return n.AsRet().Keyword() == t.IDReturn
|
||||||
|
case KWhile:
|
||||||
|
n := n.AsWhile()
|
||||||
|
return n.IsWhileTrue() && !n.HasBreak()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
+5
-5
@@ -790,17 +790,17 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
doubleCurly := p.peek1() == t.IDOpenDoubleCurly
|
|
||||||
if doubleCurly && ((condition.Operator() != 0) || (condition.Ident() != t.IDTrue)) {
|
|
||||||
return nil, fmt.Errorf(`parse: double {{ }} while loop condition isn't "true" at %s:%d`,
|
|
||||||
p.filename, p.line())
|
|
||||||
}
|
|
||||||
|
|
||||||
n := a.NewWhile(label, condition, asserts)
|
n := a.NewWhile(label, condition, asserts)
|
||||||
if !p.loops.Push(n) {
|
if !p.loops.Push(n) {
|
||||||
return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`,
|
return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`,
|
||||||
label.Str(p.tm), p.filename, p.line())
|
label.Str(p.tm), p.filename, p.line())
|
||||||
}
|
}
|
||||||
|
doubleCurly := p.peek1() == t.IDOpenDoubleCurly
|
||||||
|
if doubleCurly && !n.IsWhileTrue() {
|
||||||
|
return nil, fmt.Errorf(`parse: double {{ }} while loop condition isn't "true" at %s:%d`,
|
||||||
|
p.filename, p.line())
|
||||||
|
}
|
||||||
body, err := p.parseBlock(doubleCurly)
|
body, err := p.parseBlock(doubleCurly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in new issue
Block a user