Skip to content

Commit

Permalink
Merge branch 'trueagi-io:main' into update_snet_gate
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWesker authored Nov 15, 2024
2 parents c60092e + 7035497 commit 9732ad0
Show file tree
Hide file tree
Showing 28 changed files with 2,046 additions and 5,681 deletions.
117 changes: 0 additions & 117 deletions .github/workflows/old_interpreter.yml

This file was deleted.

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
26 changes: 13 additions & 13 deletions c/src/metta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ pub extern "C" fn atom_error_message(atom: *const atom_ref_t, buf: *mut c_char,
/// @return The `atom_t` representing the atom
/// @note The returned `atom_t` must be freed with `atom_free()`
///
#[no_mangle] pub extern "C" fn ATOM_TYPE_UNIT() -> atom_t { hyperon::metta::UNIT_TYPE().into() }
#[no_mangle] pub extern "C" fn ATOM_TYPE_UNIT() -> atom_t { hyperon::metta::UNIT_TYPE.into() }

/// @brief Creates a Symbol atom for the special MeTTa symbol used to indicate empty results
/// returned by function.
Expand All @@ -636,7 +636,7 @@ pub extern "C" fn atom_error_message(atom: *const atom_ref_t, buf: *mut c_char,
/// @note The returned `atom_t` must be freed with `atom_free()`
///
#[no_mangle] pub extern "C" fn UNIT_ATOM() -> atom_t {
hyperon::metta::UNIT_ATOM().into()
hyperon::metta::UNIT_ATOM.into()
}

/// @brief Creates a Symbol atom for the special MeTTa symbol used to indicate
Expand Down Expand Up @@ -720,19 +720,19 @@ pub struct step_result_t {
result: *mut RustStepResult,
}

struct RustStepResult(InterpreterState<'static, DynSpace>);
struct RustStepResult(InterpreterState<DynSpace>);

impl From<InterpreterState<'static, DynSpace>> for step_result_t {
fn from(state: InterpreterState<'static, DynSpace>) -> Self {
impl From<InterpreterState<DynSpace>> for step_result_t {
fn from(state: InterpreterState<DynSpace>) -> Self {
Self{ result: Box::into_raw(Box::new(RustStepResult(state))) }
}
}

impl step_result_t {
fn into_inner(self) -> InterpreterState<'static, DynSpace> {
fn into_inner(self) -> InterpreterState<DynSpace> {
unsafe{ Box::from_raw(self.result).0 }
}
fn borrow(&self) -> &InterpreterState<'static, DynSpace> {
fn borrow(&self) -> &InterpreterState<DynSpace> {
&unsafe{ &*(&*self).result }.0
}
}
Expand Down Expand Up @@ -1210,22 +1210,22 @@ impl run_context_t {
}
}

struct RustRunContext(RunContext<'static, 'static, 'static>);
struct RustRunContext(RunContext<'static, 'static>);

impl From<&mut RunContext<'_, '_, '_>> for run_context_t {
fn from(context_ref: &mut RunContext<'_, '_, '_>) -> Self {
impl From<&mut RunContext<'_, '_>> for run_context_t {
fn from(context_ref: &mut RunContext<'_, '_>) -> Self {
Self {
context: (context_ref as *mut RunContext<'_, '_, '_>).cast(),
context: (context_ref as *mut RunContext<'_, '_>).cast(),
err_string: core::ptr::null_mut(),
}
}
}

impl run_context_t {
fn borrow(&self) -> &RunContext<'static, 'static, 'static> {
fn borrow(&self) -> &RunContext<'static, 'static> {
&unsafe{ &*self.context.cast::<RustRunContext>() }.0
}
fn borrow_mut(&mut self) -> &mut RunContext<'static, 'static, 'static> {
fn borrow_mut(&mut self) -> &mut RunContext<'static, 'static> {
&mut unsafe{ &mut *self.context.cast::<RustRunContext>() }.0
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ crate-type = ["lib"]
default = ["pkg_mgmt"]
# Add one of the features below into default list to enable.
# See https://doc.rust-lang.org/cargo/reference/features.html#the-features-section
old_interpreter = [] # enables old Rust interpreter
variable_operation = [] # enables evaluation of the expressions which have
# a variable on the first position
git = ["git2", "pkg_mgmt"]
Expand Down
1 change: 0 additions & 1 deletion lib/benches/interpreter_minimal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(test)]
#[cfg(not(feature = "old_interpreter"))]
mod interpreter_minimal_bench {

extern crate test;
Expand Down
1 change: 0 additions & 1 deletion lib/src/atom/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ impl Bindings {
self.bindings.remove(from_binding_id);
}

#[allow(dead_code)] //TODO: MINIMAL only silence the warning until interpreter2 replaces interpreter
pub(crate) fn len(&self) -> usize {
self.binding_by_var.len()
}
Expand Down
58 changes: 32 additions & 26 deletions lib/src/atom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ macro_rules! expr {
use $crate::*;
(&&$crate::Wrap($x)).to_atom()
}};
(($($x:tt)*)) => { $crate::Atom::expr(vec![ $( expr!($x) , )* ]) };
($($x:tt)*) => { $crate::Atom::expr(vec![ $( expr!($x) , )* ]) };
(($($x:tt)*)) => { $crate::Atom::expr([ $( expr!($x) , )* ]) };
($($x:tt)*) => { $crate::Atom::expr([ $( expr!($x) , )* ]) };
}

#[macro_export]
macro_rules! constexpr {
() => { $crate::Atom::Expression($crate::ExpressionAtom::new($crate::common::collections::CowArray::Literal(&[]))) };
($x:literal) => { $crate::Atom::Symbol($crate::SymbolAtom::new($crate::common::collections::ImmutableString::Literal($x))) };
(($($x:tt)*)) => { $crate::Atom::Expression($crate::ExpressionAtom::new($crate::common::collections::CowArray::Literal(const { &[ $( constexpr!($x) , )* ] }))) };
($($x:tt)*) => { $crate::Atom::Expression($crate::ExpressionAtom::new($crate::common::collections::CowArray::Literal(const { &[ $( constexpr!($x) , )* ] }))) };
}

/// Constructs new symbol atom. Can be used to construct `const` instances.
Expand Down Expand Up @@ -117,7 +125,7 @@ use std::any::Any;
use std::fmt::{Display, Debug};
use std::convert::TryFrom;

use crate::common::collections::ImmutableString;
use crate::common::collections::{ImmutableString, CowArray};

// Symbol atom

Expand All @@ -130,7 +138,6 @@ pub struct SymbolAtom {
impl SymbolAtom {
/// Constructs new symbol from `name`. Not intended to be used directly,
/// use [sym!] or [Atom::sym] instead.
#[doc(hidden)]
pub const fn new(name: ImmutableString) -> Self {
Self{ name }
}
Expand All @@ -152,14 +159,13 @@ impl Display for SymbolAtom {
/// An expression atom structure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpressionAtom {
children: Vec<Atom>,
children: CowArray<Atom>,
}

impl ExpressionAtom {
/// Constructs new expression from vector of sub-atoms. Not intended to be
/// used directly, use [Atom::expr] instead.
#[doc(hidden)]
pub(crate) fn new(children: Vec<Atom>) -> Self {
/// used directly, use [expr!], [constexpr!] or [Atom::expr] instead.
pub const fn new(children: CowArray<Atom>) -> Self {
Self{ children }
}

Expand All @@ -169,18 +175,18 @@ impl ExpressionAtom {
}

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

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

/// Converts into a vector of sub-atoms.
pub fn into_children(self) -> Vec<Atom> {
self.children
self.children.into()
}
}

Expand Down Expand Up @@ -837,7 +843,7 @@ impl Atom {
/// assert_eq!(expr, same_expr);
/// assert_ne!(expr, other_expr);
/// ```
pub fn expr<T: Into<Vec<Atom>>>(children: T) -> Self {
pub fn expr<T: Into<CowArray<Atom>>>(children: T) -> Self {
Self::Expression(ExpressionAtom::new(children.into()))
}

Expand Down Expand Up @@ -1013,7 +1019,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 +1029,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 Expand Up @@ -1093,8 +1099,8 @@ mod test {
}

#[inline]
fn expression(children: Vec<Atom>) -> Atom {
Atom::Expression(ExpressionAtom{ children })
fn expression<const N: usize>(children: [Atom; N]) -> Atom {
Atom::Expression(ExpressionAtom::new(CowArray::Allocated(Box::new(children))))
}

#[inline]
Expand Down Expand Up @@ -1175,15 +1181,15 @@ mod test {
#[test]
fn test_expr_expression() {
assert_eq!(expr!("=" ("fact" n) ("*" n ("-" n "1"))),
expression(vec![symbol("="), expression(vec![symbol("fact"), variable("n")]),
expression(vec![symbol("*"), variable("n"),
expression(vec![symbol("-"), variable("n"), symbol("1") ]) ]) ]));
expression([symbol("="), expression([symbol("fact"), variable("n")]),
expression([symbol("*"), variable("n"),
expression([symbol("-"), variable("n"), symbol("1") ]) ]) ]));
assert_eq!(expr!("=" n {[1, 2, 3]}),
expression(vec![symbol("="), variable("n"), value([1, 2, 3])]));
expression([symbol("="), variable("n"), value([1, 2, 3])]));
assert_eq!(expr!("=" {6} ("fact" n)),
expression(vec![symbol("="), value(6), expression(vec![symbol("fact"), variable("n")])]));
expression([symbol("="), value(6), expression([symbol("fact"), variable("n")])]));
assert_eq!(expr!({TestMulX(3)} {TestInteger(6)}),
expression(vec![grounded(TestMulX(3)), grounded(TestInteger(6))]));
expression([grounded(TestMulX(3)), grounded(TestInteger(6))]));
}

#[test]
Expand Down Expand Up @@ -1239,8 +1245,8 @@ mod test {
assert_eq!(Atom::gnd(TestMulX(3)).clone(), grounded(TestMulX(3)));
assert_eq!(Atom::expr([Atom::sym("="), Atom::value(6),
Atom::expr([Atom::sym("fact"), Atom::var("n")])]).clone(),
expression(vec![symbol("="), value(6),
expression(vec![symbol("fact"), variable("n")])]));
expression([symbol("="), value(6),
expression([symbol("fact"), variable("n")])]));
}

#[test]
Expand Down
Loading

0 comments on commit 9732ad0

Please sign in to comment.