Began implementing fully isomorphic macros
Like Rust's Proc macros. Now we have preprocessor recursion to worry about. I also made a cool macro for enums
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use std::num::NonZero;
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
use dyn_clone::{clone_box, DynClone};
|
||||
use hashbrown::HashMap;
|
||||
@@ -8,7 +7,9 @@ use itertools::Itertools;
|
||||
use orchid_base::interner::{intern, Tok};
|
||||
use orchid_base::location::Pos;
|
||||
use orchid_base::name::Sym;
|
||||
use orchid_base::parse::Comment;
|
||||
use orchid_base::tree::{TokTree, Token};
|
||||
use ordered_float::NotNan;
|
||||
use substack::Substack;
|
||||
use trait_set::trait_set;
|
||||
|
||||
@@ -16,8 +17,9 @@ use crate::api;
|
||||
use crate::atom::{AtomFactory, ForeignAtom};
|
||||
use crate::conv::ToExpr;
|
||||
use crate::entrypoint::MemberRecord;
|
||||
use crate::expr::GenExpr;
|
||||
use crate::expr::Expr;
|
||||
use crate::func_atom::{ExprFunc, Fun};
|
||||
use crate::macros::Rule;
|
||||
use crate::system::SysCtx;
|
||||
|
||||
pub type GenTokTree<'a> = TokTree<'a, ForeignAtom<'a>, AtomFactory>;
|
||||
@@ -27,68 +29,85 @@ pub fn do_extra(f: &AtomFactory, r: Range<u32>, ctx: SysCtx) -> api::TokenTree {
|
||||
api::TokenTree { range: r, token: api::Token::Atom(f.clone().build(ctx)) }
|
||||
}
|
||||
|
||||
fn with_export(mem: GenMember, public: bool) -> Vec<GenItem> {
|
||||
(public.then(|| GenItemKind::Export(mem.name.clone()).at(Pos::Inherit)).into_iter())
|
||||
.chain([GenItemKind::Member(mem).at(Pos::Inherit)])
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub struct GenItem {
|
||||
pub item: GenItemKind,
|
||||
pub comments: Vec<(String, Pos)>,
|
||||
pub kind: GenItemKind,
|
||||
pub comments: Vec<Comment>,
|
||||
pub pos: Pos,
|
||||
}
|
||||
impl GenItem {
|
||||
pub fn into_api(self, ctx: &mut impl TreeIntoApiCtx) -> api::Item {
|
||||
let kind = match self.item {
|
||||
GenItemKind::Raw(item) => api::ItemKind::Raw(Vec::from_iter(
|
||||
item.into_iter().map(|t| t.to_api(&mut |f, r| do_extra(f, r, ctx.sys()))),
|
||||
)),
|
||||
let kind = match self.kind {
|
||||
GenItemKind::Export(n) => api::ItemKind::Export(n.marker()),
|
||||
GenItemKind::Member(mem) => api::ItemKind::Member(mem.into_api(ctx)),
|
||||
GenItemKind::Import(cn) => api::ItemKind::Import(cn.tok().marker()),
|
||||
GenItemKind::Macro(prio, rules) => api::ItemKind::Macro(api::MacroBlock {
|
||||
priority: prio,
|
||||
rules: rules.into_iter().map(|r| r.to_api() ).collect_vec(),
|
||||
})
|
||||
};
|
||||
let comments = self.comments.into_iter().map(|(s, p)| (Arc::new(s), p.to_api())).collect_vec();
|
||||
let comments = self.comments.into_iter().map(|c| c.to_api()).collect_vec();
|
||||
api::Item { location: self.pos.to_api(), comments, kind }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cnst(public: bool, name: &str, value: impl ToExpr) -> GenItem {
|
||||
let kind = GenMemberKind::Const(value.to_expr());
|
||||
GenItemKind::Member(GenMember { exported: public, name: intern(name), kind }).at(Pos::Inherit)
|
||||
pub fn cnst(public: bool, name: &str, value: impl ToExpr) -> Vec<GenItem> {
|
||||
with_export(GenMember { name: intern(name), kind: MemKind::Const(value.to_expr()) }, public)
|
||||
}
|
||||
pub fn module(
|
||||
public: bool,
|
||||
name: &str,
|
||||
imports: impl IntoIterator<Item = Sym>,
|
||||
items: impl IntoIterator<Item = GenItem>,
|
||||
) -> GenItem {
|
||||
items: impl IntoIterator<Item = Vec<GenItem>>,
|
||||
) -> Vec<GenItem> {
|
||||
let (name, kind) = root_mod(name, imports, items);
|
||||
GenItemKind::Member(GenMember { exported: public, name, kind }).at(Pos::Inherit)
|
||||
with_export(GenMember { name, kind }, public)
|
||||
}
|
||||
pub fn root_mod(
|
||||
name: &str,
|
||||
imports: impl IntoIterator<Item = Sym>,
|
||||
items: impl IntoIterator<Item = GenItem>,
|
||||
) -> (Tok<String>, GenMemberKind) {
|
||||
let kind = GenMemberKind::Mod {
|
||||
items: impl IntoIterator<Item = Vec<GenItem>>,
|
||||
) -> (Tok<String>, MemKind) {
|
||||
let kind = MemKind::Mod {
|
||||
imports: imports.into_iter().collect(),
|
||||
items: items.into_iter().collect(),
|
||||
items: items.into_iter().flatten().collect(),
|
||||
};
|
||||
(intern(name), kind)
|
||||
}
|
||||
pub fn fun<I, O>(exported: bool, name: &str, xf: impl ExprFunc<I, O>) -> GenItem {
|
||||
let fac = LazyMemberFactory::new(move |sym| GenMemberKind::Const(Fun::new(sym, xf).to_expr()));
|
||||
let mem = GenMember { exported, name: intern(name), kind: GenMemberKind::Lazy(fac) };
|
||||
GenItemKind::Member(mem).at(Pos::Inherit)
|
||||
pub fn fun<I, O>(exported: bool, name: &str, xf: impl ExprFunc<I, O>) -> Vec<GenItem> {
|
||||
let fac = LazyMemberFactory::new(move |sym| MemKind::Const(Fun::new(sym, xf).to_expr()));
|
||||
with_export(GenMember { name: intern(name), kind: MemKind::Lazy(fac) }, exported)
|
||||
}
|
||||
pub fn macro_block(prio: Option<f64>, rules: impl IntoIterator<Item = Rule>) -> Vec<GenItem> {
|
||||
let prio = prio.map(|p| NotNan::new(p).unwrap());
|
||||
vec![GenItemKind::Macro(prio, rules.into_iter().collect_vec()).gen()]
|
||||
}
|
||||
|
||||
pub fn comments<'a>(cmts: impl IntoIterator<Item = &'a str>, mut val: GenItem) -> GenItem {
|
||||
val.comments.extend(cmts.into_iter().map(|c| (c.to_string(), Pos::Inherit)));
|
||||
pub fn comments<'a>(
|
||||
cmts: impl IntoIterator<Item = &'a str> + Clone,
|
||||
mut val: Vec<GenItem>,
|
||||
) -> Vec<GenItem> {
|
||||
for v in val.iter_mut() {
|
||||
v.comments
|
||||
.extend(cmts.clone().into_iter().map(|c| Comment { text: intern(c), pos: Pos::Inherit }));
|
||||
}
|
||||
val
|
||||
}
|
||||
|
||||
trait_set! {
|
||||
trait LazyMemberCallback = FnOnce(Sym) -> GenMemberKind + Send + Sync + DynClone
|
||||
trait LazyMemberCallback = FnOnce(Sym) -> MemKind + Send + Sync + DynClone
|
||||
}
|
||||
pub struct LazyMemberFactory(Box<dyn LazyMemberCallback>);
|
||||
impl LazyMemberFactory {
|
||||
pub fn new(cb: impl FnOnce(Sym) -> GenMemberKind + Send + Sync + Clone + 'static) -> Self {
|
||||
pub fn new(cb: impl FnOnce(Sym) -> MemKind + Send + Sync + Clone + 'static) -> Self {
|
||||
Self(Box::new(cb))
|
||||
}
|
||||
pub fn build(self, path: Sym) -> GenMemberKind { (self.0)(path) }
|
||||
pub fn build(self, path: Sym) -> MemKind { (self.0)(path) }
|
||||
}
|
||||
impl Clone for LazyMemberFactory {
|
||||
fn clone(&self) -> Self { Self(clone_box(&*self.0)) }
|
||||
@@ -96,42 +115,48 @@ impl Clone for LazyMemberFactory {
|
||||
|
||||
pub enum GenItemKind {
|
||||
Member(GenMember),
|
||||
Raw(Vec<GenTokTree<'static>>),
|
||||
Export(Tok<String>),
|
||||
Import(Sym),
|
||||
Macro(Option<NotNan<f64>>, Vec<Rule>),
|
||||
}
|
||||
impl GenItemKind {
|
||||
pub fn at(self, position: Pos) -> GenItem {
|
||||
GenItem { item: self, comments: vec![], pos: position }
|
||||
pub fn at(self, pos: Pos) -> GenItem { GenItem { kind: self, comments: vec![], pos } }
|
||||
pub fn gen(self) -> GenItem { GenItem { kind: self, comments: vec![], pos: Pos::Inherit } }
|
||||
pub fn gen_equiv(self, comments: Vec<Comment>) -> GenItem {
|
||||
GenItem { kind: self, comments, pos: Pos::Inherit }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GenMember {
|
||||
exported: bool,
|
||||
name: Tok<String>,
|
||||
kind: GenMemberKind,
|
||||
kind: MemKind,
|
||||
}
|
||||
impl GenMember {
|
||||
pub fn into_api(self, ctx: &mut impl TreeIntoApiCtx) -> api::Member {
|
||||
api::Member {
|
||||
name: self.name.marker(),
|
||||
exported: self.exported,
|
||||
kind: self.kind.into_api(&mut ctx.push_path(self.name)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum GenMemberKind {
|
||||
Const(GenExpr),
|
||||
pub enum MemKind {
|
||||
Const(Expr),
|
||||
Mod { imports: Vec<Sym>, items: Vec<GenItem> },
|
||||
Lazy(LazyMemberFactory),
|
||||
}
|
||||
impl GenMemberKind {
|
||||
impl MemKind {
|
||||
pub fn into_api(self, ctx: &mut impl TreeIntoApiCtx) -> api::MemberKind {
|
||||
match self {
|
||||
Self::Lazy(lazy) => api::MemberKind::Lazy(ctx.with_lazy(lazy)),
|
||||
Self::Const(c) => api::MemberKind::Const(c.into_api(ctx.sys())),
|
||||
Self::Const(c) =>
|
||||
api::MemberKind::Const(c.api_return(ctx.sys(), &mut |_| panic!("Slot found in const tree"))),
|
||||
Self::Mod { imports, items } => api::MemberKind::Module(api::Module {
|
||||
imports: imports.into_iter().map(|t| t.tok().marker()).collect(),
|
||||
items: items.into_iter().map(|i| i.into_api(ctx)).collect_vec(),
|
||||
items: (imports.into_iter())
|
||||
.map(|t| GenItemKind::Import(t).gen())
|
||||
.chain(items)
|
||||
.map(|i| i.into_api(ctx))
|
||||
.collect_vec(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user