Parse break, continue and return.

This commit is contained in:
Nigel Tao committed 2017-05-04 18:39:47 +10:00
1 parent 14ec9e33b7
commit 2045b1a9d4
3 files changed
+66 -22

No files matched your search

+25 -10
View File
@@ -64,6 +64,18 @@ const (
// - List2: <*> if-false body // - List2: <*> if-false body
KIf KIf
// KReturn is "return LHS":
// - LHS: <KExpr>
KReturn
// KBreak is "break":
// TODO: label?
KBreak
// KContinue is "continue":
// TODO: label?
KContinue
// KType is a type, such as "u32", "pkg.foo", "ptr T" or "[8] T": // KType is a type, such as "u32", "pkg.foo", "ptr T" or "[8] T":
// - ID0: <0|package name|IDPtr> // - ID0: <0|package name|IDPtr>
// - ID1: <0|type name> // - ID1: <0|type name>
@@ -102,16 +114,19 @@ func (k Kind) String() string {
} }
var kindStrings = [...]string{ var kindStrings = [...]string{
KAssert: "KAssert", KAssert: "KAssert",
KAssign: "KAssign", KAssign: "KAssign",
KExpr: "KExpr", KBreak: "KBreak",
KFile: "KFile", KContinue: "KContinue",
KFor: "KFor", KExpr: "KExpr",
KFunc: "KFunc", KFile: "KFile",
KIf: "KIf", KFor: "KFor",
KInvalid: "KInvalid", KFunc: "KFunc",
KParam: "KParam", KIf: "KIf",
KType: "KType", KInvalid: "KInvalid",
KParam: "KParam",
KReturn: "KReturn",
KType: "KType",
} }
type Flags uint32 type Flags uint32
+23
View File
@@ -302,6 +302,18 @@ func (p *parser) parseStatement() (*a.Node, error) {
RHS: rhs, RHS: rhs,
}, nil }, nil
case t.IDBreak:
p.src = p.src[1:]
return &a.Node{
Kind: a.KBreak,
}, nil
case t.IDContinue:
p.src = p.src[1:]
return &a.Node{
Kind: a.KContinue,
}, nil
case t.IDFor: case t.IDFor:
p.src = p.src[1:] p.src = p.src[1:]
lhs, err := (*a.Node)(nil), error(nil) lhs, err := (*a.Node)(nil), error(nil)
@@ -323,6 +335,17 @@ func (p *parser) parseStatement() (*a.Node, error) {
case t.IDIf: case t.IDIf:
return p.parseIf() return p.parseIf()
case t.IDReturn:
p.src = p.src[1:]
lhs, err := p.parseExpr()
if err != nil {
return nil, err
}
return &a.Node{
Kind: a.KReturn,
LHS: lhs,
}, nil
} }
lhs, err := p.parseExpr() lhs, err := p.parseExpr()
+18 -12
View File
@@ -143,12 +143,15 @@ const (
IDAs = ID(0x83<<idShift | FlagsBinaryOp) IDAs = ID(0x83<<idShift | FlagsBinaryOp)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
IDFunc = ID(0xA0<<idShift | FlagsOther) IDFunc = ID(0xA0<<idShift | FlagsOther)
IDPtr = ID(0xA1<<idShift | FlagsOther) IDPtr = ID(0xA1<<idShift | FlagsOther)
IDAssert = ID(0xA2<<idShift | FlagsOther) IDAssert = ID(0xA2<<idShift | FlagsOther)
IDFor = ID(0xA3<<idShift | FlagsOther) IDFor = ID(0xA3<<idShift | FlagsOther)
IDIf = ID(0xA4<<idShift | FlagsOther) IDIf = ID(0xA4<<idShift | FlagsOther)
IDElse = ID(0xA5<<idShift | FlagsOther) IDElse = ID(0xA5<<idShift | FlagsOther)
IDReturn = ID(0xA6<<idShift | FlagsOther | FlagsImplicitSemicolon)
IDBreak = ID(0xA7<<idShift | FlagsOther | FlagsImplicitSemicolon)
IDContinue = ID(0xA8<<idShift | FlagsOther | FlagsImplicitSemicolon)
IDFalse = ID(0xD0<<idShift | FlagsLiteral | FlagsImplicitSemicolon) IDFalse = ID(0xD0<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0xD1<<idShift | FlagsLiteral | FlagsImplicitSemicolon) IDTrue = ID(0xD1<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -222,12 +225,15 @@ var builtInsByKey = [nBuiltInKeys]struct {
IDNot >> idShift: {"not", IDNot}, IDNot >> idShift: {"not", IDNot},
IDAs >> idShift: {"as", IDAs}, IDAs >> idShift: {"as", IDAs},
IDFunc >> idShift: {"func", IDFunc}, IDFunc >> idShift: {"func", IDFunc},
IDPtr >> idShift: {"ptr", IDPtr}, IDPtr >> idShift: {"ptr", IDPtr},
IDAssert >> idShift: {"assert", IDAssert}, IDAssert >> idShift: {"assert", IDAssert},
IDFor >> idShift: {"for", IDFor}, IDFor >> idShift: {"for", IDFor},
IDIf >> idShift: {"if", IDIf}, IDIf >> idShift: {"if", IDIf},
IDElse >> idShift: {"else", IDElse}, IDElse >> idShift: {"else", IDElse},
IDReturn >> idShift: {"return", IDReturn},
IDBreak >> idShift: {"break", IDBreak},
IDContinue >> idShift: {"continue", IDContinue},
IDFalse >> idShift: {"false", IDFalse}, IDFalse >> idShift: {"false", IDFalse},
IDTrue >> idShift: {"true", IDTrue}, IDTrue >> idShift: {"true", IDTrue},