diff --git a/cmd/puffs-c/internal/cgen/cgen.go b/cmd/puffs-c/internal/cgen/cgen.go index a400c913..444acb8a 100644 --- a/cmd/puffs-c/internal/cgen/cgen.go +++ b/cmd/puffs-c/internal/cgen/cgen.go @@ -97,6 +97,12 @@ var builtInStatuses = [...]string{ "suspension limited write", } +var builtInStatusSuffixes = map[string]string{ + // TODO: fill in the rest of the built in statuses. + "short read": "SUSPENSION_SHORT_READ", + "short write": "SUSPENSION_SHORT_WRITE", +} + func init() { // The +1 is for the error bit (the sign bit). if statusCodeNamespaceShift+base38.MaxBits+1 != 32 { @@ -107,6 +113,13 @@ func init() { } } +func trimQuotes(s string) string { + if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' { + return s[1 : len(s)-1] + } + return s +} + type replacementPolicy bool const ( diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 22656049..5c9d949f 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -122,8 +122,16 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { retExpr := (*a.Expr)(nil) if n.Keyword() != 0 { ret = g.statusMap[n.Message()] + if ret.name == "" { + msg := trimQuotes(n.Message().String(g.tm)) + suffix := builtInStatusSuffixes[msg] + if suffix == "" { + return fmt.Errorf("no status code for %q", msg) + } + ret.name = g.PKGPREFIX + suffix + } } else if retExpr = n.Value(); retExpr == nil { - ret.name = fmt.Sprintf("%sSTATUS_OK", g.PKGPREFIX) + ret.name = g.PKGPREFIX + "STATUS_OK" } if g.currFunk.suspendible { b.writes("status = ") diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index dafad451..46d39e76 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -1365,8 +1365,8 @@ puffs_flate_status puffs_flate_decoder_decode_huffman( goto exit; } v_length -= v_n; - status = PUFFS_FLATE_ERROR_BAD_HUFFMAN_CODE; - goto exit; + status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE; + goto suspend; } label_5_break:; } diff --git a/lang/check/check.go b/lang/check/check.go index fa86a125..5bec7c8c 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -95,6 +95,12 @@ type Struct struct { Struct *a.Struct } +var builtInStatuses = map[string]t.ID{ + // TODO: fill in the rest of the built in statuses. + "short read": t.IDSuspension, + "short write": t.IDSuspension, +} + func Check(tm *t.Map, files ...*a.File) (*Checker, error) { for _, f := range files { if f == nil { diff --git a/lang/check/type.go b/lang/check/type.go index 74031189..868ad012 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -120,10 +120,17 @@ func (q *checker) tcheckStatement(n *a.Node) error { case a.KReturn: n := n.Return() if nk := n.Keyword(); nk != 0 { - if s, ok := q.c.statuses[n.Message()]; !ok { - return fmt.Errorf("check: no error or status with message %q", - trimQuotes(n.Message().String(q.tm))) - } else if sk := s.Status.Keyword(); nk != sk { + sk := t.ID(0) + if s, ok := q.c.statuses[n.Message()]; ok { + sk = s.Status.Keyword() + } else { + msg := trimQuotes(n.Message().String(q.tm)) + sk, ok = builtInStatuses[msg] + if !ok { + return fmt.Errorf("check: no error or status with message %q", msg) + } + } + if nk != sk { return fmt.Errorf("check: return statement says %q but declaration says %q", nk.String(q.tm), sk.String(q.tm)) } diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 39b3d358..0874c7a8 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -351,10 +351,7 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() { } length -= n // TODO: distinguish between short and limited writes? - // - // TODO: return suspension "short write" instead of this bogus - // return error "bad Huffman code". - return error "bad Huffman code" + return suspension "short write" } } diff --git a/test/c/std/flate.c b/test/c/std/flate.c index 645159f0..633caa7e 100644 --- a/test/c/std/flate.c +++ b/test/c/std/flate.c @@ -649,7 +649,7 @@ proc tests[] = { test_puffs_flate_decode_romeo, // test_puffs_flate_decode_romeo_fixed, // test_puffs_flate_decode_split_src, // - // test_puffs_flate_history_full, // + test_puffs_flate_history_full, // test_puffs_flate_history_partial, // test_puffs_flate_table_redirect, // test_puffs_zlib_decode_midsummer, //