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,
|
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,
|
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_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,
|
get_src_code_by_span, known_data_to_type, monomorphize, pattern_has_conditions,
|
||||||
wrap_validator_condition, CodeGenFunction, SpecificClause,
|
wrap_as_multi_validator, wrap_validator_condition, CodeGenFunction, SpecificClause,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
line_numbers::LineNumbers,
|
line_numbers::LineNumbers,
|
||||||
|
@ -3769,9 +3769,9 @@ impl<'a> CodeGenerator<'a> {
|
||||||
|
|
||||||
let convert_data_to_type = |term, tipo| {
|
let convert_data_to_type = |term, tipo| {
|
||||||
if error_term == Term::Error {
|
if error_term == Term::Error {
|
||||||
builder::convert_data_to_type(term, tipo)
|
builder::unknown_data_to_type(term, tipo)
|
||||||
} else {
|
} 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() {
|
let head_list = if tipo.is_map() {
|
||||||
Term::head_list().apply(Term::var(tail_var))
|
Term::head_list().apply(Term::var(tail_var))
|
||||||
} else {
|
} else {
|
||||||
builder::convert_data_to_type(
|
builder::known_data_to_type(
|
||||||
Term::head_list().apply(Term::var(tail_var)),
|
Term::head_list().apply(Term::var(tail_var)),
|
||||||
&tipo.get_inner_types()[0],
|
&tipo.get_inner_types()[0],
|
||||||
)
|
)
|
||||||
|
@ -4772,21 +4772,18 @@ impl<'a> CodeGenerator<'a> {
|
||||||
Term::snd_pair()
|
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())),
|
builtin.apply(Term::var(subject_name.clone())),
|
||||||
&tuple_types[*index].clone(),
|
&tuple_types[*index].clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (index, name) in indices.iter() {
|
for (index, name) in indices.iter() {
|
||||||
term = term
|
term = term.lambda(name.clone()).apply(builder::known_data_to_type(
|
||||||
.lambda(name.clone())
|
Term::head_list()
|
||||||
.apply(builder::convert_data_to_type(
|
.apply(Term::var(subject_name.clone()).repeat_tail_list(*index)),
|
||||||
Term::head_list().apply(
|
&tuple_types[*index].clone(),
|
||||||
Term::var(subject_name.clone()).repeat_tail_list(*index),
|
));
|
||||||
),
|
|
||||||
&tuple_types[*index].clone(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(term)
|
Some(term)
|
||||||
|
@ -4896,21 +4893,18 @@ impl<'a> CodeGenerator<'a> {
|
||||||
Term::snd_pair()
|
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())),
|
builtin.apply(Term::var(subject_name.clone())),
|
||||||
&tuple_types[*index].clone(),
|
&tuple_types[*index].clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (index, name) in indices.iter() {
|
for (index, name) in indices.iter() {
|
||||||
term = term
|
term = term.lambda(name.clone()).apply(builder::known_data_to_type(
|
||||||
.lambda(name.clone())
|
Term::head_list()
|
||||||
.apply(builder::convert_data_to_type(
|
.apply(Term::var(subject_name.clone()).repeat_tail_list(*index)),
|
||||||
Term::head_list().apply(
|
&tuple_types[*index].clone(),
|
||||||
Term::var(subject_name.clone()).repeat_tail_list(*index),
|
));
|
||||||
),
|
|
||||||
&tuple_types[*index].clone(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(term)
|
Some(term)
|
||||||
|
@ -5240,17 +5234,31 @@ impl<'a> CodeGenerator<'a> {
|
||||||
assert!(names.len() == 2);
|
assert!(names.len() == 2);
|
||||||
|
|
||||||
if names[1] != "_" {
|
if names[1] != "_" {
|
||||||
term = term.lambda(names[1].clone()).apply(convert_data_to_type(
|
term = term.lambda(names[1].clone()).apply(if is_expect {
|
||||||
Term::snd_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
convert_data_to_type(
|
||||||
&inner_types[1],
|
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] != "_" {
|
if names[0] != "_" {
|
||||||
term = term.lambda(names[0].clone()).apply(convert_data_to_type(
|
term = term.lambda(names[0].clone()).apply(if is_expect {
|
||||||
Term::fst_pair().apply(Term::var(format!("__tuple_{list_id}"))),
|
convert_data_to_type(
|
||||||
&inner_types[0],
|
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);
|
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])
|
.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() {
|
if field_type.is_int() {
|
||||||
Term::un_i_data().apply(term)
|
Term::un_i_data().apply(term)
|
||||||
} else if field_type.is_bytearray() {
|
} 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>,
|
term: Term<Name>,
|
||||||
field_type: &Type,
|
field_type: &Type,
|
||||||
error_term: Term<Name>,
|
error_term: Term<Name>,
|
||||||
|
@ -1605,14 +1639,22 @@ pub fn list_access_to_uplc(
|
||||||
Term::unit()
|
Term::unit()
|
||||||
} else if matches!(tipo.get_uplc_type(), UplcType::Pair(_, _)) && is_list_accessor {
|
} else if matches!(tipo.get_uplc_type(), UplcType::Pair(_, _)) && is_list_accessor {
|
||||||
Term::head_list().apply(Term::var(tail_name.to_string()))
|
Term::head_list().apply(Term::var(tail_name.to_string()))
|
||||||
} else if matches!(expect_level, ExpectLevel::Full) && error_term != Term::Error {
|
} else if matches!(expect_level, ExpectLevel::Full) {
|
||||||
convert_data_to_type_debug(
|
// Expect level is full so we have an unknown piece of data to cast
|
||||||
Term::head_list().apply(Term::var(tail_name.to_string())),
|
if error_term == Term::Error {
|
||||||
&tipo.to_owned(),
|
unknown_data_to_type(
|
||||||
error_term.clone(),
|
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 {
|
} else {
|
||||||
convert_data_to_type(
|
known_data_to_type(
|
||||||
Term::head_list().apply(Term::var(tail_name.to_string())),
|
Term::head_list().apply(Term::var(tail_name.to_string())),
|
||||||
&tipo.to_owned(),
|
&tipo.to_owned(),
|
||||||
)
|
)
|
||||||
|
@ -1735,7 +1777,7 @@ pub fn undata_builtin(
|
||||||
term = term.apply(Term::var(temp_var));
|
term = term.apply(Term::var(temp_var));
|
||||||
}
|
}
|
||||||
|
|
||||||
term = convert_data_to_type(term, tipo);
|
term = known_data_to_type(term, tipo);
|
||||||
|
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
term = term.lambda(temp_var);
|
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) {
|
if !matches!(arg.tipo.get_uplc_type(), UplcType::Data) {
|
||||||
term = term
|
term = term
|
||||||
.lambda(arg.arg_name.get_variable_name().unwrap_or("_"))
|
.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("_")),
|
Term::var(arg.arg_name.get_variable_name().unwrap_or("_")),
|
||||||
&arg.tipo,
|
&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(
|
assert_uplc(
|
||||||
src,
|
src,
|
||||||
Term::equals_integer()
|
Term::equals_integer()
|
||||||
|
@ -3490,15 +3505,9 @@ fn generic_validator_type_test() {
|
||||||
)
|
)
|
||||||
.lambda("something")
|
.lambda("something")
|
||||||
.apply(
|
.apply(
|
||||||
Term::equals_integer()
|
Term::unit()
|
||||||
.apply(Term::integer(0.into()))
|
.lambda("_")
|
||||||
.apply(
|
.apply(Term::head_list().apply(Term::var("B_fields"))),
|
||||||
Term::fst_pair().apply(
|
|
||||||
Term::unconstr_data()
|
|
||||||
.apply(Term::head_list().apply(Term::var("B_fields"))),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.delayed_if_then_else(Term::unit(), Term::Error),
|
|
||||||
)
|
)
|
||||||
.lambda("B_fields")
|
.lambda("B_fields")
|
||||||
.apply(Term::var(CONSTR_FIELDS_EXPOSER).apply(Term::var("field_B")))
|
.apply(Term::var(CONSTR_FIELDS_EXPOSER).apply(Term::var("field_B")))
|
||||||
|
@ -3573,22 +3582,7 @@ fn generic_validator_type_test() {
|
||||||
.apply(
|
.apply(
|
||||||
Term::var("__val")
|
Term::var("__val")
|
||||||
.delayed_choose_data(
|
.delayed_choose_data(
|
||||||
Term::equals_integer()
|
void_check.clone(),
|
||||||
.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"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Term::Error.delayed_trace(
|
Term::Error.delayed_trace(
|
||||||
Term::var("param_msg"),
|
Term::var("param_msg"),
|
||||||
),
|
),
|
||||||
|
@ -3641,20 +3635,7 @@ fn generic_validator_type_test() {
|
||||||
.apply(
|
.apply(
|
||||||
Term::var("__val")
|
Term::var("__val")
|
||||||
.delayed_choose_data(
|
.delayed_choose_data(
|
||||||
Term::equals_integer()
|
void_check,
|
||||||
.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"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Term::Error
|
Term::Error
|
||||||
.delayed_trace(Term::var("param_msg")),
|
.delayed_trace(Term::var("param_msg")),
|
||||||
Term::Error
|
Term::Error
|
||||||
|
|
Loading…
Reference in New Issue