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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use crate::Located;
use super::*;

pub type Result = std::fmt::Result;

pub trait Display {
	fn fmt(&self, f: &mut Formatter) -> Result;
}

impl<T: Display> Display for Located<T> {
	fn fmt(&self, f: &mut Formatter) -> Result {
		self.as_ref().fmt(f)
	}
}

pub struct Formatter<'f, 'a> {
	f: &'f mut std::fmt::Formatter<'a>,
	empty: bool,
	tabs: u32
}

pub struct PrettyPrint<'a, T>(pub &'a T);

impl<'a, T: Display> std::fmt::Display for PrettyPrint<'a, T> {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> Result {
		let mut ppf = Formatter {
			f: f,
			empty: true,
			tabs: 0
		};

		self.0.fmt(&mut ppf)
	}
}

impl<'f, 'a> Formatter<'f, 'a> {
	pub fn next(&mut self) -> Result {
		if self.empty {
			self.empty = false;
			Ok(())
		} else {
			write!(self.f, " ")
		}
	}

	pub fn begin(&mut self) -> Result {
		self.next()?;
		self.empty = true;
		write!(self.f, "(")
	}

	pub fn end(&mut self) -> Result {
		self.empty = false;
		write!(self.f, ")")
	}

	pub fn tab(&mut self) {
		self.tabs += 1;
	}

	pub fn backtab(&mut self) {
		self.tabs -= 1;
	}

	pub fn keyword(&mut self, name: &str) -> Result {
		self.next()?;
		write!(self.f, "{}", name)
	}

	pub fn symbol(&mut self, sym: &Symbol) -> Result {
		self.next()?;
		write!(self.f, "{}", sym)
	}

	/// Go to the next line
	pub fn split(&mut self) -> Result {
		write!(self.f, "\n")?;
		for _ in 0..self.tabs {
			write!(self.f, "\t")?;
		}
		self.empty = true;
		Ok(())
	}

	pub fn pseudo_list<T: Display>(&mut self, list: &[T]) -> Result {
		for e in list {
			e.fmt(self)?;
		}
		Ok(())
	}

	pub fn list<T: Display>(&mut self, list: &[T]) -> Result {
		self.begin()?;
		for e in list {
			e.fmt(self)?;
		}
		self.end()
	}

	pub fn tabulated_list<T: Display>(&mut self, list: &[T]) -> Result {
		self.begin()?;
		self.tab();
		for e in list {
			self.split()?;
			e.fmt(self)?;
		}
		self.backtab();
		self.split()?;
		self.end()
	}

	pub fn comments(&mut self, comments: &str) -> Result {
		for line in comments.lines() {
			write!(self.f, "; {}", line)?;
			self.split()?;
		}

		Ok(())
	}
}

impl Display for Symbol {
	fn fmt(&self, f: &mut Formatter) -> Result {
		f.symbol(self)
	}
}

impl Display for Ident {
	fn fmt(&self, f: &mut Formatter) -> Result {
		f.symbol(&self.id)
	}
}

impl Display for Sort {
	fn fmt(&self, f: &mut Formatter) -> Result {
		if self.parameters.is_empty() {
			self.id.fmt(f)
		} else {
			f.begin()?;
			self.id.fmt(f)?;
			f.pseudo_list(&self.parameters)?;
			f.end()
		}
	}
}

impl Display for Term {
	fn fmt(&self, f: &mut Formatter) -> Result {
		use Term::*;
		match self {
			Ident(id) => id.fmt(f),
			Let { bindings, body } => {
				f.begin()?;
				f.keyword("let")?;
				f.list(bindings)?;
				body.fmt(f)?;
				f.end()
			},
			Forall { vars, body } => {
				f.begin()?;
				f.keyword("forall")?;
				f.list(vars)?;
				body.fmt(f)?;
				f.end()
			},
			Exists { vars, body } => {
				f.begin()?;
				f.keyword("exists")?;
				f.list(vars)?;
				body.fmt(f)?;
				f.end()
			},
			Match { term, cases } => {
				f.begin()?;
				f.keyword("match")?;
				term.fmt(f)?;
				f.tabulated_list(cases)?;
				f.end()
			},
			Apply { id, args } => {
				if args.is_empty() {
					id.fmt(f)
				} else {
					f.begin()?;
					id.fmt(f)?;
					f.list(args)?;
					f.end()
				}
			},
			Coerce { term, sort } => {
				f.begin()?;
				f.keyword("at")?;
				term.fmt(f)?;
				sort.fmt(f)?;
				f.end()
			}
		}
	}
}

impl Display for MatchCase {
	fn fmt(&self, f: &mut Formatter) -> Result {
		f.begin()?;
		self.pattern.fmt(f)?;
		self.term.fmt(f)?;
		f.end()
	}
}

impl Display for Pattern {
	fn fmt(&self, f: &mut Formatter) -> Result {
		if self.args.is_empty() {
			self.id.fmt(f)
		} else {
			f.begin()?;
			self.id.fmt(f)?;
			f.list(&self.args)?;
			f.end()
		}
	}
}

impl Display for Binding {
	fn fmt(&self, f: &mut Formatter) -> Result {
		f.begin()?;
		self.id.fmt(f)?;
		self.value.fmt(f)?;
		f.end()
	}
}

impl Display for SortedVar {
	fn fmt(&self, f: &mut Formatter) -> Result {
		f.begin()?;
		self.id.fmt(f)?;
		self.sort.fmt(f)?;
		f.end()
	}
}