Skip to content

Commit

Permalink
for
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 28, 2023
1 parent 2c347aa commit 3336473
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions sloth/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sloth/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
25 changes: 25 additions & 0 deletions sloth/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3336473

Please sign in to comment.