mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Parse "assert x < y".
This commit is contained in:
1 parent
5169460c2d
commit
17c6c71e53
3 files changed
+29
-11
No files matched your search
+11
-6
@@ -15,7 +15,7 @@ type Kind uint32
|
|||||||
const (
|
const (
|
||||||
KInvalid = Kind(iota)
|
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>
|
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDDot>
|
||||||
// - ID1: <0|identifier name|literal>
|
// - ID1: <0|identifier name|literal>
|
||||||
// - LHS: <nil|KExpr>
|
// - LHS: <nil|KExpr>
|
||||||
@@ -37,9 +37,13 @@ const (
|
|||||||
// For selectors, like "lhs.id1", ID0 is IDDot.
|
// For selectors, like "lhs.id1", ID0 is IDDot.
|
||||||
KExpr
|
KExpr
|
||||||
|
|
||||||
// KAssign is "Left = Right".
|
// KAssert is "assert RHS":
|
||||||
// - LHS: Left
|
// - RHS: <KExpr>
|
||||||
// - RHS: Right
|
KAssert
|
||||||
|
|
||||||
|
// KAssign is "LHS = RHS":
|
||||||
|
// - LHS: <KExpr>
|
||||||
|
// - RHS: <KExpr>
|
||||||
KAssign
|
KAssign
|
||||||
|
|
||||||
// KParam is a "name type" parameter:
|
// KParam is a "name type" parameter:
|
||||||
@@ -47,7 +51,7 @@ const (
|
|||||||
// - RHS: <KType>
|
// - RHS: <KType>
|
||||||
KParam
|
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>
|
// - ID0: <0|package name|IDPtr>
|
||||||
// - ID1: <0|type name>
|
// - ID1: <0|type name>
|
||||||
// - LHS: <nil|KExpr>
|
// - LHS: <nil|KExpr>
|
||||||
@@ -72,7 +76,7 @@ const (
|
|||||||
// - FlagsSuspendible is (List1) vs ?(List1)
|
// - FlagsSuspendible is (List1) vs ?(List1)
|
||||||
KFunc
|
KFunc
|
||||||
|
|
||||||
// KFile is a file of source code.
|
// KFile is a file of source code:
|
||||||
// - List0: <KFunc> top-level declarations
|
// - List0: <KFunc> top-level declarations
|
||||||
KFile
|
KFile
|
||||||
)
|
)
|
||||||
@@ -85,6 +89,7 @@ func (k Kind) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var kindStrings = [...]string{
|
var kindStrings = [...]string{
|
||||||
|
KAssert: "KAssert",
|
||||||
KAssign: "KAssign",
|
KAssign: "KAssign",
|
||||||
KExpr: "KExpr",
|
KExpr: "KExpr",
|
||||||
KFile: "KFile",
|
KFile: "KFile",
|
||||||
|
|||||||
+12
-1
@@ -290,7 +290,18 @@ func (p *parser) parseBlock() ([]*a.Node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseStatement() (*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()
|
lhs, err := p.parseExpr()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+6
-4
@@ -143,8 +143,9 @@ 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)
|
||||||
|
|
||||||
IDFalse = ID(0xD0<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
|
IDFalse = ID(0xD0<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||||
IDTrue = ID(0xD1<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
|
IDTrue = ID(0xD1<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||||
@@ -218,8 +219,9 @@ 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},
|
||||||
|
|
||||||
IDFalse >> idShift: {"false", IDFalse},
|
IDFalse >> idShift: {"false", IDFalse},
|
||||||
IDTrue >> idShift: {"true", IDTrue},
|
IDTrue >> idShift: {"true", IDTrue},
|
||||||
|
|||||||
Reference in new issue
Block a user