use quote::ToTokens;
use crate::apply::{Apply, err_ty};
use crate::parse::parse_primitive;
use crate::types::*;
pub(crate) fn map_range(
start: u8, end: u8, inclusive: bool, f: impl Fn(u8) -> Ty,
) -> Ty {
let ns: Vec<_> =
if inclusive { (start..=end).collect() } else { (start..end).collect() };
TyArray(ns.into_iter().map(f).collect()).into()
}
fn tuple_pow(mut elems: Vec<Ty>, n: u8) -> Ty {
match elems.len() {
0 => pow_empty(n),
1 => pow_single(elems.remove(0), n),
_ => pow_cartesian(elems, n),
}
}
fn pow_empty(n: u8) -> Ty {
if n == 0 {
return TyTuple(vec![]).into();
}
let params = fresh_params(n);
let param_names = params.iter().map(|p| p.to_token_stream()).collect::<Vec<_>>();
TyTypeParam {
params: param_names.into_iter().map(|n| (n, None)).collect(),
bindings: vec![],
}
.apply(TyTuple(params).into())
}
fn pow_single(template: Ty, n: u8) -> Ty {
if let Ty::TypeParam(tp) = template {
if tp.params.len() != 1 || tp.params[0].1.is_some() {
return err_ty(
"batch-impl: (<Trait>)⁁ 中意外收到了 bound 参数,这是内部错误",
);
}
let params = fresh_params(n);
let param_names =
params.iter().map(|p| p.to_token_stream()).collect::<Vec<_>>();
let bound_tokens = tp.params[0].0.clone().into_iter().collect::<Vec<_>>();
return TyTypeParam {
params: param_names
.into_iter()
.map(|n| (n, Some(parse_primitive(&bound_tokens, None))))
.collect(),
bindings: vec![],
}
.apply(TyTuple(params).into());
}
TyTuple((0..n).map(|_| template.clone()).collect()).into()
}
fn pow_cartesian(elems: Vec<Ty>, n: u8) -> Ty {
let mut combos = vec![vec![]];
for _ in 0..n {
let mut next = vec![];
for existing in &combos {
for elem in &elems {
let mut extended = existing.clone();
extended.push(elem.clone());
next.push(extended);
}
}
combos = next;
}
TyArray(combos.into_iter().map(instantiate_combo).collect()).into()
}
fn instantiate_combo(elems: Vec<Ty>) -> Ty {
let mut tuple_elems = vec![];
let mut param_decls = vec![];
for elem in elems {
match elem {
Ty::TypeParam(tp) => {
let name = fresh_param();
let params = tp
.params
.iter()
.map(|(b, _)| (name.clone(), Some(TyPrimitive(b.clone()).into())))
.collect();
param_decls.push(TyTypeParam { params, bindings: vec![] });
tuple_elems.push(TyPrimitive(name).into());
}
_ => tuple_elems.push(elem),
}
}
let tuple = TyTuple(tuple_elems).into();
if param_decls.is_empty() {
return tuple;
}
let mut merged = TyTypeParam { params: vec![], bindings: vec![] };
for tp in param_decls {
merged.extend(tp);
}
merged.apply(tuple)
}
fn fresh_params(n: u8) -> Vec<Ty> {
(0..n).map(|_| TyPrimitive(fresh_param()).into()).collect()
}
impl Apply for TyTuple {
fn apply(mut self, o: Ty) -> Ty {
match o {
Ty::Num(TyNum(n)) => tuple_pow(self.0, n),
_ => {
self.0.push(o);
self.into()
}
}
}
}
impl Apply for TyGroup {
fn apply(self, o: Ty) -> Ty {
match o {
Ty::Num(TyNum(n)) => tuple_pow(vec![*self.0], n),
_ => self.0.apply(o),
}
}
}
impl Apply for TyFn {
fn apply(self, o: Ty) -> Ty {
match self {
TyFn(None, None) => match o {
Ty::Tuple(t) => TyFn(Some(t.0), None).into(),
Ty::Group(t) => TyFn(Some(vec![*t.0]), None).into(),
_ => err_ty(
"batch-impl: `fn` 前缀右侧必须是元组类型,如 fn^(i32, u32)",
),
},
TyFn(Some(params), None) => TyFn(Some(params), Some(o.into())).into(),
TyFn(Some(_), Some(_)) => {
err_ty("batch-impl: `fn` 类型已有返回类型,不能重复应用")
}
TyFn(None, Some(_)) => {
err_ty("batch-impl: `fn` 类型缺少参数列表,内部错误")
}
}
}
}
impl Apply for TyWithCode {
fn apply(self, o: Ty) -> Ty {
let inner = match self.0 {
Some(t) => t.apply(o),
None => o,
};
TyWithCode(Some(inner.into()), self.1).into()
}
}
impl Apply for TyWithAttr {
fn apply(self, o: Ty) -> Ty {
TyWithAttr(self.0, Some(o.into())).into()
}
}
impl Apply for TyTypeParam {
fn apply(self, o: Ty) -> Ty {
TyWithType(self, o.into()).into()
}
}
impl Apply for TyNum {
fn apply(self, _: Ty) -> Ty {
err_ty(&format!(
"batch-impl: 数字 `{}` 不能作为左侧操作数,只能出现在右侧(如 T^{})",
self.0, self.0
))
}
}
impl Apply for TyRange {
fn apply(self, _: Ty) -> Ty {
let end_mark = if self.inclusive { "=" } else { "" };
err_ty(&format!(
"batch-impl: 范围 `{}..{}{}` 不能作为左侧操作数,只能出现在右侧(如 T^{}..{}{})",
self.start, self.end, end_mark, self.start, self.end, end_mark
))
}
}
impl Apply for TySlice {
fn apply(self, _: Ty) -> Ty {
err_ty("batch-impl: 切片类型 `[T]` 不能作为左侧操作数")
}
}
impl Apply for TyFixedArray {
fn apply(self, _: Ty) -> Ty {
err_ty("batch-impl: 固定数组类型 `[T; N]` 不能作为左侧操作数")
}
}
impl Apply for TyWithTrait {
fn apply(self, o: Ty) -> Ty {
TyWithTrait(self.0, self.1.apply(o).into()).into()
}
}
impl Apply for TyWithType {
fn apply(self, o: Ty) -> Ty {
TyWithType(self.0, self.1.apply(o).into()).into()
}
}
impl Apply for TyWithWhere {
fn apply(self, o: Ty) -> Ty {
let inner = match self.0 {
Some(t) => t.apply(o),
None => o,
};
TyWithWhere(Some(inner.into()), self.1).into()
}
}