add FromIterator for Block/Module and make code succinct

This commit is contained in:
Yuna Tomida 2022-11-08 01:54:10 +09:00
parent 14b143778f
commit be5a79a4b0
No known key found for this signature in database
GPG key ID: 3446B3EBE0190A4E
2 changed files with 22 additions and 18 deletions

View file

@ -1124,6 +1124,12 @@ impl Locational for Block {
} }
} }
impl FromIterator<Expr> for Block {
fn from_iter<T: IntoIterator<Item = Expr>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl_stream_for_wrapper!(Block, Expr); impl_stream_for_wrapper!(Block, Expr);
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, PartialEq, Eq, Hash)]
@ -3438,6 +3444,12 @@ impl Stream<Expr> for Module {
} }
} }
impl FromIterator<Expr> for Module {
fn from_iter<T: IntoIterator<Item = Expr>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl Module { impl Module {
pub const fn empty() -> Self { pub const fn empty() -> Self {
Self(Block::empty()) Self(Block::empty())

View file

@ -59,6 +59,10 @@ impl Desugarer {
module module
} }
fn desugar_all_chunks(module: Module, desugar: impl Fn(Expr) -> Expr) -> Module {
module.into_iter().map(desugar).collect()
}
fn perform_desugar(desugar: impl Fn(Expr) -> Expr, expr: Expr) -> Expr { fn perform_desugar(desugar: impl Fn(Expr) -> Expr, expr: Expr) -> Expr {
match expr { match expr {
Expr::Record(record) => match record { Expr::Record(record) => match record {
@ -541,12 +545,8 @@ impl Desugarer {
} }
/// `{x; y}` -> `{x = x; y = y}` /// `{x; y}` -> `{x = x; y = y}`
fn desugar_shortened_record(mut module: Module) -> Module { fn desugar_shortened_record(module: Module) -> Module {
let mut new = Module::with_capacity(module.len()); Self::desugar_all_chunks(module, Self::rec_desugar_shortened_record)
while let Some(chunk) = module.lpop() {
new.push(Self::rec_desugar_shortened_record(chunk));
}
new
} }
fn rec_desugar_shortened_record(expr: Expr) -> Expr { fn rec_desugar_shortened_record(expr: Expr) -> Expr {
@ -859,12 +859,8 @@ impl Desugarer {
} }
} }
fn desugar_self(mut module: Module) -> Module { fn desugar_self(module: Module) -> Module {
let mut new = Module::with_capacity(module.len()); Self::desugar_all_chunks(module, Self::desugar_self_inner)
while let Some(chunk) = module.lpop() {
new.push(Self::desugar_self_inner(chunk));
}
new
} }
fn desugar_self_inner(_expr: Expr) -> Expr { fn desugar_self_inner(_expr: Expr) -> Expr {
@ -878,12 +874,8 @@ impl Desugarer {
/// x[y] => x.__getitem__(y) /// x[y] => x.__getitem__(y)
/// x.0 => x.__Tuple_getitem__(0) /// x.0 => x.__Tuple_getitem__(0)
fn desugar_acc(mut module: Module) -> Module { fn desugar_acc(module: Module) -> Module {
let mut new = Module::with_capacity(module.len()); Self::desugar_all_chunks(module, Self::rec_desugar_acc)
while let Some(chunk) = module.lpop() {
new.push(Self::rec_desugar_acc(chunk));
}
new
} }
fn rec_desugar_acc(expr: Expr) -> Expr { fn rec_desugar_acc(expr: Expr) -> Expr {