forked from Orchid/orchid
STL rework
- fixed lots of bugs - overlay libraries work correctly and reliably - the STL is an overlay library - examples updated
This commit is contained in:
@@ -6,14 +6,21 @@ use std::fmt::Debug;
|
||||
#[allow(unused)] // for the doc comments
|
||||
use dyn_clone::DynClone;
|
||||
|
||||
#[allow(unused)] // for the doc comments
|
||||
use crate::define_fn;
|
||||
#[allow(unused)] // for the doc comments
|
||||
use crate::foreign::{Atomic, ExternFn};
|
||||
#[allow(unused)] // for the doc comments
|
||||
use crate::write_fn_step;
|
||||
#[allow(unused)] // for the doc comments
|
||||
use crate::Primitive;
|
||||
|
||||
/// A macro that generates implementations of [Atomic] to simplify the
|
||||
/// development of external bindings for Orchid.
|
||||
///
|
||||
/// Most use cases are fulfilled by [define_fn], pathological cases can combine
|
||||
/// [write_fn_step] with manual [Atomic] implementations.
|
||||
///
|
||||
/// The macro depends on implementations of [`AsRef<Clause>`] and
|
||||
/// [`From<(&Self, Clause)>`] for extracting the clause to be processed and then
|
||||
/// reconstructing the [Atomic]. Naturally, supertraits of [Atomic] are also
|
||||
@@ -32,34 +39,35 @@ use crate::Primitive;
|
||||
///
|
||||
/// _definition of the `add` function in the STL_
|
||||
/// ```
|
||||
/// use orchidlang::{Literal};
|
||||
/// use orchidlang::interpreted::ExprInst;
|
||||
/// use orchidlang::stl::Numeric;
|
||||
/// use orchidlang::stl::litconv::with_lit;
|
||||
/// use orchidlang::{atomic_impl, atomic_redirect, externfn_impl};
|
||||
///
|
||||
/// /// Convert a literal to a string using Rust's conversions for floats, chars and
|
||||
/// /// uints respectively
|
||||
/// #[derive(Clone)]
|
||||
/// pub struct Add2;
|
||||
/// externfn_impl!(Add2, |_: &Self, x: ExprInst| Ok(Add1 { x }));
|
||||
/// struct ToString;
|
||||
///
|
||||
/// #[derive(Debug, Clone)]
|
||||
/// pub struct Add1 {
|
||||
/// x: ExprInst,
|
||||
/// externfn_impl!{
|
||||
/// ToString, |_: &Self, expr_inst: ExprInst|{
|
||||
/// Ok(InternalToString {
|
||||
/// expr_inst
|
||||
/// })
|
||||
/// }
|
||||
/// }
|
||||
/// 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,
|
||||
/// #[derive(std::fmt::Debug,Clone)]
|
||||
/// struct InternalToString {
|
||||
/// expr_inst: ExprInst,
|
||||
/// }
|
||||
/// atomic_redirect!(Add0, x);
|
||||
/// atomic_impl!(Add0, |Self { a, x }: &Self, _| {
|
||||
/// let b: Numeric = x.clone().try_into()?;
|
||||
/// Ok((*a + b).into())
|
||||
/// atomic_redirect!(InternalToString, expr_inst);
|
||||
/// atomic_impl!(InternalToString, |Self { expr_inst }: &Self, _|{
|
||||
/// with_lit(expr_inst, |l| Ok(match l {
|
||||
/// Literal::Char(c) => c.to_string(),
|
||||
/// Literal::Uint(i) => i.to_string(),
|
||||
/// Literal::Num(n) => n.to_string(),
|
||||
/// Literal::Str(s) => s.clone(),
|
||||
/// })).map(|s| Literal::Str(s).into())
|
||||
/// });
|
||||
/// ```
|
||||
#[macro_export]
|
||||
@@ -92,7 +100,11 @@ macro_rules! atomic_impl {
|
||||
// branch off or wrap up
|
||||
let clause = if inert {
|
||||
let closure = $next_phase;
|
||||
match closure(&next_self, ctx) {
|
||||
let res: Result<
|
||||
$crate::interpreted::Clause,
|
||||
std::rc::Rc<dyn $crate::foreign::ExternError>,
|
||||
> = closure(&next_self, ctx);
|
||||
match res {
|
||||
Ok(r) => r,
|
||||
Err(e) => return Err($crate::interpreter::RuntimeError::Extern(e)),
|
||||
}
|
||||
|
||||
@@ -26,6 +26,15 @@ use crate::write_fn_step;
|
||||
/// defined in the first step and returns a [Result] of the success type or
|
||||
/// `Rc<dyn ExternError>`.
|
||||
///
|
||||
/// To avoid typing the same expression a lot, the conversion is optional.
|
||||
/// If it is omitted, the field is initialized with a [TryInto::try_into] call
|
||||
/// from `&ExprInst` to the target type. In this case, the error is
|
||||
/// short-circuited using `?` so conversions through `FromResidual` are allowed.
|
||||
/// The optional syntax starts with `as`.
|
||||
///
|
||||
/// If all conversions are omitted, the alias definition (`expr=$ident in`) has
|
||||
/// no effect and is therefore optional.
|
||||
///
|
||||
/// Finally, the body of the function is provided as an expression which can
|
||||
/// reference all of the arguments by their names, each bound to a ref of the
|
||||
/// specified type.
|
||||
@@ -45,13 +54,59 @@ use crate::write_fn_step;
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// A simpler format is also offered for unary functions:
|
||||
///
|
||||
/// ```
|
||||
/// use orchidlang::stl::litconv::with_lit;
|
||||
/// use orchidlang::{define_fn, Literal};
|
||||
///
|
||||
/// define_fn! {
|
||||
/// /// Convert a literal to a string using Rust's conversions for floats,
|
||||
/// /// chars and uints respectively
|
||||
/// ToString = |x| with_lit(x, |l| Ok(match l {
|
||||
/// Literal::Char(c) => c.to_string(),
|
||||
/// Literal::Uint(i) => i.to_string(),
|
||||
/// Literal::Num(n) => n.to_string(),
|
||||
/// Literal::Str(s) => s.clone(),
|
||||
/// })).map(|s| Literal::Str(s).into())
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! define_fn {
|
||||
// Unary function entry
|
||||
($( #[ $attr:meta ] )* $qual:vis $name:ident = $body:expr) => {paste::paste!{
|
||||
$crate::write_fn_step!(
|
||||
$( #[ $attr ] )* $qual $name
|
||||
>
|
||||
[< Internal $name >]
|
||||
);
|
||||
$crate::write_fn_step!(
|
||||
[< Internal $name >]
|
||||
{}
|
||||
out = expr => Ok(expr);
|
||||
{
|
||||
let lambda = $body;
|
||||
lambda(out)
|
||||
}
|
||||
);
|
||||
}};
|
||||
// xname is optional only if every conversion is implicit
|
||||
($( #[ $attr:meta ] )* $qual:vis $name:ident {
|
||||
$( $arg:ident: $typ:ty ),+
|
||||
} => $body:expr) => {
|
||||
$crate::define_fn!{expr=expr in
|
||||
$( #[ $attr ] )* $qual $name {
|
||||
$( $arg: $typ ),*
|
||||
} => $body
|
||||
}
|
||||
};
|
||||
// multi-parameter function entry
|
||||
(expr=$xname:ident in
|
||||
$( #[ $attr:meta ] )*
|
||||
$qual:vis $name:ident {
|
||||
$arg0:ident: $typ0:ty as $parse0:expr
|
||||
$(, $arg:ident: $typ:ty as $parse:expr )*
|
||||
$arg0:ident: $typ0:ty $( as $parse0:expr )?
|
||||
$(, $arg:ident: $typ:ty $( as $parse:expr )? )*
|
||||
} => $body:expr
|
||||
) => {paste::paste!{
|
||||
// Generate initial state
|
||||
@@ -64,8 +119,10 @@ macro_rules! define_fn {
|
||||
$crate::define_fn!(@MIDDLE $xname [< Internal $name >] ($body)
|
||||
()
|
||||
(
|
||||
($arg0: $typ0 as $parse0)
|
||||
$( ($arg: $typ as $parse) )*
|
||||
( $arg0: $typ0 $( as $parse0)? )
|
||||
$(
|
||||
( $arg: $typ $( as $parse)? )
|
||||
)*
|
||||
)
|
||||
);
|
||||
}};
|
||||
@@ -80,10 +137,10 @@ macro_rules! define_fn {
|
||||
// later fields
|
||||
(
|
||||
// field that should be processed by this step
|
||||
( $arg0:ident: $typ0:ty as $parse0:expr )
|
||||
( $arg0:ident: $typ0:ty $( as $parse0:expr )? )
|
||||
// ensure that we have a next stage
|
||||
$(
|
||||
( $arg:ident: $typ:ty as $parse:expr )
|
||||
( $arg:ident: $typ:ty $( as $parse:expr )? )
|
||||
)+
|
||||
)
|
||||
) => {paste::paste!{
|
||||
@@ -93,7 +150,7 @@ macro_rules! define_fn {
|
||||
$( $arg_prev:ident : $typ_prev:ty ),*
|
||||
}
|
||||
[< $name $arg0:upper >]
|
||||
where $arg0:$typ0 = $xname => $parse0;
|
||||
where $arg0:$typ0 $( = $xname => $parse0 )? ;
|
||||
);
|
||||
$crate::define_fn!(@MIDDLE $xname [< $name $arg0:upper >] ($body)
|
||||
(
|
||||
@@ -101,7 +158,9 @@ macro_rules! define_fn {
|
||||
($arg0: $typ0)
|
||||
)
|
||||
(
|
||||
$( ($arg: $typ as $parse) )+
|
||||
$(
|
||||
( $arg: $typ $( as $parse)? )
|
||||
)+
|
||||
)
|
||||
);
|
||||
}};
|
||||
@@ -113,7 +172,7 @@ macro_rules! define_fn {
|
||||
)
|
||||
// the last one is initialized before the body runs
|
||||
(
|
||||
($arg0:ident: $typ0:ty as $parse0:expr)
|
||||
($arg0:ident: $typ0:ty $( as $parse0:expr )? )
|
||||
)
|
||||
) => {
|
||||
$crate::write_fn_step!(
|
||||
@@ -121,7 +180,7 @@ macro_rules! define_fn {
|
||||
{
|
||||
$( $arg_prev: $typ_prev ),*
|
||||
}
|
||||
$arg0:$typ0 = $xname => $parse0;
|
||||
$arg0:$typ0 $( = $xname => $parse0 )? ;
|
||||
$body
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
#[allow(unused)] // for doc
|
||||
use crate::define_fn;
|
||||
#[allow(unused)] // for doc
|
||||
use crate::foreign::Atomic;
|
||||
#[allow(unused)] // for doc
|
||||
use crate::foreign::ExternFn;
|
||||
#[allow(unused)] // for doc
|
||||
use crate::interpreted::ExprInst;
|
||||
|
||||
/// Write one step in the state machine representing a simple n-ary non-variadic
|
||||
/// Orchid function. There are no known use cases for it that aren't expressed
|
||||
/// better with [define_fn] which generates calls to this macro.
|
||||
/// Orchid function. Most use cases are better covered by [define_fn] which
|
||||
/// generates calls to this macro. This macro can be used in combination with
|
||||
/// manual [Atomic] implementations to define a function that only behaves like
|
||||
/// a simple n-ary non-variadic function with respect to some of its arguments.
|
||||
///
|
||||
/// There are three ways to call this macro for the initial state, internal
|
||||
/// state, and exit state. All of them are demonstrated in one example and
|
||||
@@ -25,14 +29,14 @@ use crate::interpreted::ExprInst;
|
||||
/// // Middle state
|
||||
/// write_fn_step!(
|
||||
/// CharAt1 {}
|
||||
/// CharAt0 where s = x => with_str(x, |s| Ok(s.clone()));
|
||||
/// CharAt0 where s: String = x => with_str(x, |s| Ok(s.clone()));
|
||||
/// );
|
||||
/// // Exit state
|
||||
/// write_fn_step!(
|
||||
/// CharAt0 { s: String }
|
||||
/// i = x => with_uint(x, Ok);
|
||||
/// {
|
||||
/// if let Some(c) = s.chars().nth(i as usize) {
|
||||
/// if let Some(c) = s.chars().nth(*i as usize) {
|
||||
/// Ok(Clause::P(Primitive::Literal(Literal::Char(c))))
|
||||
/// } else {
|
||||
/// RuntimeError::fail(
|
||||
@@ -52,7 +56,7 @@ use crate::interpreted::ExprInst;
|
||||
/// struct definition. A field called `expr_inst` of type [ExprInst] is added
|
||||
/// implicitly, so the first middle state has an empty field list. The next
|
||||
/// state is also provided, alongside the name and conversion of the next
|
||||
/// parameter from a [&ExprInst] under the provided alias to a
|
||||
/// parameter from a `&ExprInst` under the provided alias to a
|
||||
/// `Result<_, Rc<dyn ExternError>>`. The success type is inferred from the
|
||||
/// type of the field at the place of its actual definition. This conversion is
|
||||
/// done in the implementation of [ExternFn] which also places the new
|
||||
@@ -67,6 +71,12 @@ use crate::interpreted::ExprInst;
|
||||
/// argument names bound. The arguments here are all references to their actual
|
||||
/// types except for the last one which is converted from [ExprInst] immediately
|
||||
/// before the body is evaluated.
|
||||
///
|
||||
/// To avoid typing the same parsing process a lot, the conversion is optional.
|
||||
/// If it is omitted, the field is initialized with a [TryInto::try_into] call
|
||||
/// from `&ExprInst` to the target type. In this case, the error is
|
||||
/// short-circuited using `?` so conversions through `FromResidual` are allowed.
|
||||
/// The optional syntax starts with the `=` sign and ends before the semicolon.
|
||||
#[macro_export]
|
||||
macro_rules! write_fn_step {
|
||||
// write entry stage
|
||||
@@ -87,7 +97,7 @@ macro_rules! write_fn_step {
|
||||
$( $arg:ident : $typ:ty ),*
|
||||
}
|
||||
$next:ident where
|
||||
$added:ident $( : $added_typ:ty )? = $xname:ident => $extract:expr ;
|
||||
$added:ident $( : $added_typ:ty )? $( = $xname:ident => $extract:expr )? ;
|
||||
) => {
|
||||
$( #[ $attr ] )*
|
||||
#[derive(std::fmt::Debug, Clone)]
|
||||
@@ -100,8 +110,8 @@ macro_rules! write_fn_step {
|
||||
$crate::externfn_impl!(
|
||||
$name,
|
||||
|this: &Self, expr_inst: $crate::interpreted::ExprInst| {
|
||||
let $xname = &this.expr_inst;
|
||||
let $added $( :$added_typ )? = $extract?;
|
||||
let $added $( :$added_typ )? =
|
||||
$crate::write_fn_step!(@CONV &this.expr_inst $(, $xname $extract )?);
|
||||
Ok($next{
|
||||
$( $arg: this.$arg.clone(), )*
|
||||
$added, expr_inst
|
||||
@@ -114,23 +124,37 @@ macro_rules! write_fn_step {
|
||||
$( #[ $attr:meta ] )* $quant:vis $name:ident {
|
||||
$( $arg:ident: $typ:ty ),*
|
||||
}
|
||||
$added:ident $(: $added_typ:ty )? = $xname:ident => $extract:expr ;
|
||||
$added:ident $(: $added_typ:ty )? $( = $xname:ident => $extract:expr )? ;
|
||||
$process:expr
|
||||
) => {
|
||||
$( #[ $attr ] )*
|
||||
#[derive(std::fmt::Debug, Clone)]
|
||||
$quant struct $name {
|
||||
$( $arg: $typ, )+
|
||||
$( $arg: $typ, )*
|
||||
expr_inst: $crate::interpreted::ExprInst,
|
||||
}
|
||||
$crate::atomic_redirect!($name, expr_inst);
|
||||
$crate::atomic_impl!(
|
||||
$name,
|
||||
|Self{ $($arg, )* expr_inst }: &Self, _| {
|
||||
let $xname = expr_inst;
|
||||
let $added $(: $added_typ )? = $extract?;
|
||||
let added $(: $added_typ )? =
|
||||
$crate::write_fn_step!(@CONV expr_inst $(, $xname $extract )?);
|
||||
let $added = &added;
|
||||
$process
|
||||
}
|
||||
);
|
||||
};
|
||||
// Write conversion expression for an ExprInst
|
||||
(@CONV $locxname:expr, $xname:ident $extract:expr) => {
|
||||
{
|
||||
let $xname = $locxname;
|
||||
match $extract {
|
||||
Err(e) => return Err(e),
|
||||
Ok(r) => r,
|
||||
}
|
||||
}
|
||||
};
|
||||
(@CONV $locxname:expr) => {
|
||||
($locxname).try_into()?
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user