1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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"]))
}
}