aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-06-27 22:30:30 -0500
committernic-gaffney <gaffney_nic@protonmail.com>2023-06-27 22:30:30 -0500
commit33364734df31543a3d6b7f7dbc3852edb402d9ca (patch)
tree811f9ed01f3342767fea8cff0995aefe9d30410a
parent2c347aae8ab225cc873b7c36c8a1c73ca6ee4123 (diff)
downloadsloth-33364734df31543a3d6b7f7dbc3852edb402d9ca.tar.gz
for
-rw-r--r--sloth/src/parser/ast.rs5
-rw-r--r--sloth/src/parser/mod.rs2
-rw-r--r--sloth/src/parser/stmt.rs25
3 files changed, 31 insertions, 1 deletions
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<Stmt>,
},
+ ForStmt {
+ iter: Expr,
+ identifier: String,
+ body: Box<Stmt>,
+ }
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<Stmt, ParsingError> {
+ // 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<Stmt, ParsingError> {
// Consume the var token