diff options
Diffstat (limited to 'src/com/craftinginterpreters/lox/Parser.java')
-rw-r--r-- | src/com/craftinginterpreters/lox/Parser.java | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/com/craftinginterpreters/lox/Parser.java b/src/com/craftinginterpreters/lox/Parser.java index 7d7b27a..786d317 100644 --- a/src/com/craftinginterpreters/lox/Parser.java +++ b/src/com/craftinginterpreters/lox/Parser.java @@ -44,6 +44,8 @@ class Parser { } private Stmt statement() { + if (match(BREAK)) + return breakStatement(); if (match(FOR)) return forStatement(); if (match(IF)) @@ -84,13 +86,11 @@ class Parser { Stmt body = statement(); if (increment != null) { - body = new Stmt.Block( - Arrays.asList( - body, - new Stmt.Expression(increment))); + body = new Stmt.Block(Arrays.asList(body, new Stmt.Expression(increment))); } - if (condition == null) condition = new Expr.Literal(true); + if (condition == null) + condition = new Expr.Literal(true); body = new Stmt.While(condition, body); if (initializer != null) { @@ -100,6 +100,11 @@ class Parser { return body; } + private Stmt breakStatement() { + consume(SEMICOLON, "Expect ';' after break statement."); + return new Stmt.Break(); + } + private Stmt ifStatement() { consume(LEFT_PAREN, "Expect '(' after 'if'."); Expr condition = expression(); |