Skip to main content

quilt/langs/bootstrap/
strlift.rs

1//! String-style lifting used by bootstrap meta-language.
2
3use crate::{prelude::*, term::CmdOrHole};
4
5/**************************************************************/
6
7pub trait StrLift {
8    /// Lift this to a string that reduces to it.
9    fn strlift(&self) -> String;
10}
11
12impl StrLift for Arc<QTerm> {
13    fn strlift(&self) -> String {
14        match &**self {
15            // span is dropped: lifted code rebuilds the term without one
16            QTerm::Quote {
17                tag,
18                index,
19                lang,
20                term,
21                cmds,
22                ..
23            } => format!(
24                "quote({}, {}, {}, {}, &{})",
25                (**tag).strlift(),
26                index.strlift(),
27                (**lang).strlift(),
28                (*term).strlift(),
29                (**cmds).strlift(),
30            ),
31            QTerm::Unquote {
32                tag,
33                index,
34                lang,
35                term,
36                cmds,
37                ..
38            } => format!(
39                "unquote({}, {}, {}, {}, &{})",
40                (**tag).strlift(),
41                index.strlift(),
42                (**lang).strlift(),
43                (*term).strlift(),
44                (**cmds).strlift(),
45            ),
46            QTerm::Tuple { tag, terms, cmds } => {
47                // use shorthands when possible
48                if terms.is_empty() && cmds.len() == 1 {
49                    if let CmdOrHole::Cmd(StrCmd::Write(code)) = &cmds[0] {
50                        return if tag == code {
51                            format!("sym({})", (**tag).strlift())
52                        } else {
53                            format!("leaf({}, {})", (**tag).strlift(), (**code).strlift())
54                        };
55                    }
56                }
57                // fall back to full representation
58                let mut children = terms.iter();
59                let mut ret = format!("tb({})", (**tag).strlift());
60                for cmd in cmds {
61                    let s = match cmd {
62                        CmdOrHole::Cmd(StrCmd::Write(s)) => &format!(".w({})", (**s).strlift()),
63                        CmdOrHole::Cmd(StrCmd::NewLine) => ".n()",
64                        CmdOrHole::Cmd(StrCmd::Push(s)) => &format!(".p({})", (**s).strlift()),
65                        CmdOrHole::Cmd(StrCmd::Pop) => ".x()",
66                        CmdOrHole::Hole => &format!(".c(&{})", children.next().unwrap().strlift()),
67                    };
68                    ret.push_str(s);
69                }
70                ret.push_str(".b()");
71                ret
72            }
73        }
74    }
75}
76
77impl StrLift for u8 {
78    fn strlift(&self) -> String {
79        format!("{self}")
80    }
81}
82
83impl<T: StrLift> StrLift for Box<T> {
84    fn strlift(&self) -> String {
85        format!("bx({})", (**self).strlift())
86    }
87}
88
89impl<T: StrLift> StrLift for Vec<T> {
90    fn strlift(&self) -> String {
91        format!(
92            "vec![{}]",
93            self.iter()
94                .map(|x| x.strlift())
95                .collect::<Vec<_>>()
96                .join(", ")
97        )
98    }
99}
100
101impl<T: StrLift> StrLift for [T] {
102    fn strlift(&self) -> String {
103        format!(
104            "[{}]",
105            self.iter()
106                .map(|x| x.strlift())
107                .collect::<Vec<_>>()
108                .join(", ")
109        )
110    }
111}
112
113// why is this necessary given impls for Box<T> and [T]?
114impl<T: StrLift> StrLift for Box<[T]> {
115    fn strlift(&self) -> String {
116        format!("bx({})", (**self).strlift())
117    }
118}
119
120// why is this necessary given impls for Box<T> and str?
121impl StrLift for Box<str> {
122    fn strlift(&self) -> String {
123        format!("bx({})", (**self).strlift())
124    }
125}
126
127impl<T: StrLift, const N: usize> StrLift for [T; N] {
128    fn strlift(&self) -> String {
129        format!(
130            "[{}]",
131            self.iter()
132                .map(|x| x.strlift())
133                .collect::<Vec<_>>()
134                .join(", ")
135        )
136    }
137}
138
139impl StrLift for StrCmd {
140    fn strlift(&self) -> String {
141        match self {
142            StrCmd::Write(s) => format!("write({})", (**s).strlift()),
143            StrCmd::NewLine => "NL".to_string(),
144            StrCmd::Push(s) => format!("push({})", (**s).strlift()),
145            StrCmd::Pop => "POP".to_string(),
146        }
147    }
148}
149
150impl StrLift for str {
151    fn strlift(&self) -> String {
152        let escaped = self.replace('\\', "\\\\").replace('\"', "\\\"");
153        format!("\"{escaped}\"")
154    }
155}
156
157impl StrLift for &str {
158    fn strlift(&self) -> String {
159        format!("\"{self}\"")
160    }
161}
162
163impl StrLift for char {
164    fn strlift(&self) -> String {
165        format!("'{self}'")
166    }
167}
168
169impl StrLift for i32 {
170    fn strlift(&self) -> String {
171        format!("{self}")
172    }
173}
174
175impl<T: StrLift> StrLift for Arc<T> {
176    fn strlift(&self) -> String {
177        format!("arc({})", (**self).strlift())
178    }
179}
180
181impl StrLift for CmdOrHole {
182    fn strlift(&self) -> String {
183        match self {
184            CmdOrHole::Cmd(cmd) => format!("cmd({})", cmd.strlift()),
185            CmdOrHole::Hole => "HOLE".to_string(),
186        }
187    }
188}
189
190impl StrLift for String {
191    fn strlift(&self) -> String {
192        format!("\"{}\"", self.replace('\"', "\\\""))
193    }
194}
195
196impl StrLift for usize {
197    fn strlift(&self) -> String {
198        format!("{self}")
199    }
200}