mod gen;
pub use gen::{PYTHON, RUST, TYPESCRIPT};
use crate::prelude::*;
use crate::qterm::QTermBuilder;
use crate::term::{is_aligned_splice, CmdOrHole};
pub struct Shapes {
pub tb: [&'static str; 2],
pub w: [&'static str; 2],
pub n: &'static str,
pub p: [&'static str; 2],
pub x: &'static str,
pub c: [&'static str; 2],
pub e: [&'static str; 2],
pub t: Option<[&'static str; 2]>,
pub b: &'static str,
pub sym: [&'static str; 2],
pub leaf: [&'static str; 3],
pub quote: [&'static str; 6],
pub unquote: [&'static str; 6],
pub list: [&'static str; 3],
pub cmd: [&'static str; 2],
pub write: [&'static str; 2],
pub push: [&'static str; 2],
pub nl: &'static str,
pub pop: &'static str,
pub hole: &'static str,
}
#[derive(Clone, Copy)]
pub enum Lit {
Flat(fn(&str) -> String),
Term(fn(&str) -> Arc<QTerm>),
}
fn is_fresh(child: &Arc<QTerm>, fresh_calls: &[&str]) -> bool {
let QTerm::Tuple { tag, terms, .. } = &**child else {
return false;
};
match &**tag {
"_" => true,
"call_expression" => terms.first().is_some_and(|f| {
matches!(&**f, QTerm::Tuple { tag, .. } if &**tag == "identifier")
&& fresh_calls.contains(&f.coparse().trim())
}),
_ => false,
}
}
#[derive(Clone, Copy)]
pub struct Chain {
shapes: &'static Shapes,
lit: Lit,
fresh_calls: &'static [&'static str],
}
impl Chain {
#[must_use]
pub const fn new(shapes: &'static Shapes, lit: Lit) -> Self {
Self {
shapes,
lit,
fresh_calls: &[],
}
}
#[must_use]
pub const fn fresh_calls(self, fresh_calls: &'static [&'static str]) -> Self {
Self {
fresh_calls,
..self
}
}
fn lit(&self, b: &mut QTermBuilder, s: &str) {
match self.lit {
Lit::Flat(esc) => b.write(&esc(s)),
Lit::Term(term) => b.child(&term(s)),
};
}
fn call1(&self, b: &mut QTermBuilder, f: [&'static str; 2], s: &str) {
b.write(f[0]);
self.lit(b, s);
b.write(f[1]);
}
fn splice(b: &mut QTermBuilder, f: [&'static str; 2], child: &Arc<QTerm>) {
b.write(f[0]).child(child).write(f[1]);
}
fn shorthand(
&self,
tag: &str,
cmds: &[CmdOrHole],
children: &[Arc<QTerm>],
) -> Option<Arc<QTerm>> {
if !children.is_empty() || cmds.len() != 1 {
return None;
}
let CmdOrHole::Cmd(StrCmd::Write(code)) = &cmds[0] else {
return None;
};
let mut b = tb("_");
if tag == &**code {
self.call1(&mut b, self.shapes.sym, tag);
} else {
let f = self.shapes.leaf;
b.write(f[0]);
self.lit(&mut b, tag);
b.write(f[1]);
self.lit(&mut b, code);
b.write(f[2]);
}
Some(b.b())
}
fn chain(
&self,
tag: &str,
cmds: &[CmdOrHole],
children: &[Arc<QTerm>],
hole: [&'static str; 2],
who: &str,
) -> Arc<QTerm> {
if let Some(short) = self.shorthand(tag, cmds, children) {
return short;
}
let s = self.shapes;
let mut b = tb("_");
self.call1(&mut b, s.tb, tag);
let mut it = children.iter();
let mut skip_pop = false;
for (k, c) in cmds.iter().enumerate() {
match c {
CmdOrHole::Cmd(StrCmd::Push(t)) if t.is_empty() && is_aligned_splice(cmds, k) => {
skip_pop = true;
}
CmdOrHole::Cmd(StrCmd::Pop) if skip_pop => skip_pop = false,
CmdOrHole::Cmd(StrCmd::Write(t)) => self.call1(&mut b, s.w, t),
CmdOrHole::Cmd(StrCmd::NewLine) => {
b.write(s.n);
}
CmdOrHole::Cmd(StrCmd::Push(t)) => self.call1(&mut b, s.p, t),
CmdOrHole::Cmd(StrCmd::Pop) => {
b.write(s.x);
}
CmdOrHole::Hole => {
let child = it
.next()
.unwrap_or_else(|| panic!("{who}: not enough children"));
let f = match s.t {
Some(t) if is_fresh(child, self.fresh_calls) => t,
_ => hole,
};
Self::splice(&mut b, f, child);
}
}
}
b.write(s.b);
b.b()
}
#[must_use]
pub fn tuple_code(&self, tag: &str, cmds: &[CmdOrHole], children: &[Arc<QTerm>]) -> Arc<QTerm> {
self.chain(tag, cmds, children, self.shapes.c, "build_tuple_code")
}
#[must_use]
pub fn variadic_code(
&self,
tag: &str,
cmds: &[CmdOrHole],
children: &[Arc<QTerm>],
) -> Arc<QTerm> {
self.chain(tag, cmds, children, self.shapes.e, "build_variadic_block")
}
#[must_use]
pub fn cmds_code(&self, cmds: &[CmdOrHole]) -> Arc<QTerm> {
let s = self.shapes;
let mut b = tb("_");
b.write(s.list[0]);
for (i, c) in cmds.iter().enumerate() {
if i > 0 {
b.write(s.list[1]);
}
match c {
CmdOrHole::Hole => {
b.write(s.hole);
}
CmdOrHole::Cmd(cmd) => {
b.write(s.cmd[0]);
match cmd {
StrCmd::Write(t) => self.call1(&mut b, s.write, t),
StrCmd::NewLine => {
b.write(s.nl);
}
StrCmd::Push(t) => self.call1(&mut b, s.push, t),
StrCmd::Pop => {
b.write(s.pop);
}
}
b.write(s.cmd[1]);
}
}
}
b.write(s.list[2]);
b.b()
}
#[must_use]
pub fn quote_code(
&self,
tag: &str,
index: Index,
lang: &str,
term: &Arc<QTerm>,
cmds: &[CmdOrHole],
) -> Arc<QTerm> {
self.wrapper(self.shapes.quote, tag, index, lang, term, cmds)
}
#[must_use]
pub fn unquote_code(
&self,
tag: &str,
index: Index,
lang: &str,
term: &Arc<QTerm>,
cmds: &[CmdOrHole],
) -> Arc<QTerm> {
self.wrapper(self.shapes.unquote, tag, index, lang, term, cmds)
}
fn wrapper(
&self,
f: [&'static str; 6],
tag: &str,
index: Index,
lang: &str,
term: &Arc<QTerm>,
cmds: &[CmdOrHole],
) -> Arc<QTerm> {
let mut b = tb("_");
b.write(f[0]);
self.lit(&mut b, tag);
b.write(&format!("{}{index}{}", f[1], f[2]));
self.lit(&mut b, lang);
b.write(f[3]);
b.child(term);
b.write(f[4]);
b.child(&self.cmds_code(cmds));
b.write(f[5]);
b.b()
}
}