refactor: with_thunk_scope
This commit is contained in:
@@ -518,7 +518,7 @@ impl DowngradeContext for DowngradeCtx<'_> {
|
|||||||
fn downgrade(mut self, root: rnix::ast::Expr) -> Result<ExprId> {
|
fn downgrade(mut self, root: rnix::ast::Expr) -> Result<ExprId> {
|
||||||
use crate::ir::TopLevel;
|
use crate::ir::TopLevel;
|
||||||
let body = root.downgrade(&mut self)?;
|
let body = root.downgrade(&mut self)?;
|
||||||
let thunks = self.pop_thunk_scope();
|
let thunks = self.thunk_scopes.pop().expect("no thunk scope left???");
|
||||||
let span = self.get_ir(body).span();
|
let span = self.get_ir(body).span();
|
||||||
let top_level = self.new_expr(TopLevel { body, thunks, span }.to_ir());
|
let top_level = self.new_expr(TopLevel { body, thunks, span }.to_ir());
|
||||||
self.ctx.irs.extend(self.irs);
|
self.ctx.irs.extend(self.irs);
|
||||||
@@ -553,14 +553,16 @@ impl DowngradeContext for DowngradeCtx<'_> {
|
|||||||
f(guard.as_ctx())
|
f(guard.as_ctx())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_thunk_scope(&mut self) {
|
fn with_thunk_scope<F, R>(&mut self, f: F) -> (R, Vec<(ExprId, ExprId)>)
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut Self) -> R,
|
||||||
|
{
|
||||||
self.thunk_scopes.push(Vec::new());
|
self.thunk_scopes.push(Vec::new());
|
||||||
}
|
let ret = f(self);
|
||||||
|
(
|
||||||
fn pop_thunk_scope(&mut self) -> Vec<(ExprId, ExprId)> {
|
ret,
|
||||||
self.thunk_scopes
|
self.thunk_scopes.pop().expect("no thunk scope left???"),
|
||||||
.pop()
|
)
|
||||||
.expect("pop_thunk_scope without active scope")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register_thunk(&mut self, slot: ExprId, inner: ExprId) {
|
fn register_thunk(&mut self, slot: ExprId, inner: ExprId) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ pub trait DowngradeContext {
|
|||||||
fn new_expr(&mut self, expr: Ir) -> ExprId;
|
fn new_expr(&mut self, expr: Ir) -> ExprId;
|
||||||
fn new_arg(&mut self, span: TextRange) -> ExprId;
|
fn new_arg(&mut self, span: TextRange) -> ExprId;
|
||||||
fn maybe_thunk(&mut self, id: ExprId) -> ExprId;
|
fn maybe_thunk(&mut self, id: ExprId) -> ExprId;
|
||||||
|
fn register_thunk(&mut self, slot: ExprId, inner: ExprId);
|
||||||
|
|
||||||
fn new_sym(&mut self, sym: String) -> SymId;
|
fn new_sym(&mut self, sym: String) -> SymId;
|
||||||
fn get_sym(&self, id: SymId) -> Symbol<'_>;
|
fn get_sym(&self, id: SymId) -> Symbol<'_>;
|
||||||
@@ -37,10 +38,9 @@ pub trait DowngradeContext {
|
|||||||
fn with_with_scope<F, R>(&mut self, namespace: ExprId, f: F) -> R
|
fn with_with_scope<F, R>(&mut self, namespace: ExprId, f: F) -> R
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self) -> R;
|
F: FnOnce(&mut Self) -> R;
|
||||||
|
fn with_thunk_scope<F, R>(&mut self, f: F) -> (R, Vec<(ExprId, ExprId)>)
|
||||||
fn push_thunk_scope(&mut self);
|
where
|
||||||
fn pop_thunk_scope(&mut self) -> Vec<(ExprId, ExprId)>;
|
F: FnOnce(&mut Self) -> R;
|
||||||
fn register_thunk(&mut self, slot: ExprId, inner: ExprId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Downgrade<Ctx: DowngradeContext> {
|
pub trait Downgrade<Ctx: DowngradeContext> {
|
||||||
@@ -419,8 +419,12 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Lambda {
|
|||||||
let raw_param = self.param().unwrap();
|
let raw_param = self.param().unwrap();
|
||||||
let arg = ctx.new_arg(raw_param.syntax().text_range());
|
let arg = ctx.new_arg(raw_param.syntax().text_range());
|
||||||
|
|
||||||
ctx.push_thunk_scope();
|
struct Ret {
|
||||||
|
param: Option<Param>,
|
||||||
|
body: ExprId,
|
||||||
|
}
|
||||||
|
|
||||||
|
let (ret, thunks) = ctx.with_thunk_scope(|ctx| {
|
||||||
let param;
|
let param;
|
||||||
let body;
|
let body;
|
||||||
|
|
||||||
@@ -431,8 +435,9 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Lambda {
|
|||||||
param = None;
|
param = None;
|
||||||
|
|
||||||
// Downgrade body in Param scope
|
// Downgrade body in Param scope
|
||||||
body = ctx
|
body = ctx.with_param_scope(param_sym, arg, |ctx| {
|
||||||
.with_param_scope(param_sym, arg, |ctx| self.body().unwrap().downgrade(ctx))?;
|
self.body().unwrap().downgrade(ctx)
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
ast::Param::Pattern(pattern) => {
|
ast::Param::Pattern(pattern) => {
|
||||||
let alias = pattern
|
let alias = pattern
|
||||||
@@ -460,7 +465,10 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Lambda {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let thunks = ctx.pop_thunk_scope();
|
Result::Ok(Ret { param, body })
|
||||||
|
});
|
||||||
|
let Ret { param, body } = ret?;
|
||||||
|
|
||||||
let span = self.syntax().text_range();
|
let span = self.syntax().text_range();
|
||||||
Ok(ctx.new_expr(
|
Ok(ctx.new_expr(
|
||||||
Func {
|
Func {
|
||||||
|
|||||||
Reference in New Issue
Block a user