From 0f86add17e39b6732530aca5e18c64287a31f7c1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 2 Sep 2017 09:40:39 +1000 Subject: [PATCH] Add a status.is_suspension method. --- cmd/puffs-c/internal/cgen/cgen.go | 2 +- cmd/puffs-c/internal/cgen/func.go | 29 ++++++++++++++++++++++++++--- gen/c/std/flate.c | 2 ++ lang/check/bounds.go | 4 ++++ lang/check/type.go | 24 ++++++++++++++++++++++++ lang/token/list.go | 9 +++++++++ std/flate/decode_flate.puffs | 2 ++ 7 files changed, 68 insertions(+), 4 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/cgen.go b/cmd/puffs-c/internal/cgen/cgen.go index b8a5ad71..ad509901 100644 --- a/cmd/puffs-c/internal/cgen/cgen.go +++ b/cmd/puffs-c/internal/cgen/cgen.go @@ -673,7 +673,7 @@ func (g *gen) writeCtorImpl(b *buffer, n *a.Struct) error { return nil } -func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32) error { +func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { // TODO. return nil } diff --git a/cmd/puffs-c/internal/cgen/func.go b/cmd/puffs-c/internal/cgen/func.go index 9395e8c3..44b91b16 100644 --- a/cmd/puffs-c/internal/cgen/func.go +++ b/cmd/puffs-c/internal/cgen/func.go @@ -1320,11 +1320,11 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthes switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { case 0: - if err := g.writeExprOther(b, n, rp, depth); err != nil { + if err := g.writeExprOther(b, n, rp, pp, depth); err != nil { return err } case t.FlagsUnaryOp: - if err := g.writeExprUnaryOp(b, n, rp, depth); err != nil { + if err := g.writeExprUnaryOp(b, n, rp, pp, depth); err != nil { return err } case t.FlagsBinaryOp: @@ -1342,7 +1342,7 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthes return nil } -func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32) error { +func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { switch n.ID0().Key() { case 0: if id1 := n.ID1(); id1.Key() == t.KeyThis { @@ -1394,6 +1394,21 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth u b.writes(")))") return nil } + if isIsErrorOKSuspension(g.tm, n, t.KeyIsSuspension) { + if pp == parenthesesMandatory { + b.writeb('(') + } + x := n.LHS().Expr().LHS().Expr() + if err := g.writeExpr(b, x, rp, parenthesesMandatory, depth); err != nil { + return err + } + // TODO: write < or == instead of > for KeyIsError or KeyIsOK. + b.writes(" > 0") + if pp == parenthesesMandatory { + b.writeb(')') + } + return nil + } // TODO. case t.KeyOpenBracket: @@ -1504,6 +1519,14 @@ func isLowHighBits(tm *t.Map, n *a.Expr, methodName t.Key) bool { return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName } +func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool { + if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 0 { + return false + } + n = n.LHS().Expr() + return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName +} + func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool { // TODO: check that n.Args() is "(lw:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 5d9a3ca0..48e852dc 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -536,6 +536,8 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, puffs_flate_status t_0 = puffs_flate_decoder_decode_blocks(self, a_dst, a_src); v_z = t_0; + if (v_z > 0) { + } status = v_z; goto suspend; self->private_impl.c_decode[0].coro_susp_point = 0; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index fe224942..6db7953d 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -687,6 +687,10 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, } return zero, bitMask(int(aMax.Int64())), nil } + // TODO: delete this hack that only matches "foo.is_suspension(etc)". + if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) { + return nil, nil, nil + } // TODO: delete this hack that only matches "foo.set_literal_width(etc)". if isSetLiteralWidth(q.tm, n) { a := n.Args()[0].Arg().Value() diff --git a/lang/check/type.go b/lang/check/type.go index b7719d6f..d041ef0e 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -391,6 +391,22 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetMType(foo.MType().Unrefined()) return nil } + // TODO: delete this hack that only matches "foo.is_suspension(etc)". + if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) { + foo := n.LHS().Expr().LHS().Expr() + if err := q.tcheckExpr(foo, depth); err != nil { + return err + } + n.LHS().SetTypeChecked() + n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK. + for _, o := range n.Args() { + if err := q.tcheckArg(o.Arg(), depth); err != nil { + return err + } + } + n.SetMType(typeExprBool) + return nil + } // TODO: delete this hack that only matches "foo.set_literal_width(etc)". if isSetLiteralWidth(q.tm, n) { foo := n.LHS().Expr().LHS().Expr() @@ -553,6 +569,14 @@ func isLowHighBits(tm *t.Map, n *a.Expr, methodName t.Key) bool { return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName } +func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool { + if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 0 { + return false + } + n = n.LHS().Expr() + return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName +} + func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool { // TODO: check that n.Args() is "(lw:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { diff --git a/lang/token/list.go b/lang/token/list.go index f79e61bb..e03f2f33 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -266,6 +266,9 @@ const ( KeySkip32 = Key(IDSkip32 >> KeyShift) KeySkip64 = Key(IDSkip64 >> KeyShift) KeyCopyHistory32 = Key(IDCopyHistory32 >> KeyShift) + KeyIsError = Key(IDIsError >> KeyShift) + KeyIsOK = Key(IDIsOK >> KeyShift) + KeyIsSuspension = Key(IDIsSuspension >> KeyShift) KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift) KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift) @@ -436,6 +439,9 @@ const ( IDSkip32 = ID(0xC2<