Parse if-statement

This commit is contained in:
Sofia 2026-03-14 16:03:58 +02:00
parent 5ed3edd433
commit b3fec86fbd
4 changed files with 19 additions and 3 deletions

View File

@ -1,7 +1,7 @@
function max (a, b)
local m = a
-- if b > a then
-- m = b
-- end
if a then
local m = b
end
return m
end

View File

@ -203,6 +203,7 @@ impl Parse for Block {
pub enum Statement {
Assignment(Option<DefinitionKind>, Node<String>, Node<Expression>),
Return(Node<Expression>),
If(Node<Expression>, Block),
}
impl Parse for Statement {
@ -211,6 +212,13 @@ impl Parse for Statement {
if peeked == Some(Token::Keyword(Keyword::Return)) {
stream.next();
Ok(Statement::Return(stream.parse()?))
} else if peeked == Some(Token::Keyword(Keyword::If)) {
stream.next(); // Consume if
let cond = stream.parse()?;
stream.expect(Token::Keyword(Keyword::Then))?;
let then = stream.parse()?;
stream.expect(Token::Keyword(Keyword::End))?;
Ok(Self::If(cond, then))
} else if peeked == Some(Token::Keyword(Keyword::Local)) {
stream.next();
let name = stream.parse()?;

View File

@ -18,6 +18,8 @@ fn main() {
let tokens = tokenize(TEST).unwrap();
let mut stream = TokenStream::from(&file_path, &tokens);
dbg!(&tokens);
let mut functions = Vec::new();
while stream.peek() != Some(Token::Eof) {
functions.push(stream.parse::<Function>().unwrap());

View File

@ -13,6 +13,8 @@ pub enum Keyword {
End,
Local,
Return,
If,
Then,
}
impl Keyword {
@ -22,6 +24,8 @@ impl Keyword {
"end" => Keyword::End,
"local" => Keyword::Local,
"return" => Keyword::Return,
"if" => Keyword::If,
"then" => Keyword::Then,
_ => None?,
})
}
@ -34,6 +38,8 @@ impl ToString for Keyword {
Keyword::End => "end",
Keyword::Local => "local",
Keyword::Return => "return",
Keyword::If => "if",
Keyword::Then => "then",
}
.to_string()
}