feat: graph (WIP)
This commit is contained in:
@@ -24,11 +24,11 @@ mod value;
|
||||
pub trait EvalContext {
|
||||
fn eval_root(self, expr: ExprId) -> Result<Value>;
|
||||
|
||||
|
||||
/// Evaluates an expression by its ID.
|
||||
fn eval(&mut self, expr: ExprId) -> Result<Value>;
|
||||
|
||||
fn call(&mut self, func: ExprId, arg: Option<Value>, frame: StackFrame) -> Result<Value>;
|
||||
|
||||
/// Enters a `with` scope for the duration of a closure's execution.
|
||||
fn with_with_env<T>(
|
||||
&mut self,
|
||||
@@ -83,9 +83,15 @@ impl<Ctx: EvalContext> Evaluate<Ctx> for lir::Lir {
|
||||
Str(x) => x.eval(ctx),
|
||||
Var(x) => x.eval(ctx),
|
||||
Path(x) => x.eval(ctx),
|
||||
&StackRef(idx) => Ok(ctx.lookup_stack(idx).clone()),
|
||||
&StackRef(idx) => {
|
||||
let mut val = ctx.lookup_stack(idx).clone();
|
||||
val.force(ctx)?;
|
||||
Ok(val)
|
||||
}
|
||||
&ExprRef(expr) => ctx.eval(expr),
|
||||
&FuncRef(body) => Ok(Value::Closure(Closure::new(body, ctx.capture_stack().clone()).into())),
|
||||
&FuncRef(body) => Ok(Value::Closure(
|
||||
Closure::new(body, ctx.capture_stack().clone()).into(),
|
||||
)),
|
||||
&Arg(_) => unreachable!(),
|
||||
&PrimOp(primop) => Ok(Value::PrimOp(primop)),
|
||||
&Thunk(id) => Ok(Value::Thunk(id)),
|
||||
|
||||
@@ -18,7 +18,11 @@ pub struct Closure {
|
||||
}
|
||||
|
||||
impl Closure {
|
||||
pub fn call<Ctx: EvalContext>(self: Rc<Self>, arg: Option<Value>, ctx: &mut Ctx) -> Result<Value> {
|
||||
pub fn call<Ctx: EvalContext>(
|
||||
self: Rc<Self>,
|
||||
arg: Option<Value>,
|
||||
ctx: &mut Ctx,
|
||||
) -> Result<Value> {
|
||||
let Self { body: func, frame } = Rc::unwrap_or_clone(self);
|
||||
ctx.call(func, arg, frame)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user