start changing machine to use rc instead of box (does compile)

This commit is contained in:
Kasey White 2022-08-15 00:51:12 -04:00 committed by Lucas
parent c050a5647f
commit fb81955f51
1 changed files with 23 additions and 23 deletions

View File

@ -1,3 +1,5 @@
use std::rc::Rc;
use crate::{ use crate::{
ast::{Constant, NamedDeBruijn, Term, Type}, ast::{Constant, NamedDeBruijn, Term, Type},
builtins::DefaultFunction, builtins::DefaultFunction,
@ -13,8 +15,8 @@ pub use error::Error;
use self::{cost_model::CostModel, runtime::BuiltinRuntime}; use self::{cost_model::CostModel, runtime::BuiltinRuntime};
enum MachineStep { enum MachineStep {
Return(Context, Value), Return(Rc<Context>, Value),
Compute(Context, Vec<Value>, Term<NamedDeBruijn>), Compute(Rc<Context>, Vec<Value>, Term<NamedDeBruijn>),
Done(Term<NamedDeBruijn>), Done(Term<NamedDeBruijn>),
} }
@ -58,7 +60,7 @@ impl Machine {
self.spend_budget(startup_budget)?; self.spend_budget(startup_budget)?;
self.stack self.stack
.push(Compute(Context::NoFrame, vec![], term.clone())); .push(Compute(Rc::new(Context::NoFrame), vec![], term.clone()));
while let Some(step) = self.stack.pop() { while let Some(step) = self.stack.pop() {
match step { match step {
@ -81,7 +83,7 @@ impl Machine {
fn compute( fn compute(
&mut self, &mut self,
context: Context, context: Rc<Context>,
env: Vec<Value>, env: Vec<Value>,
term: Term<NamedDeBruijn>, term: Term<NamedDeBruijn>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -118,7 +120,7 @@ impl Machine {
self.step_and_maybe_spend(StepKind::Apply)?; self.step_and_maybe_spend(StepKind::Apply)?;
self.stack.push(MachineStep::Compute( self.stack.push(MachineStep::Compute(
Context::FrameApplyArg(env.clone(), *argument, Box::new(context)), Rc::new(Context::FrameApplyArg(env.clone(), *argument, context)),
env, env,
*function, *function,
)); ));
@ -132,7 +134,7 @@ impl Machine {
self.step_and_maybe_spend(StepKind::Force)?; self.step_and_maybe_spend(StepKind::Force)?;
self.stack.push(MachineStep::Compute( self.stack.push(MachineStep::Compute(
Context::FrameForce(Box::new(context)), Rc::new(Context::FrameForce(context)),
env, env,
*body, *body,
)); ));
@ -145,11 +147,7 @@ impl Machine {
self.stack.push(MachineStep::Return( self.stack.push(MachineStep::Return(
context, context,
Value::Builtin { Value::Builtin { fun, term, runtime },
fun,
term: term.clone(),
runtime,
},
)); ));
} }
}; };
@ -157,17 +155,19 @@ impl Machine {
Ok(()) Ok(())
} }
fn return_compute(&mut self, context: Context, value: Value) -> Result<(), Error> { fn return_compute(&mut self, context: Rc<Context>, value: Value) -> Result<(), Error> {
match context { match context.as_ref() {
Context::FrameApplyFun(function, ctx) => self.apply_evaluate(*ctx, function, value)?, Context::FrameApplyFun(function, ctx) => {
self.apply_evaluate(ctx.to_owned(), function.to_owned(), value)?
}
Context::FrameApplyArg(arg_var_env, arg, ctx) => { Context::FrameApplyArg(arg_var_env, arg, ctx) => {
self.stack.push(MachineStep::Compute( self.stack.push(MachineStep::Compute(
Context::FrameApplyFun(value, ctx), Rc::new(Context::FrameApplyFun(value, ctx.to_owned())),
arg_var_env, arg_var_env.to_owned(),
arg, arg.to_owned(),
)); ));
} }
Context::FrameForce(ctx) => self.force_evaluate(*ctx, value)?, Context::FrameForce(ctx) => self.force_evaluate(ctx.to_owned(), value)?,
Context::NoFrame => { Context::NoFrame => {
if self.unbudgeted_steps[7] > 0 { if self.unbudgeted_steps[7] > 0 {
self.spend_unbudgeted_steps()?; self.spend_unbudgeted_steps()?;
@ -246,7 +246,7 @@ impl Machine {
rec(0, term, self, &env) rec(0, term, self, &env)
} }
fn force_evaluate(&mut self, context: Context, value: Value) -> Result<(), Error> { fn force_evaluate(&mut self, context: Rc<Context>, value: Value) -> Result<(), Error> {
match value { match value {
Value::Delay(body, env) => { Value::Delay(body, env) => {
self.stack.push(MachineStep::Compute(context, env, body)); self.stack.push(MachineStep::Compute(context, env, body));
@ -278,7 +278,7 @@ impl Machine {
fn apply_evaluate( fn apply_evaluate(
&mut self, &mut self,
context: Context, context: Rc<Context>,
function: Value, function: Value,
argument: Value, argument: Value,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -386,9 +386,9 @@ impl Machine {
#[derive(Clone)] #[derive(Clone)]
enum Context { enum Context {
FrameApplyFun(Value, Box<Context>), FrameApplyFun(Value, Rc<Context>),
FrameApplyArg(Vec<Value>, Term<NamedDeBruijn>, Box<Context>), FrameApplyArg(Vec<Value>, Term<NamedDeBruijn>, Rc<Context>),
FrameForce(Box<Context>), FrameForce(Rc<Context>),
NoFrame, NoFrame,
} }