Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions compiler/plc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,13 @@ pub fn flatten_expression_list(list: &AstNode) -> Vec<&AstNode> {
expressions.iter().by_ref().flat_map(flatten_expression_list).collect()
}
AstStatement::MultipliedStatement(MultipliedStatement { multiplier, element }, ..) => {
std::iter::repeat_n(flatten_expression_list(element), *multiplier as usize).flatten().collect()
if let Some(count) = multiplier.get_literal_integer_value() {
std::iter::repeat_n(flatten_expression_list(element), count as usize).flatten().collect()
} else {
// Non-literal multiplier (e.g. variable reference) — cannot flatten
// at this stage; the array lowering pass will handle it.
vec![list]
}
}
AstStatement::ParenExpression(expression) => flatten_expression_list(expression),
_ => vec![list],
Expand Down Expand Up @@ -2100,13 +2106,16 @@ impl AstFactory {
}

pub fn create_multiplied_statement(
multiplier: u32,
multiplier: AstNode,
element: AstNode,
location: SourceLocation,
id: AstId,
) -> AstNode {
AstNode::new(
AstStatement::MultipliedStatement(MultipliedStatement { multiplier, element: Box::new(element) }),
AstStatement::MultipliedStatement(MultipliedStatement {
multiplier: Box::new(multiplier),
element: Box::new(element),
}),
id,
location,
)
Expand Down Expand Up @@ -2190,7 +2199,7 @@ pub struct CastStatement {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(bound(deserialize = "'de: 'static"))]
pub struct MultipliedStatement {
pub multiplier: u32,
pub multiplier: Box<AstNode>,
pub element: Box<AstNode>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion compiler/plc_ast/src/mut_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ impl WalkerMut for MultipliedStatement {
where
V: AstVisitorMut,
{
visitor.visit(&mut self.element)
visitor.visit(&mut self.multiplier);
visitor.visit(&mut self.element);
}
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/plc_ast/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ impl Walker for MultipliedStatement {
where
V: AstVisitor,
{
visitor.visit(&self.element)
visitor.visit(&self.multiplier);
visitor.visit(&self.element);
}
}

Expand Down
324 changes: 215 additions & 109 deletions compiler/plc_lowering/src/array_lowering.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions compiler/plc_lowering/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub fn is_const_expression(node: &AstNode, index: Option<&Index>, pou_name: Opti
match node.get_stmt() {
AstStatement::Literal(..) => true,
AstStatement::ExpressionList(exprs) => exprs.iter().all(|e| is_const_expression(e, index, pou_name)),
AstStatement::MultipliedStatement(MultipliedStatement { element, .. }) => {
is_const_expression(element, index, pou_name)
AstStatement::MultipliedStatement(MultipliedStatement { multiplier, element }) => {
is_const_expression(multiplier, index, pou_name) && is_const_expression(element, index, pou_name)
}
AstStatement::ParenExpression(inner) => is_const_expression(inner, index, pou_name),
AstStatement::Identifier(..) | AstStatement::ReferenceExpr(..) => {
Expand Down
Loading
Loading