Skip to content

Commit

Permalink
Prettyprint ir (#428)
Browse files Browse the repository at this point in the history
* Add pretty printer for ir

* Add zero-sized term to ir

---------

Co-authored-by: Tim Süberkrüb <[email protected]>
  • Loading branch information
BinderDavid and timsueberkrueb authored Jan 4, 2025
1 parent ddc83a1 commit bdf4821
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lang/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ categories.workspace = true
url = { workspace = true }
# workspace dependencies
ast = { path = "../ast" }
printer = { path = "../printer" }
93 changes: 93 additions & 0 deletions lang/backend/src/ir/decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
use url::Url;

use ast::UseDecl;
use printer::theme::ThemeExt;
use printer::tokens::*;
use printer::util::{BracesExt, IsNilExt};
use printer::{Alloc, Builder, DocAllocator, Print, PrintCfg};

use super::exprs::{print_cases, print_params};
use super::exprs::{Case, Exp};

#[derive(Debug, Clone)]
Expand All @@ -16,6 +21,43 @@ pub struct Module {
pub let_decls: Vec<Let>,
}

impl Print for Module {
fn print<'a>(&'a self, cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> {
let Module { uri: _, use_decls, def_decls, codef_decls, let_decls } = self;

// UseDecls
//
//

let use_decls =
alloc.intersperse(use_decls.iter().map(|decl| decl.print(cfg, alloc)), alloc.line());

// Decls
//
//

// We usually separate declarations with an empty line, except when the `omit_decl_sep` option is set.
// This is useful for typesetting examples in papers which have to make economic use of vertical space.
let sep = if cfg.omit_decl_sep { alloc.line() } else { alloc.line().append(alloc.line()) };

let def_decls = def_decls.iter().map(|decl| decl.print(cfg, alloc));
let codef_decls = codef_decls.iter().map(|decl| decl.print(cfg, alloc));
let let_decls = let_decls.iter().map(|decl| decl.print(cfg, alloc));

let decls = alloc.intersperse(def_decls.chain(codef_decls).chain(let_decls), sep);

// UseDecls + Decls
//
//

if use_decls.is_nil() {
decls
} else {
use_decls.append(alloc.line()).append(alloc.line()).append(decls)
}
}
}

#[derive(Debug, Clone)]
pub struct Def {
pub name: String,
Expand All @@ -24,16 +66,67 @@ pub struct Def {
pub cases: Vec<Case>,
}

impl Print for Def {
fn print<'a>(&'a self, cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> {
let Def { name, self_param, params, cases } = self;
let head = alloc
.keyword(DEF)
.append(alloc.space())
.append(self_param.print(cfg, alloc))
.append(DOT)
.append(alloc.dtor(name))
.append(print_params(params, alloc))
.group();

let body = print_cases(cases, cfg, alloc);

head.append(alloc.space()).append(body)
}
}

#[derive(Debug, Clone)]
pub struct Codef {
pub name: String,
pub params: Vec<String>,
pub cases: Vec<Case>,
}

impl Print for Codef {
fn print<'a>(&'a self, cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> {
let Codef { name, params, cases } = self;
let head = alloc
.keyword(CODEF)
.append(alloc.space())
.append(alloc.ctor(name))
.append(print_params(params, alloc))
.group();

let body = print_cases(cases, cfg, alloc);

head.append(alloc.space()).append(body)
}
}

#[derive(Debug, Clone)]
pub struct Let {
pub name: String,
pub params: Vec<String>,
pub body: Box<Exp>,
}

impl Print for Let {
fn print<'a>(&'a self, cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> {
let Let { name, params, body } = self;

let head = alloc
.keyword(LET)
.append(alloc.space())
.append(name)
.append(print_params(params, alloc))
.group();

let body = body.print(cfg, alloc).braces_anno();

head.append(alloc.space()).append(body)
}
}
Loading

0 comments on commit bdf4821

Please sign in to comment.