Skip to content

Commit

Permalink
reduce tuple-proj
Browse files Browse the repository at this point in the history
  • Loading branch information
KeenS committed May 30, 2022
1 parent 0b6c229 commit 0d5743e
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/hir/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,55 @@ impl Transform for Simplify {
}
}

fn transform_proj(&mut self, ty: HTy, index: u32, tuple: Box<Expr>) -> Expr {
match self.transform_expr(*tuple) {
Expr::Tuple { tys, mut tuple } => {
let tuple_var = self.gensym();
let tuple_ty = HTy::Tuple(tys.clone());
let nth_var = self.gensym();
let nth_ty = tys[index as usize].clone();
let nth_expr = std::mem::replace(
&mut tuple[index as usize],
Expr::Sym {
ty: nth_ty.clone(),
name: nth_var.clone(),
},
);
Expr::Let {
ty: ty.clone(),
bind: Box::new(Val {
ty: nth_ty,
rec: false,
name: nth_var,
expr: nth_expr,
}),
ret: Box::new(Expr::Let {
ty: ty.clone(),
bind: Box::new(Val {
ty: tuple_ty.clone(),
rec: false,
name: tuple_var.clone(),
expr: Expr::Tuple { tys, tuple },
}),
ret: Box::new(Expr::Proj {
ty,
index,
tuple: Box::new(Expr::Sym {
ty: tuple_ty,
name: tuple_var,
}),
}),
}),
}
}
tuple => Expr::Proj {
ty,
index,
tuple: Box::new(tuple),
},
}
}

fn transform_sym(&mut self, ty: HTy, name: Symbol) -> Expr {
match self.aliases.get(&name) {
Some(alias) => Sym {
Expand Down

0 comments on commit 0d5743e

Please sign in to comment.