Add a yield keyword

This commit is contained in:
Nigel Tao committed 2017-12-01 15:24:48 +11:00
1 parent 93665d50fe
commit 65dcf6a5b9
19 files changed
+122 -89

No files matched your search

+1 -1
View File
@@ -228,7 +228,7 @@ func (g *gen) genHeader(b *buffer) error {
for i, z := range builtin.StatusList {
code := uint32(0)
if z.Keyword == t.IDError {
if z.Keyword.Key() == t.KeyError {
code |= 1 << 31
}
code |= uint32(i)
+10 -5
View File
@@ -219,8 +219,8 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
b.printf("goto label_%d_%s;\n", jt, keyword)
return nil
case a.KReturn:
n := n.Return()
case a.KRet:
n := n.Ret()
retExpr := n.Value()
if g.currFunk.suspendible {
@@ -238,15 +238,20 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
}
b.writes(";")
if n.Keyword().Key() == t.KeyYield {
return g.writeCoroSuspPoint(b, true)
}
switch retKeyword {
case t.KeyError:
b.writes("goto exit;")
return nil
case t.KeyStatus:
b.writes("goto ok;")
return nil
default:
b.printf("if (status == 0) { goto ok; } else if (status > 0) { "+
"status = %sERROR_CANNOT_RETURN_A_SUSPENSION; } goto exit;", g.PKGPREFIX)
}
return g.writeCoroSuspPoint(b, true)
return nil
}
b.writes("return ")
+3 -2
View File
@@ -3,14 +3,15 @@
## Work In Progress
- Renamed Puffs to Wuffs
- Renamed Puffs to Wuffs.
- Added a `yield` keyword.
## 2017-11-16
- [Initial open source
release](https://groups.google.com/d/topic/puffslang/2z61mNTAMns/discussion),
under the "Puffs" name
under the "Puffs" name.
---
+2 -1
View File
@@ -27,7 +27,7 @@ be enclosed by curly `{}`s. There is no 'dangling else' ambiguity.
- `pri`
- `pub`
7 keywords deal with control flow within a function:
8 keywords deal with control flow within a function:
- `break`
- `continue`
@@ -36,6 +36,7 @@ be enclosed by curly `{}`s. There is no 'dangling else' ambiguity.
- `iterate`
- `return`
- `while`
- `yield`
5 keywords deal with assertions:
+21 -14
View File
@@ -125,16 +125,17 @@ typedef int32_t wuffs_flate__status;
#define wuffs_flate__packageid 967230 // 0x000ec23e
#define WUFFS_FLATE__STATUS_OK 0 // 0x00000000
#define WUFFS_FLATE__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_FLATE__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_FLATE__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_FLATE__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_FLATE__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_FLATE__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_FLATE__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_FLATE__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_FLATE__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_FLATE__STATUS_OK 0 // 0x00000000
#define WUFFS_FLATE__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_FLATE__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_FLATE__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_FLATE__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_FLATE__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_FLATE__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_FLATE__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_FLATE__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_FLATE__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_FLATE__ERROR_CANNOT_RETURN_A_SUSPENSION -2147483638 // 0x8000000a
#define WUFFS_FLATE__ERROR_BAD_HUFFMAN_CODE_OVER_SUBSCRIBED \
-1157040128 // 0xbb08f800
@@ -690,7 +691,7 @@ bool wuffs_flate__status__is_error(wuffs_flate__status s) {
return s < 0;
}
const char* wuffs_flate__status__strings0[10] = {
const char* wuffs_flate__status__strings0[11] = {
"flate: ok",
"flate: bad wuffs version",
"flate: bad receiver",
@@ -701,6 +702,7 @@ const char* wuffs_flate__status__strings0[10] = {
"flate: unexpected EOF",
"flate: short read",
"flate: short write",
"flate: cannot return a suspension",
};
const char* wuffs_flate__status__strings1[22] = {
@@ -734,7 +736,7 @@ const char* wuffs_flate__status__string(wuffs_flate__status s) {
switch ((s >> 10) & 0x1fffff) {
case 0:
a = wuffs_flate__status__strings0;
n = 10;
n = 11;
break;
case wuffs_flate__packageid:
a = wuffs_flate__status__strings1;
@@ -962,7 +964,12 @@ wuffs_flate__status wuffs_flate__flate_decoder__decode(
}
if (!(v_z > 0)) {
status = v_z;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
if (status == 0) {
goto ok;
} else if (status > 0) {
status = WUFFS_FLATE__ERROR_CANNOT_RETURN_A_SUSPENSION;
}
goto exit;
}
v_written = ((wuffs_base__slice_u8){
.ptr = a_dst.private_impl.mark,
@@ -1003,7 +1010,7 @@ wuffs_flate__status wuffs_flate__flate_decoder__decode(
}
}
status = v_z;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
}
goto ok;
+14 -12
View File
@@ -125,16 +125,17 @@ typedef int32_t wuffs_gif__status;
#define wuffs_gif__packageid 1017222 // 0x000f8586
#define WUFFS_GIF__STATUS_OK 0 // 0x00000000
#define WUFFS_GIF__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_GIF__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_GIF__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_GIF__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_GIF__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_GIF__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_GIF__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_GIF__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_GIF__STATUS_OK 0 // 0x00000000
#define WUFFS_GIF__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_GIF__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_GIF__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_GIF__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_GIF__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_GIF__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_GIF__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_GIF__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_GIF__ERROR_CANNOT_RETURN_A_SUSPENSION -2147483638 // 0x8000000a
#define WUFFS_GIF__ERROR_BAD_GIF_BLOCK -1105848320 // 0xbe161800
#define WUFFS_GIF__ERROR_BAD_GIF_EXTENSION_LABEL -1105848319 // 0xbe161801
@@ -632,7 +633,7 @@ bool wuffs_gif__status__is_error(wuffs_gif__status s) {
return s < 0;
}
const char* wuffs_gif__status__strings0[10] = {
const char* wuffs_gif__status__strings0[11] = {
"gif: ok",
"gif: bad wuffs version",
"gif: bad receiver",
@@ -643,6 +644,7 @@ const char* wuffs_gif__status__strings0[10] = {
"gif: unexpected EOF",
"gif: short read",
"gif: short write",
"gif: cannot return a suspension",
};
const char* wuffs_gif__status__strings1[8] = {
@@ -662,7 +664,7 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) {
switch ((s >> 10) & 0x1fffff) {
case 0:
a = wuffs_gif__status__strings0;
n = 10;
n = 11;
break;
case wuffs_gif__packageid:
a = wuffs_gif__status__strings1;
+11 -10
View File
@@ -125,16 +125,17 @@ typedef int32_t wuffs_flate__status;
#define wuffs_flate__packageid 967230 // 0x000ec23e
#define WUFFS_FLATE__STATUS_OK 0 // 0x00000000
#define WUFFS_FLATE__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_FLATE__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_FLATE__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_FLATE__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_FLATE__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_FLATE__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_FLATE__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_FLATE__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_FLATE__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_FLATE__STATUS_OK 0 // 0x00000000
#define WUFFS_FLATE__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_FLATE__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_FLATE__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_FLATE__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_FLATE__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_FLATE__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_FLATE__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_FLATE__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_FLATE__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_FLATE__ERROR_CANNOT_RETURN_A_SUSPENSION -2147483638 // 0x8000000a
#define WUFFS_FLATE__ERROR_BAD_HUFFMAN_CODE_OVER_SUBSCRIBED \
-1157040128 // 0xbb08f800
+11 -10
View File
@@ -125,16 +125,17 @@ typedef int32_t wuffs_gif__status;
#define wuffs_gif__packageid 1017222 // 0x000f8586
#define WUFFS_GIF__STATUS_OK 0 // 0x00000000
#define WUFFS_GIF__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_GIF__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_GIF__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_GIF__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_GIF__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_GIF__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_GIF__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_GIF__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_GIF__STATUS_OK 0 // 0x00000000
#define WUFFS_GIF__ERROR_BAD_WUFFS_VERSION -2147483647 // 0x80000001
#define WUFFS_GIF__ERROR_BAD_RECEIVER -2147483646 // 0x80000002
#define WUFFS_GIF__ERROR_BAD_ARGUMENT -2147483645 // 0x80000003
#define WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED -2147483644 // 0x80000004
#define WUFFS_GIF__ERROR_INVALID_I_O_OPERATION -2147483643 // 0x80000005
#define WUFFS_GIF__ERROR_CLOSED_FOR_WRITES -2147483642 // 0x80000006
#define WUFFS_GIF__ERROR_UNEXPECTED_EOF -2147483641 // 0x80000007
#define WUFFS_GIF__SUSPENSION_SHORT_READ 8 // 0x00000008
#define WUFFS_GIF__SUSPENSION_SHORT_WRITE 9 // 0x00000009
#define WUFFS_GIF__ERROR_CANNOT_RETURN_A_SUSPENSION -2147483638 // 0x8000000a
#define WUFFS_GIF__ERROR_BAD_GIF_BLOCK -1105848320 // 0xbe161800
#define WUFFS_GIF__ERROR_BAD_GIF_EXTENSION_LABEL -1105848319 // 0xbe161801
+14 -11
View File
@@ -44,7 +44,7 @@ const (
KIterate
KJump
KPackageID
KReturn
KRet
KStatus
KStruct
KTypeExpr
@@ -75,7 +75,7 @@ var kindStrings = [...]string{
KIterate: "KIterate",
KJump: "KJump",
KPackageID: "KPackageID",
KReturn: "KReturn",
KRet: "KRet",
KStatus: "KStatus",
KStruct: "KStruct",
KTypeExpr: "KTypeExpr",
@@ -160,7 +160,7 @@ func (n *Node) Iterate() *Iterate { return (*Iterate)(n) }
func (n *Node) Jump() *Jump { return (*Jump)(n) }
func (n *Node) PackageID() *PackageID { return (*PackageID)(n) }
func (n *Node) Raw() *Raw { return (*Raw)(n) }
func (n *Node) Return() *Return { return (*Return)(n) }
func (n *Node) Ret() *Ret { return (*Ret)(n) }
func (n *Node) Status() *Status { return (*Status)(n) }
func (n *Node) Struct() *Struct { return (*Struct)(n) }
func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) }
@@ -507,16 +507,19 @@ func NewIf(condition *Expr, elseIf *If, bodyIfTrue []*Node, bodyIfFalse []*Node)
}
}
// Return is "return LHS":
// Ret is "return LHS" or "yield LHS":
// - ID0: <IDReturn|IDYield>
// - LHS: <nil|Expr>
type Return Node
type Ret Node
func (n *Return) Node() *Node { return (*Node)(n) }
func (n *Return) Value() *Expr { return n.lhs.Expr() }
func (n *Ret) Node() *Node { return (*Node)(n) }
func (n *Ret) Keyword() t.ID { return n.id0 }
func (n *Ret) Value() *Expr { return n.lhs.Expr() }
func NewReturn(value *Expr) *Return {
return &Return{
kind: KReturn,
func NewRet(keyword t.ID, value *Expr) *Ret {
return &Ret{
kind: KRet,
id0: keyword,
lhs: value.Node(),
}
}
@@ -663,7 +666,7 @@ const MaxBodyDepth = 255
// - If
// - Iterate
// - Jump
// - Return
// - Ret
// - Var
// - While
type Func Node
+2
View File
@@ -36,6 +36,7 @@ func (z Status) String() string {
}
var StatusList = [...]Status{
// TODO: sort these somehow, when the list has stabilized?
{0, "ok"},
{t.IDError, "bad wuffs version"},
{t.IDError, "bad receiver"},
@@ -46,6 +47,7 @@ var StatusList = [...]Status{
{t.IDError, "unexpected EOF"}, // Used if reading when closed == true.
{t.IDSuspension, "short read"}, // Used if reading when closed == false.
{t.IDSuspension, "short write"},
{t.IDError, "cannot return a suspension"},
}
var StatusMap = map[string]Status{}
+17 -5
View File
@@ -218,12 +218,22 @@ func bcheckField(tm *t.Map, n *a.Field) error {
}
func (q *checker) bcheckBlock(block []*a.Node) error {
loop:
for _, o := range block {
if err := q.bcheckStatement(o); err != nil {
return err
}
if k := o.Kind(); k == a.KJump || k == a.KReturn {
break
switch o.Kind() {
case a.KJump:
break loop
case a.KRet:
if o.Ret().Keyword().Key() == t.KeyReturn {
break loop
}
// o is a yield statement.
//
// TODO: invalidate this-, in- and out-related facts because the
// suspension can change the state of this and its fields.
}
}
return nil
@@ -276,7 +286,7 @@ func (q *checker) bcheckStatement(n *a.Node) error {
q.facts = q.facts[:0]
return nil
case a.KReturn:
case a.KRet:
// TODO.
case a.KVar:
@@ -475,8 +485,6 @@ func terminates(body []*a.Node) bool {
if len(body) > 0 {
n := body[len(body)-1]
switch n.Kind() {
case a.KReturn, a.KJump:
return true
case a.KIf:
n := n.If()
for {
@@ -492,6 +500,10 @@ func terminates(body []*a.Node) bool {
return len(bif) > 0
}
}
case a.KJump:
return true
case a.KRet:
return n.Ret().Keyword().Key() == t.KeyReturn
}
return false
}
+2 -2
View File
@@ -156,8 +156,8 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
n.SetJumpTarget(jumpTarget)
case a.KReturn:
n := n.Return()
case a.KRet:
n := n.Ret()
if value := n.Value(); value != nil {
if err := q.tcheckExpr(value, 0); err != nil {
return err
+2 -2
View File
@@ -612,7 +612,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
}
return a.NewIterate(label, unroll, vars, asserts, body).Node(), nil
case t.KeyReturn:
case t.KeyReturn, t.KeyYield:
p.src = p.src[1:]
value, err := (*a.Expr)(nil), error(nil)
if p.peek1().Key() != t.KeySemicolon {
@@ -621,7 +621,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
return nil, err
}
}
return a.NewReturn(value).Node(), nil
return a.NewRet(x, value).Node(), nil
case t.KeyVar:
p.src = p.src[1:]
+3
View File
@@ -243,6 +243,7 @@ const (
KeyConst = Key(IDConst >> KeyShift)
KeyTry = Key(IDTry >> KeyShift)
KeyIterate = Key(IDIterate >> KeyShift)
KeyYield = Key(IDYield >> KeyShift)
KeyFalse = Key(IDFalse >> KeyShift)
KeyTrue = Key(IDTrue >> KeyShift)
@@ -431,6 +432,7 @@ const (
IDConst = ID(0x66<<KeyShift | FlagsOther)
IDTry = ID(0x67<<KeyShift | FlagsOther)
IDIterate = ID(0x68<<KeyShift | FlagsOther)
IDYield = ID(0x69<<KeyShift | FlagsOther)
IDFalse = ID(0x70<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0x71<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -635,6 +637,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyConst: {"const", IDConst},
KeyTry: {"try", IDTry},
KeyIterate: {"iterate", IDIterate},
KeyYield: {"yield", IDYield},
KeyFalse: {"false", IDFalse},
KeyTrue: {"true", IDTrue},
+3 -4
View File
@@ -127,7 +127,6 @@ pub func flate_decoder.decode?(dst writer1, src reader1)() {
in.dst.mark()
var z status = try this.decode_blocks?(dst:in.dst, src:in.src)
if not z.is_suspension() {
// TODO: rename "return" to "raise"?
return z
}
// TODO: should "since_mark" be "since_mark!", as the return value lets
@@ -171,7 +170,7 @@ pub func flate_decoder.decode?(dst writer1, src reader1)() {
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as u32) + already_full
}
}
return z
yield z
}
}
@@ -239,9 +238,9 @@ pri func flate_decoder.decode_uncompressed?(dst writer1, src reader1)() {
length -= n_copied
if in.dst.available() == 0 {
// TODO: "closed for writes" instead?
return suspension "short write"
yield suspension "short write"
} else {
return suspension "short read"
yield suspension "short read"
}
}
}
+3 -7
View File
@@ -212,13 +212,9 @@ pri func flate_decoder.decode_huffman_slow?(dst writer1, src reader1)() {
}
}
// TODO: "closed for writes" instead?
return suspension "short write"
// TODO: this "assert false" should be a compiler error.
// This might mean differentiating "return" and "raise".
//
yield suspension "short write"
// TODO: invalidate this-related facts because the
// suspension can change the state of this and its fields.
assert false
}
// Copy from the start of this.history, if we wrapped around.
if hlen > 0 {
@@ -231,7 +227,7 @@ pri func flate_decoder.decode_huffman_slow?(dst writer1, src reader1)() {
hlen -= n_copied
hdist = (hdist + (n_copied & 0x7FFF)) & 0x7FFF
// TODO: "closed for writes" instead?
return suspension "short write"
yield suspension "short write"
}
}
@@ -249,7 +245,7 @@ pri func flate_decoder.decode_huffman_slow?(dst writer1, src reader1)() {
}
length -= n_copied
// TODO: "closed for writes" instead?
return suspension "short write"
yield suspension "short write"
}
}
+1 -1
View File
@@ -54,7 +54,7 @@ pub func zlib_decoder.decode?(dst writer1, src reader1)() {
if z.is_ok() {
break
}
return z
yield z
}
if checksum != in.src.read_u32be?() {
return error "checksum mismatch"
+1 -1
View File
@@ -182,7 +182,7 @@ pri func decoder.decode_id?(dst writer1, src reader1)() {
if (block_size == 0) and (z == suspension "short read") {
break
}
return z
yield z
}
}
}
+1 -1
View File
@@ -118,7 +118,7 @@ pub func lzw_decoder.decode?(dst writer1, src reader1)() {
}
s = (s + ((n_copied & 4095) as u32)) & 4095
// TODO: "closed for writes" instead?
return suspension "short write"
yield suspension "short write"
}
if save_code <= 4095 {