New macro system and stdlib additions
This commit is contained in:
1
orchid-std/src/std/reflection/mod.rs
Normal file
1
orchid-std/src/std/reflection/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod sym_atom;
|
||||
67
orchid-std/src/std/reflection/sym_atom.rs
Normal file
67
orchid-std/src/std/reflection/sym_atom.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use orchid_api::TStrv;
|
||||
use orchid_api_derive::{Coding, Hierarchy};
|
||||
use orchid_api_traits::Request;
|
||||
use orchid_base::error::mk_errv;
|
||||
use orchid_base::name::{NameLike, Sym};
|
||||
use orchid_extension::atom::{Atomic, Supports, TAtom};
|
||||
use orchid_extension::atom_owned::{OwnedAtom, OwnedVariant, own};
|
||||
use orchid_extension::context::i;
|
||||
use orchid_extension::expr::{Expr, ExprHandle};
|
||||
use orchid_extension::system::dep_req;
|
||||
use orchid_extension::tree::{GenMember, fun, prefix};
|
||||
|
||||
use crate::std::std_system::StdReq;
|
||||
use crate::std::string::str_atom::IntStrAtom;
|
||||
use crate::std::string::to_string::ToStringMethod;
|
||||
use crate::{HomoTpl, StdSystem, api};
|
||||
|
||||
#[derive(Clone, Coding)]
|
||||
pub struct SymAtomData(pub api::TStrv);
|
||||
#[derive(Clone)]
|
||||
pub struct SymAtom(pub(crate) Sym);
|
||||
impl Atomic for SymAtom {
|
||||
type Data = SymAtomData;
|
||||
type Variant = OwnedVariant;
|
||||
}
|
||||
impl OwnedAtom for SymAtom {
|
||||
type Refs = ();
|
||||
async fn val(&self) -> Cow<'_, Self::Data> { Cow::Owned(SymAtomData(self.0.tok().to_api())) }
|
||||
}
|
||||
impl Supports<ToStringMethod> for SymAtom {
|
||||
async fn handle(&self, _: ToStringMethod) -> <ToStringMethod as Request>::Response {
|
||||
self.0.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Coding, Hierarchy)]
|
||||
#[extends(StdReq)]
|
||||
pub struct CreateSymAtom(pub TStrv);
|
||||
impl Request for CreateSymAtom {
|
||||
type Response = api::ExprTicket;
|
||||
}
|
||||
|
||||
pub async fn sym_expr(sym: Sym) -> Expr {
|
||||
Expr::from_handle(ExprHandle::deserialize(
|
||||
dep_req::<StdSystem, _>(CreateSymAtom(sym.to_api())).await,
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn gen_sym_lib() -> Vec<GenMember> {
|
||||
prefix("std::refl::sym", [
|
||||
fun(true, "from_str", async move |str: TAtom<IntStrAtom>| {
|
||||
match Sym::parse(&i().ex(*str).await, &i()).await {
|
||||
Ok(sym) => Ok(SymAtom(sym)),
|
||||
Err(_) => Err(mk_errv(
|
||||
i().i("Cannot parse sym from empty string").await,
|
||||
"Empty string passed to std::refl::sym::from_str",
|
||||
[str.pos()],
|
||||
)),
|
||||
}
|
||||
}),
|
||||
fun(true, "to_tpl", async move |sym: TAtom<SymAtom>| {
|
||||
HomoTpl(own(&sym).await.0.segs().map(IntStrAtom).collect())
|
||||
}),
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user