Skip to content

Commit

Permalink
Return slice from ExpressionAtom::children()
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbogd committed Nov 13, 2024
1 parent 3136f80 commit 631a41a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion c/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ pub unsafe extern "C" fn atom_get_space(atom: *const atom_ref_t) -> space_t {
}

/// Private convenience function to call an `c_atom_vec_callback_t` callback with each atom in a vec
pub(crate) fn return_atoms(atoms: &Vec<Atom>, callback: c_atom_vec_callback_t, context: *mut c_void) {
pub(crate) fn return_atoms(atoms: &[Atom], callback: c_atom_vec_callback_t, context: *mut c_void) {
callback(&(&atoms[..]).into(), context);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/atom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ impl ExpressionAtom {
}

/// Returns a reference to a vector of sub-atoms.
pub fn children(&self) -> &Vec<Atom> {
pub fn children(&self) -> &[Atom] {
&self.children
}

/// Returns a mutable reference to a vector of sub-atoms.
pub fn children_mut(&mut self) -> &mut Vec<Atom> {
pub fn children_mut(&mut self) -> &mut [Atom] {
&mut self.children
}

Expand Down Expand Up @@ -1013,7 +1013,7 @@ impl<'a> TryFrom<&'a Atom> for &'a [Atom] {
type Error = &'static str;
fn try_from(atom: &Atom) -> Result<&[Atom], &'static str> {
match atom {
Atom::Expression(expr) => Ok(expr.children().as_slice()),
Atom::Expression(expr) => Ok(expr.children()),
_ => Err("Atom is not an ExpressionAtom")
}
}
Expand All @@ -1023,7 +1023,7 @@ impl<'a> TryFrom<&'a mut Atom> for &'a mut [Atom] {
type Error = &'static str;
fn try_from(atom: &mut Atom) -> Result<&mut [Atom], &'static str> {
match atom {
Atom::Expression(expr) => Ok(expr.children_mut().as_mut_slice()),
Atom::Expression(expr) => Ok(expr.children_mut()),
_ => Err("Atom is not an ExpressionAtom")
}
}
Expand Down
33 changes: 19 additions & 14 deletions lib/src/metta/interpreter_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,16 +652,18 @@ fn function_ret(stack: Rc<RefCell<Stack>>, atom: Atom, bindings: Bindings) -> Op
}

fn collapse_bind(stack: Stack, bindings: Bindings) -> Vec<InterpretedAtom> {
let Stack{ prev, atom: mut collapse, ret: _, finished: _, vars } = stack;
let Stack{ prev, atom: collapse, ret: _, finished: _, vars } = stack;

let mut nested = Atom::expr([]);
match &mut collapse {
let collapse = match collapse {
Atom::Expression(expr) => {
std::mem::swap(&mut nested, &mut expr.children_mut()[1]);
expr.children_mut().push(Atom::value(bindings.clone()))
let mut children = expr.into_children();
std::mem::swap(&mut nested, &mut children[1]);
children.push(Atom::value(bindings.clone()));
Atom::expr(children)
},
_ => panic!("Unexpected state"),
}
};

let prev = Stack::from_prev_with_vars(prev, collapse, vars, collapse_bind_ret);
let prev = Rc::new(RefCell::new(prev));
Expand All @@ -672,16 +674,19 @@ fn collapse_bind(stack: Stack, bindings: Bindings) -> Vec<InterpretedAtom> {

fn collapse_bind_ret(stack: Rc<RefCell<Stack>>, atom: Atom, bindings: Bindings) -> Option<(Stack, Bindings)> {
let nested = atom;
{
if nested != EMPTY_SYMBOL {
let stack_ref = &mut *stack.borrow_mut();
let Stack{ prev: _, atom: collapse, ret: _, finished: _, vars: _ } = stack_ref;
let finished = match atom_as_slice_mut(collapse) {
Some([_op, Atom::Expression(finished), _bindings]) => finished,
match atom_as_slice_mut(collapse) {
Some([_op, Atom::Expression(finished_placeholder), _bindings]) => {
let mut finished = ExpressionAtom::new(Vec::new());
std::mem::swap(&mut finished, finished_placeholder);
let mut finished = finished.into_children();
finished.push(atom_bindings_into_atom(nested, bindings));
std::mem::swap(&mut ExpressionAtom::new(finished), finished_placeholder);
},
_ => panic!("Unexpected state"),
};
if nested != EMPTY_SYMBOL {
finished.children_mut().push(atom_bindings_into_atom(nested, bindings));
}
}

// all alternatives are evaluated
Expand Down Expand Up @@ -1105,9 +1110,9 @@ fn interpret_function(args: Atom, bindings: Bindings) -> MettaResult {
let mut call = atom.clone().into_children();
let head = call.remove(0);
let args = call;
let mut arg_types = op_type.clone();
arg_types.children_mut().remove(0);
let arg_types = Atom::Expression(arg_types);
let mut arg_types: Vec<Atom> = op_type.children().into();
arg_types.remove(0);
let arg_types = Atom::expr(arg_types);
let rop = Atom::Variable(VariableAtom::new("rop").make_unique());
let rargs = Atom::Variable(VariableAtom::new("rargs").make_unique());
let result = Atom::Variable(VariableAtom::new("result").make_unique());
Expand Down
16 changes: 8 additions & 8 deletions lib/src/metta/runner/stdlib_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ impl CustomExecute for UniqueAtomOp {
let arg_error = || ExecError::from("unique expects single expression atom as an argument");
let expr = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?;

let mut atoms = expr.children().clone();
let mut atoms: Vec<Atom> = expr.children().into();
let mut set = GroundingSpace::new();
atoms.retain(|x| {
let not_contained = set.query(x).is_empty();
Expand Down Expand Up @@ -1028,8 +1028,8 @@ impl Grounded for UnionAtomOp {
impl CustomExecute for UnionAtomOp {
fn execute(&self, args: &[Atom]) -> Result<Vec<Atom>, ExecError> {
let arg_error = || ExecError::from("union expects and executable LHS and RHS atom");
let mut lhs = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children().clone();
let rhs = TryInto::<&ExpressionAtom>::try_into(args.get(1).ok_or_else(arg_error)?)?.children().clone();
let mut lhs: Vec<Atom> = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children().into();
let rhs: Vec<Atom> = TryInto::<&ExpressionAtom>::try_into(args.get(1).ok_or_else(arg_error)?)?.children().into();

lhs.extend(rhs);

Expand All @@ -1055,8 +1055,8 @@ impl Grounded for IntersectionAtomOp {
impl CustomExecute for IntersectionAtomOp {
fn execute(&self, args: &[Atom]) -> Result<Vec<Atom>, ExecError> {
let arg_error = || ExecError::from("intersection expects and executable LHS and RHS atom");
let mut lhs = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children().clone();
let rhs = TryInto::<&ExpressionAtom>::try_into(args.get(1).ok_or_else(arg_error)?)?.children().clone();
let mut lhs: Vec<Atom> = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children().into();
let rhs = TryInto::<&ExpressionAtom>::try_into(args.get(1).ok_or_else(arg_error)?)?.children();

let mut rhs_index: MultiTrie<SymbolAtom, Vec<usize>> = MultiTrie::new();
for (index, rhs_item) in rhs.iter().enumerate() {
Expand Down Expand Up @@ -1238,8 +1238,8 @@ impl Grounded for SubtractionAtomOp {
impl CustomExecute for SubtractionAtomOp {
fn execute(&self, args: &[Atom]) -> Result<Vec<Atom>, ExecError> {
let arg_error = || ExecError::from("subtraction expects and executable LHS and RHS atom");
let mut lhs = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children().clone();
let rhs = TryInto::<&ExpressionAtom>::try_into(args.get(1).ok_or_else(arg_error)?)?.children().clone();
let mut lhs: Vec<Atom> = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children().into();
let rhs = TryInto::<&ExpressionAtom>::try_into(args.get(1).ok_or_else(arg_error)?)?.children();

let mut rhs_index: MultiTrie<SymbolAtom, Vec<usize>> = MultiTrie::new();
for (index, rhs_item) in rhs.iter().enumerate() {
Expand Down Expand Up @@ -1558,7 +1558,7 @@ impl CustomExecute for AssertEqualToResultOp {

let actual = interpret_no_error(self.space.clone(), actual_atom)?;

assert_results_equal(&actual, expected, actual_atom)
assert_results_equal(&actual, &expected.into(), actual_atom)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/metta/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn query_types(space: &dyn Space, atom: &Atom) -> Vec<Atom> {
pub fn get_arg_types<'a>(fn_typ: &'a Atom) -> (&'a [Atom], &'a Atom) {
match fn_typ {
Atom::Expression(expr) => {
let children = expr.children().as_slice();
let children = expr.children();
match children {
[op, args @ .., res] if *op == ARROW_SYMBOL => (args, res),
_ => panic!("Incorrect function type: {}", fn_typ)
Expand All @@ -151,7 +151,7 @@ fn get_op(expr: &ExpressionAtom) -> &Atom {
}

fn get_args(expr: &ExpressionAtom) -> &[Atom] {
&expr.children().as_slice()[1..]
&expr.children()[1..]
}

/// Returns vector of the types for the given `atom` in context of the given
Expand Down

0 comments on commit 631a41a

Please sign in to comment.