Add tuples and pretty-printing
Getty Ritter
6 years ago
2 | 2 | |
3 | 3 | type Ident = String; |
4 | 4 | type Env = HashMap<Ident, Value>; |
5 | ||
6 | trait Show { | |
7 | fn show(&self, buf: &mut String); | |
8 | } | |
5 | 9 | |
6 | 10 | #[derive(Debug, Clone)] |
7 | 11 | pub enum Stmt { |
8 | 12 | Let(Pat, Option<Type>, Vec<Pat>, Expr), |
9 | 13 | Expr(Expr), |
14 | } | |
15 | ||
16 | impl Show for Stmt { | |
17 | fn show(&self, buf: &mut String) { | |
18 | match *self { | |
19 | Stmt::Let(ref p, _, _, ref e) => { | |
20 | p.show(buf); | |
21 | buf.push_str(" = "); | |
22 | e.show(buf); | |
23 | } | |
24 | Stmt::Expr(ref e) => | |
25 | e.show(buf), | |
26 | } | |
27 | buf.push_str(";\n"); | |
28 | } | |
10 | 29 | } |
11 | 30 | |
12 | 31 | impl Stmt { |
24 | 43 | #[derive(Debug, Clone)] |
25 | 44 | pub enum Pat { |
26 | 45 | Var(Ident), |
46 | Tuple(Vec<Pat>), | |
47 | } | |
48 | ||
49 | impl Show for Pat { | |
50 | fn show(&self, buf: &mut String) { | |
51 | match *self { | |
52 | Pat::Var(ref i) => buf.push_str(i), | |
53 | Pat::Tuple(ref ps) => { | |
54 | buf.push_str("("); | |
55 | for (n, t) in ps.iter().enumerate() { | |
56 | if n > 0 { | |
57 | buf.push_str(", "); | |
58 | } | |
59 | t.show(buf); | |
60 | } | |
61 | buf.push_str(")"); | |
62 | } | |
63 | } | |
64 | } | |
27 | 65 | } |
28 | 66 | |
29 | 67 | impl Pat { |
31 | 69 | match *self { |
32 | 70 | Pat::Var(ref n) => { |
33 | 71 | let _ = env.insert(n.clone(), val); |
72 | }, | |
73 | Pat::Tuple(ref ps) => { | |
74 | match val { | |
75 | Value::Tuple(es) => { | |
76 | if es.len() != ps.len() { | |
77 | panic!("non-matching tuples!"); | |
78 | } | |
79 | for (p, e) in ps.iter().zip(es.into_iter()) { | |
80 | p.bind(e, env); | |
81 | } | |
82 | } | |
83 | _ => panic!("not matching a tuple!"), | |
84 | } | |
34 | 85 | } |
35 | 86 | } |
36 | 87 | } |
37 | 88 | |
38 | 89 | fn mk_var<S: Into<String>>(s: S) -> Pat { |
39 | 90 | Pat::Var(s.into()) |
91 | } | |
92 | ||
93 | fn mk_tuple(pats: Vec<Pat>) -> Pat { | |
94 | Pat::Tuple(pats) | |
40 | 95 | } |
41 | 96 | } |
42 | 97 | |
59 | 114 | Fn(Box<Pat>, Box<Expr>), |
60 | 115 | App(Box<Expr>, Box<Expr>), |
61 | 116 | Do(Vec<Stmt>), |
117 | Tuple(Vec<Expr>), | |
118 | } | |
119 | ||
120 | impl Show for Expr { | |
121 | fn show(&self, buf: &mut String) { | |
122 | match *self { | |
123 | Expr::Var(ref v) => buf.push_str(v), | |
124 | Expr::Lit(ref l) => l.show(buf), | |
125 | Expr::Fn(ref p, ref e) => { | |
126 | buf.push_str("fn "); | |
127 | p.show(buf); | |
128 | buf.push_str(" => "); | |
129 | e.show(buf); | |
130 | } | |
131 | Expr::App(ref e1, ref e2) => { | |
132 | buf.push_str("("); | |
133 | e1.show(buf); | |
134 | buf.push_str(")("); | |
135 | e2.show(buf); | |
136 | buf.push_str(")"); | |
137 | } | |
138 | Expr::Do(ref ss) => { | |
139 | buf.push_str("do\n"); | |
140 | for s in ss.iter() { | |
141 | s.show(buf); | |
142 | } | |
143 | buf.push_str("end\n"); | |
144 | } | |
145 | Expr::Tuple(ref ts) => { | |
146 | buf.push_str("("); | |
147 | for (n, t) in ts.iter().enumerate() { | |
148 | if n > 0 { | |
149 | buf.push_str(", "); | |
150 | } | |
151 | t.show(buf); | |
152 | } | |
153 | buf.push_str(")"); | |
154 | } | |
155 | } | |
156 | } | |
62 | 157 | } |
63 | 158 | |
64 | 159 | #[derive(Debug, Clone)] |
65 | 160 | pub enum Value { |
66 | 161 | Closure(Pat, Expr, Box<Env>), |
67 | 162 | Lit(Literal), |
163 | Tuple(Vec<Value>), | |
68 | 164 | Unit, |
165 | } | |
166 | ||
167 | impl Show for Value { | |
168 | fn show(&self, buf: &mut String) { | |
169 | match *self { | |
170 | Value::Unit => buf.push_str("()"), | |
171 | Value::Lit(ref l) => l.show(buf), | |
172 | Value::Tuple(ref ts) => { | |
173 | buf.push_str("("); | |
174 | for (n, t) in ts.iter().enumerate() { | |
175 | if n > 0 { | |
176 | buf.push_str(", "); | |
177 | } | |
178 | t.show(buf); | |
179 | } | |
180 | buf.push_str(")"); | |
181 | } | |
182 | Value::Closure(ref p, ref e, _) => { | |
183 | buf.push_str("(\\"); | |
184 | p.show(buf); | |
185 | buf.push_str("."); | |
186 | e.show(buf); | |
187 | buf.push_str(")"); | |
188 | } | |
189 | } | |
190 | } | |
69 | 191 | } |
70 | 192 | |
71 | 193 | impl Expr { |
101 | 223 | } |
102 | 224 | Ok(result) |
103 | 225 | }, |
226 | ||
227 | Expr::Tuple(ref exprs) => { | |
228 | let res: Result<Vec<Value>, String> = | |
229 | exprs.iter().map(|e|e.eval(env)).collect(); | |
230 | Ok(Value::Tuple(res?)) | |
231 | }, | |
104 | 232 | } |
105 | 233 | } |
106 | 234 | |
115 | 243 | fn mk_var<S: Into<String>>(s: S) -> Expr { |
116 | 244 | Expr::Var(s.into()) |
117 | 245 | } |
246 | ||
247 | fn mk_tuple(es: Vec<Expr>) -> Expr { | |
248 | Expr::Tuple(es) | |
249 | } | |
118 | 250 | } |
119 | 251 | |
120 | 252 | #[derive(Debug, Clone)] |
122 | 254 | Int(i64), |
123 | 255 | } |
124 | 256 | |
257 | impl Show for Literal { | |
258 | fn show(&self, buf: &mut String) { | |
259 | match *self { | |
260 | Literal::Int(i) => | |
261 | buf.push_str(&format!("{}", i)), | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
266 | fn show<S: Show>(s: &S) -> String { | |
267 | let mut buf = String::new(); | |
268 | s.show(&mut buf); | |
269 | buf | |
270 | } | |
271 | ||
272 | fn test(e: &Expr) { | |
273 | let mut env = HashMap::new(); | |
274 | println!("> {}", show(e)); | |
275 | println!(" -> {}", show(&e.eval(&mut env).unwrap())); | |
276 | } | |
125 | 277 | |
126 | 278 | fn main() { |
127 | 279 | let sample = Expr::mk_app( |
129 | 281 | Pat::mk_var("x"), |
130 | 282 | Expr::mk_fn( |
131 | 283 | Pat::mk_var("y"), |
132 |
Expr::mk_ |
|
284 | Expr::mk_tuple(vec![ | |
285 | Expr::mk_var("x"), | |
286 | Expr::mk_var("y"), | |
287 | ]), | |
133 | 288 | ), |
134 | 289 | ), |
135 | 290 | Expr::Lit(Literal::Int(5)), |
136 | 291 | ); |
137 | 292 | |
138 | let mut env = HashMap::new(); | |
139 | println!("{:#?}", sample.eval(&mut env)); | |
140 | println!("{:#?}", Expr::mk_app(sample, Expr::Lit(Literal::Int(6))).eval(&mut env)); | |
141 | } | |
293 | test(&sample); | |
294 | ||
295 | let rs = Expr::mk_app(sample, Expr::Lit(Literal::Int(6))); | |
296 | test(&rs); | |
297 | ||
298 | let sample2 = Expr::mk_fn( | |
299 | Pat::mk_tuple(vec![ | |
300 | Pat::mk_var("a"), | |
301 | Pat::mk_var("b"), | |
302 | ]), | |
303 | Expr::mk_var("a"), | |
304 | ); | |
305 | ||
306 | test(&Expr::mk_app(sample2, rs)); | |
307 | } |