Prohibit effect-ful subexpressions

This commit is contained in:
Nigel Tao committed 2018-12-01 11:33:26 +11:00
1 parent 3e70383c6c
commit ad9851ea75
3 files changed
+32 -25

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@
- Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc. - Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc.
- Renamed `u32`, `io_reader`, etc to `base.u32`, `base.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`. - Renamed `unread_u8?` to `undo_byte!`; added `can_undo_byte`.
- Redesigned `!` vs `?` effects. - Redesigned `!` vs `?` effects; prohibited effect-ful subexpressions.
- Redesigned iterate blocks. - Redesigned iterate blocks.
- Added `{frame,image,pixel}_config` and `pixel_buffer` types. - Added `{frame,image,pixel}_config` and `pixel_buffer` types.
- Added a `reset` method. - Added a `reset` method.
+19 -11
View File
@@ -109,20 +109,24 @@ const (
EffectOptional = Effect(0x02) // TODO: rename to Statusful or Throws? EffectOptional = Effect(0x02) // TODO: rename to Statusful or Throws?
EffectCoroutine = Effect(0x04) EffectCoroutine = Effect(0x04)
EffectSubExprHasEffect = Effect(0x08)
// TODO: will we need an EffectRootCause if every ?-call requires a "do" or // TODO: will we need an EffectRootCause if every ?-call requires a "do" or
// "try"? // "try"?
effectMaskNonRootCause = Effect(0x07) effectMaskNonRootCause = Effect(0x7F)
EffectRootCause = Effect(0x08)
EffectRootCause = Effect(0x80)
) )
func (e Effect) AsFlags() Flags { return Flags(e) } func (e Effect) AsFlags() Flags { return Flags(e) }
// TODO: replace Pure calls with the stronger "e == 0"? // TODO: replace Pure calls with the stronger "e == 0"?
func (e Effect) Pure() bool { return e&EffectImpure == 0 } func (e Effect) Pure() bool { return e&EffectImpure == 0 }
func (e Effect) Impure() 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) Optional() bool { return e&EffectOptional != 0 }
func (e Effect) Coroutine() bool { return e&EffectCoroutine != 0 } func (e Effect) Coroutine() bool { return e&EffectCoroutine != 0 }
func (e Effect) RootCause() bool { return e&EffectRootCause != 0 } func (e Effect) SubExprHasEffect() bool { return e&EffectSubExprHasEffect != 0 }
func (e Effect) RootCause() bool { return e&EffectRootCause != 0 }
func (e Effect) String() string { func (e Effect) String() string {
switch e & effectMaskNonRootCause { 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 (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 { 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 { if lhs != nil {
flags |= lhs.flags & Flags(effectMaskNonRootCause) subExprEffect |= lhs.flags & Flags(effectMaskNonRootCause)
} }
if mhs != nil { if mhs != nil {
flags |= mhs.flags & Flags(effectMaskNonRootCause) subExprEffect |= mhs.flags & Flags(effectMaskNonRootCause)
} }
if rhs != nil { if rhs != nil {
flags |= rhs.flags & Flags(effectMaskNonRootCause) subExprEffect |= rhs.flags & Flags(effectMaskNonRootCause)
} }
for _, o := range args { for _, o := range args {
flags |= o.flags & Flags(effectMaskNonRootCause) subExprEffect |= o.flags & Flags(effectMaskNonRootCause)
}
if subExprEffect != 0 {
flags |= subExprEffect | EffectSubExprHasEffect.AsFlags()
} }
return &Expr{ return &Expr{
+12 -13
View File
@@ -869,6 +869,10 @@ func (p *parser) parseVarNode(inIterate bool) (*a.Node, error) {
if err != nil { if err != nil {
return nil, err 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 { } else if p.peek1() == t.IDEq {
op = 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 return a.NewExpr(0, t.IDComma, 0, 0, nil, nil, nil, args), nil
} }
func (p *parser) parseExprNode() (*a.Node, error) { func (p *parser) parseExpr() (*a.Expr, error) {
n, err := p.parseExpr() e, err := p.parseExpr1()
if err != nil { if err != nil {
return nil, err 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() lhs, err := p.parseOperand()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -1033,15 +1041,6 @@ func (p *parser) parseOperand() (*a.Expr, error) {
if err != nil { if err != nil {
return nil, err 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) lhs = a.NewExpr(flags, t.IDOpenParen, 0, 0, lhs.AsNode(), nil, nil, args)
case t.IDOpenBracket: case t.IDOpenBracket: