From d11b8858bab6b334df3bf4976cb5e31018db6217 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Sat, 12 Nov 2022 17:39:00 -0500 Subject: [PATCH] clean up some unused parts in created hashmaps --- crates/lang/src/uplc.rs | 85 ++++++++++++++++++------------ examples/sample/lib/sample.ak | 2 +- examples/sample/validators/swap.ak | 19 ++++++- 3 files changed, 70 insertions(+), 36 deletions(-) diff --git a/crates/lang/src/uplc.rs b/crates/lang/src/uplc.rs index 1968a2ea..a0830e38 100644 --- a/crates/lang/src/uplc.rs +++ b/crates/lang/src/uplc.rs @@ -84,7 +84,7 @@ impl Default for ScopeLevels { pub struct CodeGenerator<'a> { uplc_function_holder: Vec<(String, Term)>, - uplc_function_holder_lookup: IndexMap<(String, String), (ScopeLevels, TypedExpr)>, + uplc_function_holder_lookup: IndexMap<(String, String), ScopeLevels>, uplc_data_holder_lookup: IndexMap<(String, String, String), (ScopeLevels, TypedExpr)>, uplc_data_constr_lookup: IndexMap<(String, String), ScopeLevels>, uplc_data_usage_holder_lookup: IndexMap<(String, String), ScopeLevels>, @@ -232,6 +232,34 @@ impl<'a> CodeGenerator<'a> { } (ValueConstructorVariant::Record { .. }, Type::App { .. }) => {} (ValueConstructorVariant::Record { .. }, Type::Fn { .. }) => {} + (ValueConstructorVariant::ModuleFn { name, module, .. }, Type::Fn { .. }) => { + if self + .uplc_function_holder_lookup + .get(&(module.to_string(), name.to_string())) + .is_none() + { + let func_def = self + .functions + .get(&(module.to_string(), name.to_string())) + .unwrap(); + + self.recurse_scope_level( + &func_def.body, + scope_level.scope_increment(func_def.arguments.len() as i32 + 1), + ); + + self.uplc_function_holder_lookup + .insert((module, name), scope_level); + } else if scope_level.is_less_than( + self.uplc_function_holder_lookup + .get(&(module.to_string(), name.to_string())) + .unwrap(), + false, + ) { + self.uplc_function_holder_lookup + .insert((module, name), scope_level); + } + } _ => todo!(), }, TypedExpr::Fn { .. } => todo!(), @@ -361,7 +389,7 @@ impl<'a> CodeGenerator<'a> { .insert((module, current_var_name), current_scope); } } - a @ TypedExpr::ModuleSelect { constructor, .. } => match constructor { + TypedExpr::ModuleSelect { constructor, .. } => match constructor { ModuleValueConstructor::Record { .. } => todo!(), ModuleValueConstructor::Fn { module, name, .. } => { if self @@ -379,22 +407,16 @@ impl<'a> CodeGenerator<'a> { scope_level.scope_increment(func_def.arguments.len() as i32 + 1), ); - self.uplc_function_holder_lookup.insert( - (module.to_string(), name.to_string()), - (scope_level, a.clone()), - ); + self.uplc_function_holder_lookup + .insert((module.to_string(), name.to_string()), scope_level); } else if scope_level.is_less_than( - &self - .uplc_function_holder_lookup + self.uplc_function_holder_lookup .get(&(module.to_string(), name.to_string())) - .unwrap() - .0, + .unwrap(), false, ) { - self.uplc_function_holder_lookup.insert( - (module.to_string(), name.to_string()), - (scope_level, a.clone()), - ); + self.uplc_function_holder_lookup + .insert((module.to_string(), name.to_string()), scope_level); } } ModuleValueConstructor::Constant { .. } => todo!(), @@ -424,7 +446,7 @@ impl<'a> CodeGenerator<'a> { // name: constructor_name, tipo, // arguments, - // constructor, + constructor: _constructor, // module, .. } => { @@ -526,7 +548,7 @@ impl<'a> CodeGenerator<'a> { if name == "True" || name == "False" { Term::Constant(Constant::Bool(name == "True")) } else { - match dbg!(constructor.variant.clone()) { + match constructor.variant.clone() { ValueConstructorVariant::LocalVariable { .. } => Term::Var(Name { text: name.to_string(), unique: 0.into(), @@ -1211,14 +1233,11 @@ impl<'a> CodeGenerator<'a> { scope_level: ScopeLevels, ) -> Term { let mut term = current_term; + + // attempt to insert function definitions where needed for func in self.uplc_function_holder_lookup.clone().keys() { if scope_level.is_less_than( - &self - .uplc_function_holder_lookup - .clone() - .get(func) - .unwrap() - .0, + self.uplc_function_holder_lookup.clone().get(func).unwrap(), false, ) { let func_def = self @@ -1370,14 +1389,14 @@ impl<'a> CodeGenerator<'a> { // Pull out all uplc data holder and data usage, filter by Scope Level, Sort By Scope Depth, Then Apply #[allow(clippy::type_complexity)] - let mut data_holder: Vec<((String, String, String), (bool, ScopeLevels, u64))> = self + let mut data_holder: Vec<((String, String, String), (ScopeLevels, i128))> = self .uplc_data_usage_holder_lookup .iter() .filter(|record_scope| scope_level.is_less_than(record_scope.1, false)) .map(|((module, name), scope)| { ( (module.to_string(), name.to_string(), "".to_string()), - (true, scope.clone(), 0), + (scope.clone(), -1), ) }) .collect(); @@ -1393,27 +1412,27 @@ impl<'a> CodeGenerator<'a> { }; ( (module.to_string(), name.to_string(), label.to_string()), - (false, scope.clone(), *index), + (scope.clone(), *index as i128), ) }) - .collect::>(), + .collect::>(), ); - data_holder.sort_by(|b, d| { - if b.1 .1.is_less_than(&d.1 .1, true) { + data_holder.sort_by(|item1, item2| { + if item1.1 .0.is_less_than(&item2.1 .0, true) { Ordering::Less - } else if d.1 .1.is_less_than(&b.1 .1, true) { + } else if item2.1 .0.is_less_than(&item1.1 .0, true) { Ordering::Greater - } else if b.1 .0 && !d.1 .0 { + } else if item1.1 .1 < item2.1 .1 { Ordering::Less - } else if d.1 .0 && !b.1 .0 { + } else if item2.1 .1 < item1.1 .1 { Ordering::Greater } else { Ordering::Equal } }); - for (key @ (module, name, label), (is_data_usage, _, index)) in data_holder.iter().rev() { - if *is_data_usage { + for (key @ (module, name, label), (_, index)) in data_holder.iter().rev() { + if index < &0 { term = Term::Apply { function: Term::Lambda { parameter_name: Name { diff --git a/examples/sample/lib/sample.ak b/examples/sample/lib/sample.ak index 4786bb2d..adf20226 100644 --- a/examples/sample/lib/sample.ak +++ b/examples/sample/lib/sample.ak @@ -1,5 +1,5 @@ pub type Signer { - hash: Int, + hash: ByteArray, } pub type ScriptContext { diff --git a/examples/sample/validators/swap.ak b/examples/sample/validators/swap.ak index f2037edf..098e6e68 100644 --- a/examples/sample/validators/swap.ak +++ b/examples/sample/validators/swap.ak @@ -16,13 +16,28 @@ pub type Reen { } +pub fn twice(f: fn(Int) -> Int, x: Int) -> Int { + f(f(x)) +} + +pub fn add_one(x: Int) -> Int { + x + 1 +} + +pub fn add_two(x: Int) -> Int { + twice(add_one, x) +} + pub fn spend( datum: sample.Datum, rdmr: Redeemer, ctx: spend.ScriptContext, ) -> Bool { - 1 % 2 != 3 || 5 / 4 - 2 != 0 + let x = rdmr.signer + let z = datum.sc.signer.hash + let y = datum.sc + let a = datum.fin + a > 0 && z == x - True }