forked from Orchid/orchid
Gitbutler >:(
I don't understand this piece of software at all
This commit is contained in:
@@ -2,7 +2,7 @@ use std::any::{type_name, Any, TypeId};
|
||||
use std::fmt;
|
||||
use std::io::{Read, Write};
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::{Deref, Range};
|
||||
use std::ops::Deref;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use dyn_clone::{clone_box, DynClone};
|
||||
@@ -12,7 +12,7 @@ use orchid_base::intern;
|
||||
use orchid_base::location::Pos;
|
||||
use orchid_base::name::Sym;
|
||||
use orchid_base::reqnot::Requester;
|
||||
use orchid_base::tree::AtomTok;
|
||||
use orchid_base::tree::AtomRepr;
|
||||
use trait_set::trait_set;
|
||||
|
||||
use crate::api;
|
||||
@@ -92,7 +92,7 @@ impl ForeignAtom<'static> {
|
||||
pub fn request<M: AtomMethod>(&self, m: M) -> Option<M::Response> {
|
||||
let rep = self.ctx.reqnot.request(api::Fwd(
|
||||
self.atom.clone(),
|
||||
Sym::parse(M::NAME).unwrap().tok().marker(),
|
||||
Sym::parse(M::NAME).unwrap().tok().to_api(),
|
||||
enc_vec(&m)
|
||||
))?;
|
||||
Some(M::Response::decode(&mut &rep[..]))
|
||||
@@ -106,16 +106,10 @@ impl<'a> fmt::Display for ForeignAtom<'a> {
|
||||
impl<'a> fmt::Debug for ForeignAtom<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "ForeignAtom({self})") }
|
||||
}
|
||||
impl<'a> AtomTok for ForeignAtom<'a> {
|
||||
type Context = SysCtx;
|
||||
fn from_api(atom: &api::Atom, pos: Range<u32>, ctx: &mut Self::Context) -> Self {
|
||||
Self {
|
||||
atom: atom.clone(),
|
||||
_life: PhantomData,
|
||||
ctx: ctx.clone(),
|
||||
expr: None,
|
||||
pos: Pos::Range(pos),
|
||||
}
|
||||
impl<'a> AtomRepr for ForeignAtom<'a> {
|
||||
type Ctx = SysCtx;
|
||||
fn from_api(atom: &api::Atom, pos: Pos, ctx: &mut Self::Ctx) -> Self {
|
||||
Self { atom: atom.clone(), _life: PhantomData, ctx: ctx.clone(), expr: None, pos }
|
||||
}
|
||||
fn to_api(&self) -> orchid_api::Atom { self.atom.clone() }
|
||||
}
|
||||
@@ -208,7 +202,7 @@ impl<'a, A: AtomicFeatures> TypAtom<'a, A> {
|
||||
M::Response::decode(
|
||||
&mut &self.data.ctx.reqnot.request(api::Fwd(
|
||||
self.data.atom.clone(),
|
||||
Sym::parse(M::NAME).unwrap().tok().marker(),
|
||||
Sym::parse(M::NAME).unwrap().tok().to_api(),
|
||||
enc_vec(&req)
|
||||
)).unwrap()[..]
|
||||
)
|
||||
|
||||
@@ -6,11 +6,10 @@ use std::{mem, process, thread};
|
||||
|
||||
use hashbrown::HashMap;
|
||||
use itertools::Itertools;
|
||||
use orchid_api::ExtMsgSet;
|
||||
use orchid_api_traits::{enc_vec, Decode, Encode};
|
||||
use orchid_base::char_filter::{char_filter_match, char_filter_union, mk_char_filter};
|
||||
use orchid_base::clone;
|
||||
use orchid_base::interner::{deintern, init_replica, sweep_replica};
|
||||
use orchid_base::interner::{init_replica, sweep_replica, Tok};
|
||||
use orchid_base::logging::Logger;
|
||||
use orchid_base::macros::{mtreev_from_api, mtreev_to_api};
|
||||
use orchid_base::name::{PathSlice, Sym};
|
||||
@@ -30,8 +29,8 @@ use crate::system::{atom_by_idx, SysCtx};
|
||||
use crate::system_ctor::{CtedObj, DynSystemCtor};
|
||||
use crate::tree::{do_extra, GenTok, GenTokTree, LazyMemberFactory, TIACtxImpl};
|
||||
|
||||
pub type ExtReq = RequestHandle<ExtMsgSet>;
|
||||
pub type ExtReqNot = ReqNot<ExtMsgSet>;
|
||||
pub type ExtReq = RequestHandle<api::ExtMsgSet>;
|
||||
pub type ExtReqNot = ReqNot<api::ExtMsgSet>;
|
||||
|
||||
pub struct ExtensionData {
|
||||
pub name: &'static str,
|
||||
@@ -136,7 +135,7 @@ fn extension_main_logic(data: ExtensionData) {
|
||||
path: Substack::Bottom,
|
||||
};
|
||||
let const_root = (cted.inst().dyn_env().into_iter())
|
||||
.map(|(k, v)| (k.marker(), v.into_api(&mut tia_ctx)))
|
||||
.map(|(k, v)| (k.to_api(), v.into_api(&mut tia_ctx)))
|
||||
.collect();
|
||||
systems.lock().unwrap().insert(new_sys.id, SystemRecord {
|
||||
declfs: cted.inst().dyn_vfs().to_api_rec(&mut vfses),
|
||||
@@ -180,14 +179,14 @@ fn extension_main_logic(data: ExtensionData) {
|
||||
api::HostExtReq::VfsReq(api::VfsReq::VfsRead(vfs_read)) => {
|
||||
let api::VfsRead(sys_id, vfs_id, path) = &vfs_read;
|
||||
let systems_g = systems.lock().unwrap();
|
||||
let path = path.iter().map(|t| deintern(*t)).collect_vec();
|
||||
let path = path.iter().map(|t| Tok::from_api(*t)).collect_vec();
|
||||
hand.handle(&vfs_read, &systems_g[sys_id].vfses[vfs_id].load(PathSlice::new(&path)))
|
||||
}
|
||||
api::HostExtReq::LexExpr(lex @ api::LexExpr{ sys, text, pos, id }) => {
|
||||
let systems_g = systems.lock().unwrap();
|
||||
let lexers = systems_g[&sys].cted.inst().dyn_lexers();
|
||||
mem::drop(systems_g);
|
||||
let text = deintern(text);
|
||||
let text = Tok::from_api(text);
|
||||
let ctx = LexContext { sys, id, pos, reqnot: hand.reqnot(), text: &text };
|
||||
let trigger_char = text.chars().nth(pos as usize).unwrap();
|
||||
for lx in lexers.iter().filter(|l| char_filter_match(l.char_filter(), trigger_char)) {
|
||||
@@ -241,7 +240,7 @@ fn extension_main_logic(data: ExtensionData) {
|
||||
api::AtomReq::Fwded(fwded) => {
|
||||
let api::Fwded(_, key, payload) = &fwded;
|
||||
let mut reply = Vec::new();
|
||||
let some = nfo.handle_req(actx, Sym::deintern(*key), &mut &payload[..], &mut reply);
|
||||
let some = nfo.handle_req(actx, Sym::from_api(*key), &mut &payload[..], &mut reply);
|
||||
hand.handle(fwded, &some.then_some(reply))
|
||||
}
|
||||
api::AtomReq::CallRef(call@api::CallRef(_, arg)) => {
|
||||
@@ -279,13 +278,20 @@ fn extension_main_logic(data: ExtensionData) {
|
||||
let tok = hand.will_handle_as(&am);
|
||||
let sys_ctx = mk_ctx(am.sys, hand.reqnot());
|
||||
let ctx = RuleCtx {
|
||||
args: am.params.into_iter().map(|(k, v)| (deintern(k), mtreev_from_api(&v))).collect(),
|
||||
args: (am.params.into_iter())
|
||||
.map(|(k, v)| (
|
||||
Tok::from_api(k),
|
||||
mtreev_from_api(&v, &mut |_| panic!("No atom in macro prompt!"))
|
||||
))
|
||||
.collect(),
|
||||
run_id: am.run_id,
|
||||
sys: sys_ctx.clone(),
|
||||
};
|
||||
hand.handle_as(tok, &match apply_rule(am.id, ctx) {
|
||||
Err(e) => e.keep_only(|e| *e != err_cascade()).map(|e| Err(e.to_api())),
|
||||
Ok(t) => Some(Ok(mtreev_to_api(&t))),
|
||||
Ok(t) => Some(Ok(mtreev_to_api(&t, &mut |a| {
|
||||
api::MacroToken::Atom(a.clone().build(sys_ctx.clone()))
|
||||
}))),
|
||||
})
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::ops::Deref;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use derive_destructure::destructure;
|
||||
use orchid_api::InspectedKind;
|
||||
use orchid_base::error::{OrcErr, OrcErrv};
|
||||
use orchid_base::interner::Tok;
|
||||
use orchid_base::location::Pos;
|
||||
@@ -51,9 +50,10 @@ impl Expr {
|
||||
let details = handle.ctx.reqnot.request(api::Inspect { target: handle.tk });
|
||||
let pos = Pos::from_api(&details.location);
|
||||
let kind = match details.kind {
|
||||
InspectedKind::Atom(a) => ExprKind::Atom(ForeignAtom::new(handle.clone(), a, pos.clone())),
|
||||
InspectedKind::Bottom(b) => ExprKind::Bottom(OrcErrv::from_api(&b)),
|
||||
InspectedKind::Opaque => ExprKind::Opaque,
|
||||
api::InspectedKind::Atom(a) =>
|
||||
ExprKind::Atom(ForeignAtom::new(handle.clone(), a, pos.clone())),
|
||||
api::InspectedKind::Bottom(b) => ExprKind::Bottom(OrcErrv::from_api(&b)),
|
||||
api::InspectedKind::Opaque => ExprKind::Opaque,
|
||||
};
|
||||
ExprData { pos, kind }
|
||||
})
|
||||
@@ -124,7 +124,7 @@ impl ExprKind {
|
||||
K::Seq(Box::new(a.api_return(ctx.clone(), do_slot)), Box::new(b.api_return(ctx, do_slot))),
|
||||
Self::Lambda(arg, body) => K::Lambda(arg, Box::new(body.api_return(ctx, do_slot))),
|
||||
Self::Arg(arg) => K::Arg(arg),
|
||||
Self::Const(name) => K::Const(name.marker()),
|
||||
Self::Const(name) => K::Const(name.to_api()),
|
||||
Self::Bottom(err) => K::Bottom(err.to_api()),
|
||||
Self::NewAtom(fac) => K::NewAtom(fac.clone().build(ctx)),
|
||||
kind @ (Self::Atom(_) | Self::Opaque) => panic!("{kind:?} should have a token"),
|
||||
|
||||
@@ -24,7 +24,7 @@ impl DeclFs {
|
||||
api::EagerVfs::Lazy(id)
|
||||
},
|
||||
DeclFs::Mod(children) => api::EagerVfs::Eager(
|
||||
children.iter().map(|(k, v)| (intern(*k).marker(), v.to_api_rec(vfses))).collect(),
|
||||
children.iter().map(|(k, v)| (intern(*k).to_api(), v.to_api_rec(vfses))).collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,3 @@ pub mod system;
|
||||
pub mod system_ctor;
|
||||
pub mod tree;
|
||||
pub mod macros;
|
||||
pub mod api_conv;
|
||||
|
||||
@@ -1,39 +1,48 @@
|
||||
use ahash::HashMap;
|
||||
use lazy_static::lazy_static;
|
||||
use never::Never;
|
||||
use orchid_base::{error::OrcRes, interner::{intern, Tok}, location::Pos, macros::{mtreev_from_api, mtreev_to_api, MTree}, parse::Comment, reqnot::Requester};
|
||||
use trait_set::trait_set;
|
||||
use crate::{api, lexer::err_cascade, system::SysCtx};
|
||||
use crate::{api, atom::AtomFactory, lexer::err_cascade, system::SysCtx};
|
||||
use std::{num::NonZero, sync::RwLock};
|
||||
|
||||
pub trait Macro {
|
||||
fn pattern() -> MTree<'static>;
|
||||
fn apply(binds: HashMap<Tok<String>, MTree<'_>>) -> MTree<'_>;
|
||||
fn pattern() -> MTree<'static, Never>;
|
||||
fn apply(binds: HashMap<Tok<String>, MTree<'_, Never>>) -> MTree<'_, AtomFactory>;
|
||||
}
|
||||
|
||||
pub trait DynMacro {
|
||||
fn pattern(&self) -> MTree<'static>;
|
||||
fn apply<'a>(&self, binds: HashMap<Tok<String>, MTree<'a>>) -> MTree<'a>;
|
||||
fn pattern(&self) -> MTree<'static, Never>;
|
||||
fn apply<'a>(&self, binds: HashMap<Tok<String>, MTree<'a, Never>>) -> MTree<'a, AtomFactory>;
|
||||
}
|
||||
|
||||
impl<T: Macro> DynMacro for T {
|
||||
fn pattern(&self) -> MTree<'static> { Self::pattern() }
|
||||
fn apply<'a>(&self, binds: HashMap<Tok<String>, MTree<'a>>) -> MTree<'a> { Self::apply(binds) }
|
||||
fn pattern(&self) -> MTree<'static, Never> { Self::pattern() }
|
||||
fn apply<'a>(&self, binds: HashMap<Tok<String>, MTree<'a, Never>>) -> MTree<'a, AtomFactory> {
|
||||
Self::apply(binds)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RuleCtx<'a> {
|
||||
pub(crate) args: HashMap<Tok<String>, Vec<MTree<'a>>>,
|
||||
pub(crate) args: HashMap<Tok<String>, Vec<MTree<'a, Never>>>,
|
||||
pub(crate) run_id: api::ParsId,
|
||||
pub(crate) sys: SysCtx,
|
||||
}
|
||||
impl<'a> RuleCtx<'a> {
|
||||
pub fn recurse(&mut self, tree: &[MTree<'a>]) -> OrcRes<Vec<MTree<'a>>> {
|
||||
let req = api::RunMacros{ run_id: self.run_id, query: mtreev_to_api(tree) };
|
||||
Ok(mtreev_from_api(&self.sys.reqnot.request(req).ok_or_else(err_cascade)?))
|
||||
pub fn recurse(&mut self, tree: &[MTree<'a, Never>]) -> OrcRes<Vec<MTree<'a, Never>>> {
|
||||
let req = api::RunMacros{
|
||||
run_id: self.run_id,
|
||||
query: mtreev_to_api(tree, &mut |b| match *b {})
|
||||
};
|
||||
Ok(mtreev_from_api(
|
||||
&self.sys.reqnot.request(req).ok_or_else(err_cascade)?,
|
||||
&mut |_| panic!("Returned atom from Rule recursion")
|
||||
))
|
||||
}
|
||||
pub fn getv(&mut self, key: &Tok<String>) -> Vec<MTree<'a>> {
|
||||
pub fn getv(&mut self, key: &Tok<String>) -> Vec<MTree<'a, Never>> {
|
||||
self.args.remove(key).expect("Key not found")
|
||||
}
|
||||
pub fn gets(&mut self, key: &Tok<String>) -> MTree<'a> {
|
||||
pub fn gets(&mut self, key: &Tok<String>) -> MTree<'a, Never> {
|
||||
let v = self.getv(key);
|
||||
assert!(v.len() == 1, "Not a scalar");
|
||||
v.into_iter().next().unwrap()
|
||||
@@ -44,7 +53,7 @@ impl<'a> RuleCtx<'a> {
|
||||
}
|
||||
|
||||
trait_set! {
|
||||
pub trait RuleCB = for<'a> Fn(RuleCtx<'a>) -> OrcRes<Vec<MTree<'a>>> + Send + Sync;
|
||||
pub trait RuleCB = for<'a> Fn(RuleCtx<'a>) -> OrcRes<Vec<MTree<'a, AtomFactory>>> + Send + Sync;
|
||||
}
|
||||
|
||||
lazy_static!{
|
||||
@@ -53,7 +62,7 @@ lazy_static!{
|
||||
|
||||
pub struct Rule {
|
||||
pub(crate) comments: Vec<Comment>,
|
||||
pub(crate) pattern: Vec<MTree<'static>>,
|
||||
pub(crate) pattern: Vec<MTree<'static, Never>>,
|
||||
pub(crate) id: api::MacroId,
|
||||
}
|
||||
impl Rule {
|
||||
@@ -61,7 +70,7 @@ impl Rule {
|
||||
api::MacroRule {
|
||||
comments: self.comments.iter().map(|c| c.to_api()).collect(),
|
||||
location: api::Location::Inherit,
|
||||
pattern: mtreev_to_api(&self.pattern),
|
||||
pattern: mtreev_to_api(&self.pattern, &mut |b| match *b {}),
|
||||
id: self.id,
|
||||
}
|
||||
}
|
||||
@@ -69,7 +78,7 @@ impl Rule {
|
||||
|
||||
pub fn rule_cmt<'a>(
|
||||
cmt: impl IntoIterator<Item = &'a str>,
|
||||
pattern: Vec<MTree<'static>>,
|
||||
pattern: Vec<MTree<'static, Never>>,
|
||||
apply: impl RuleCB + 'static
|
||||
) -> Rule {
|
||||
let mut rules = RULES.write().unwrap();
|
||||
@@ -79,11 +88,14 @@ pub fn rule_cmt<'a>(
|
||||
Rule { comments, pattern, id }
|
||||
}
|
||||
|
||||
pub fn rule(pattern: Vec<MTree<'static>>, apply: impl RuleCB + 'static) -> Rule {
|
||||
pub fn rule(pattern: Vec<MTree<'static, Never>>, apply: impl RuleCB + 'static) -> Rule {
|
||||
rule_cmt([], pattern, apply)
|
||||
}
|
||||
|
||||
pub(crate) fn apply_rule(id: api::MacroId, ctx: RuleCtx<'static>) -> OrcRes<Vec<MTree<'static>>> {
|
||||
pub(crate) fn apply_rule(
|
||||
id: api::MacroId,
|
||||
ctx: RuleCtx<'static>
|
||||
) -> OrcRes<Vec<MTree<'static, AtomFactory>>> {
|
||||
let rules = RULES.read().unwrap();
|
||||
rules[&id](ctx)
|
||||
}
|
||||
@@ -43,9 +43,9 @@ pub struct GenItem {
|
||||
impl GenItem {
|
||||
pub fn into_api(self, ctx: &mut impl TreeIntoApiCtx) -> api::Item {
|
||||
let kind = match self.kind {
|
||||
GenItemKind::Export(n) => api::ItemKind::Export(n.marker()),
|
||||
GenItemKind::Export(n) => api::ItemKind::Export(n.to_api()),
|
||||
GenItemKind::Member(mem) => api::ItemKind::Member(mem.into_api(ctx)),
|
||||
GenItemKind::Import(cn) => api::ItemKind::Import(cn.tok().marker()),
|
||||
GenItemKind::Import(cn) => api::ItemKind::Import(cn.tok().to_api()),
|
||||
GenItemKind::Macro(prio, rules) => api::ItemKind::Macro(api::MacroBlock {
|
||||
priority: prio,
|
||||
rules: rules.into_iter().map(|r| r.to_api() ).collect_vec(),
|
||||
@@ -134,7 +134,7 @@ pub struct GenMember {
|
||||
impl GenMember {
|
||||
pub fn into_api(self, ctx: &mut impl TreeIntoApiCtx) -> api::Member {
|
||||
api::Member {
|
||||
name: self.name.marker(),
|
||||
name: self.name.to_api(),
|
||||
kind: self.kind.into_api(&mut ctx.push_path(self.name)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user