use miette::Result;
use super::ops::{
build_qt_code, build_quote_code, build_str_code, build_tuple_code, build_unquote_code,
build_variadic_block,
};
use crate::lang::Arity;
use crate::prelude::{Index, *};
use crate::{
meta::{MetaLanguage, Piece, Prelude},
qterm::{QTerm, Repr},
term::CmdOrHole,
};
const STR_JOIN: &str = r#"((items, sep = "\n") => Array.from(items, String).join(sep))"#;
#[derive(Default)]
pub struct TypeScriptMetaLanguage;
impl MetaLanguage for TypeScriptMetaLanguage {
fn expand_quote(
&self,
_lang1: &str,
tag: &str,
i: Index,
lang2: &str,
qterm: &Arc<QTerm>,
cmds: &[CmdOrHole],
) -> Result<Arc<QTerm>> {
Ok(build_quote_code(tag, i, lang2, qterm, cmds))
}
fn expand_unquote(
&self,
_lang1: &str,
tag: &str,
i: Index,
lang2: &str,
qterm: &Arc<QTerm>,
cmds: &[CmdOrHole],
) -> Result<Arc<QTerm>> {
Ok(build_unquote_code(tag, i, lang2, qterm, cmds))
}
fn expand_tuple(
&self,
_lang1: &str,
tag: &str,
qterms: &[Arc<QTerm>],
cmds: &[CmdOrHole],
arity: Arity,
) -> Result<Arc<QTerm>> {
Ok(if arity == Arity::Variadic {
build_variadic_block(tag, cmds, qterms)
} else {
build_tuple_code(tag, cmds, qterms)
})
}
fn lift_str(&self, target: &str) -> Result<&'static str> {
match target {
"typescript" | "ts" => Ok("qlift"),
"html" => Ok("qlift_html"),
"text" | "txt" => Ok("qlift_text"),
crate::lift::TEXT_IN_QUOTES => Ok("qlift_text_in_quotes"),
_ => miette::bail!("typescript can't lift into {target:?}: no spelling registered"),
}
}
fn reduce_str(&self, target: &str) -> Result<&'static str> {
match target {
"" | "typescript" | "ts" => Ok("reduce()"),
_ => miette::bail!("typescript can't reduce via {target:?}: no spelling registered"),
}
}
fn name_str(&self) -> Result<&'static str> {
Ok("name")
}
fn type_str(&self) -> Result<&'static str> {
Ok("QTerm")
}
fn emit_str(&self) -> Result<&'static str> {
Ok("qseq")
}
fn lift_str_in(&self, target: &str, repr: Repr) -> Result<&'static str> {
match (repr, target) {
(Repr::Str, "typescript" | "ts") => Ok("qlift_str"),
_ => self.lift_str(target),
}
}
fn emit_str_in(&self, repr: Repr) -> Result<&'static str> {
match repr {
Repr::Str => Ok(STR_JOIN),
Repr::Cst | Repr::Qt => self.emit_str(),
}
}
fn expand_flat(&self, repr: Repr, _lang2: &str, pieces: &[Piece]) -> Result<Arc<QTerm>> {
Ok(match repr {
Repr::Str => build_str_code(pieces),
Repr::Qt => build_qt_code(pieces),
Repr::Cst => miette::bail!("a `⟨cst⟩` quote is not flattened"),
})
}
fn prelude(&self, targets: &[&str]) -> Option<Prelude> {
const HEAD: [&str; 11] = [
"tb",
"leaf",
"sym",
"quote",
"unquote",
"cmd",
"write",
"push",
"name",
"qseq",
"qlift_str",
];
const TAIL: [&str; 3] = ["NL", "POP", "HOLE"];
let mut names: Vec<&str> = HEAD.to_vec();
for target in targets {
let positions: &[&str] = if matches!(*target, "text" | "txt") {
&[target, crate::lift::TEXT_IN_QUOTES]
} else {
&[target]
};
for position in positions {
if let Ok(lift) = self.lift_str(position) {
if !names.contains(&lift) {
names.push(lift);
}
}
}
}
names.extend_from_slice(&TAIL);
Some(Prelude::new(
format!("import {{ {} }} from \"quilt\";", names.join(", ")),
["from \"quilt\"", "from 'quilt'"],
))
}
}