fixing the tests lead to me create a new function for converting from data
This commit is contained in:
parent
892da06e14
commit
bdd84dc952
|
@ -26,8 +26,8 @@ use crate::{
|
|||
check_replaceable_opaque_type, convert_opaque_type, erase_opaque_type_operations,
|
||||
find_and_replace_generics, find_list_clause_or_default_first, get_arg_type_name,
|
||||
get_generic_id_and_type, get_generic_variant_name, get_line_columns_by_span,
|
||||
get_src_code_by_span, monomorphize, pattern_has_conditions, wrap_as_multi_validator,
|
||||
wrap_validator_condition, CodeGenFunction, SpecificClause,
|
||||
get_src_code_by_span, known_data_to_type, monomorphize, pattern_has_conditions,
|
||||
wrap_as_multi_validator, wrap_validator_condition, CodeGenFunction, SpecificClause,
|
||||
},
|
||||
},
|
||||
line_numbers::LineNumbers,
|
||||
|
@ -3769,9 +3769,9 @@ impl<'a> CodeGenerator<'a> {
|
|||
|
||||
let convert_data_to_type = |term, tipo| {
|
||||
if error_term == Term::Error {
|
||||
builder::convert_data_to_type(term, tipo)
|
||||
builder::unknown_data_to_type(term, tipo)
|
||||
} else {
|
||||
builder::convert_data_to_type_debug(term, tipo, error_term.clone())
|
||||
builder::unknown_data_to_type_debug(term, tipo, error_term.clone())
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -4114,7 +4114,7 @@ impl<'a> CodeGenerator<'a> {
|
|||
let head_list = if tipo.is_map() {
|
||||
Term::head_list().apply(Term::var(tail_var))
|
||||
} else {
|
||||
builder::convert_data_to_type(
|
||||
builder::known_data_to_type(
|
||||
Term::head_list().apply(Term::var(tail_var)),
|
||||
&tipo.get_inner_types()[0],
|
||||
)
|
||||
|
@ -4772,21 +4772,18 @@ impl<'a> CodeGenerator<'a> {
|
|||
Term::snd_pair()
|
||||
};
|
||||
|
||||
term = term.lambda(name).apply(builder::convert_data_to_type(
|
||||
term = term.lambda(name).apply(builder::known_data_to_type(
|
||||
builtin.apply(Term::var(subject_name.clone())),
|
||||
&tuple_types[*index].clone(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
for (index, name) in indices.iter() {
|
||||
term = term
|
||||
.lambda(name.clone())
|
||||
.apply(builder::convert_data_to_type(
|
||||
Term::head_list().apply(
|
||||
Term::var(subject_name.clone()).repeat_tail_list(*index),
|
||||
),
|
||||
&tuple_types[*index].clone(),
|
||||
));
|
||||
term = term.lambda(name.clone()).apply(builder::known_data_to_type(
|
||||
Term::head_list()
|
||||
.apply(Term::var(subject_name.clone()).repeat_tail_list(*index)),
|
||||
&tuple_types[*index].clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Some(term)
|
||||
|
@ -4896,21 +4893,18 @@ impl<'a> CodeGenerator<'a> {
|
|||
Term::snd_pair()
|
||||
};
|
||||
|
||||
term = term.lambda(name).apply(builder::convert_data_to_type(
|
||||
term = term.lambda(name).apply(builder::known_data_to_type(
|
||||
builtin.apply(Term::var(subject_name.clone())),
|
||||
&tuple_types[*index].clone(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
for (index, name) in indices.iter() {
|
||||
term = term
|
||||
.lambda(name.clone())
|
||||
.apply(builder::convert_data_to_type(
|
||||
Term::head_list().apply(
|
||||
Term::var(subject_name.clone()).repeat_tail_list(*index),
|
||||
),
|
||||
&tuple_types[*index].clone(),
|
||||
));
|
||||
term = term.lambda(name.clone()).apply(builder::known_data_to_type(
|
||||
Term::head_list()
|
||||
.apply(Term::var(subject_name.clone()).repeat_tail_list(*index)),
|
||||
&tuple_types[*index].clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Some(term)
|
||||
|
@ -5240,17 +5234,31 @@ impl<'a> CodeGenerator<'a> {
|
|||
assert!(names.len() == 2);
|
||||
|
||||
if names[1] != "_" {
|
||||
term = term.lambda(names[1].clone()).apply(convert_data_to_type(
|
||||
Term::snd_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
||||
&inner_types[1],
|
||||
));
|
||||
term = term.lambda(names[1].clone()).apply(if is_expect {
|
||||
convert_data_to_type(
|
||||
Term::snd_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
||||
&inner_types[1],
|
||||
)
|
||||
} else {
|
||||
known_data_to_type(
|
||||
Term::snd_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
||||
&inner_types[1],
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
if names[0] != "_" {
|
||||
term = term.lambda(names[0].clone()).apply(convert_data_to_type(
|
||||
Term::fst_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
||||
&inner_types[0],
|
||||
))
|
||||
term = term.lambda(names[0].clone()).apply(if is_expect {
|
||||
convert_data_to_type(
|
||||
Term::snd_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
||||
&inner_types[0],
|
||||
)
|
||||
} else {
|
||||
known_data_to_type(
|
||||
Term::snd_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
||||
&inner_types[0],
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
term = term.lambda(format!("__tuple_{list_id}")).apply(value);
|
||||
|
|
|
@ -1167,7 +1167,41 @@ pub fn find_list_clause_or_default_first(clauses: &[TypedClause]) -> &TypedClaus
|
|||
.unwrap_or(&clauses[0])
|
||||
}
|
||||
|
||||
pub fn convert_data_to_type(term: Term<Name>, field_type: &Type) -> Term<Name> {
|
||||
pub fn known_data_to_type(term: Term<Name>, field_type: &Type) -> Term<Name> {
|
||||
if field_type.is_int() {
|
||||
Term::un_i_data().apply(term)
|
||||
} else if field_type.is_bytearray() {
|
||||
Term::un_b_data().apply(term)
|
||||
} else if field_type.is_void() {
|
||||
Term::unit().lambda("_").apply(term)
|
||||
} else if field_type.is_map() {
|
||||
Term::unmap_data().apply(term)
|
||||
} else if field_type.is_string() {
|
||||
Term::Builtin(DefaultFunction::DecodeUtf8).apply(Term::un_b_data().apply(term))
|
||||
} else if field_type.is_tuple() && matches!(field_type.get_uplc_type(), UplcType::Pair(_, _)) {
|
||||
Term::mk_pair_data()
|
||||
.apply(Term::head_list().apply(Term::var("__list_data")))
|
||||
.apply(Term::head_list().apply(Term::tail_list().apply(Term::var("__list_data"))))
|
||||
.lambda("__list_data")
|
||||
.apply(Term::unlist_data().apply(term))
|
||||
} else if field_type.is_list() || field_type.is_tuple() {
|
||||
Term::unlist_data().apply(term)
|
||||
} else if field_type.is_bool() {
|
||||
Term::less_than_integer()
|
||||
.apply(Term::integer(0.into()))
|
||||
.apply(Term::fst_pair().apply(Term::unconstr_data().apply(term)))
|
||||
} else if field_type.is_bls381_12_g1() {
|
||||
Term::bls12_381_g1_uncompress().apply(Term::un_b_data().apply(term))
|
||||
} else if field_type.is_bls381_12_g2() {
|
||||
Term::bls12_381_g2_uncompress().apply(Term::un_b_data().apply(term))
|
||||
} else if field_type.is_ml_result() {
|
||||
panic!("ML Result not supported")
|
||||
} else {
|
||||
term
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unknown_data_to_type(term: Term<Name>, field_type: &Type) -> Term<Name> {
|
||||
if field_type.is_int() {
|
||||
Term::un_i_data().apply(term)
|
||||
} else if field_type.is_bytearray() {
|
||||
|
@ -1231,7 +1265,7 @@ pub fn convert_data_to_type(term: Term<Name>, field_type: &Type) -> Term<Name> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn convert_data_to_type_debug(
|
||||
pub fn unknown_data_to_type_debug(
|
||||
term: Term<Name>,
|
||||
field_type: &Type,
|
||||
error_term: Term<Name>,
|
||||
|
@ -1605,14 +1639,22 @@ pub fn list_access_to_uplc(
|
|||
Term::unit()
|
||||
} else if matches!(tipo.get_uplc_type(), UplcType::Pair(_, _)) && is_list_accessor {
|
||||
Term::head_list().apply(Term::var(tail_name.to_string()))
|
||||
} else if matches!(expect_level, ExpectLevel::Full) && error_term != Term::Error {
|
||||
convert_data_to_type_debug(
|
||||
Term::head_list().apply(Term::var(tail_name.to_string())),
|
||||
&tipo.to_owned(),
|
||||
error_term.clone(),
|
||||
)
|
||||
} else if matches!(expect_level, ExpectLevel::Full) {
|
||||
// Expect level is full so we have an unknown piece of data to cast
|
||||
if error_term == Term::Error {
|
||||
unknown_data_to_type(
|
||||
Term::head_list().apply(Term::var(tail_name.to_string())),
|
||||
&tipo.to_owned(),
|
||||
)
|
||||
} else {
|
||||
unknown_data_to_type_debug(
|
||||
Term::head_list().apply(Term::var(tail_name.to_string())),
|
||||
&tipo.to_owned(),
|
||||
error_term.clone(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
convert_data_to_type(
|
||||
known_data_to_type(
|
||||
Term::head_list().apply(Term::var(tail_name.to_string())),
|
||||
&tipo.to_owned(),
|
||||
)
|
||||
|
@ -1735,7 +1777,7 @@ pub fn undata_builtin(
|
|||
term = term.apply(Term::var(temp_var));
|
||||
}
|
||||
|
||||
term = convert_data_to_type(term, tipo);
|
||||
term = known_data_to_type(term, tipo);
|
||||
|
||||
if count == 0 {
|
||||
term = term.lambda(temp_var);
|
||||
|
@ -1953,7 +1995,7 @@ pub fn cast_validator_args(term: Term<Name>, arguments: &[TypedArg]) -> Term<Nam
|
|||
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(
|
||||
.apply(known_data_to_type(
|
||||
Term::var(arg.arg_name.get_variable_name().unwrap_or("_")),
|
||||
&arg.tipo,
|
||||
));
|
||||
|
|
|
@ -3477,6 +3477,21 @@ fn generic_validator_type_test() {
|
|||
}
|
||||
"#;
|
||||
|
||||
let void_check = Term::equals_integer()
|
||||
.apply(Term::integer(0.into()))
|
||||
.apply(Term::fst_pair().apply(Term::unconstr_data().apply(Term::var("__val"))))
|
||||
.delayed_if_then_else(
|
||||
Term::snd_pair().apply(
|
||||
Term::unconstr_data()
|
||||
.apply(Term::var("__val"))
|
||||
.delayed_choose_list(
|
||||
Term::unit(),
|
||||
Term::Error.delayed_trace(Term::var("param_msg")),
|
||||
),
|
||||
),
|
||||
Term::Error.delayed_trace(Term::var("param_msg")),
|
||||
);
|
||||
|
||||
assert_uplc(
|
||||
src,
|
||||
Term::equals_integer()
|
||||
|
@ -3490,15 +3505,9 @@ fn generic_validator_type_test() {
|
|||
)
|
||||
.lambda("something")
|
||||
.apply(
|
||||
Term::equals_integer()
|
||||
.apply(Term::integer(0.into()))
|
||||
.apply(
|
||||
Term::fst_pair().apply(
|
||||
Term::unconstr_data()
|
||||
.apply(Term::head_list().apply(Term::var("B_fields"))),
|
||||
),
|
||||
)
|
||||
.delayed_if_then_else(Term::unit(), Term::Error),
|
||||
Term::unit()
|
||||
.lambda("_")
|
||||
.apply(Term::head_list().apply(Term::var("B_fields"))),
|
||||
)
|
||||
.lambda("B_fields")
|
||||
.apply(Term::var(CONSTR_FIELDS_EXPOSER).apply(Term::var("field_B")))
|
||||
|
@ -3573,22 +3582,7 @@ fn generic_validator_type_test() {
|
|||
.apply(
|
||||
Term::var("__val")
|
||||
.delayed_choose_data(
|
||||
Term::equals_integer()
|
||||
.apply(Term::integer(0.into()))
|
||||
.apply(
|
||||
Term::fst_pair().apply(
|
||||
Term::unconstr_data()
|
||||
.apply(Term::var(
|
||||
"__val",
|
||||
)),
|
||||
),
|
||||
)
|
||||
.delayed_if_then_else(
|
||||
Term::unit(),
|
||||
Term::Error.delayed_trace(
|
||||
Term::var("param_msg"),
|
||||
),
|
||||
),
|
||||
void_check.clone(),
|
||||
Term::Error.delayed_trace(
|
||||
Term::var("param_msg"),
|
||||
),
|
||||
|
@ -3641,20 +3635,7 @@ fn generic_validator_type_test() {
|
|||
.apply(
|
||||
Term::var("__val")
|
||||
.delayed_choose_data(
|
||||
Term::equals_integer()
|
||||
.apply(Term::integer(0.into()))
|
||||
.apply(
|
||||
Term::fst_pair().apply(
|
||||
Term::unconstr_data()
|
||||
.apply(Term::var("__val")),
|
||||
),
|
||||
)
|
||||
.delayed_if_then_else(
|
||||
Term::unit(),
|
||||
Term::Error.delayed_trace(
|
||||
Term::var("param_msg"),
|
||||
),
|
||||
),
|
||||
void_check,
|
||||
Term::Error
|
||||
.delayed_trace(Term::var("param_msg")),
|
||||
Term::Error
|
||||
|
|
Loading…
Reference in New Issue