feat: graph (WIP)

This commit is contained in:
2025-08-15 12:38:11 +08:00
parent d8ad7fe904
commit 3c7722a3d2
7 changed files with 78 additions and 30 deletions

View File

@@ -71,6 +71,8 @@ pub trait ResolveContext {
/// Triggers the resolution of a given expression.
fn resolve(&mut self, expr: ExprId) -> Result<()>;
fn resolve_call(&mut self, func: ExprId, arg: ExprId) -> Result<()>;
fn resolve_root(self, expr: ExprId) -> Result<()>;
/// Looks up a variable by name in the current scope.
@@ -89,7 +91,7 @@ pub trait ResolveContext {
) -> T;
/// Enters a function parameter scope for the duration of a closure.
fn with_param_env<T>(
fn with_closure_env<T>(
&mut self,
func: ExprId,
ident: Option<String>,
@@ -129,7 +131,7 @@ impl<Ctx: ResolveContext> Resolve<Ctx> for hir::Hir {
ctx.resolve(x)?;
Ok(Lir::Thunk(x))
}
Arg(_) => Ok(Lir::StackRef(ctx.lookup_arg()))
Arg(_) => Ok(Lir::StackRef(ctx.lookup_arg())),
}
}
}
@@ -219,17 +221,19 @@ impl<Ctx: ResolveContext> Resolve<Ctx> for If {
/// It then registers the function with the context.
impl<Ctx: ResolveContext> Resolve<Ctx> for Func {
fn resolve(self, ctx: &mut Ctx) -> Result<Lir> {
ctx.with_param_env(self.body, self.param.ident.clone(), |ctx| ctx.resolve(self.body))?;
ctx.with_closure_env(self.body, self.param.ident.clone(), |ctx| {
ctx.resolve(self.body)
})?;
ctx.new_func(self.body, self.param);
Ok(Lir::FuncRef(self.body))
}
}
/// Resolves a `Call` by resolving the function and all of its arguments.
impl<Ctx: ResolveContext> Resolve<Ctx> for Call {
fn resolve(self, ctx: &mut Ctx) -> Result<Lir> {
ctx.resolve(self.func)?;
ctx.resolve(self.arg)?;
ctx.resolve_call(self.func, self.arg)?;
Ok(self.to_lir())
}
}