diff --git a/doc/changelog.md b/doc/changelog.md index 8213a536..e764a8d1 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -25,7 +25,7 @@ - Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc. - Renamed `u32`, `io_reader`, etc to `base.u32`, `base.io_reader`, etc. - Renamed `unread_u8?` to `undo_byte!`; added `can_undo_byte`. -- Redesigned `!` vs `?` effects. +- Redesigned `!` vs `?` effects; prohibited effect-ful subexpressions. - Redesigned iterate blocks. - Added `{frame,image,pixel}_config` and `pixel_buffer` types. - Added a `reset` method. diff --git a/lang/ast/ast.go b/lang/ast/ast.go index ff116382..d0638eec 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -109,20 +109,24 @@ const ( EffectOptional = Effect(0x02) // TODO: rename to Statusful or Throws? EffectCoroutine = Effect(0x04) + EffectSubExprHasEffect = Effect(0x08) + // TODO: will we need an EffectRootCause if every ?-call requires a "do" or // "try"? - effectMaskNonRootCause = Effect(0x07) - EffectRootCause = Effect(0x08) + effectMaskNonRootCause = Effect(0x7F) + + EffectRootCause = Effect(0x80) ) func (e Effect) AsFlags() Flags { return Flags(e) } // TODO: replace Pure calls with the stronger "e == 0"? -func (e Effect) Pure() bool { return e&EffectImpure == 0 } -func (e Effect) Impure() bool { return e&EffectImpure != 0 } -func (e Effect) Optional() bool { return e&EffectOptional != 0 } -func (e Effect) Coroutine() bool { return e&EffectCoroutine != 0 } -func (e Effect) RootCause() bool { return e&EffectRootCause != 0 } +func (e Effect) Pure() bool { return e&EffectImpure == 0 } +func (e Effect) Impure() bool { return e&EffectImpure != 0 } +func (e Effect) Optional() bool { return e&EffectOptional != 0 } +func (e Effect) Coroutine() bool { return e&EffectCoroutine != 0 } +func (e Effect) SubExprHasEffect() bool { return e&EffectSubExprHasEffect != 0 } +func (e Effect) RootCause() bool { return e&EffectRootCause != 0 } func (e Effect) String() string { switch e & effectMaskNonRootCause { @@ -353,17 +357,21 @@ func (n *Expr) SetMBounds(x Bounds) { n.mBounds = x } func (n *Expr) SetMType(x *TypeExpr) { n.mType = x } func NewExpr(flags Flags, operator t.ID, statusPkg t.ID, ident t.ID, lhs *Node, mhs *Node, rhs *Node, args []*Node) *Expr { + subExprEffect := Flags(0) if lhs != nil { - flags |= lhs.flags & Flags(effectMaskNonRootCause) + subExprEffect |= lhs.flags & Flags(effectMaskNonRootCause) } if mhs != nil { - flags |= mhs.flags & Flags(effectMaskNonRootCause) + subExprEffect |= mhs.flags & Flags(effectMaskNonRootCause) } if rhs != nil { - flags |= rhs.flags & Flags(effectMaskNonRootCause) + subExprEffect |= rhs.flags & Flags(effectMaskNonRootCause) } for _, o := range args { - flags |= o.flags & Flags(effectMaskNonRootCause) + subExprEffect |= o.flags & Flags(effectMaskNonRootCause) + } + if subExprEffect != 0 { + flags |= subExprEffect | EffectSubExprHasEffect.AsFlags() } return &Expr{ diff --git a/lang/parse/parse.go b/lang/parse/parse.go index aa1d50cd..9c896d96 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -869,6 +869,10 @@ func (p *parser) parseVarNode(inIterate bool) (*a.Node, error) { if err != nil { return nil, err } + if value.Effect() != 0 { + return nil, fmt.Errorf(`parse: iterate value %q is not effect-free at %s:%d`, + value.Str(p.tm), p.filename, p.line()) + } } else if p.peek1() == t.IDEq { op = t.IDEq @@ -903,15 +907,19 @@ func (p *parser) parsePossibleListExpr() (*a.Expr, error) { return a.NewExpr(0, t.IDComma, 0, 0, nil, nil, nil, args), nil } -func (p *parser) parseExprNode() (*a.Node, error) { - n, err := p.parseExpr() +func (p *parser) parseExpr() (*a.Expr, error) { + e, err := p.parseExpr1() if err != nil { return nil, err } - return n.AsNode(), err + if e.Effect().SubExprHasEffect() { + return nil, fmt.Errorf(`parse: expression %q has an effect-ful sub-expression at %s:%d`, + e.Str(p.tm), p.filename, p.line()) + } + return e, nil } -func (p *parser) parseExpr() (*a.Expr, error) { +func (p *parser) parseExpr1() (*a.Expr, error) { lhs, err := p.parseOperand() if err != nil { return nil, err @@ -1033,15 +1041,6 @@ func (p *parser) parseOperand() (*a.Expr, error) { if err != nil { return nil, err } - if flags.AsEffect() != 0 { - for _, arg := range args { - o := arg.AsArg().Value() - if o.Effect() != 0 { - return nil, fmt.Errorf(`parse: effectful function call %q has effectful argument %q at %s:%d`, - lhs.Str(p.tm), o.Str(p.tm), p.filename, p.line()) - } - } - } lhs = a.NewExpr(flags, t.IDOpenParen, 0, 0, lhs.AsNode(), nil, nil, args) case t.IDOpenBracket: