fix: List clauses were destructuring the next element unnecessarily

feat: finish nested constructor clauses
This commit is contained in:
microproofs 2023-07-02 13:17:14 -04:00 committed by Kasey
parent f6e163d16d
commit c025073056
2 changed files with 128 additions and 68 deletions

View File

@ -122,6 +122,47 @@ impl ClauseProperties {
} }
} }
} }
pub fn init_inner(
t: &Arc<Type>,
constr_var: String,
subject_name: String,
final_clause: bool,
) -> Self {
if t.is_list() {
ClauseProperties {
clause_var_name: constr_var,
complex_clause: false,
original_subject_name: subject_name,
final_clause,
needs_constr_var: false,
specific_clause: SpecificClause::ListClause {
current_index: 0,
defined_tails: vec![],
},
}
} else if t.is_tuple() {
ClauseProperties {
clause_var_name: constr_var,
complex_clause: false,
original_subject_name: subject_name,
needs_constr_var: false,
final_clause,
specific_clause: SpecificClause::TupleClause {
defined_tuple_indices: IndexSet::new(),
},
}
} else {
ClauseProperties {
clause_var_name: constr_var,
complex_clause: false,
original_subject_name: subject_name,
needs_constr_var: false,
final_clause,
specific_clause: SpecificClause::ConstrClause,
}
}
}
} }
pub fn convert_type_to_data(term: Term<Name>, field_type: &Arc<Type>) -> Term<Name> { pub fn convert_type_to_data(term: Term<Name>, field_type: &Arc<Type>) -> Term<Name> {

View File

@ -1256,11 +1256,10 @@ impl<'a> CodeGenerator<'a> {
}; };
let next_tail_name = { let next_tail_name = {
let next_clause = if rest_clauses.is_empty() { if rest_clauses.is_empty() {
&final_clause None
} else { } else {
&rest_clauses[0] let next_clause = &rest_clauses[0];
};
let next_elements_len = match &next_clause.pattern { let next_elements_len = match &next_clause.pattern {
Pattern::List { elements, .. } => elements.len(), Pattern::List { elements, .. } => elements.len(),
_ => 0, _ => 0,
@ -1269,13 +1268,14 @@ impl<'a> CodeGenerator<'a> {
if (*current_index as usize) < next_elements_len { if (*current_index as usize) < next_elements_len {
Some(format!( Some(format!(
"tail_index_{}_span_{}_{}", "tail_index_{}_span_{}_{}",
*current_index, *current_index + 1,
next_clause.pattern.location().start, next_clause.pattern.location().start,
next_clause.pattern.location().end next_clause.pattern.location().end
)) ))
} else { } else {
None None
} }
}
}; };
let mut use_wrap_clause = false; let mut use_wrap_clause = false;
@ -1384,8 +1384,6 @@ impl<'a> CodeGenerator<'a> {
let ClauseProperties { let ClauseProperties {
specific_clause: SpecificClause::ListClause { defined_tails, .. }, specific_clause: SpecificClause::ListClause { defined_tails, .. },
complex_clause, complex_clause,
original_subject_name: _,
final_clause: _,
.. ..
} = props } = props
else { unreachable!() }; else { unreachable!() };
@ -1415,10 +1413,11 @@ impl<'a> CodeGenerator<'a> {
), ),
}; };
let mut elem_props = ClauseProperties::init( let mut elem_props = ClauseProperties::init_inner(
list_elem_type, list_elem_type,
elem_name.clone(), elem_name.clone(),
elem_name.clone(), elem_name.clone(),
props.final_clause,
); );
let statement = let statement =
@ -1457,14 +1456,12 @@ impl<'a> CodeGenerator<'a> {
), ),
}; };
let mut elem_props = ClauseProperties { let mut elem_props = ClauseProperties::init_inner(
clause_var_name: elem_name.clone(), subject_tipo,
complex_clause: false, elem_name.clone(),
needs_constr_var: false, elem_name.clone(),
original_subject_name: elem_name.clone(), props.final_clause,
final_clause: props.final_clause, );
specific_clause: props.specific_clause.clone(),
};
let statement = let statement =
self.nested_clause_condition(elem, subject_tipo, &mut elem_props); self.nested_clause_condition(elem, subject_tipo, &mut elem_props);
@ -1557,14 +1554,12 @@ impl<'a> CodeGenerator<'a> {
) )
}); });
let mut field_props = ClauseProperties { let mut field_props = ClauseProperties::init_inner(
clause_var_name: field_name.clone(), arg_type,
complex_clause: false, field_name.clone(),
needs_constr_var: false, field_name.clone(),
original_subject_name: field_name.clone(), props.final_clause,
final_clause: props.final_clause, );
specific_clause: props.specific_clause.clone(),
};
let statement = self.nested_clause_condition( let statement = self.nested_clause_condition(
&arg.value, &arg.value,
@ -1614,38 +1609,62 @@ impl<'a> CodeGenerator<'a> {
subject_tipo: &Arc<Type>, subject_tipo: &Arc<Type>,
props: &mut ClauseProperties, props: &mut ClauseProperties,
) -> AirTree { ) -> AirTree {
if props.final_clause {
props.complex_clause = false;
let (_, assign) = self.clause_pattern(pattern, subject_tipo, props);
assign
} else {
match pattern { match pattern {
Pattern::Int { value, .. } => { Pattern::Int { value, .. } => {
props.complex_clause = true;
AirTree::clause_guard(&props.original_subject_name, AirTree::int(value), int()) AirTree::clause_guard(&props.original_subject_name, AirTree::int(value), int())
} }
Pattern::Var { name, .. } => AirTree::let_assignment( Pattern::Var { name, .. } => AirTree::let_assignment(
name, name,
AirTree::local_var(&props.clause_var_name, subject_tipo.clone()), AirTree::local_var(&props.clause_var_name, subject_tipo.clone()),
), ),
Pattern::Assign { Pattern::Assign { name, pattern, .. } => AirTree::UnhoistedSequence(vec![
AirTree::let_assignment(
name, name,
location, AirTree::local_var(&props.clause_var_name, subject_tipo.clone()),
pattern, ),
} => { self.nested_clause_condition(pattern, subject_tipo, props),
todo!(); ]),
} Pattern::Discard { .. } => AirTree::no_op(),
Pattern::Discard { name, location } => todo!(),
Pattern::List { Pattern::List {
location, location,
elements, elements,
tail, tail,
} => todo!(), } => {
todo!();
}
Pattern::Constructor { Pattern::Constructor {
is_record, name: constr_name, ..
location, } => {
name, props.complex_clause = true;
arguments, if subject_tipo.is_bool() {
module, AirTree::clause_guard(
constructor, &props.original_subject_name,
with_spread, AirTree::bool(constr_name == "True"),
tipo, bool(),
} => todo!(), )
} else if subject_tipo.is_void() {
todo!()
} else {
let (cond, assign) = self.clause_pattern(pattern, subject_tipo, props);
AirTree::UnhoistedSequence(vec![
AirTree::clause_guard(
&props.original_subject_name,
cond,
subject_tipo.clone(),
),
assign,
])
}
}
Pattern::Tuple { location, elems } => todo!(), Pattern::Tuple { location, elems } => todo!(),
} }
} }
}
} }