chore: rename unwrapData and wrapData

add validator cast function for extra validator params
This commit is contained in:
microproofs 2023-07-25 12:40:17 -04:00 committed by Kasey
parent 55dd1a1a56
commit 18ea44adb0
4 changed files with 51 additions and 31 deletions

View File

@ -119,9 +119,12 @@ impl<'a> CodeGenerator<'a> {
// optimizations on air tree
let full_vec = full_tree.to_vec();
println!("FULL VEC {:#?}", full_vec);
let mut term = self.uplc_code_gen(full_vec);
if let Some(other) = other_fun {}
todo!()
}
@ -241,7 +244,7 @@ impl<'a> CodeGenerator<'a> {
let mut arg_val = self.build(&arg.value);
if arg_tipo.is_data() && !arg.value.tipo().is_data() {
arg_val = AirTree::wrap_data(arg_val, arg.value.tipo())
arg_val = AirTree::cast_to_data(arg_val, arg.value.tipo())
}
arg_val
})
@ -273,7 +276,7 @@ impl<'a> CodeGenerator<'a> {
let mut arg_val = self.build(&arg.value);
if arg_tipo.is_data() && !arg.value.tipo().is_data() {
arg_val = AirTree::wrap_data(arg_val, arg.value.tipo())
arg_val = AirTree::cast_to_data(arg_val, arg.value.tipo())
}
arg_val
})
@ -295,7 +298,7 @@ impl<'a> CodeGenerator<'a> {
let mut arg_val = self.build(&arg.value);
if arg_tipo.is_data() && !arg.value.tipo().is_data() {
arg_val = AirTree::wrap_data(arg_val, arg.value.tipo())
arg_val = AirTree::cast_to_data(arg_val, arg.value.tipo())
}
arg_val
})
@ -558,9 +561,9 @@ impl<'a> CodeGenerator<'a> {
props: AssignmentProperties,
) -> AirTree {
if props.value_type.is_data() && props.kind.is_expect() && !tipo.is_data() {
value = AirTree::unwrap_data(value, tipo.clone());
value = AirTree::cast_from_data(value, tipo.clone());
} else if !props.value_type.is_data() && tipo.is_data() {
value = AirTree::wrap_data(value, tipo.clone());
value = AirTree::cast_to_data(value, tipo.clone());
}
match pattern {
@ -2802,7 +2805,7 @@ impl<'a> CodeGenerator<'a> {
);
}
fn uplc_code_gen(&mut self, ir_stack: &mut Vec<Air>) -> Term<Name> {
fn uplc_code_gen(&mut self, mut ir_stack: Vec<Air>) -> Term<Name> {
let mut arg_stack: Vec<Term<Name>> = vec![];
while let Some(ir_element) = ir_stack.pop() {
@ -3128,7 +3131,7 @@ impl<'a> CodeGenerator<'a> {
let name_module = format!("{module_name}_{function_name}");
let name = function_name.to_string();
if text == &name || text == &name_module {
let mut term = self.uplc_code_gen(&mut ir.clone());
let mut term = self.uplc_code_gen(ir.clone());
term = term
.constr_get_field()
.constr_fields_exposer()
@ -3391,14 +3394,14 @@ impl<'a> CodeGenerator<'a> {
arg_stack.push(term);
}
Air::UnWrapData { tipo, .. } => {
Air::CastFromData { tipo, .. } => {
let mut term = arg_stack.pop().unwrap();
term = builder::convert_data_to_type(term, &tipo);
arg_stack.push(term);
}
Air::WrapData { tipo, .. } => {
Air::CastToData { tipo, .. } => {
let mut term = arg_stack.pop().unwrap();
term = builder::convert_type_to_data(term, &tipo);

View File

@ -70,10 +70,10 @@ pub enum Air {
Let {
name: String,
},
UnWrapData {
CastFromData {
tipo: Arc<Type>,
},
WrapData {
CastToData {
tipo: Arc<Type>,
},
AssertConstr {

View File

@ -14,7 +14,7 @@ use uplc::{
};
use crate::{
ast::{AssignmentKind, DataType, Pattern, Span, TypedClause, TypedDataType},
ast::{AssignmentKind, DataType, Pattern, Span, TypedArg, TypedClause, TypedDataType},
builtins::bool,
expr::TypedExpr,
tipo::{PatternConstructor, TypeVar, ValueConstructor, ValueConstructorVariant},
@ -1357,3 +1357,20 @@ pub fn get_list_elements_len_and_tail(
None
}
}
pub fn cast_validator_args(term: Term<Name>, arguments: &[TypedArg]) -> Term<Name> {
let mut term = term;
for arg in arguments.iter().rev() {
if !matches!(arg.tipo.get_uplc_type(), UplcType::Data) {
term = term
.lambda(arg.arg_name.get_variable_name().unwrap_or("_"))
.apply(convert_data_to_type(
Term::var(arg.arg_name.get_variable_name().unwrap_or("_")),
&arg.tipo,
));
}
term = term.lambda(arg.arg_name.get_variable_name().unwrap_or("_"))
}
term
}

View File

@ -248,11 +248,11 @@ pub enum AirExpression {
arg: Box<AirTree>,
},
UnWrapData {
CastFromData {
tipo: Arc<Type>,
value: Box<AirTree>,
},
WrapData {
CastToData {
tipo: Arc<Type>,
value: Box<AirTree>,
},
@ -478,14 +478,14 @@ impl AirTree {
hoisted_over: None,
}
}
pub fn unwrap_data(value: AirTree, tipo: Arc<Type>) -> AirTree {
AirTree::Expression(AirExpression::UnWrapData {
pub fn cast_from_data(value: AirTree, tipo: Arc<Type>) -> AirTree {
AirTree::Expression(AirExpression::CastFromData {
tipo,
value: value.into(),
})
}
pub fn wrap_data(value: AirTree, tipo: Arc<Type>) -> AirTree {
AirTree::Expression(AirExpression::WrapData {
pub fn cast_to_data(value: AirTree, tipo: Arc<Type>) -> AirTree {
AirTree::Expression(AirExpression::CastToData {
tipo,
value: value.into(),
})
@ -1066,12 +1066,12 @@ impl AirTree {
air_vec.push(Air::UnOp { op: *op });
arg.create_air_vec(air_vec);
}
AirExpression::UnWrapData { tipo, value } => {
air_vec.push(Air::UnWrapData { tipo: tipo.clone() });
AirExpression::CastFromData { tipo, value } => {
air_vec.push(Air::CastFromData { tipo: tipo.clone() });
value.create_air_vec(air_vec);
}
AirExpression::WrapData { tipo, value } => {
air_vec.push(Air::WrapData { tipo: tipo.clone() });
AirExpression::CastToData { tipo, value } => {
air_vec.push(Air::CastToData { tipo: tipo.clone() });
value.create_air_vec(air_vec);
}
AirExpression::When {
@ -1254,7 +1254,7 @@ impl AirTree {
| AirExpression::Call { tipo, .. }
| AirExpression::Builtin { tipo, .. }
| AirExpression::BinOp { tipo, .. }
| AirExpression::UnWrapData { tipo, .. }
| AirExpression::CastFromData { tipo, .. }
| AirExpression::When { tipo, .. }
| AirExpression::If { tipo, .. }
| AirExpression::Constr { tipo, .. }
@ -1270,7 +1270,7 @@ impl AirTree {
UnOp::Not => bool(),
UnOp::Negate => int(),
},
AirExpression::WrapData { .. } => data(),
AirExpression::CastToData { .. } => data(),
AirExpression::Clause { then, .. }
| AirExpression::ListClause { then, .. }
| AirExpression::WrapClause { then, .. }
@ -1303,8 +1303,8 @@ impl AirTree {
| AirExpression::Tuple { tipo, .. }
| AirExpression::Call { tipo, .. }
| AirExpression::Builtin { tipo, .. }
| AirExpression::UnWrapData { tipo, .. }
| AirExpression::WrapData { tipo, .. }
| AirExpression::CastFromData { tipo, .. }
| AirExpression::CastToData { tipo, .. }
| AirExpression::If { tipo, .. }
| AirExpression::RecordUpdate { tipo, .. }
| AirExpression::RecordAccess { tipo, .. }
@ -1523,7 +1523,7 @@ impl AirTree {
with,
);
}
AirExpression::UnWrapData { value, .. } => {
AirExpression::CastFromData { value, .. } => {
value.do_traverse_tree_with(
tree_path,
current_depth + 1,
@ -1531,7 +1531,7 @@ impl AirTree {
with,
);
}
AirExpression::WrapData { value, .. } => {
AirExpression::CastToData { value, .. } => {
value.do_traverse_tree_with(
tree_path,
current_depth + 1,
@ -1912,14 +1912,14 @@ impl AirTree {
panic!("Tree Path index outside tree children nodes")
}
}
AirExpression::UnWrapData { value, .. } => {
AirExpression::CastFromData { value, .. } => {
if *index == 0 {
value.as_mut().do_find_air_tree_node(tree_path_iter)
} else {
panic!("Tree Path index outside tree children nodes")
}
}
AirExpression::WrapData { value, .. } => {
AirExpression::CastToData { value, .. } => {
if *index == 0 {
value.as_mut().do_find_air_tree_node(tree_path_iter)
} else {