diff options
| author | Cody <cody@codyq.dev> | 2023-02-27 12:14:16 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-02-27 12:14:16 -0600 |
| commit | e4199d2837d2179f17e97b8d50366d96c8babded (patch) | |
| tree | e07543001ba1aa7e05ff8b289b4d3154d7123fd6 /src/ast | |
| parent | f464fe944808df7865262962b8231c0009874692 (diff) | |
| download | sloth-e4199d2837d2179f17e97b8d50366d96c8babded.tar.gz | |
Added while loops & guessing game
Diffstat (limited to 'src/ast')
| -rw-r--r-- | src/ast/mod.rs | 4 | ||||
| -rw-r--r-- | src/ast/parser.rs | 11 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/ast/mod.rs b/src/ast/mod.rs index d006737..4e1d639 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -36,6 +36,10 @@ pub enum Stmt { range: (Expr, Expr), body: Vec<Stmt>, }, + While { + condition: Expr, + body: Vec<Stmt>, + }, Return { value: Expr, }, diff --git a/src/ast/parser.rs b/src/ast/parser.rs index 842ad09..bc29f94 100644 --- a/src/ast/parser.rs +++ b/src/ast/parser.rs @@ -118,6 +118,10 @@ impl<'a> AstParser<'a> { return self.for_statement(); } + if self.advance_if_eq(&TokenType::While) { + return self.while_statement(); + } + // If we couldn't parse a statement return an expression statement self.expression_statement() } @@ -186,6 +190,13 @@ impl<'a> AstParser<'a> { } } + fn while_statement(&mut self) -> Stmt { + let condition = self.expression(); + let body = self.block(); + + Stmt::While { condition, body } + } + fn expression_statement(&mut self) -> Stmt { let expr = self.expression(); |
