Parse "assert x < y".

This commit is contained in:
Nigel Tao committed 2017-05-04 17:55:53 +10:00
1 parent 5169460c2d
commit 17c6c71e53
3 files changed
+29 -11

No files matched your search

+11 -6
View File
@@ -15,7 +15,7 @@ type Kind uint32
const (
KInvalid = Kind(iota)
// KExpr is an expression, such as "i", "+j" or "k + l[m(n, o)].p".
// KExpr is an expression, such as "i", "+j" or "k + l[m(n, o)].p":
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDDot>
// - ID1: <0|identifier name|literal>
// - LHS: <nil|KExpr>
@@ -37,9 +37,13 @@ const (
// For selectors, like "lhs.id1", ID0 is IDDot.
KExpr
// KAssign is "Left = Right".
// - LHS: Left
// - RHS: Right
// KAssert is "assert RHS":
// - RHS: <KExpr>
KAssert
// KAssign is "LHS = RHS":
// - LHS: <KExpr>
// - RHS: <KExpr>
KAssign
// KParam is a "name type" parameter:
@@ -47,7 +51,7 @@ const (
// - RHS: <KType>
KParam
// 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>
// - ID1: <0|type name>
// - LHS: <nil|KExpr>
@@ -72,7 +76,7 @@ const (
// - FlagsSuspendible is (List1) vs ?(List1)
KFunc
// KFile is a file of source code.
// KFile is a file of source code:
// - List0: <KFunc> top-level declarations
KFile
)
@@ -85,6 +89,7 @@ func (k Kind) String() string {
}
var kindStrings = [...]string{
KAssert: "KAssert",
KAssign: "KAssign",
KExpr: "KExpr",
KFile: "KFile",
+12 -1
View File
@@ -290,7 +290,18 @@ func (p *parser) parseBlock() ([]*a.Node, error) {
}
func (p *parser) parseStatement() (*a.Node, error) {
// TODO: parse statements other than x = y.
switch x := p.peekID(); x {
case t.IDAssert:
p.src = p.src[1:]
rhs, err := p.parseExpr()
if err != nil {
return nil, err
}
return &a.Node{
Kind: a.KAssert,
RHS: rhs,
}, nil
}
lhs, err := p.parseExpr()
if err != nil {
+6 -4
View File
@@ -143,8 +143,9 @@ const (
IDAs = ID(0x83<<idShift | FlagsBinaryOp)
// TODO: sort these by name, when the list has stabilized.
IDFunc = ID(0xA0<<idShift | FlagsOther)
IDPtr = ID(0xA1<<idShift | FlagsOther)
IDFunc = ID(0xA0<<idShift | FlagsOther)
IDPtr = ID(0xA1<<idShift | FlagsOther)
IDAssert = ID(0xA2<<idShift | FlagsOther)
IDFalse = ID(0xD0<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0xD1<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -218,8 +219,9 @@ var builtInsByKey = [nBuiltInKeys]struct {
IDNot >> idShift: {"not", IDNot},
IDAs >> idShift: {"as", IDAs},
IDFunc >> idShift: {"func", IDFunc},
IDPtr >> idShift: {"ptr", IDPtr},
IDFunc >> idShift: {"func", IDFunc},
IDPtr >> idShift: {"ptr", IDPtr},
IDAssert >> idShift: {"assert", IDAssert},
IDFalse >> idShift: {"false", IDFalse},
IDTrue >> idShift: {"true", IDTrue},