From 33364734df31543a3d6b7f7dbc3852edb402d9ca Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Tue, 27 Jun 2023 22:30:30 -0500 Subject: for --- sloth/src/parser/ast.rs | 5 +++++ sloth/src/parser/mod.rs | 2 +- sloth/src/parser/stmt.rs | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sloth/src/parser/ast.rs b/sloth/src/parser/ast.rs index 00719e7..bae3340 100644 --- a/sloth/src/parser/ast.rs +++ b/sloth/src/parser/ast.rs @@ -215,6 +215,11 @@ pub enum StmtKind { condition: Expr, body: Box, }, + ForStmt { + iter: Expr, + identifier: String, + body: Box, + } DefineVariable { identifier: String, value: Expr, diff --git a/sloth/src/parser/mod.rs b/sloth/src/parser/mod.rs index 09a26fd..9bf4dd6 100644 --- a/sloth/src/parser/mod.rs +++ b/sloth/src/parser/mod.rs @@ -40,7 +40,7 @@ impl<'a> AstParser<'a> { let mut statements = Vec::new(); while !parser.eof() { statements.push(parser.statement()?); - } + } let root = Stmt::new( parser.reserve_id(), diff --git a/sloth/src/parser/stmt.rs b/sloth/src/parser/stmt.rs index 6af4634..4771abc 100644 --- a/sloth/src/parser/stmt.rs +++ b/sloth/src/parser/stmt.rs @@ -83,6 +83,31 @@ impl<'a> AstParser<'a> { )) } + fn for_stmt(&mut self) -> Result { + // Consume the for token + self.consume(TokenType::For, "Expected for")?; + + + let identifier = self.consume_identifier()?; + self.consume(TokenType::In, "Expected in")?; + let iterator = self.expression()?; + + let body = self.block()?; + + let kind = StmtKind::ForStmt { + iterator, + identifier, + body: body.into(), + }; + + Ok(Stmt::new( + self.reserve_id(), + self.line, + kind, + self.top.clone(), + )) + } + // TODO: Make variable types optional fn define_variable(&mut self) -> Result { // Consume the var token -- cgit v1.2.3