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
use miette::Result;

use super::ops::{build_quote_code, build_tuple_code, build_unquote_code, build_variadic_block};
use crate::lang::Arity;
use crate::prelude::{Index, *};
use crate::{
    meta::{spawn_spelling, MetaLanguage, Prelude},
    qterm::QTerm,
    term::CmdOrHole,
};

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

#[derive(Default)]
pub struct PythonMetaLanguage;

impl MetaLanguage for PythonMetaLanguage {
    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)
        })
    }

    // `↑` is target-directed and written prefix, `↑(value)`: into Python it
    // spells the `qlift` function (int, str, or QTerm — a method can't hang
    // off builtin ints); into HTML it spells `qlift_html`, which
    // entity-escapes lifted strings at runtime; into TypeScript `qlift_ts`, and
    // into text `qlift_text`. All live in the `quilt_python` runtime.
    fn lift_str(&self, target: &str) -> Result<&'static str> {
        match target {
            "python" | "py" => Ok("qlift"),
            "html" => Ok("qlift_html"),
            "typescript" | "ts" => Ok("qlift_ts"),
            // Into text: `qlift_text`, or `qlift_text_in_quotes` for a hole the
            // author wrapped in `"…"` (`lift::text`). Both take an optional
            // dialect, and both are plain module globals, so a module that
            // rebinds them picks a dialect for every `↑` it contains.
            "text" | "txt" => Ok("qlift_text"),
            crate::lift::TEXT_IN_QUOTES => Ok("qlift_text_in_quotes"),
            _ => miette::bail!("python can't lift into {target:?}: no spelling registered"),
        }
    }

    fn reduce_str(&self, target: &str) -> Result<&'static str> {
        match target {
            "" | "python" | "py" => Ok("reduce()"),
            "rust" | "rs" => Ok("reduce_rs()"),
            _ => miette::bail!("python can't reduce via {target:?}: no reduce_spelling registered"),
        }
    }

    /// Machine-directed reduce: `db.↓(term)` spells the machine binding's
    /// `eval` method (#268). Target-agnostic — the machine value, not the
    /// annotation, knows its language.
    fn reduce_method_str(&self, target: &str) -> Result<&'static str> {
        match target {
            "" => Ok("eval"),
            _ => miette::bail!(
                "python spells machine eval as a method on the machine value — write \
                 `m.↓(term)` with no annotation; the machine knows its own language"
            ),
        }
    }

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

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

    /// The typing judgment on a machine: `db.⟨T⟩(term)` spells the machine
    /// binding's `type_of` method (#273), the companion of
    /// [`Self::reduce_method_str`]'s value judgment.
    fn type_method_str(&self) -> Result<&'static str> {
        Ok("type_of")
    }

    /// `lang⟨M⟩` spells the `spawn` the Python bindings already export
    /// (`quilt-python`, #271) — the same provider choice `Multi::machine`
    /// makes, so the machine a `.py.quilt` file gets is the machine the
    /// conformance battery verifies.
    fn spawn_str(&self, lang: &str) -> Result<String> {
        spawn_spelling("python", lang, |l| format!("spawn({l:?})"))
    }

    /// No spelling: `←` needs a named `b_` accumulator in scope, and this host
    /// has none (issue #152).
    ///
    /// Rust's variadic block is a *block expression*, so it can bind
    /// `let mut b_ = tb(..)` and let ground statements append to it. Python has
    /// no statement-block in expression position, so
    /// [`build_variadic_block`](super::ops::build_variadic_block) emits the
    /// fluent `tb(..).e(child).b()` chain instead — nothing to bind, and the
    /// hole sits in argument position where a ground `for` is a syntax error.
    /// The `quilt_python` runtime also exposes no `emit` method on a term, so
    /// even a `b_` in scope would have nothing to call.
    ///
    /// This used to return `"emit(b_)"`, which quietly expanded a ground `←`
    /// into generated Python referencing an undefined name — the same
    /// silent-corruption failure the string-based hosts had in #190, and the
    /// reason this accessor returns `Result` at all.
    ///
    /// The working alternative needs no accumulator from *us*: ground Python
    /// builds the sequence with its own builder and splices the finished term,
    /// which goes through `wrap_child` and the `.e(..)` chain as usual.
    ///
    /// ```python
    /// b = tb("block")
    /// for n in names:
    ///     b.e(↖print(↙name(n)↘)↗)
    /// body = b.b()
    /// out = ↖def f():
    ///     ↙body↘
    /// ↗
    /// ```
    fn emit_str(&self) -> Result<&'static str> {
        miette::bail!(
            "python can't emit `←`: the fluent `.e(child)` chain has no named `b_` accumulator \
             to emit into — build the sequence with your own `tb(..)` builder in ground code and \
             splice the finished term with `↙…↘`"
        )
    }

    /// `from quilt import *` — the `quilt` package whose `__init__.py`
    /// re-exports the native `quilt._quilt` module (issue #274). One glob, so
    /// the targets do not matter: `qlift`/`qlift_html` come along with
    /// everything else.
    ///
    /// The marker is the import *statement* rather than the whole line, so a
    /// hand-written selective import (`from quilt import tb, name`) counts as
    /// already-imported and is left alone.
    fn prelude(&self, _targets: &[&str]) -> Option<Prelude> {
        Some(Prelude::new("from quilt import *", ["from quilt import"]))
    }
}