Fixed doctest

This commit is contained in:
2023-05-29 20:59:47 +01:00
parent 0cc8094823
commit f28c922f66
7 changed files with 47 additions and 24 deletions

View File

@@ -9,7 +9,7 @@ use dyn_clone::DynClone;
#[allow(unused)] // for the doc comments #[allow(unused)] // for the doc comments
use crate::foreign::{Atomic, ExternFn}; use crate::foreign::{Atomic, ExternFn};
#[allow(unused)] // for the doc comments #[allow(unused)] // for the doc comments
use crate::representations::Primitive; use crate::Primitive;
/// A macro that generates implementations of [Atomic] to simplify the /// A macro that generates implementations of [Atomic] to simplify the
/// development of external bindings for Orchid. /// development of external bindings for Orchid.
@@ -17,7 +17,7 @@ use crate::representations::Primitive;
/// The macro depends on implementations of [`AsRef<Clause>`] and /// The macro depends on implementations of [`AsRef<Clause>`] and
/// [`From<(&Self, Clause)>`] for extracting the clause to be processed and then /// [`From<(&Self, Clause)>`] for extracting the clause to be processed and then
/// reconstructing the [Atomic]. Naturally, supertraits of [Atomic] are also /// 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 /// The simplest form just requires the typename to be specified. This
/// additionally depends on an implementation of [ExternFn] because after the /// 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 /// function where validation and the next state are defined in
/// [ExternFn::apply]. /// [ExternFn::apply].
/// ///
/// ```
/// atomic_impl!(Multiply1)
/// ```
///
/// The last stage of the function should use the extended form of the macro /// 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 /// which takes an additional closure to explicitly describe what happens when
/// the argument is fully processed. /// the argument is fully processed.
/// ///
/// _definition of the `add` function in the STL_
/// ``` /// ```
/// // excerpt from the exact implementation of Multiply /// use orchidlang::stl::Numeric;
/// atomic_impl!(Multiply0, |Self(a, cls): &Self| { /// use orchidlang::interpreted::ExprInst;
/// let b: Numeric = /// use orchidlang::{atomic_impl, atomic_redirect, externfn_impl};
/// cls.clone().try_into().map_err(AssertionError::into_extern)?; ///
/// Ok(*a * b).into() /// #[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_export]
macro_rules! atomic_impl { macro_rules! atomic_impl {
@@ -60,7 +75,7 @@ macro_rules! atomic_impl {
) -> $crate::foreign::AtomicResult { ) -> $crate::foreign::AtomicResult {
// extract the expression // extract the expression
let expr = <Self as AsRef< let expr = <Self as AsRef<
$crate::representations::interpreted::ExprInst, $crate::interpreted::ExprInst,
>>::as_ref(self) >>::as_ref(self)
.clone(); .clone();
// run the expression // run the expression
@@ -69,7 +84,7 @@ macro_rules! atomic_impl {
// rebuild the atomic // rebuild the atomic
let next_self = <Self as From<( let next_self = <Self as From<(
&Self, &Self,
$crate::representations::interpreted::ExprInst, $crate::interpreted::ExprInst,
)>>::from((self, state)); )>>::from((self, state));
// branch off or wrap up // branch off or wrap up
let clause = if inert { let clause = if inert {

View File

@@ -18,17 +18,17 @@ macro_rules! atomic_redirect {
} }
}; };
($typ:ident, $field:ident) => { ($typ:ident, $field:ident) => {
impl AsRef<$crate::representations::interpreted::ExprInst> for $typ { impl AsRef<$crate::interpreted::ExprInst> for $typ {
fn as_ref(&self) -> &$crate::representations::interpreted::ExprInst { fn as_ref(&self) -> &$crate::interpreted::ExprInst {
&self.$field &self.$field
} }
} }
impl From<(&Self, $crate::representations::interpreted::ExprInst)> impl From<(&Self, $crate::interpreted::ExprInst)>
for $typ for $typ
{ {
#[allow(clippy::needless_update)] #[allow(clippy::needless_update)]
fn from( fn from(
(old, $field): (&Self, $crate::representations::interpreted::ExprInst), (old, $field): (&Self, $crate::interpreted::ExprInst),
) -> Self { ) -> Self {
Self { $field, ..old.clone() } Self { $field, ..old.clone() }
} }

View File

@@ -28,14 +28,14 @@ macro_rules! externfn_impl {
} }
fn apply( fn apply(
&self, &self,
arg: $crate::representations::interpreted::ExprInst, arg: $crate::interpreted::ExprInst,
_ctx: $crate::interpreter::Context, _ctx: $crate::interpreter::Context,
) -> $crate::foreign::XfnResult { ) -> $crate::foreign::XfnResult {
let closure = $next_atomic; let closure = $next_atomic;
match closure(self, arg) { match closure(self, arg) {
// ? casts the result but we want to strictly forward it // ? casts the result but we want to strictly forward it
Ok(r) => Ok($crate::representations::interpreted::Clause::P( Ok(r) => Ok($crate::interpreted::Clause::P(
$crate::representations::Primitive::Atom( $crate::Primitive::Atom(
$crate::foreign::Atom::new(r), $crate::foreign::Atom::new(r),
), ),
)), )),

View File

@@ -1,6 +1,10 @@
#![deny(missing_docs)] #![deny(missing_docs)]
#![doc(html_logo_url = "https://raw.githubusercontent.com/lbfalvy/orchid/master/icon.svg")] #![doc(
#![doc(html_favicon_url = "https://raw.githubusercontent.com/lbfalvy/orchid/master/icon.svg")] 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 //! Orchid is a lazy, pure scripting language to be embedded in Rust
//! applications. Check out the repo for examples and other links. //! applications. Check out the repo for examples and other links.
pub mod foreign; pub mod foreign;

View File

@@ -17,7 +17,7 @@ pub enum Location {
/// Argument to the file loading callback that produced this code /// Argument to the file loading callback that produced this code
file: Rc<Vec<String>>, file: Rc<Vec<String>>,
/// Index of the unicode code points associated with the code /// Index of the unicode code points associated with the code
range: Range<usize> range: Range<usize>,
}, },
} }

View File

@@ -11,3 +11,5 @@ mod str;
pub use cpsio::{handle as handleIO, IO}; pub use cpsio::{handle as handleIO, IO};
pub use mk_stl::mk_stl; pub use mk_stl::mk_stl;
pub use self::bool::Boolean;
pub use self::num::Numeric;

View File

@@ -12,7 +12,9 @@ use crate::representations::{Literal, Primitive};
/// A number, either floating point or unsigned int, visible to Orchid. /// A number, either floating point or unsigned int, visible to Orchid.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Numeric { pub enum Numeric {
/// A nonnegative integer such as a size, index or count
Uint(u64), Uint(u64),
/// A float other than NaN. Orchid has no silent errors
Num(NotNan<f64>), Num(NotNan<f64>),
} }