1use super::ops::{
4 build_quote_code, build_tuple_code, build_unquote_code, build_variadic_block, lift_spelling,
5 pattern_let_code, pattern_var_code, prelude_spelling, reduce_method_spelling, reduce_spelling,
6 spawn_spelling, type_method_spelling, wrap_emit, wrap_separated, wrap_splice,
7};
8use crate::lang::Arity;
9use crate::meta::OuterKind;
10use crate::meta::Prelude;
11use crate::prelude::{Index, *};
12use crate::qmatch::pattern_binding_at;
13use crate::{meta::MetaLanguage, qterm::QTerm, term::CmdOrHole};
14
15#[derive(Default)]
18pub struct RustMetaLanguage;
19
20impl MetaLanguage for RustMetaLanguage {
21 fn expand_quote(
22 &self,
23 _lang1: &str,
24 tag: &str,
25 i: Index,
26 lang2: &str,
27 qterm: &Arc<QTerm>,
28 cmds: &[CmdOrHole],
29 ) -> Result<Arc<QTerm>> {
30 Ok(build_quote_code(tag, i, lang2, qterm, cmds))
31 }
32
33 fn expand_unquote(
34 &self,
35 _lang1: &str,
36 tag: &str,
37 i: Index,
38 lang2: &str,
39 qterm: &Arc<QTerm>,
40 cmds: &[CmdOrHole],
41 ) -> Result<Arc<QTerm>> {
42 Ok(build_unquote_code(tag, i, lang2, qterm, cmds))
43 }
44
45 fn expand_tuple(
46 &self,
47 _lang1: &str,
48 tag: &str,
49 qterms: &[Arc<QTerm>],
50 cmds: &[CmdOrHole],
51 arity: Arity,
52 ) -> Result<Arc<QTerm>> {
53 Ok(if arity == Arity::Variadic {
54 build_variadic_block(tag, cmds, qterms)
55 } else {
56 build_tuple_code(tag, cmds, qterms)
57 })
58 }
59
60 fn wrap_child(&self, qterm: Arc<QTerm>, okind: OuterKind) -> Result<Arc<QTerm>> {
61 Ok(match okind {
62 OuterKind::None => qterm,
63 OuterKind::Emit => wrap_emit(&qterm),
64 OuterKind::Splice => wrap_splice(&qterm),
65 })
66 }
67
68 fn wrap_separated(&self, qterm: Arc<QTerm>, sep: &str) -> Result<Arc<QTerm>> {
69 Ok(wrap_separated(&qterm, sep))
70 }
71
72 fn pattern_tag(&self) -> Option<&'static str> {
73 Some("let_declaration")
74 }
75
76 fn pattern_binding(&self, terms: &[Arc<QTerm>]) -> Option<(usize, usize)> {
77 pattern_binding_at(terms, "=", Some(":"))
78 }
79
80 fn pattern_var(&self, name: &str) -> Result<Arc<QTerm>> {
81 Ok(pattern_var_code(name))
82 }
83
84 fn pattern_let(
85 &self,
86 names: &[Box<str>],
87 pattern: &Arc<QTerm>,
88 value: &Arc<QTerm>,
89 ) -> Result<(Arc<QTerm>, Arc<QTerm>)> {
90 Ok(pattern_let_code(names, pattern, value))
91 }
92
93 fn lift_str(&self, target: &str) -> Result<&'static str> {
94 lift_spelling(target)
95 }
96
97 fn reduce_str(&self, target: &str) -> Result<&'static str> {
98 reduce_spelling(target)
99 }
100
101 fn reduce_method_str(&self, target: &str) -> Result<&'static str> {
102 reduce_method_spelling(target)
103 }
104
105 fn name_str(&self) -> Result<&'static str> {
106 Ok("name")
107 }
108
109 fn type_str(&self) -> Result<&'static str> {
110 Ok("Arc<QTerm>")
111 }
112
113 fn type_method_str(&self) -> Result<&'static str> {
114 type_method_spelling()
115 }
116
117 fn spawn_str(&self, lang: &str) -> Result<String> {
118 spawn_spelling(lang)
119 }
120
121 fn emit_str(&self) -> Result<&'static str> {
122 Ok("emit(&mut b_)")
123 }
124
125 fn prelude(&self, _targets: &[&str]) -> Option<Prelude> {
126 Some(prelude_spelling())
127 }
128}
129
130pub trait Lift {
131 fn lift(&self) -> Arc<QTerm>;
133}
134
135pub fn lift<T: Lift>(x: &T) -> Arc<QTerm> {
136 x.lift()
137}
138
139impl Lift for i8 {
140 fn lift(&self) -> Arc<QTerm> {
141 leaf("integer_literal", &format!("{self}i8"))
142 }
143}
144impl Lift for i16 {
145 fn lift(&self) -> Arc<QTerm> {
146 leaf("integer_literal", &format!("{self}i16"))
147 }
148}
149impl Lift for i32 {
150 fn lift(&self) -> Arc<QTerm> {
151 leaf("integer_literal", &format!("{self}i32"))
152 }
153}
154impl Lift for i64 {
155 fn lift(&self) -> Arc<QTerm> {
156 leaf("integer_literal", &format!("{self}i64"))
157 }
158}
159impl Lift for i128 {
160 fn lift(&self) -> Arc<QTerm> {
161 leaf("integer_literal", &format!("{self}i128"))
162 }
163}
164impl Lift for isize {
165 fn lift(&self) -> Arc<QTerm> {
166 leaf("integer_literal", &format!("{self}isize"))
167 }
168}
169impl Lift for u8 {
170 fn lift(&self) -> Arc<QTerm> {
171 leaf("integer_literal", &format!("{self}u8"))
172 }
173}
174impl Lift for u16 {
175 fn lift(&self) -> Arc<QTerm> {
176 leaf("integer_literal", &format!("{self}u16"))
177 }
178}
179impl Lift for u32 {
180 fn lift(&self) -> Arc<QTerm> {
181 leaf("integer_literal", &format!("{self}u32"))
182 }
183}
184impl Lift for u64 {
185 fn lift(&self) -> Arc<QTerm> {
186 leaf("integer_literal", &format!("{self}u64"))
187 }
188}
189impl Lift for u128 {
190 fn lift(&self) -> Arc<QTerm> {
191 leaf("integer_literal", &format!("{self}u128"))
192 }
193}
194impl Lift for usize {
195 fn lift(&self) -> Arc<QTerm> {
196 leaf("integer_literal", &format!("{self}usize"))
197 }
198}
199impl Lift for f32 {
200 fn lift(&self) -> Arc<QTerm> {
201 leaf("float_literal", &format!("{self}f32"))
202 }
203}
204impl Lift for f64 {
205 fn lift(&self) -> Arc<QTerm> {
206 leaf("float_literal", &format!("{self}f64"))
207 }
208}
209
210pub fn foo() {
211 let bar = 123;
212 dbg!(bar);
213}