use super::{children, OwnedScalarExpr, Result, ScalarExpr};
use uqa_core::{memory::ProductionControl, Value};
impl OwnedScalarExpr {
pub fn replace_child(
mut self,
index: usize,
replacement: Self,
control: &ProductionControl<'_>,
) -> Result<(Self, Self)> {
self.assert_owner(control);
replacement.assert_owner(control);
control.check()?;
let previous = self.take_child(index);
let Self { expression, memory } = replacement;
*children::get_mut(&mut self.expression, index).expect("scalar child index") = expression;
if let Some(parent) = &mut self.memory {
parent.children[index] = memory;
}
control.check()?;
Ok((self, previous))
}
pub fn swap_children(
mut self,
left: usize,
right: usize,
control: &ProductionControl<'_>,
) -> Result<Self> {
self.assert_owner(control);
control.check()?;
let count = self.child_count();
assert!(left < count && right < count, "scalar child index");
if left != right {
let first = std::mem::replace(
children::get_mut(&mut self.expression, left).expect("scalar child index"),
ScalarExpr::Literal(Value::Null),
);
let second = std::mem::replace(
children::get_mut(&mut self.expression, right).expect("scalar child index"),
first,
);
*children::get_mut(&mut self.expression, left).expect("scalar child index") = second;
if let Some(memory) = &mut self.memory {
memory.children.swap(left, right);
}
}
control.check()?;
Ok(self)
}
pub fn truncate_call_arguments(
mut self,
count: usize,
control: &ProductionControl<'_>,
) -> Result<Self> {
self.assert_owner(control);
control.check()?;
let ScalarExpr::Func { args, .. } = &mut self.expression else {
panic!("function argument edit requires a function call");
};
let removed = count.min(args.len())..args.len();
args.truncate(count);
self.remove_child_owners(removed);
control.check()?;
Ok(self)
}
pub fn truncate_case_arms(
mut self,
count: usize,
control: &ProductionControl<'_>,
) -> Result<Self> {
self.assert_owner(control);
control.check()?;
let ScalarExpr::Case { base, when, .. } = &mut self.expression else {
panic!("CASE arm edit requires a CASE expression");
};
let base_count = usize::from(base.is_some());
let removed = base_count + 2 * count.min(when.len())..base_count + 2 * when.len();
when.truncate(count);
self.remove_child_owners(removed);
control.check()?;
Ok(self)
}
pub fn truncate_boolean_children(
mut self,
count: usize,
control: &ProductionControl<'_>,
) -> Result<Self> {
self.assert_owner(control);
control.check()?;
let (ScalarExpr::And(items) | ScalarExpr::Or(items)) = &mut self.expression else {
panic!("boolean child edit requires AND or OR");
};
let removed = count.min(items.len())..items.len();
items.truncate(count);
self.remove_child_owners(removed);
control.check()?;
Ok(self)
}
pub fn set_case_else(
mut self,
replacement: Self,
control: &ProductionControl<'_>,
) -> Result<Self> {
self.assert_owner(control);
replacement.assert_owner(control);
control.check()?;
let ScalarExpr::Case { else_branch, .. } = &mut self.expression else {
panic!("CASE fallback edit requires a CASE expression");
};
if let Some(branch) = else_branch {
let Self { expression, memory } = replacement;
**branch = expression;
if let Some(parent) = &mut self.memory {
*parent.children.last_mut().expect("CASE fallback owner") = memory;
}
} else {
if let Some(parent) = &mut self.memory {
parent.reserve_children(1)?;
}
let boxed = control.reserve(size_of::<ScalarExpr>())?;
let Self { expression, memory } = replacement;
*else_branch = Some(Box::new(expression));
if let Some(parent) = &mut self.memory {
parent
.push_child(memory)
.expect("CASE fallback slot was admitted");
parent
.direct
.absorb(boxed.expect("controlled CASE fallback box"));
}
}
control.check()?;
Ok(self)
}
pub(super) fn remove_child_owners(&mut self, removed: std::ops::Range<usize>) {
if let Some(memory) = &mut self.memory {
let count = removed.len();
let retained = memory.children.len() - count;
memory.children[removed.start..].rotate_left(count);
memory.children.truncate(retained);
}
}
}