Add array expression parsing

This commit is contained in:
Sofia 2025-07-13 17:41:57 +03:00
parent 887071eeb6
commit 8f95d445c0
4 changed files with 16 additions and 2 deletions

View File

@ -1,7 +1,7 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn array() -> [u16; 4] { fn array() -> [u16; 4] {
return true; return [1, 2, 3, 4];
} }
fn main() -> u16 { fn main() -> u16 {

View File

@ -38,6 +38,7 @@ pub struct Expression(pub ExpressionKind, pub TokenRange);
pub enum ExpressionKind { pub enum ExpressionKind {
VariableName(String), VariableName(String),
Literal(Literal), Literal(Literal),
Array(Vec<Expression>),
Binop(BinaryOperator, Box<Expression>, Box<Expression>), Binop(BinaryOperator, Box<Expression>, Box<Expression>),
FunctionCall(Box<FunctionCallExpression>), FunctionCall(Box<FunctionCallExpression>),
BlockExpr(Box<Block>), BlockExpr(Box<Block>),

View File

@ -97,7 +97,19 @@ impl Parse for PrimaryExpression {
stream.expect(Token::ParenClose)?; stream.expect(Token::ParenClose)?;
exp exp
} }
_ => Err(stream.expected_err("identifier, constant or parentheses")?)?, Token::BracketOpen => {
let mut expressions = Vec::new();
if let Ok(exp) = stream.parse() {
expressions.push(exp);
while let Some(Token::Comma) = stream.peek() {
stream.next(); // Consume comma
expressions.push(stream.parse()?);
}
}
stream.expect(Token::BracketClose)?;
Expression(Kind::Array(expressions), stream.get_range().unwrap())
}
_ => Err(stream.expected_err("identifier, constant, parentheses or brackets")?)?,
} }
} else { } else {
Err(stream.expected_err("expression")?)? Err(stream.expected_err("expression")?)?

View File

@ -149,6 +149,7 @@ impl ast::Expression {
}; };
mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block)) mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block))
} }
ast::ExpressionKind::Array(expressions) => todo!(),
}; };
mir::Expression(kind, self.1.into()) mir::Expression(kind, self.1.into())