about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin/rlox/src/parser.rs')
-rw-r--r--users/tazjin/rlox/src/parser.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs
index ed5e670ecb79..495304686b2f 100644
--- a/users/tazjin/rlox/src/parser.rs
+++ b/users/tazjin/rlox/src/parser.rs
@@ -80,6 +80,11 @@ pub struct Var {
 }
 
 #[derive(Debug)]
+pub struct Return {
+    pub value: Expr,
+}
+
+#[derive(Debug)]
 pub struct If {
     pub condition: Expr,
     pub then_branch: Box<Statement>,
@@ -110,6 +115,7 @@ pub enum Statement {
     If(If),
     While(While),
     Function(Rc<Function>),
+    Return(Return),
 }
 
 // Parser
@@ -130,6 +136,7 @@ statement      → exprStmt
                | forStmt
                | ifStmt
                | printStmt
+               | returnStmt
                | whileStmt
                | block ;
 
@@ -137,6 +144,8 @@ forStmt        → "for" "(" ( varDecl | exprStmt | ";" )
                  expression? ";"
                  expression? ")" statement ;
 
+returnStmt     → "return" expression? ";" ;
+
 whileStmt      → "while" "(" expression ")" statement ;
 
 exprStmt       → expression ";" ;
@@ -256,6 +265,8 @@ impl Parser {
             self.while_statement()
         } else if self.match_token(&TokenKind::For) {
             self.for_statement()
+        } else if self.match_token(&TokenKind::Return) {
+            self.return_statement()
         } else {
             self.expr_statement()
         }
@@ -379,6 +390,12 @@ impl Parser {
         Ok(body)
     }
 
+    fn return_statement(&mut self) -> StmtResult {
+        let value = self.expression()?;
+        self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?;
+        Ok(Statement::Return(Return { value }))
+    }
+
     fn expr_statement(&mut self) -> StmtResult {
         let expr = self.expression()?;
         self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?;