From f28c922f66b8ae497447c909823e35a5f993ca1c Mon Sep 17 00:00:00 2001 From: Lawrence Bethlenfalvy Date: Mon, 29 May 2023 20:59:47 +0100 Subject: [PATCH] Fixed doctest --- src/foreign_macros/atomic_impl.rs | 43 ++++++++++++++++++--------- src/foreign_macros/atomic_redirect.rs | 8 ++--- src/foreign_macros/externfn_impl.rs | 6 ++-- src/lib.rs | 8 +++-- src/representations/location.rs | 2 +- src/stl/mod.rs | 2 ++ src/stl/num/numeric.rs | 2 ++ 7 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/foreign_macros/atomic_impl.rs b/src/foreign_macros/atomic_impl.rs index 6ac785f..92a945c 100644 --- a/src/foreign_macros/atomic_impl.rs +++ b/src/foreign_macros/atomic_impl.rs @@ -9,7 +9,7 @@ use dyn_clone::DynClone; #[allow(unused)] // for the doc comments use crate::foreign::{Atomic, ExternFn}; #[allow(unused)] // for the doc comments -use crate::representations::Primitive; +use crate::Primitive; /// A macro that generates implementations of [Atomic] to simplify the /// development of external bindings for Orchid. @@ -17,7 +17,7 @@ use crate::representations::Primitive; /// The macro depends on implementations of [`AsRef`] and /// [`From<(&Self, Clause)>`] for extracting the clause to be processed and then /// reconstructing the [Atomic]. Naturally, supertraits of [Atomic] are also -/// dependencies. These are [Any], [Debug] and [DynClone]. +/// dependencies. These are [Any], [Debug] and [Clone]. /// /// The simplest form just requires the typename to be specified. This /// additionally depends on an implementation of [ExternFn] because after the @@ -26,21 +26,36 @@ use crate::representations::Primitive; /// function where validation and the next state are defined in /// [ExternFn::apply]. /// -/// ``` -/// atomic_impl!(Multiply1) -/// ``` -/// /// The last stage of the function should use the extended form of the macro /// which takes an additional closure to explicitly describe what happens when /// the argument is fully processed. /// +/// _definition of the `add` function in the STL_ /// ``` -/// // excerpt from the exact implementation of Multiply -/// atomic_impl!(Multiply0, |Self(a, cls): &Self| { -/// let b: Numeric = -/// cls.clone().try_into().map_err(AssertionError::into_extern)?; -/// Ok(*a * b).into() -/// }) +/// use orchidlang::stl::Numeric; +/// use orchidlang::interpreted::ExprInst; +/// use orchidlang::{atomic_impl, atomic_redirect, externfn_impl}; +/// +/// #[derive(Clone)] +/// pub struct Add2; +/// externfn_impl!(Add2, |_: &Self, x: ExprInst| Ok(Add1 { x })); +/// +/// #[derive(Debug, Clone)] +/// pub struct Add1 { x: ExprInst } +/// atomic_redirect!(Add1, x); +/// atomic_impl!(Add1); +/// externfn_impl!(Add1, |this: &Self, x: ExprInst| { +/// let a: Numeric = this.x.clone().try_into()?; +/// Ok(Add0 { a, x }) +/// }); +/// +/// #[derive(Debug, Clone)] +/// pub struct Add0 { a: Numeric, x: ExprInst } +/// atomic_redirect!(Add0, x); +/// atomic_impl!(Add0, |Self { a, x }: &Self, _| { +/// let b: Numeric = x.clone().try_into()?; +/// Ok((*a + b).into()) +/// }); /// ``` #[macro_export] macro_rules! atomic_impl { @@ -60,7 +75,7 @@ macro_rules! atomic_impl { ) -> $crate::foreign::AtomicResult { // extract the expression let expr = >::as_ref(self) .clone(); // run the expression @@ -69,7 +84,7 @@ macro_rules! atomic_impl { // rebuild the atomic let next_self = >::from((self, state)); // branch off or wrap up let clause = if inert { diff --git a/src/foreign_macros/atomic_redirect.rs b/src/foreign_macros/atomic_redirect.rs index 353a9a3..79e51a5 100644 --- a/src/foreign_macros/atomic_redirect.rs +++ b/src/foreign_macros/atomic_redirect.rs @@ -18,17 +18,17 @@ macro_rules! atomic_redirect { } }; ($typ:ident, $field:ident) => { - impl AsRef<$crate::representations::interpreted::ExprInst> for $typ { - fn as_ref(&self) -> &$crate::representations::interpreted::ExprInst { + impl AsRef<$crate::interpreted::ExprInst> for $typ { + fn as_ref(&self) -> &$crate::interpreted::ExprInst { &self.$field } } - impl From<(&Self, $crate::representations::interpreted::ExprInst)> + impl From<(&Self, $crate::interpreted::ExprInst)> for $typ { #[allow(clippy::needless_update)] fn from( - (old, $field): (&Self, $crate::representations::interpreted::ExprInst), + (old, $field): (&Self, $crate::interpreted::ExprInst), ) -> Self { Self { $field, ..old.clone() } } diff --git a/src/foreign_macros/externfn_impl.rs b/src/foreign_macros/externfn_impl.rs index ca5f094..d4b0d79 100644 --- a/src/foreign_macros/externfn_impl.rs +++ b/src/foreign_macros/externfn_impl.rs @@ -28,14 +28,14 @@ macro_rules! externfn_impl { } fn apply( &self, - arg: $crate::representations::interpreted::ExprInst, + arg: $crate::interpreted::ExprInst, _ctx: $crate::interpreter::Context, ) -> $crate::foreign::XfnResult { let closure = $next_atomic; match closure(self, arg) { // ? casts the result but we want to strictly forward it - Ok(r) => Ok($crate::representations::interpreted::Clause::P( - $crate::representations::Primitive::Atom( + Ok(r) => Ok($crate::interpreted::Clause::P( + $crate::Primitive::Atom( $crate::foreign::Atom::new(r), ), )), diff --git a/src/lib.rs b/src/lib.rs index a704f6f..84706a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,10 @@ #![deny(missing_docs)] -#![doc(html_logo_url = "https://raw.githubusercontent.com/lbfalvy/orchid/master/icon.svg")] -#![doc(html_favicon_url = "https://raw.githubusercontent.com/lbfalvy/orchid/master/icon.svg")] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/lbfalvy/orchid/master/icon.svg" +)] +#![doc( + html_favicon_url = "https://raw.githubusercontent.com/lbfalvy/orchid/master/icon.svg" +)] //! Orchid is a lazy, pure scripting language to be embedded in Rust //! applications. Check out the repo for examples and other links. pub mod foreign; diff --git a/src/representations/location.rs b/src/representations/location.rs index 03873cb..33e0126 100644 --- a/src/representations/location.rs +++ b/src/representations/location.rs @@ -17,7 +17,7 @@ pub enum Location { /// Argument to the file loading callback that produced this code file: Rc>, /// Index of the unicode code points associated with the code - range: Range + range: Range, }, } diff --git a/src/stl/mod.rs b/src/stl/mod.rs index 8c0ce3d..1bfbaf1 100644 --- a/src/stl/mod.rs +++ b/src/stl/mod.rs @@ -11,3 +11,5 @@ mod str; pub use cpsio::{handle as handleIO, IO}; pub use mk_stl::mk_stl; +pub use self::bool::Boolean; +pub use self::num::Numeric; \ No newline at end of file diff --git a/src/stl/num/numeric.rs b/src/stl/num/numeric.rs index 10b1c0f..fdee860 100644 --- a/src/stl/num/numeric.rs +++ b/src/stl/num/numeric.rs @@ -12,7 +12,9 @@ use crate::representations::{Literal, Primitive}; /// A number, either floating point or unsigned int, visible to Orchid. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Numeric { + /// A nonnegative integer such as a size, index or count Uint(u64), + /// A float other than NaN. Orchid has no silent errors Num(NotNan), }