From 595ddc789d838c0dee5ebbcaac2de8c29844dfe3 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 12:38:25 +0200 Subject: [PATCH] Fixed array expr --- crates/codegen/src/expr.rs | 34 ++++++++++++------------- crates/parser/src/ast/expr.rs | 4 +-- crates/parser/src/grammar.pest | 10 +++++--- crates/parser/src/parser/common/expr.rs | 13 +++++++++- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index fc4f41d..c5658d8 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -48,24 +48,6 @@ impl GenRust for Literal { cg.add(")"); } - - Self::Array(values) => { - cg.add("["); - - for val in values { - val.gen_rust(ctx, cg); - } - - cg.add("]"); - } - - Self::ArrayRepeat(value, repeat) => { - cg.add("["); - value.gen_rust(ctx, cg); - cg.add("; "); - repeat.gen_rust(ctx, cg); - cg.add("]"); - } } } } @@ -84,6 +66,22 @@ impl GenRust for Expression { Expression::Path(path) => cg.add(&path.get_rust()), Expression::Literal(literal) => literal.gen_rust(ctx, cg), Expression::Statement(stmt) => stmt.gen_rust(ctx, cg), + Expression::Array(values) => { + cg.add("["); + + for val in values { + val.gen_rust(ctx, cg); + } + + cg.add("]"); + } + Expression::ArrayRepeat(value, repeat) => { + cg.add("["); + value.gen_rust(ctx, cg); + cg.add("; "); + repeat.gen_rust(ctx, cg); + cg.add("]"); + } Expression::Fix { initial, prefixes, diff --git a/crates/parser/src/ast/expr.rs b/crates/parser/src/ast/expr.rs index 2f91ece..04715c6 100644 --- a/crates/parser/src/ast/expr.rs +++ b/crates/parser/src/ast/expr.rs @@ -7,6 +7,8 @@ pub enum Expression { Literal(Literal), Path(ExprPath), Statement(Box), + Array(Vec), + ArrayRepeat(Box, Box), Closure { return_type: Option, params: Vec, @@ -31,8 +33,6 @@ pub enum Literal { Float(f64), Bool(bool), Tuple(Vec), - Array(Vec), - ArrayRepeat(Box, Box), } #[derive(Debug, Clone, Serialize)] diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index ab6daaf..20c79fd 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -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* } + diff --git a/crates/parser/src/parser/common/expr.rs b/crates/parser/src/parser/common/expr.rs index 3834f1d..e105511 100644 --- a/crates/parser/src/parser/common/expr.rs +++ b/crates/parser/src/parser/common/expr.rs @@ -76,7 +76,7 @@ impl<'a> TryFrom> 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> 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())),