Add lexing to loops

This commit is contained in:
Sofia 2025-07-23 19:52:46 +03:00
parent ccb5741666
commit 431aae0b0d
5 changed files with 31 additions and 2 deletions

15
examples/loops.reid Normal file
View File

@ -0,0 +1,15 @@
// Arithmetic, function calls and imports!
fn main() -> u32 {
for i in [0, 1, 2, 3, 4, 5, 6, 7, 9, 10] {
print("hello")
}
let mut num = 0;
while num < 10 {
num = num + 1;
}
return num;
}

View File

@ -178,6 +178,8 @@ pub enum BlockLevelStatement {
},
Expression(Expression),
Return(ReturnType, Expression),
ForLoop(String, Expression, Block),
WhileLoop(Expression, Block),
}
#[derive(Debug, Clone)]

View File

@ -231,10 +231,10 @@ impl Parse for PrimaryExpression {
stream.expect(Token::BracketClose)?;
Expression(Kind::Array(expressions), stream.get_range().unwrap())
}
_ => Err(stream.expected_err("expression inner")?)?,
_ => Err(stream.expecting_err("expression")?)?,
}
} else {
Err(stream.expected_err("expression")?)?
Err(stream.expecting_err("expression")?)?
};
while let Ok(index) = stream.parse::<ValueIndex>() {

View File

@ -148,6 +148,10 @@ impl ast::Block {
ast::BlockLevelStatement::Return(_, e) => {
(StmtKind::Expression(e.process(module_id)), e.1)
}
ast::BlockLevelStatement::ForLoop(expression, expression1, block) => {
todo!()
}
ast::BlockLevelStatement::WhileLoop(expression, block) => todo!(),
};
mir_statements.push(mir::Statement(kind, range.as_meta(module_id)));

View File

@ -42,6 +42,10 @@ pub enum Token {
Extern,
/// `struct`
Struct,
/// `while`
While,
/// `for`
For,
// Symbols
/// `;`
@ -136,6 +140,8 @@ impl ToString for Token {
Token::Extern => String::from("extern"),
Token::Struct => String::from("struct"),
Token::AsKeyword => String::from("as"),
Token::For => String::from("for"),
Token::While => String::from("while"),
Token::Semi => String::from(';'),
Token::Equals => String::from('='),
Token::Colon => String::from(':'),
@ -322,6 +328,8 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"pub" => Token::PubKeyword,
"struct" => Token::Struct,
"as" => Token::AsKeyword,
"for" => Token::For,
"while" => Token::While,
_ => Token::Identifier(value),
};
variant