mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Require if and while conditions to be effect-free
This commit is contained in:
1 parent
5cca9471ec
commit
df19bcdb03
4 files changed
+16
-28
No files matched your search
@@ -214,25 +214,7 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error {
|
func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error {
|
||||||
// TODO: for writeSuspendibles, make sure that we get order of
|
for {
|
||||||
// sub-expression evaluation correct.
|
|
||||||
nCloseCurly := 1
|
|
||||||
for first := true; ; first = false {
|
|
||||||
// TODO: restrict the if condition to effect-free expressions?
|
|
||||||
if n.Condition().Effect().Coroutine() {
|
|
||||||
if !first {
|
|
||||||
b.writeb('{')
|
|
||||||
const maxCloseCurly = 1000
|
|
||||||
if nCloseCurly == maxCloseCurly {
|
|
||||||
return fmt.Errorf("too many nested if's")
|
|
||||||
}
|
|
||||||
nCloseCurly++
|
|
||||||
}
|
|
||||||
if err := g.writeSuspendibles(b, n.Condition(), depth); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
condition := buffer(nil)
|
condition := buffer(nil)
|
||||||
if err := g.writeExpr(&condition, n.Condition(), replaceCallSuspendibles, 0); err != nil {
|
if err := g.writeExpr(&condition, n.Condition(), replaceCallSuspendibles, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -259,9 +241,7 @@ func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error {
|
|||||||
}
|
}
|
||||||
b.writes("} else ")
|
b.writes("} else ")
|
||||||
}
|
}
|
||||||
for ; nCloseCurly > 0; nCloseCurly-- {
|
b.writes("}\n")
|
||||||
b.writes("}\n")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,8 +399,6 @@ func (g *gen) writeStatementVar(b *buffer, n *a.Var, depth uint32) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) writeStatementWhile(b *buffer, n *a.While, depth uint32) error {
|
func (g *gen) writeStatementWhile(b *buffer, n *a.While, depth uint32) error {
|
||||||
// TODO: consider suspendible calls.
|
|
||||||
|
|
||||||
if n.HasContinue() {
|
if n.HasContinue() {
|
||||||
jt, err := g.currFunk.jumpTarget(n)
|
jt, err := g.currFunk.jumpTarget(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -525,8 +525,6 @@ func (q *checker) bcheckIf(n *a.If) error {
|
|||||||
for n != nil {
|
for n != nil {
|
||||||
snap := snapshot(q.facts)
|
snap := snapshot(q.facts)
|
||||||
// Check the if condition.
|
// Check the if condition.
|
||||||
//
|
|
||||||
// TODO: check that n.Condition() has no side effects.
|
|
||||||
if _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
|
if _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -581,8 +579,6 @@ func (q *checker) bcheckWhile(n *a.While) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check the while condition.
|
// Check the while condition.
|
||||||
//
|
|
||||||
// TODO: check that n.Condition() has no side effects.
|
|
||||||
if _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
|
if _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,9 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
case a.KIf:
|
case a.KIf:
|
||||||
for n := n.AsIf(); n != nil; n = n.ElseIf() {
|
for n := n.AsIf(); n != nil; n = n.ElseIf() {
|
||||||
cond := n.Condition()
|
cond := n.Condition()
|
||||||
|
if cond.Effect() != 0 {
|
||||||
|
return fmt.Errorf("check: internal error: if-condition is not effect-free")
|
||||||
|
}
|
||||||
if err := q.tcheckExpr(cond, 0); err != nil {
|
if err := q.tcheckExpr(cond, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -224,6 +227,9 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
case a.KWhile:
|
case a.KWhile:
|
||||||
n := n.AsWhile()
|
n := n.AsWhile()
|
||||||
cond := n.Condition()
|
cond := n.Condition()
|
||||||
|
if cond.Effect() != 0 {
|
||||||
|
return fmt.Errorf("check: internal error: while-condition is not effect-free")
|
||||||
|
}
|
||||||
if err := q.tcheckExpr(cond, 0); err != nil {
|
if err := q.tcheckExpr(cond, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -656,6 +656,10 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if condition.Effect() != 0 {
|
||||||
|
return nil, fmt.Errorf(`parse: while-condition %q is not effect-free at %s:%d`,
|
||||||
|
condition.Str(p.tm), p.filename, p.line())
|
||||||
|
}
|
||||||
asserts, err := p.parseAsserts()
|
asserts, err := p.parseAsserts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -709,6 +713,10 @@ func (p *parser) parseIf() (*a.If, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if condition.Effect() != 0 {
|
||||||
|
return nil, fmt.Errorf(`parse: if-condition %q is not effect-free at %s:%d`,
|
||||||
|
condition.Str(p.tm), p.filename, p.line())
|
||||||
|
}
|
||||||
bodyIfTrue, err := p.parseBlock()
|
bodyIfTrue, err := p.parseBlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in new issue
Block a user