quiltlang 0.6.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::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,
            pattern_let_code, pattern_var_code, prelude_spelling, reduce_method_spelling,
            reduce_spelling, spawn_spelling, type_method_spelling, wrap_emit, wrap_separated,
            wrap_splice,
        };
        use crate::lang::Arity;
        use crate::meta::OuterKind;
        use crate::prelude::{Index, *};
        use crate::qmatch::pattern_binding_at;
        use crate::meta::Prelude;
        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 wrap_separated(&self, qterm: ⟨T⟩, sep: &str) -> Result<⟨T⟩> {
                Ok(wrap_separated(&qterm, sep))
            }

            fn pattern_tag(&self) -> Option<&'static str> {
                Some("let_declaration")
            }

            fn pattern_binding(&self, terms: &[⟨T⟩]) -> Option<(usize, usize)> {
                pattern_binding_at(terms, "=", Some(":"))
            }

            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 reduce_method_str(&self, target: &str) -> Result<&'static str> {
                reduce_method_spelling(target)
            }

            fn name_str(&self) -> Result<&'static str> {
                Ok("name")
            }

            fn type_str(&self) -> Result<&'static str> {
                Ok("Arc<QTerm>")
            }

            fn type_method_str(&self) -> Result<&'static str> {
                type_method_spelling()
            }

            fn spawn_str(&self, lang: &str) -> Result<String> {
                spawn_spelling(lang)
            }

            fn emit_str(&self) -> Result<&'static str> {
                Ok("emit(&mut b_)")
            }

            fn prelude(&self, _targets: &[&str]) -> Option<Prelude> {
                Some(prelude_spelling())
            }
        }

        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 int_types = [
                "i8", "i16", "i32", "i64", "i128", "isize",
                "u8", "u16", "u32", "u64", "u128", "usize",
            ];
            ⟨//⟩ ex: `leaf("integer_literal", "0")`
            let code_int_0: ⟨T⟩ = ↖0↗.↑;
            ⟨//⟩ ex: `"0"`
            let str_int_0: ⟨T⟩ = ↖"0"↗;
            for &ty in &int_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_int_0, &format)↘
                        }
                    }
                ↗.←;
            }

            ⟨//⟩ Floats need their own template. The tag comes from whatever
            ⟨//⟩ literal the template was lifted from, so reusing the integer one
            ⟨//⟩ made every float impl emit `leaf("integer_literal", "1f32")` — a
            ⟨//⟩ float tagged as an integer (issue #174).
            let float_types = ["f32", "f64"];
            ⟨//⟩ ex: `leaf("float_literal", "0.0")`
            let code_float_0: ⟨T⟩ = ↖0.0↗.↑;
            ⟨//⟩ ex: `"0.0"`
            let str_float_0: ⟨T⟩ = ↖"0.0"↗;
            for &ty in &float_types {
                ⟨//⟩ ex: `format!("{self}f32")`
                let format: ⟨T⟩ = ↖&format!(↙format!("{{self}}{ty}").↑↘)↗;
                ↖
                    impl Lift for ↙⟨N⟩(ty)↘ {
                        fn lift(&self) -> ⟨T⟩ {
                            ⟨//⟩ ex: `leaf("float_literal", &format!("{self}f32"))`
                            ↙code_float_0.rewrite_naive(&str_float_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(())
}