Start turning AirTree statements into expressions (let)

Still many places to fix, WIP.
This commit is contained in:
KtorZ 2023-10-06 19:16:53 +02:00 committed by Kasey
parent cf51b30045
commit a12c374258
3 changed files with 569 additions and 570 deletions

File diff suppressed because it is too large Load Diff

View File

@ -659,10 +659,7 @@ pub fn erase_opaque_type_operations(
/// Determine whether this air_tree node introduces any shadowing over `potential_matches` /// Determine whether this air_tree node introduces any shadowing over `potential_matches`
pub fn find_introduced_variables(air_tree: &AirTree) -> Vec<String> { pub fn find_introduced_variables(air_tree: &AirTree) -> Vec<String> {
match air_tree { match air_tree {
AirTree::Statement { AirTree::Expression(AirExpression::Let { name, .. }) => vec![name.clone()],
statement: AirStatement::Let { name, .. },
..
} => vec![name.clone()],
AirTree::Statement { AirTree::Statement {
statement: AirStatement::TupleGuard { indices, .. }, statement: AirStatement::TupleGuard { indices, .. },
.. ..

View File

@ -117,11 +117,6 @@ pub enum AirTree {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum AirStatement { pub enum AirStatement {
// Assignment
Let {
name: String,
value: Box<AirTree>,
},
DefineFunc { DefineFunc {
func_name: String, func_name: String,
module_name: String, module_name: String,
@ -209,6 +204,13 @@ pub enum AirStatement {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum AirExpression { pub enum AirExpression {
// Assignment
Let {
name: String,
value: Box<AirTree>,
then: Box<AirTree>,
},
// Primitives // Primitives
Int { Int {
value: String, value: String,
@ -494,14 +496,12 @@ impl AirTree {
arg: arg.into(), arg: arg.into(),
}) })
} }
pub fn let_assignment(name: impl ToString, value: AirTree) -> AirTree { pub fn let_assignment(name: impl ToString, value: AirTree, then: AirTree) -> AirTree {
AirTree::Statement { AirTree::Expression(AirExpression::Let {
statement: AirStatement::Let {
name: name.to_string(), name: name.to_string(),
value: value.into(), value: value.into(),
}, then: then.into(),
hoisted_over: None, })
}
} }
pub fn cast_from_data(value: AirTree, tipo: Rc<Type>, msg: Option<AirMsg>) -> AirTree { pub fn cast_from_data(value: AirTree, tipo: Rc<Type>, msg: Option<AirMsg>) -> AirTree {
AirTree::Expression(AirExpression::CastFromData { AirTree::Expression(AirExpression::CastFromData {
@ -755,6 +755,7 @@ impl AirTree {
hoisted_over: None, hoisted_over: None,
} }
} }
pub fn list_access( pub fn list_access(
names: Vec<String>, names: Vec<String>,
tipo: Rc<Type>, tipo: Rc<Type>,
@ -889,8 +890,6 @@ impl AirTree {
vec![head_list], vec![head_list],
); );
let assign = AirTree::let_assignment("_", expect_on_head);
let next_call = AirTree::call( let next_call = AirTree::call(
AirTree::var( AirTree::var(
ValueConstructor::public( ValueConstructor::public(
@ -918,11 +917,13 @@ impl AirTree {
], ],
); );
let assign = AirTree::let_assignment("_", expect_on_head, next_call);
AirTree::list_clause( AirTree::list_clause(
"__list_to_check", "__list_to_check",
void(), void(),
AirTree::void(), AirTree::void(),
assign.hoist_over(next_call), assign,
None, None,
false, false,
) )
@ -941,10 +942,6 @@ impl AirTree {
hoisted_over: Some(exp), hoisted_over: Some(exp),
} => { } => {
match statement { match statement {
AirStatement::Let { value, name } => {
air_vec.push(Air::Let { name: name.clone() });
value.create_air_vec(air_vec);
}
AirStatement::DefineFunc { AirStatement::DefineFunc {
func_name, func_name,
module_name, module_name,
@ -1143,6 +1140,11 @@ impl AirTree {
exp.create_air_vec(air_vec); exp.create_air_vec(air_vec);
} }
AirTree::Expression(exp) => match exp { AirTree::Expression(exp) => match exp {
AirExpression::Let { value, then, name } => {
air_vec.push(Air::Let { name: name.clone() });
value.create_air_vec(air_vec);
then.create_air_vec(air_vec);
}
AirExpression::Int { value } => air_vec.push(Air::Int { AirExpression::Int { value } => air_vec.push(Air::Int {
value: value.clone(), value: value.clone(),
}), }),
@ -1517,9 +1519,10 @@ impl AirTree {
let mut index_count = IndexCounter::new(); let mut index_count = IndexCounter::new();
tree_path.push(current_depth, depth_index); tree_path.push(current_depth, depth_index);
if let AirTree::Statement { statement, .. } = self { match self {
match statement { AirTree::UnhoistedSequence(..) => unreachable!("No unhoisted sequence at this point"),
AirStatement::Let { value, .. } => { AirTree::Expression(expr) => match expr {
AirExpression::Let { value, .. } => {
value.do_traverse_tree_with( value.do_traverse_tree_with(
tree_path, tree_path,
current_depth + 1, current_depth + 1,
@ -1528,6 +1531,9 @@ impl AirTree {
apply_with_func_last, apply_with_func_last,
); );
} }
_ => (),
},
AirTree::Statement { statement, .. } => match statement {
AirStatement::DefineFunc { func_body, .. } => { AirStatement::DefineFunc { func_body, .. } => {
func_body.do_traverse_tree_with( func_body.do_traverse_tree_with(
tree_path, tree_path,
@ -1628,7 +1634,7 @@ impl AirTree {
apply_with_func_last, apply_with_func_last,
); );
} }
}; },
} }
if !apply_with_func_last { if !apply_with_func_last {
@ -1649,6 +1655,15 @@ impl AirTree {
); );
} }
AirTree::Expression(e) => match e { AirTree::Expression(e) => match e {
AirExpression::Let { then, .. } => {
then.do_traverse_tree_with(
tree_path,
current_depth + 1,
index_count.next_number(),
with,
apply_with_func_last,
);
}
AirExpression::List { items, .. } => { AirExpression::List { items, .. } => {
for item in items { for item in items {
item.do_traverse_tree_with( item.do_traverse_tree_with(
@ -1987,15 +2002,6 @@ impl AirTree {
statement, statement,
hoisted_over: Some(hoisted_over), hoisted_over: Some(hoisted_over),
} => match statement { } => match statement {
AirStatement::Let { value, .. } => {
if *index == 0 {
value.as_mut().do_find_air_tree_node(tree_path_iter)
} else if *index == 1 {
hoisted_over.as_mut().do_find_air_tree_node(tree_path_iter)
} else {
panic!("Tree Path index outside tree children nodes")
}
}
AirStatement::AssertConstr { constr, .. } => { AirStatement::AssertConstr { constr, .. } => {
if *index == 0 { if *index == 0 {
constr.as_mut().do_find_air_tree_node(tree_path_iter) constr.as_mut().do_find_air_tree_node(tree_path_iter)
@ -2100,6 +2106,15 @@ impl AirTree {
} }
}, },
AirTree::Expression(e) => match e { AirTree::Expression(e) => match e {
AirExpression::Let { value, then, .. } => {
if *index == 0 {
value.as_mut().do_find_air_tree_node(tree_path_iter)
} else if *index == 1 {
then.as_mut().do_find_air_tree_node(tree_path_iter)
} else {
panic!("Tree Path index outside tree children nodes")
}
}
AirExpression::List { items, .. } AirExpression::List { items, .. }
| AirExpression::Tuple { items, .. } | AirExpression::Tuple { items, .. }
| AirExpression::Builtin { args: items, .. } => { | AirExpression::Builtin { args: items, .. } => {