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

+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},