From 935cd73686d623277573e1e0988d722d377bf709 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 1 Sep 2017 16:41:42 +1000 Subject: [PATCH] Add a "status" type and "try" keyword. --- cmd/puffs-c/internal/cgen/func.go | 33 ++++++++++++++++++++++++------- gen/c/std/flate.c | 14 +++++++++---- gen/h/std/flate.h | 1 + lang/ast/ast.go | 2 ++ lang/ast/string.go | 6 +++++- lang/check/bounds.go | 2 +- lang/check/check.go | 9 +++++---- lang/check/type.go | 14 +++++++++---- lang/parse/parse.go | 28 ++++++++++++++++++++++++-- lang/token/list.go | 6 ++++++ std/flate/decode_flate.puffs | 3 ++- 11 files changed, 94 insertions(+), 24 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/func.go b/cmd/puffs-c/internal/cgen/func.go index 5277065a..9395e8c3 100644 --- a/cmd/puffs-c/internal/cgen/func.go +++ b/cmd/puffs-c/internal/cgen/func.go @@ -722,13 +722,22 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { case a.KReturn: n := n.Return() ret := status{} - if n.Keyword() == 0 { - ret.name = fmt.Sprintf("%sSTATUS_OK", g.PKGPREFIX) - } else { + retExpr := (*a.Expr)(nil) + if n.Keyword() != 0 { ret = g.statusMap[n.Message()] + } else if retExpr = n.Value(); retExpr == nil { + ret.name = fmt.Sprintf("%sSTATUS_OK", g.PKGPREFIX) } if g.currFunk.suspendible { - b.printf("status = %s;", ret.name) + b.writes("status = ") + if retExpr == nil { + b.writes(ret.name) + // TODO: check that retExpr has no call-suspendibles. + } else if err := g.writeExpr( + b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } + b.writes(";") if ret.isError { b.writes("goto exit;") } else { @@ -1104,12 +1113,19 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { b.writes("if (status) { goto suspend; }\n") } else if isThisMethod(g.tm, n, "decode_blocks", 2) { - b.printf("status = %s%s_decode_blocks(self, %sdst, %ssrc);\n", + // TODO: don't hard code being inside a try call. + if g.currFunk.tempW > maxTemp { + return fmt.Errorf("too many temporary variables required") + } + temp := g.currFunk.tempW + g.currFunk.tempW++ + + b.printf("%sstatus %s%d = %s%s_decode_blocks(self, %sdst, %ssrc);\n", + g.pkgPrefix, tPrefix, temp, g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix) if err := g.writeLoadExprDerivedVars(b, n); err != nil { return err } - b.writes("if (status) { goto suspend; }\n") } else if isThisMethod(g.tm, n, "decode_uncompressed", 2) { b.printf("status = %s%s_decode_uncompressed(self, %sdst, %ssrc);\n", @@ -1465,7 +1481,10 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool { // TODO: check that n.Args() is "(src:in.src)". - if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs { + if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry { + return false + } + if !n.CallSuspendible() || len(n.Args()) != nArgs { return false } n = n.LHS().Expr() diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 3b4abe01..5d9a3ca0 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -161,6 +161,7 @@ typedef struct { struct { uint32_t coro_susp_point; + puffs_flate_status v_z; } c_decode[1]; struct { uint32_t coro_susp_point; @@ -522,17 +523,21 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, } puffs_flate_status status = PUFFS_FLATE_STATUS_OK; + puffs_flate_status v_z; + uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; if (coro_susp_point) { + v_z = self->private_impl.c_decode[0].v_z; } switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); PUFFS_COROUTINE_SUSPENSION_POINT(1); - status = puffs_flate_decoder_decode_blocks(self, a_dst, a_src); - if (status) { - goto suspend; - } + puffs_flate_status t_0 = + puffs_flate_decoder_decode_blocks(self, a_dst, a_src); + v_z = t_0; + status = v_z; + goto suspend; self->private_impl.c_decode[0].coro_susp_point = 0; goto exit; } @@ -540,6 +545,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, goto suspend; suspend: self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; + self->private_impl.c_decode[0].v_z = v_z; exit: self->private_impl.status = status; diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index e60be2a1..9513f886 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -161,6 +161,7 @@ typedef struct { struct { uint32_t coro_susp_point; + puffs_flate_status v_z; } c_decode[1]; struct { uint32_t coro_susp_point; diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 5c3ce06a..f0a279a8 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -201,6 +201,8 @@ const MaxExprDepth = 255 // // For function calls, like "LHS(List0)", ID0 is IDOpenParen. // +// For try/function calls, like "try LHS(List0)", ID0 is IDTry. +// // For indexes, like "LHS[RHS]", ID0 is IDOpenBracket. // // For slices, like "LHS[MHS:RHS]", ID0 is IDColon. diff --git a/lang/ast/string.go b/lang/ast/string.go index d2fd9594..cc53f264 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -31,6 +31,10 @@ func (n *Expr) appendString(buf []byte, tm *t.Map, parenthesize bool, depth uint case 0: buf = append(buf, tm.ByID(n.id1)...) + case t.KeyTry: + buf = append(buf, "try "...) + fallthrough + case t.KeyOpenParen: buf = n.lhs.Expr().appendString(buf, tm, true, depth) if n.flags&FlagsSuspendible != 0 { @@ -77,7 +81,7 @@ func (n *Expr) appendString(buf []byte, tm *t.Map, parenthesize bool, depth uint buf = append(buf, ')') case t.KeyLimit: - // TODO. + buf = append(buf, "TODO(limit)"...) } case t.FlagsUnaryOp: diff --git a/lang/check/bounds.go b/lang/check/bounds.go index c38325ac..fe224942 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -641,7 +641,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, case 0: // No-op. - case t.KeyOpenParen: + case t.KeyOpenParen, t.KeyTry: // TODO: delete this hack that only matches "in.src.read_u8?()" etc. if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) || diff --git a/lang/check/check.go b/lang/check/check.go index 8b8ceae8..5e913a85 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -56,10 +56,11 @@ func trimQuotes(s string) string { // typeExprFoo is an *ast.Node MType (implicit type). var ( - typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) - typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil) - typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil) - typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil) + typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) + typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil) + typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil) + typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil) + typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil) // TODO: delete this. typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil) diff --git a/lang/check/type.go b/lang/check/type.go index 98a3d075..b7719d6f 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -340,7 +340,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { // TODO. } - case t.KeyOpenParen: + case t.KeyOpenParen, t.KeyTry: // n is a function call. // TODO: delete this hack that only matches "in.src.read_u8?()" etc. if isInSrc(q.tm, n, t.KeyReadU8, 0) || @@ -364,7 +364,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return err } } - if isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) { + if n.ID0().Key() == t.KeyTry { + n.SetMType(typeExprStatus) + } else if isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) { n.SetMType(typeExprPlaceholder32) // HACK. } else if isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU16LE, 0) { n.SetMType(typeExprPlaceholder16) // HACK. @@ -528,7 +530,10 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool { // TODO: check that n.Args() is "(src:in.src)". - if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs { + if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry { + return false + } + if !n.CallSuspendible() || len(n.Args()) != nArgs { return false } n = n.LHS().Expr() @@ -885,7 +890,8 @@ swtch: if n.Min() != nil || n.Max() != nil { // TODO: reject. You can only refine numeric types. } - if name := n.Name().Key(); name == t.KeyBool || name == t.KeyReader1 || name == t.KeyWriter1 { + if name := n.Name().Key(); name == t.KeyBool || name == t.KeyStatus || + name == t.KeyReader1 || name == t.KeyWriter1 { break } for _, s := range q.c.structs { diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 351d8235..198e274e 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -614,12 +614,18 @@ func (p *parser) parseStatement1() (*a.Node, error) { value := (*a.Expr)(nil) if p.peek1().Key() == t.KeyEq { p.src = p.src[1:] - if p.peek1().Key() == t.KeyLimit { + switch p.peek1().Key() { + case t.KeyLimit: value, err = p.parseLimitExpr() if err != nil { return nil, err } - } else { + case t.KeyTry: + value, err = p.parseTryExpr() + if err != nil { + return nil, err + } + default: value, err = p.parseExpr() if err != nil { return nil, err @@ -735,6 +741,24 @@ func (p *parser) parseLimitExpr() (*a.Expr, error) { return a.NewExpr(0, t.IDLimit, 0, lhs.Node(), nil, rhs.Node(), nil), nil } +func (p *parser) parseTryExpr() (*a.Expr, error) { + if x := p.peek1().Key(); x != t.KeyTry { + got := p.tm.ByKey(x) + return nil, fmt.Errorf(`parse: expected "try", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + call, err := p.parseExpr() + if err != nil { + return nil, err + } + if call.ID0() != t.IDOpenParen { + return nil, fmt.Errorf(`parse: expected function call after "try", got %q at %s:%d`, + call.String(p.tm), p.filename, p.line()) + } + return a.NewExpr(call.Node().Raw().Flags(), t.IDTry, call.ID1(), + call.LHS(), call.MHS(), call.RHS(), call.Args()), nil +} + func (p *parser) parseExprNode() (*a.Node, error) { n, err := p.parseExpr() if err != nil { diff --git a/lang/token/list.go b/lang/token/list.go index 97dde2c8..f79e61bb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -215,6 +215,7 @@ const ( KeyLimit = Key(IDLimit >> KeyShift) KeyPackageID = Key(IDPackageID >> KeyShift) KeyConst = Key(IDConst >> KeyShift) + KeyTry = Key(IDTry >> KeyShift) KeyFalse = Key(IDFalse >> KeyShift) KeyTrue = Key(IDTrue >> KeyShift) @@ -234,6 +235,7 @@ const ( KeyBuf2 = Key(IDBuf2 >> KeyShift) KeyReader1 = Key(IDReader1 >> KeyShift) KeyWriter1 = Key(IDWriter1 >> KeyShift) + KeyStatus = Key(IDStatus >> KeyShift) KeyUnderscore = Key(IDUnderscore >> KeyShift) KeyThis = Key(IDThis >> KeyShift) @@ -383,6 +385,7 @@ const ( IDLimit = ID(0x75<