From a701f99fd6fc5eb21c138312dc93b581329bf50b Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Sun, 22 Sep 2024 18:56:26 -0700 Subject: [PATCH] add repeat() --- crates/fayalite/src/expr.rs | 17 ++++++++++++++++- crates/fayalite/src/prelude.rs | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/fayalite/src/expr.rs b/crates/fayalite/src/expr.rs index 644d058..fa50852 100644 --- a/crates/fayalite/src/expr.rs +++ b/crates/fayalite/src/expr.rs @@ -9,7 +9,7 @@ use crate::{ ops::ExprCastTo, target::{GetTarget, Target}, }, - int::{Bool, DynSize, IntType, SIntType, SIntValue, Size, UInt, UIntType, UIntValue}, + int::{Bool, DynSize, IntType, SIntType, SIntValue, Size, SizeType, UInt, UIntType, UIntValue}, intern::{Intern, Interned}, memory::{DynPortType, MemPort, PortType}, module::{ @@ -720,3 +720,18 @@ impl MakeUninitExpr for T { ops::Uninit::new(self).to_expr() } } + +pub fn repeat( + element: impl ToExpr, + len: L, +) -> Expr> { + let element = element.to_expr(); + let canonical_element = Expr::canonical(element); + ops::ArrayLiteral::new( + Expr::ty(element), + std::iter::repeat(canonical_element) + .take(L::Size::as_usize(len)) + .collect(), + ) + .to_expr() +} diff --git a/crates/fayalite/src/prelude.rs b/crates/fayalite/src/prelude.rs index 5dc503e..7b7d718 100644 --- a/crates/fayalite/src/prelude.rs +++ b/crates/fayalite/src/prelude.rs @@ -7,7 +7,7 @@ pub use crate::{ clock::{Clock, ClockDomain, ToClock}, enum_::{HdlNone, HdlOption, HdlSome}, expr::{ - CastBitsTo, CastTo, CastToBits, Expr, HdlPartialEq, HdlPartialOrd, MakeUninitExpr, + repeat, CastBitsTo, CastTo, CastToBits, Expr, HdlPartialEq, HdlPartialOrd, MakeUninitExpr, ReduceBits, ToExpr, }, hdl, hdl_module,