This commit is contained in:
2024-07-18 16:07:36 +02:00
parent 949b3758fd
commit cc3699bbe7
31 changed files with 1021 additions and 312 deletions

View File

@@ -4,38 +4,34 @@ use ahash::HashMap;
use dyn_clone::{clone_box, DynClone};
use itertools::Itertools;
use orchid_api::tree::{
MacroRule, Paren, Placeholder, PlaceholderKind, Token, TokenTree, Tree, TreeId, TreeModule,
MacroRule, Paren, PlaceholderKind, Token, TokenTree, Tree, TreeId, TreeModule,
TreeTicket,
};
use orchid_base::interner::{intern, Tok};
use orchid_base::interner::intern;
use orchid_base::location::Pos;
use orchid_base::name::VName;
use orchid_base::tokens::OwnedPh;
use ordered_float::NotNan;
use trait_set::trait_set;
use crate::atom::AtomFactory;
use crate::conv::ToExpr;
use crate::error::{err_or_ref_to_api, ProjectErrorObj};
use crate::expr::GenExpr;
use crate::system::DynSystem;
#[derive(Clone)]
pub struct GenPh {
pub name: Tok<String>,
pub kind: PlaceholderKind,
}
#[derive(Clone)]
pub struct GenTokTree {
pub tok: GenTok,
pub struct OwnedTokTree {
pub tok: OwnedTok,
pub range: Range<u32>,
}
impl GenTokTree {
impl OwnedTokTree {
pub fn into_api(self, sys: &dyn DynSystem) -> TokenTree {
TokenTree { token: self.tok.into_api(sys), range: self.range }
}
}
pub fn ph(s: &str) -> GenPh {
pub fn ph(s: &str) -> OwnedPh {
match s.strip_prefix("..") {
Some(v_tail) => {
let (mid, priority) = match v_tail.split_once(':') {
@@ -49,29 +45,30 @@ pub fn ph(s: &str) -> GenPh {
if konst::string::starts_with(name, "_") {
panic!("Names starting with an underscore indicate a single-name scalar placeholder")
}
GenPh { name: intern(name), kind: PlaceholderKind::Vector { nonzero, priority } }
OwnedPh { name: intern(name), kind: PlaceholderKind::Vector { nonzero, priority } }
},
None => match konst::string::strip_prefix(s, "$_") {
Some(name) => GenPh { name: intern(name), kind: PlaceholderKind::Name },
Some(name) => OwnedPh { name: intern(name), kind: PlaceholderKind::Name },
None => match konst::string::strip_prefix(s, "$") {
None => panic!("Invalid placeholder"),
Some(name) => GenPh { name: intern(name), kind: PlaceholderKind::Scalar },
Some(name) => OwnedPh { name: intern(name), kind: PlaceholderKind::Scalar },
},
},
}
}
#[derive(Clone)]
pub enum GenTok {
Lambda(Vec<GenTokTree>, Vec<GenTokTree>),
pub enum OwnedTok {
Lambda(Vec<OwnedTokTree>, Vec<OwnedTokTree>),
Name(VName),
S(Paren, Vec<GenTokTree>),
S(Paren, Vec<OwnedTokTree>),
Atom(AtomFactory),
Slot(TreeTicket),
Ph(GenPh),
Ph(OwnedPh),
Bottom(ProjectErrorObj),
}
impl GenTok {
pub fn at(self, range: Range<u32>) -> GenTokTree { GenTokTree { tok: self, range } }
impl OwnedTok {
pub fn at(self, range: Range<u32>) -> OwnedTokTree { OwnedTokTree { tok: self, range } }
pub fn into_api(self, sys: &dyn DynSystem) -> Token {
match self {
Self::Lambda(x, body) => Token::Lambda(
@@ -79,32 +76,33 @@ impl GenTok {
body.into_iter().map(|tt| tt.into_api(sys)).collect_vec(),
),
Self::Name(n) => Token::Name(n.into_iter().map(|t| t.marker()).collect_vec()),
Self::Ph(GenPh { name, kind }) => Token::Ph(Placeholder { name: name.marker(), kind }),
Self::Ph(ph) => Token::Ph(ph.to_api()),
Self::S(p, body) => Token::S(p, body.into_iter().map(|tt| tt.into_api(sys)).collect_vec()),
Self::Slot(tk) => Token::Slot(tk),
Self::Atom(at) => Token::Atom(at.build(sys)),
Self::Bottom(err) => Token::Bottom(err_or_ref_to_api(err)),
}
}
}
#[derive(Clone)]
pub struct GenMacro {
pub pattern: Vec<GenTokTree>,
pub pattern: Vec<OwnedTokTree>,
pub priority: NotNan<f64>,
pub template: Vec<GenTokTree>,
pub template: Vec<OwnedTokTree>,
}
pub fn tokv_into_api(
tokv: impl IntoIterator<Item = GenTokTree>,
tokv: impl IntoIterator<Item = OwnedTokTree>,
sys: &dyn DynSystem,
) -> Vec<TokenTree> {
tokv.into_iter().map(|tok| tok.into_api(sys)).collect_vec()
}
pub fn wrap_tokv(items: Vec<GenTokTree>, range: Range<u32>) -> GenTokTree {
pub fn wrap_tokv(items: Vec<OwnedTokTree>, range: Range<u32>) -> OwnedTokTree {
match items.len() {
1 => items.into_iter().next().unwrap(),
_ => GenTok::S(Paren::Round, items).at(range),
_ => OwnedTok::S(Paren::Round, items).at(range),
}
}
@@ -120,8 +118,8 @@ impl GenTree {
}
pub fn rule(
prio: f64,
pat: impl IntoIterator<Item = GenTokTree>,
tpl: impl IntoIterator<Item = GenTokTree>,
pat: impl IntoIterator<Item = OwnedTokTree>,
tpl: impl IntoIterator<Item = OwnedTokTree>,
) -> Self {
GenItem::Rule(GenMacro {
pattern: pat.into_iter().collect(),