use hir_lang::OpKind;
use crate::template::{LabelRef, TLit, Template, TemplateBuilder};
fn done(t: TemplateBuilder, root: crate::template::TExpr, name: &str) -> Template {
t.finish(root).unwrap_or_else(|_| Template {
name: name.into(),
kind: hir_lang::ExpnKind::Template,
nodes: alloc::vec![crate::template::TNode::Tuple((0, 0))],
lists: alloc::vec::Vec::new(),
texts: alloc::vec::Vec::new(),
holes: alloc::vec::Vec::new(),
binders: alloc::vec::Vec::new(),
labels: alloc::vec::Vec::new(),
args: alloc::vec::Vec::new(),
applies: alloc::vec::Vec::new(),
root: 0,
text_base: 0,
})
}
#[must_use]
pub fn while_loop() -> Template {
let mut t = TemplateBuilder::new("while").desugar();
let cond = t.expr_hole("cond");
let body = t.expr_hole("body");
let label = t.label_hole("label");
let c = t.hole(cond);
let then = t.block(&[], None);
let brk = t.break_(None, None);
let test = t.if_(c, then, Some(brk));
let test = t.expr_stmt(test);
let b = t.hole(body);
let inner = t.block(&[test], Some(b));
let root = t.loop_(Some(LabelRef::Hole(label)), inner, None);
done(t, root, "while")
}
#[must_use]
pub fn do_while() -> Template {
let mut t = TemplateBuilder::new("do-while").desugar();
let body = t.expr_hole("body");
let cond = t.expr_hole("cond");
let label = t.label_hole("label");
let b = t.hole(body);
let c = t.hole(cond);
let then = t.block(&[], None);
let brk = t.break_(None, None);
let step = t.if_(c, then, Some(brk));
let root = t.loop_(Some(LabelRef::Hole(label)), b, Some(step));
done(t, root, "do-while")
}
#[must_use]
pub fn c_for() -> Template {
let mut t = TemplateBuilder::new("for").desugar();
let init = t.stmts_hole("init");
let cond = t.optional_expr_hole("cond", Some(TLit::Bool(true)));
let step = t.optional_expr_hole("step", None);
let body = t.expr_hole("body");
let label = t.label_hole("label");
let init = t.stmts(init);
let c = t.hole(cond);
let then = t.block(&[], None);
let brk = t.break_(None, None);
let test = t.if_(c, then, Some(brk));
let test = t.expr_stmt(test);
let b = t.hole(body);
let inner = t.block(&[test], Some(b));
let s = t.hole(step);
let lp = t.loop_(Some(LabelRef::Hole(label)), inner, Some(s));
let root = t.block(&[init], Some(lp));
done(t, root, "for")
}
#[must_use]
pub fn for_each(iter: &str, next: &str) -> Template {
let mut t = TemplateBuilder::new("foreach").desugar();
let source = t.expr_hole("source");
let key = t.pat_hole("key", true);
let value = t.pat_hole("value", false);
let body = t.expr_hole("body");
let label = t.label_hole("label");
let it = t.binder("it");
let iter_fn = t.extern_(iter);
let src = t.hole(source);
let start = t.call(iter_fn, &[src]);
let it_pat = t.bind(it);
let decl = t.let_(it_pat, None, Some(start));
let next_fn = t.extern_(next);
let it_use = t.var(it);
let step = t.call(next_fn, &[it_use]);
let null = t.lit_pat(TLit::Null);
let brk = t.break_(None, None);
let k = t.pat(key);
let v = t.pat(value);
let pair = t.tuple_pat(&[k, v]);
let b = t.hole(body);
let m = t.match_(step, &[(null, None, brk), (pair, None, b)]);
let lp = t.loop_(Some(LabelRef::Hole(label)), m, None);
let root = t.block(&[decl], Some(lp));
done(t, root, "foreach")
}
#[must_use]
pub fn logical_and() -> Template {
let mut t = TemplateBuilder::new("&&").desugar();
let lhs = t.expr_hole("lhs");
let rhs = t.expr_hole("rhs");
let l = t.hole(lhs);
let r = t.hole(rhs);
let no = t.lit(TLit::Bool(false));
let root = t.if_(l, r, Some(no));
done(t, root, "&&")
}
#[must_use]
pub fn logical_or() -> Template {
let mut t = TemplateBuilder::new("||").desugar();
let lhs = t.expr_hole("lhs");
let rhs = t.expr_hole("rhs");
let l = t.hole(lhs);
let yes = t.lit(TLit::Bool(true));
let r = t.hole(rhs);
let root = t.if_(l, yes, Some(r));
done(t, root, "||")
}
#[must_use]
pub fn or_value() -> Template {
value_choice("or", false)
}
#[must_use]
pub fn and_value() -> Template {
value_choice("and", true)
}
fn value_choice(name: &str, and: bool) -> Template {
let mut t = TemplateBuilder::new(name).desugar();
let lhs = t.expr_hole("lhs");
let rhs = t.expr_hole("rhs");
let tmp = t.binder("t");
let l = t.hole(lhs);
let p = t.bind(tmp);
let decl = t.let_(p, None, Some(l));
let cond = t.var(tmp);
let keep = t.var(tmp);
let r = t.hole(rhs);
let test = if and {
t.if_(cond, r, Some(keep))
} else {
t.if_(cond, keep, Some(r))
};
let root = t.block(&[decl], Some(test));
done(t, root, name)
}
#[must_use]
pub fn coalesce() -> Template {
let mut t = TemplateBuilder::new("??").desugar();
let lhs = t.expr_hole("lhs");
let rhs = t.expr_hole("rhs");
let tmp = t.binder("t");
let l = t.hole(lhs);
let p = t.bind(tmp);
let decl = t.let_(p, None, Some(l));
let probe = t.var(tmp);
let null = t.lit(TLit::Null);
let is_null = t.op(OpKind::Eq, &[probe, null]);
let r = t.hole(rhs);
let keep = t.var(tmp);
let test = t.if_(is_null, r, Some(keep));
let root = t.block(&[decl], Some(test));
done(t, root, "??")
}
#[must_use]
pub fn try_operator() -> Template {
let mut t = TemplateBuilder::new("?").desugar();
let expr = t.expr_hole("expr");
let v = t.binder("v");
let e = t.binder("e");
let scrutinee = t.hole(expr);
let bind_v = t.bind(v);
let ok = t.ctor_pat("Ok", &[bind_v]);
let use_v = t.var(v);
let bind_e = t.bind(e);
let err = t.ctor_pat("Err", &[bind_e]);
let err_fn = t.global("Err");
let use_e = t.var(e);
let rewrap = t.call(err_fn, &[use_e]);
let ret = t.return_(Some(rewrap));
let root = t.match_(scrutinee, &[(ok, None, use_v), (err, None, ret)]);
done(t, root, "?")
}
#[must_use]
pub fn post_increment() -> Template {
let mut t = TemplateBuilder::new("post++").desugar();
let place = t.expr_hole("place");
let alias = t.binder("p");
let old = t.binder("old");
let target = t.hole(place);
let read = t.var(alias);
let p = t.bind(old);
let decl = t.let_(p, None, Some(read));
let cur = t.var(old);
let one = t.lit(TLit::Int(1));
let sum = t.op(OpKind::Add, &[cur, one]);
let write_to = t.var(alias);
let write = t.assign(write_to, sum);
let write = t.expr_stmt(write);
let result = t.var(old);
let body = t.block(&[decl, write], Some(result));
let root = t.let_place(alias, target, body);
done(t, root, "post++")
}
#[cfg(test)]
pub(crate) fn all() -> alloc::vec::Vec<Template> {
alloc::vec![
while_loop(),
do_while(),
c_for(),
for_each("iter", "next"),
logical_and(),
logical_or(),
or_value(),
and_value(),
coalesce(),
try_operator(),
post_increment(),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_every_builtin_template_is_valid() {
let names = [
"while", "do-while", "for", "foreach", "&&", "||", "or", "and", "??", "?", "post++",
];
for (t, name) in all().iter().zip(names) {
assert_eq!(t.name(), name);
assert!(t.hole_count() > 0, "{name} fell back");
assert_ne!(t.nodes.len(), 1, "{name} fell back");
}
}
}