Fixed array expr

This commit is contained in:
2026-06-05 12:38:25 +02:00
parent d5501ef25e
commit 595ddc789d
4 changed files with 37 additions and 24 deletions
+2 -2
View File
@@ -7,6 +7,8 @@ pub enum Expression {
Literal(Literal),
Path(ExprPath),
Statement(Box<Statement>),
Array(Vec<Expression>),
ArrayRepeat(Box<Expression>, Box<Expression>),
Closure {
return_type: Option<TypeExpr>,
params: Vec<VarDecl>,
@@ -31,8 +33,6 @@ pub enum Literal {
Float(f64),
Bool(bool),
Tuple(Vec<Expression>),
Array(Vec<Expression>),
ArrayRepeat(Box<Expression>, Box<Expression>),
}
#[derive(Debug, Clone, Serialize)]
+7 -3
View File
@@ -459,10 +459,12 @@ tuple = {
"(" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ ")"
}
array = {
"[" ~ expr ~ ";" ~ expr ~ "]" // Repeat notation: [0; 10]
array_repeat = {
"[" ~ expr ~ ";" ~ expr ~ "]"
}
| "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" // Standard elements initialization
array = {
"[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]"
}
// ------------------------------------------------------
@@ -496,6 +498,7 @@ primary = {
| literal
| expr_path
| tuple
| array_repeat
| array
}
@@ -624,3 +627,4 @@ bin_op = {
expr = { term ~ (bin_op ~ term)* }
term = { prefix* ~ primary ~ postfix* }
+12 -1
View File
@@ -76,7 +76,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
}
Rule::closure => {
ast_expr!(Self::Closure {
ast_expr!(Expression::Closure {
return_type: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose(),
@@ -85,6 +85,17 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
})
}
Rule::array => {
ast_expr!(Expression::Array(collect_recovered(inner)))
}
Rule::array_repeat => {
ast_expr!(Expression::ArrayRepeat(
Expression::try_from(inner.next().unwrap()).map(Box::new),
Expression::try_from(inner.next().unwrap()).map(Box::new)
))
}
Rule::primary => pair.into_inner().next().unwrap().try_into(),
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),