From 0be5229f1c30bde7ec50e1e6430a9f01a705ef7a Mon Sep 17 00:00:00 2001 From: microproofs Date: Mon, 29 Jul 2024 14:45:54 -0400 Subject: [PATCH] Only 2 errors and todos left to finish --- crates/aiken-lang/src/gen_uplc.rs | 100 ++++++++++++---------- crates/aiken-lang/src/gen_uplc/builder.rs | 15 ++-- crates/uplc/src/optimize/shrinker.rs | 2 +- 3 files changed, 65 insertions(+), 52 deletions(-) diff --git a/crates/aiken-lang/src/gen_uplc.rs b/crates/aiken-lang/src/gen_uplc.rs index 86994e57..9eb9c844 100644 --- a/crates/aiken-lang/src/gen_uplc.rs +++ b/crates/aiken-lang/src/gen_uplc.rs @@ -38,7 +38,7 @@ use crate::{ }, IdGenerator, }; -use builder::unknown_data_to_type; +use builder::{softcast_data_to_type_otherwise, unknown_data_to_type}; use indexmap::{IndexMap, IndexSet}; use itertools::Itertools; use petgraph::{algo, Graph}; @@ -656,7 +656,10 @@ impl<'a> CodeGenerator<'a> { kind: AssignmentKind::Expect { backpassing: () }, remove_unused: false, full_check: true, - otherwise: AirTree::local_var("acc_var", void()), + otherwise: Some(AirTree::local_var( + "acc_var", + tipo.clone(), + )), }, ), ) @@ -3216,17 +3219,17 @@ impl<'a> CodeGenerator<'a> { let msg_func_name = msg.split_whitespace().join(""); - self.special_functions.insert_new_function( - msg_func_name.clone(), - if msg.is_empty() { - Term::Error.delay() - } else { - Term::Error.delayed_trace(Term::string(msg)).delay() - }, - void(), - ); + if msg.is_empty() { + None + } else { + self.special_functions.insert_new_function( + msg_func_name.clone(), + Term::Error.delayed_trace(Term::string(msg)).delay(), + void(), + ); - self.special_functions.use_function_tree(msg_func_name) + Some(self.special_functions.use_function_tree(msg_func_name)) + } }; let inner_then = self.assignment( @@ -4186,14 +4189,6 @@ impl<'a> CodeGenerator<'a> { } fn gen_uplc(&mut self, ir: Air, arg_stack: &mut Vec>) -> Option> { - let convert_data_to_type = |term, tipo, otherwise| { - if otherwise == Term::Error.delay() { - builder::unknown_data_to_type(term, tipo) - } else { - builder::softcast_data_to_type_otherwise(term, tipo, otherwise) - } - }; - match ir { Air::Int { value } => Some(Term::integer(value.parse().unwrap())), Air::String { value } => Some(Term::string(value)), @@ -4911,7 +4906,7 @@ impl<'a> CodeGenerator<'a> { known_data_to_type(term, &tipo) }; - if extract_constant(&term).is_some() { + if extract_constant(&term.pierce_no_inlines()).is_some() { let mut program: Program = Program { version: (1, 0, 0), term, @@ -5738,33 +5733,45 @@ impl<'a> CodeGenerator<'a> { let list_id = self.id_gen.next(); if let Some(name) = snd { - term = term.lambda(name).apply(if is_expect { - convert_data_to_type( - Term::snd_pair().apply(Term::var(format!("__pair_{list_id}"))), - &inner_types[1], - otherwise.clone(), - ) + let value = Term::snd_pair().apply(Term::var(format!("__pair_{list_id}"))); + term = if is_expect { + if otherwise == Term::Error.delay() { + term.lambda(name) + .apply(unknown_data_to_type(value, &inner_types[1])) + } else { + softcast_data_to_type_otherwise( + value, + &name, + &inner_types[1], + term, + otherwise.clone(), + ) + } } else { - known_data_to_type( - Term::snd_pair().apply(Term::var(format!("__pair_{list_id}"))), - &inner_types[1], - ) - }); + term.lambda(name) + .apply(known_data_to_type(value, &inner_types[1])) + } } if let Some(name) = fst { - term = term.lambda(name).apply(if is_expect { - convert_data_to_type( - Term::fst_pair().apply(Term::var(format!("__pair_{list_id}"))), - &inner_types[0], - otherwise, - ) + let value = Term::fst_pair().apply(Term::var(format!("__pair_{list_id}"))); + term = if is_expect { + if otherwise == Term::Error.delay() { + term.lambda(name) + .apply(unknown_data_to_type(value, &inner_types[0])) + } else { + softcast_data_to_type_otherwise( + value, + &name, + &inner_types[0], + term, + otherwise, + ) + } } else { - known_data_to_type( - Term::fst_pair().apply(Term::var(format!("__pair_{list_id}"))), - &inner_types[0], - ) - }) + term.lambda(name) + .apply(known_data_to_type(value, &inner_types[0])) + } } term = term.lambda(format!("__pair_{list_id}")).apply(value); @@ -5807,6 +5814,13 @@ impl<'a> CodeGenerator<'a> { Some(term) } + Air::SoftCastLet { name, tipo } => { + let value = arg_stack.pop().unwrap(); + let then = arg_stack.pop().unwrap(); + let otherwise = arg_stack.pop().unwrap(); + + todo!() + } } } } diff --git a/crates/aiken-lang/src/gen_uplc/builder.rs b/crates/aiken-lang/src/gen_uplc/builder.rs index f5931813..5a5b801b 100644 --- a/crates/aiken-lang/src/gen_uplc/builder.rs +++ b/crates/aiken-lang/src/gen_uplc/builder.rs @@ -858,8 +858,8 @@ pub fn known_data_to_type(term: Term, field_type: &Type) -> Term { match uplc_type { Some(UplcType::Integer) => Term::un_i_data().apply(term), Some(UplcType::ByteString) => Term::un_b_data().apply(term), - Some(UplcType::Bool) => Term::less_than_integer() - .apply(Term::integer(0.into())) + Some(UplcType::Bool) => Term::equals_integer() + .apply(Term::integer(1.into())) .apply(Term::fst_pair().apply(Term::unconstr_data().apply(term))), Some(UplcType::String) => Term::decode_utf8().apply(Term::un_b_data().apply(term)), Some(UplcType::Unit) => Term::unit().lambda("_").apply(term), @@ -915,15 +915,14 @@ pub fn unknown_data_to_type(term: Term, field_type: &Type) -> Term { Some(UplcType::Bool) => Term::snd_pair() .apply(Term::var("__pair__")) .delayed_choose_list( - Term::equals_integer() - .apply(Term::integer(1.into())) + Term::less_than_equals_integer() + .apply(Term::integer(2.into())) .apply(Term::fst_pair().apply(Term::var("__pair__"))) .delayed_if_then_else( - Term::bool(true), + Term::Error, Term::equals_integer() - .apply(Term::integer(0.into())) - .apply(Term::fst_pair().apply(Term::var("__pair__"))) - .delayed_if_then_else(Term::bool(false), Term::Error), + .apply(Term::integer(1.into())) + .apply(Term::fst_pair().apply(Term::var("__pair__"))), ), Term::Error, ) diff --git a/crates/uplc/src/optimize/shrinker.rs b/crates/uplc/src/optimize/shrinker.rs index fe042bce..b30bdf1d 100644 --- a/crates/uplc/src/optimize/shrinker.rs +++ b/crates/uplc/src/optimize/shrinker.rs @@ -988,7 +988,7 @@ impl Term { } } - fn pierce_no_inlines(&self) -> &Self { + pub fn pierce_no_inlines(&self) -> &Self { let mut term = self; while let Term::Lambda {