Skip to content

Commit bc830e6

Browse files
committed
Hook up core pretty-printer to CLI
1 parent cd285e1 commit bc830e6

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

fathom/src/core/pretty.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! use codespan_reporting::term::termcolor::{BufferedStandardStream, ColorChoice};
99
//! use fathom::core::pretty::Context;
1010
//! use fathom::core::Module;
11-
//! use fathom::StringInterner;
11+
//! use fathom::source::StringInterner;
1212
//! use std::cell::RefCell;
1313
//! use std::io::Write;
1414
//!
@@ -28,7 +28,7 @@ use pretty::RcDoc;
2828
use std::cell::RefCell;
2929

3030
use crate::core::{Item, Module, Term};
31-
use crate::{StringId, StringInterner};
31+
use crate::source::{StringId, StringInterner};
3232

3333
/// Term precedences
3434
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
@@ -307,9 +307,12 @@ impl<'interner, 'arena> Context<'interner> {
307307
self.term_prec(Prec::Top, body_expr),
308308
])
309309
})
310-
.chain(default_expr.iter().map(|default| {
310+
.chain(default_expr.iter().map(|&(name, default)| {
311311
RcDoc::concat([
312-
RcDoc::text("_"),
312+
match name {
313+
Some(name) => self.string_id(name),
314+
None => RcDoc::text("_"),
315+
},
313316
RcDoc::space(),
314317
RcDoc::text("=>"),
315318
RcDoc::space(),

fathom/src/driver.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::cell::RefCell;
55
use std::io::Read;
66
use std::path::Path;
77

8-
use crate::core::binary;
9-
use crate::core::binary::{BufferError, ReadError};
8+
use crate::core;
9+
use crate::core::binary::{self, BufferError, ReadError};
1010
use crate::source::{ByteRange, FileId, Span, StringInterner};
1111
use crate::surface::{self, elaboration};
1212
use crate::BUG_REPORT_URL;
@@ -189,7 +189,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
189189
}
190190
}
191191

192-
pub fn elaborate_and_emit_module(&mut self, file_id: FileId) -> Status {
192+
pub fn elaborate_and_emit_module(&mut self, file_id: FileId, pretty_core: bool) -> Status {
193193
let mut context = elaboration::Context::new(&self.interner, &self.core_scope);
194194

195195
let surface_module = self.parse_module(file_id);
@@ -201,6 +201,9 @@ impl<'surface, 'core> Driver<'surface, 'core> {
201201
if *self.seen_errors.borrow() && !self.allow_errors {
202202
return Status::Error;
203203
}
204+
if pretty_core {
205+
self.emit_core_module(&module);
206+
}
204207

205208
self.surface_scope.reset(); // Reuse the surface scope for distillation
206209
let context = context.distillation_context(&self.surface_scope);
@@ -348,6 +351,15 @@ impl<'surface, 'core> Driver<'surface, 'core> {
348351
self.emit_doc(context.module(module).into_doc());
349352
}
350353

354+
fn emit_core_module(&self, module: &core::Module<'_>) {
355+
let context = core::pretty::Context::new(&self.interner);
356+
// TODO: Ideally this would be a call to emit_doc
357+
let doc = context.module(module);
358+
let mut emit_writer = self.emit_writer.borrow_mut();
359+
writeln!(emit_writer, "{}", doc.pretty(self.emit_width)).unwrap();
360+
emit_writer.flush().unwrap();
361+
}
362+
351363
fn emit_term(&self, term: &surface::Term<'_, ()>) {
352364
let context = surface::pretty::Context::new(&self.interner, &self.surface_scope);
353365
self.emit_doc(context.term(term).into_doc());

fathom/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ enum Cli {
2828
/// Continue even if errors were encountered
2929
#[clap(long = "allow-errors")]
3030
allow_errors: bool,
31+
/// Pretty print core module
32+
#[clap(long = "pretty-core", conflicts_with("TERM_FILE"))]
33+
pretty_core: bool,
3134
},
3235
/// Normalise a Fathom term, printing its normal form and type
3336
Norm {
@@ -139,6 +142,7 @@ fn main() -> ! {
139142
module_file,
140143
term_file,
141144
allow_errors,
145+
pretty_core,
142146
} => {
143147
let mut driver = fathom::Driver::new();
144148
driver.install_panic_hook();
@@ -148,7 +152,7 @@ fn main() -> ! {
148152
let status = match (module_file, term_file) {
149153
(Some(module_file), None) => {
150154
let file_id = load_file_or_exit(&mut driver, module_file);
151-
driver.elaborate_and_emit_module(file_id)
155+
driver.elaborate_and_emit_module(file_id, pretty_core)
152156
}
153157
(None, Some(term_file)) => {
154158
let file_id = load_file_or_exit(&mut driver, term_file);

tests/cmd/fathom-elab.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ OPTIONS:
1717
--term <TERM_FILE> Path to a term to elaborate
1818
--allow-errors Continue even if errors were encountered
1919
-h, --help Print help information
20+
--pretty-core Pretty print core module
2021

2122
```
2223

@@ -35,6 +36,7 @@ OPTIONS:
3536
--term <TERM_FILE> Path to a term to elaborate
3637
--allow-errors Continue even if errors were encountered
3738
-h, --help Print help information
39+
--pretty-core Pretty print core module
3840

3941
```
4042

0 commit comments

Comments
 (0)