use super::ops::{
build_quote_code, build_tuple_code, build_unquote_code, build_variadic_block, lift_spelling,
reduce_spelling, wrap_emit, wrap_splice,
};
use crate::lang::Arity;
use crate::meta::OuterKind;
use crate::prelude::{Index, *};
use crate::qmatch::{pattern_let_code, pattern_var_code};
use crate::{meta::MetaLanguage, qterm::QTerm, term::CmdOrHole};
#[derive(Default)]
pub struct RustMetaLanguage;
impl MetaLanguage for RustMetaLanguage {
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 wrap_child(&self, qterm: Arc<QTerm>, okind: OuterKind) -> Result<Arc<QTerm>> {
Ok(match okind {
OuterKind::None => qterm,
OuterKind::Emit => wrap_emit(&qterm),
OuterKind::Splice => wrap_splice(&qterm),
})
}
fn pattern_tag(&self) -> Option<&'static str> {
Some("let_declaration")
}
fn pattern_var(&self, name: &str) -> Result<Arc<QTerm>> {
Ok(pattern_var_code(name))
}
fn pattern_let(
&self,
names: &[Box<str>],
pattern: &Arc<QTerm>,
value: &Arc<QTerm>,
) -> Result<(Arc<QTerm>, Arc<QTerm>)> {
Ok(pattern_let_code(names, pattern, value))
}
fn lift_str(&self, target: &str) -> Result<&'static str> {
lift_spelling(target)
}
fn reduce_str(&self, target: &str) -> Result<&'static str> {
reduce_spelling(target)
}
fn name_str(&self) -> &'static str {
"name"
}
fn type_str(&self) -> &'static str {
"Arc<QTerm>"
}
fn emit_str(&self) -> &'static str {
"emit(&mut b_)"
}
}
pub trait Lift {
fn lift(&self) -> Arc<QTerm>;
}
pub fn lift<T: Lift>(x: &T) -> Arc<QTerm> {
x.lift()
}
impl Lift for i8 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}i8"))
}
}
impl Lift for i16 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}i16"))
}
}
impl Lift for i32 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}i32"))
}
}
impl Lift for i64 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}i64"))
}
}
impl Lift for i128 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}i128"))
}
}
impl Lift for isize {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}isize"))
}
}
impl Lift for u8 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}u8"))
}
}
impl Lift for u16 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}u16"))
}
}
impl Lift for u32 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}u32"))
}
}
impl Lift for u64 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}u64"))
}
}
impl Lift for u128 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}u128"))
}
}
impl Lift for usize {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}usize"))
}
}
impl Lift for f32 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}f32"))
}
}
impl Lift for f64 {
fn lift(&self) -> Arc<QTerm> {
leaf("integer_literal", &format!("{self}f64"))
}
}
pub fn foo() {
let bar = 123;
dbg!(bar);
}