Skip to content

Commit

Permalink
chore: rename unwrapData and wrapData
Browse files Browse the repository at this point in the history
add validator cast function for extra validator params
  • Loading branch information
MicroProofs committed Jul 25, 2023
1 parent b911ce8 commit 99a5234
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
23 changes: 13 additions & 10 deletions crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}

Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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
})
Expand All @@ -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
})
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions crates/aiken-lang/src/gen_uplc/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ pub enum Air {
Let {
name: String,
},
UnWrapData {
CastFromData {
tipo: Arc<Type>,
},
WrapData {
CastToData {
tipo: Arc<Type>,
},
AssertConstr {
Expand Down
19 changes: 18 additions & 1 deletion crates/aiken-lang/src/gen_uplc/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
}
36 changes: 18 additions & 18 deletions crates/aiken-lang/src/gen_uplc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
},
Expand Down Expand Up @@ -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(),
})
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, .. }
Expand All @@ -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, .. }
Expand Down Expand Up @@ -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, .. }
Expand Down Expand Up @@ -1523,15 +1523,15 @@ impl AirTree {
with,
);
}
AirExpression::UnWrapData { value, .. } => {
AirExpression::CastFromData { value, .. } => {
value.do_traverse_tree_with(
tree_path,
current_depth + 1,
index_count.next_number(),
with,
);
}
AirExpression::WrapData { value, .. } => {
AirExpression::CastToData { value, .. } => {
value.do_traverse_tree_with(
tree_path,
current_depth + 1,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 99a5234

Please sign in to comment.