From 93f7849fc088b12bc3f7f3ad1e5effc6e2b341b8 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Wed, 6 Jul 2022 23:44:24 -0400 Subject: [PATCH] Finish machine evaluation for all terms except builtin Co-authored-by: rvcas --- crates/uplc/src/machine.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 2e746df3..98cae768 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -130,7 +130,7 @@ impl Machine { match value { Value::Con(x) => Term::Constant(x), Value::Builtin(_, t) => t, - Value::Delay(_) => todo!(), + Value::Delay(body) => self.discharge_value_env(Term::Delay(Box::new(body))), Value::Lambda { parameter_name, body, @@ -145,27 +145,37 @@ impl Machine { } fn discharge_value_env(&mut self, term: Term) -> Term { - fn rec(i: u32, t: Term) -> Term { + fn rec(lam_cnt: usize, t: Term, this: &mut Machine) -> Term { match t { - Term::Var(_x) => todo!(), + Term::Var(name) => { + let index: usize = name.index.into(); + if lam_cnt >= index { + Term::Var(name) + } else { + this.env + .get::(index - lam_cnt) + .cloned() + .map_or(Term::Var(name), |v| this.discharge_value(v)) + } + } Term::Lambda { parameter_name, body, } => Term::Lambda { parameter_name, - body: Box::new(rec(i + 1, *body)), + body: Box::new(rec(lam_cnt + 1, *body, this)), }, Term::Apply { function, argument } => Term::Apply { - function: Box::new(rec(i, *function)), - argument: Box::new(rec(i, *argument)), + function: Box::new(rec(lam_cnt, *function, this)), + argument: Box::new(rec(lam_cnt, *argument, this)), }, - Term::Delay(x) => Term::Delay(Box::new(rec(i, *x))), - Term::Force(x) => Term::Force(Box::new(rec(i, *x))), + Term::Delay(x) => Term::Delay(Box::new(rec(lam_cnt, *x, this))), + Term::Force(x) => Term::Force(Box::new(rec(lam_cnt, *x, this))), rest => rest, } } - rec(0, term) + rec(0, term, self) } fn force_evaluate(&mut self, value: Value) -> Result, Error> {