From e5c86ec191cf4a90f2acce5f9253311b2f60bac3 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 3 Apr 2020 10:46:05 +1100 Subject: [PATCH] Let an unbroken "while true" terminate a block --- lang/ast/ast.go | 13 ++++++++++--- lang/parse/parse.go | 10 +++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 363c9a7d..78a7819a 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -593,6 +593,11 @@ func (n *While) SetBody(body []*Node) { n.list2 = body } func (n *While) SetHasBreak() { n.flags |= FlagsHasBreak } 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 { return &While{ kind: KWhile, @@ -1013,10 +1018,9 @@ func NewFile(filename string, topLevelDecls []*Node) *File { // Terminates returns whether a block of statements terminates. In other words, // 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 { - // TODO: strengthen this to include "while" statements? For inspiration, - // the Go spec has https://golang.org/ref/spec#Terminating_statements if len(body) == 0 { return false } @@ -1041,6 +1045,9 @@ func Terminates(body []*Node) bool { return true case KRet: return n.AsRet().Keyword() == t.IDReturn + case KWhile: + n := n.AsWhile() + return n.IsWhileTrue() && !n.HasBreak() } return false } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index ad1a2276..994dd22d 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -790,17 +790,17 @@ func (p *parser) parseStatement1() (*a.Node, error) { if err != nil { 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) if !p.loops.Push(n) { return nil, fmt.Errorf(`parse: duplicate loop label %s at %s:%d`, 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) if err != nil { return nil, err