remove some unnecessary clones with more optimizations to come

This commit is contained in:
Kasey White 2022-08-15 00:20:47 -04:00 committed by Lucas
parent c3f0814c9b
commit c050a5647f
1 changed files with 13 additions and 16 deletions

View File

@ -63,7 +63,7 @@ impl Machine {
while let Some(step) = self.stack.pop() { while let Some(step) = self.stack.pop() {
match step { match step {
Compute(context, env, t) => { Compute(context, env, t) => {
self.compute(context, env, &t)?; self.compute(context, env, t)?;
} }
Return(context, value) => { Return(context, value) => {
self.return_compute(context, value)?; self.return_compute(context, value)?;
@ -83,23 +83,21 @@ impl Machine {
&mut self, &mut self,
context: Context, context: Context,
env: Vec<Value>, env: Vec<Value>,
term: &Term<NamedDeBruijn>, term: Term<NamedDeBruijn>,
) -> Result<(), Error> { ) -> Result<(), Error> {
match term { match term {
Term::Var(name) => { Term::Var(name) => {
self.step_and_maybe_spend(StepKind::Var)?; self.step_and_maybe_spend(StepKind::Var)?;
let val = self.lookup_var(name.clone(), env)?; let val = self.lookup_var(name, env)?;
self.stack.push(MachineStep::Return(context, val)); self.stack.push(MachineStep::Return(context, val));
} }
Term::Delay(body) => { Term::Delay(body) => {
self.step_and_maybe_spend(StepKind::Delay)?; self.step_and_maybe_spend(StepKind::Delay)?;
self.stack.push(MachineStep::Return( self.stack
context, .push(MachineStep::Return(context, Value::Delay(*body, env)));
Value::Delay(*body.clone(), env),
));
} }
Term::Lambda { Term::Lambda {
parameter_name, parameter_name,
@ -110,8 +108,8 @@ impl Machine {
self.stack.push(MachineStep::Return( self.stack.push(MachineStep::Return(
context, context,
Value::Lambda { Value::Lambda {
parameter_name: parameter_name.clone(), parameter_name,
body: *body.clone(), body: *body,
env, env,
}, },
)); ));
@ -120,16 +118,15 @@ 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.clone(), Box::new(context)), Context::FrameApplyArg(env.clone(), *argument, Box::new(context)),
env, env,
*function.clone(), *function,
)); ));
} }
Term::Constant(x) => { Term::Constant(x) => {
self.step_and_maybe_spend(StepKind::Constant)?; self.step_and_maybe_spend(StepKind::Constant)?;
self.stack self.stack.push(MachineStep::Return(context, Value::Con(x)));
.push(MachineStep::Return(context, Value::Con(x.clone())));
} }
Term::Force(body) => { Term::Force(body) => {
self.step_and_maybe_spend(StepKind::Force)?; self.step_and_maybe_spend(StepKind::Force)?;
@ -137,19 +134,19 @@ impl Machine {
self.stack.push(MachineStep::Compute( self.stack.push(MachineStep::Compute(
Context::FrameForce(Box::new(context)), Context::FrameForce(Box::new(context)),
env, env,
*body.clone(), *body,
)); ));
} }
Term::Error => return Err(Error::EvaluationFailure), Term::Error => return Err(Error::EvaluationFailure),
Term::Builtin(fun) => { Term::Builtin(fun) => {
self.step_and_maybe_spend(StepKind::Builtin)?; self.step_and_maybe_spend(StepKind::Builtin)?;
let runtime: BuiltinRuntime = (*fun).into(); let runtime: BuiltinRuntime = (fun).into();
self.stack.push(MachineStep::Return( self.stack.push(MachineStep::Return(
context, context,
Value::Builtin { Value::Builtin {
fun: *fun, fun,
term: term.clone(), term: term.clone(),
runtime, runtime,
}, },