diff options
Diffstat (limited to 'sloth/src/parser')
| -rw-r--r-- | sloth/src/parser/ast.rs | 5 | ||||
| -rw-r--r-- | sloth/src/parser/mod.rs | 2 | ||||
| -rw-r--r-- | sloth/src/parser/stmt.rs | 25 | 
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 | 
