quilt/langs/python/ops.rs
1//! Direct term-lifting for the Python meta-language.
2//!
3//! These helpers build the `Arc<QTerm>` that *reconstructs* a term by writing
4//! constructor source (`tb(..).c(child)..b()`, `quote(..)`, ...) directly and
5//! splicing child terms at holes. The emitted source is **Python**, targeting a
6//! `quilt` Python runtime whose builder mirrors the Rust `QTermBuilder` fluent
7//! API (`tb`/`.c`/`.w`/`.n`/`.p`/`.x`/`.e`/`.b`, `quote`/`unquote`, `leaf`/`sym`,
8//! `cmd`/`write`/`push`/`NL`/`POP`/`HOLE`).
9//!
10//! The fold that assembles those chains is shared with the Rust and TypeScript
11//! metas — see [`crate::langs::chain`]. All that is Python-specific is which
12//! fragments it writes, which is a generated table, and the escaping below.
13//!
14//! One difference is not a spelling and so is not in the table: a variadic node
15//! is a fluent `.e()` emit chain rather than Rust's imperative
16//! `{ let mut b_ = ..; ..; b_.b() }` block, which has no Python
17//! expression-context equivalent. (A consequence: statement-context splicing —
18//! control flow inside `↙..↘` that emits into a named `b_` — is not expressible
19//! in Python and is unsupported.)
20
21use crate::langs::chain::{Chain, Lit, PYTHON};
22use crate::prelude::*;
23use crate::term::CmdOrHole;
24
25/**************************************************************/
26
27/// Render a Python double-quoted string literal, escaping `\` and `"` and the
28/// control characters a one-line literal cannot hold.
29///
30/// Escaping only `"` used to be enough by accident: no literal it was handed
31/// held a newline. A multi-line token whose continuation line sits outside the
32/// prefix is now written as a raw `"\n"` (so it is not re-indented), which in
33/// an unescaped `"…"` is a syntax error. And an unescaped `\` was always wrong:
34/// `"a\\nb"` in a quoted string came out as `"a\nb"`, a different string.
35fn str_lit(s: &str) -> String {
36 use std::fmt::Write as _;
37 let mut out = String::with_capacity(s.len() + 2);
38 out.push('"');
39 for c in s.chars() {
40 match c {
41 '"' | '\\' => {
42 out.push('\\');
43 out.push(c);
44 }
45 '\n' => out.push_str("\\n"),
46 '\r' => out.push_str("\\r"),
47 '\t' => out.push_str("\\t"),
48 c if c.is_control() => write!(out, "\\u{:04x}", c as u32).unwrap(),
49 c => out.push(c),
50 }
51 }
52 out.push('"');
53 out
54}
55
56/// The shared builder-call fold, spelled for Python.
57const CHAIN: Chain = Chain::new(&PYTHON, Lit::Flat(str_lit));
58
59/**************************************************************/
60
61/// Build code that reconstructs a tuple: `tb(tag).w(..).c(child)..b()`, using
62/// the `sym`/`leaf` shorthands when possible. `children` are the already-built
63/// child expressions spliced at hole positions.
64pub fn build_tuple_code(tag: &str, cmds: &[CmdOrHole], children: &[Arc<QTerm>]) -> Arc<QTerm> {
65 CHAIN.tuple_code(tag, cmds, children)
66}
67
68/// Build `quote(tag, index, lang, <term>, [..cmds..])`, splicing `term`.
69pub fn build_quote_code(
70 tag: &str,
71 index: Index,
72 lang: &str,
73 term: &Arc<QTerm>,
74 cmds: &[CmdOrHole],
75) -> Arc<QTerm> {
76 CHAIN.quote_code(tag, index, lang, term, cmds)
77}
78
79/// Build `unquote(tag, index, lang, <term>, [..cmds..])`, splicing `term`.
80pub fn build_unquote_code(
81 tag: &str,
82 index: Index,
83 lang: &str,
84 term: &Arc<QTerm>,
85 cmds: &[CmdOrHole],
86) -> Arc<QTerm> {
87 CHAIN.unquote_code(tag, index, lang, term, cmds)
88}
89
90/// Build a variadic node as a fluent emit chain:
91/// `tb(tag).e(child).p("..").n()..b()`. `children` are the already-expanded
92/// terms; each is emitted with `.e(..)` (which, like Rust's `.emit(&mut b_)`,
93/// appends one-or-more children). Unlike Rust's variadic, there is no named
94/// `b_`, so statement-context splicing is unsupported (see module docs).
95pub fn build_variadic_block(tag: &str, cmds: &[CmdOrHole], children: &[Arc<QTerm>]) -> Arc<QTerm> {
96 CHAIN.variadic_code(tag, cmds, children)
97}
98
99/**************************************************************/
100
101/// Make an identifier term (the `⟨N⟩` operator).
102pub fn name(s: &str) -> Arc<QTerm> {
103 leaf("identifier", s)
104}