quiltlang 0.4.0

Quilt, a polyglot metaprogramming language: source code with arrow-bracket syntax for embedding and manipulating code fragments of other languages at code-generation time.
Documentation
#!/usr/bin/env rust-script
use quilt::prelude::*;
use quilt::term::STerm;
use std::process::Command;

/**************************************************************/

fn main() -> Result<()> {
    let meta: ⟨T⟩ = ↖
        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: &⟨T⟩,
                cmds: &[CmdOrHole],
            ) -> Result<⟨T⟩> {
                Ok(build_quote_code(tag, i, lang2, qterm, cmds))
            }

            fn expand_unquote(
                &self,
                _lang1: &str,
                tag: &str,
                i: Index,
                lang2: &str,
                qterm: &⟨T⟩,
                cmds: &[CmdOrHole],
            ) -> Result<⟨T⟩> {
                Ok(build_unquote_code(tag, i, lang2, qterm, cmds))
            }

            fn expand_tuple(
                &self,
                _lang1: &str,
                tag: &str,
                qterms: &[⟨T⟩],
                cmds: &[CmdOrHole],
                arity: Arity,
            ) -> Result<⟨T⟩> {
                Ok(if arity == Arity::Variadic {
                    build_variadic_block(tag, cmds, qterms)
                } else {
                    build_tuple_code(tag, cmds, qterms)
                })
            }

            fn wrap_child(&self, qterm: ⟨T⟩, okind: OuterKind) -> Result<⟨T⟩> {
                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<⟨T⟩> {
                Ok(pattern_var_code(name))
            }

            fn pattern_let(
                &self,
                names: &[Box<str>],
                pattern: &⟨T⟩,
                value: &⟨T⟩,
            ) -> Result<(⟨T⟩, ⟨T⟩)> {
                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 {
            /// Lift to a `QTerm` that evaluates to the same term.
            fn lift(&self) -> ⟨T⟩;
        }

        pub fn lift<T: Lift>(x: &T) -> ⟨T⟩ {
            x.lift()
        }

        ↙
            let num_types = [
                "i8", "i16", "i32", "i64", "i128", "isize",
                "u8", "u16", "u32", "u64", "u128", "usize",
                "f32", "f64",
            ];
            ⟨//⟩ ex: `leaf("integer_literal", "0")`
            let code_int_0: ⟨T⟩ = ↖0↗.↑;
            ⟨//⟩ ex: `"0"`
            let str_0: ⟨T⟩ = ↖"0"↗;
            for &ty in &num_types {
                ⟨//⟩ ex: `format!("{self}i8")`
                let format: ⟨T⟩ = ↖&format!(↙format!("{{self}}{ty}").↑↘)↗;
                ↖
                    impl Lift for ↙⟨N⟩(ty)↘ {
                        fn lift(&self) -> ⟨T⟩ {
                            ⟨//⟩ ex: `leaf("integer_literal", &format!("{self}i8"))`
                            ↙code_int_0.rewrite_naive(&str_0, &format)↘
                        }
                    }
                ↗.←;
            }
        ↘

        pub fn foo() {
            let ↙⟨N⟩("bar")↘ = 123;
            dbg!(↙⟨N⟩("bar")↘);
        }

    ↗;
    meta.dump_with_cmds(
        "src/langs/rust/meta.rs",
        &[write("//! DO NOT EDIT. GENERATED BY `bootstrap`."), NL, NL],
        &[],
    )?;
    Command::new("cargo")
        .args(["fmt", "--", "src/langs/rust/meta.rs"])
        .status()
        .ok();
    Ok(())
}