Add "while:label" and "break:label" syntax.

This commit is contained in:
Nigel Tao committed 2017-06-01 13:53:27 +10:00
1 parent 30165e06df
commit 2ed019b670
3 files changed
+36 -12

No files matched your search

+11 -5
View File
@@ -319,20 +319,23 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
}
}
// While is "while LHS, List0 { List1 }":
// While is "while:ID1 LHS, List0 { List1 }":
// - ID1: <0|label>
// - LHS: <Expr>
// - List0: <Assert> asserts
// - List1: <Statement> loop body
type While Node
func (n *While) Node() *Node { return (*Node)(n) }
func (n *While) Label() t.ID { return n.id1 }
func (n *While) Condition() *Expr { return n.lhs.Expr() }
func (n *While) Asserts() []*Node { return n.list0 }
func (n *While) Body() []*Node { return n.list1 }
func NewWhile(condition *Expr, asserts []*Node, body []*Node) *While {
func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While {
return &While{
kind: KWhile,
id1: label,
lhs: condition.Node(),
list0: asserts,
list1: body,
@@ -376,17 +379,20 @@ func NewReturn(value *Expr) *Return {
}
}
// Jump is "break" or "continue":
// TODO: label?
// Jump is "break" or "continue", with an optional label, "break:label":
// - ID0: <IDBreak|IDContinue>
// - ID1: <0|label>
type Jump Node
func (n *Jump) Node() *Node { return (*Node)(n) }
func (n *Jump) Keyword() t.ID { return n.id0 }
func (n *Jump) Label() t.ID { return n.id1 }
func NewJump(keyword t.ID) *Jump {
func NewJump(keyword t.ID, label t.ID) *Jump {
return &Jump{
kind: KJump,
id0: keyword,
id1: label,
}
}
+7 -5
View File
@@ -18,9 +18,9 @@ import (
"github.com/google/puffs/lang/token"
)
func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, src string) error {
func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, comments []string, src string) error {
buf := &bytes.Buffer{}
if err := render.Render(buf, idMap, tokens, nil); err != nil {
if err := render.Render(buf, idMap, tokens, comments); err != nil {
return err
}
got := strings.Split(buf.String(), "\n")
@@ -70,18 +70,20 @@ func TestCheck(t *testing.T) {
assert true
while p == q,
while:label p == q,
pre true,
inv true,
post p != q,
{
// Redundant, but shows the labeled jump syntax.
continue:label
}
}
`) + "\n"
idMap := &token.IDMap{}
tokens, _, err := token.Tokenize(idMap, filename, []byte(src))
tokens, comments, err := token.Tokenize(idMap, filename, []byte(src))
if err != nil {
t.Fatalf("Tokenize: %v", err)
}
@@ -91,7 +93,7 @@ func TestCheck(t *testing.T) {
t.Fatalf("Parse: %v", err)
}
if err := compareToPuffsfmt(idMap, tokens, src); err != nil {
if err := compareToPuffsfmt(idMap, tokens, comments, src); err != nil {
t.Fatalf("compareToPuffsfmt: %v", err)
}
+18 -2
View File
@@ -461,6 +461,14 @@ func (p *parser) parseStatement() (*a.Node, error) {
return n, err
}
func (p *parser) parseLabel() (t.ID, error) {
if p.peek1().Key() == t.KeyColon {
p.src = p.src[1:]
return p.parseIdent()
}
return 0, nil
}
func (p *parser) parseStatement1() (*a.Node, error) {
switch x := p.peek1(); x.Key() {
case t.KeyAssert, t.KeyPre, t.KeyPost:
@@ -468,10 +476,18 @@ func (p *parser) parseStatement1() (*a.Node, error) {
case t.KeyBreak, t.KeyContinue:
p.src = p.src[1:]
return a.NewJump(x).Node(), nil
label, err := p.parseLabel()
if err != nil {
return nil, err
}
return a.NewJump(x, label).Node(), nil
case t.KeyWhile:
p.src = p.src[1:]
label, err := p.parseLabel()
if err != nil {
return nil, err
}
condition, err := p.parseExpr()
if err != nil {
return nil, err
@@ -491,7 +507,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
if err != nil {
return nil, err
}
return a.NewWhile(condition, asserts, body).Node(), nil
return a.NewWhile(label, condition, asserts, body).Node(), nil
case t.KeyIf:
o, err := p.parseIf()