fix pretty

This commit is contained in:
nora 2022-07-02 22:41:42 +02:00
parent 68258a9a95
commit 909c3e9326
4 changed files with 32 additions and 10 deletions

4
.gitignore vendored
View file

@ -1,3 +1,5 @@
/target /target
.idea .idea
.vscode .vscode
# for testing things
_*.c

View file

@ -62,7 +62,11 @@ pub fn parse_file(src: &str) {
let declarations = parser::parse_declarations(lexer); let declarations = parser::parse_declarations(lexer);
match declarations { match declarations {
Ok(declarations) => { Ok(declarations) => {
dbg_pls::color!(declarations); dbg_pls::color!(&declarations);
let mut printer = pretty::PrettyPrinter::new(std::io::stdout().lock(), false);
println!("// START CODE -------------------");
printer.translation_unit(&declarations).unwrap();
println!("// END CODE -------------------");
} }
Err(err) => eprintln!("error :(\n{:#?}", err), Err(err) => eprintln!("error :(\n{:#?}", err),
} }

View file

@ -9,6 +9,14 @@ fn lex_and_pre(src: &str) -> impl Iterator<Item = (Tok<'_>, Span)> + '_ {
fn pretty_print(ast: &Result<Vec<Spanned<ExternalDecl>>, ParserError>) -> String { fn pretty_print(ast: &Result<Vec<Spanned<ExternalDecl>>, ParserError>) -> String {
let mut vec = Vec::new(); let mut vec = Vec::new();
match ast {
Ok(ast) => {
let mut printer = crate::pretty::PrettyPrinter::new(&mut vec, true);
printer.translation_unit(ast).unwrap();
}
Err(err) => vec.extend_from_slice(format!("{err:?}").as_bytes()),
}
String::from_utf8_lossy(&vec).into_owned() String::from_utf8_lossy(&vec).into_owned()
} }

View file

@ -60,27 +60,33 @@ impl<W: Write> PrettyPrinter<W> {
fn external_decl(&mut self, decl: &ExternalDecl) -> Result { fn external_decl(&mut self, decl: &ExternalDecl) -> Result {
match decl { match decl {
ExternalDecl::FunctionDef(def) => self.function_def(def), ExternalDecl::FunctionDef(def) => self.function_def(def),
ExternalDecl::Decl(decl) => self.decl(decl), ExternalDecl::Decl(decl) => self.decl(decl, false),
} }
} }
fn function_def(&mut self, def: &FunctionDef) -> Result { fn function_def(&mut self, def: &FunctionDef) -> Result {
self.decl(&def.decl)?; self.decl(&def.decl, true)?;
self.string(" {")?; self.string(" {")?;
self.indent();
self.linebreak()?; self.linebreak()?;
self.indent();
// TODO: body // TODO: body
self.dedent(); self.dedent();
self.string("}")?;
self.linebreak()?; self.linebreak()?;
Ok(()) Ok(())
} }
fn decl(&mut self, decl: &Decl) -> Result { fn decl(&mut self, decl: &Decl, func: bool) -> Result {
match decl { match decl {
Decl::StaticAssert => todo!(), Decl::StaticAssert => todo!(),
Decl::Normal(normal_decl) => self.normal_decl(normal_decl), Decl::Normal(normal_decl) => self.normal_decl(normal_decl)?,
} }
if !func {
self.string(";")?;
self.linebreak()?;
}
Ok(())
} }
fn normal_decl(&mut self, decl: &NormalDecl) -> Result { fn normal_decl(&mut self, decl: &NormalDecl) -> Result {
@ -99,9 +105,8 @@ impl<W: Write> PrettyPrinter<W> {
} }
fn decl_spec(&mut self, decl_spec: &DeclSpec) -> Result { fn decl_spec(&mut self, decl_spec: &DeclSpec) -> Result {
self.type_specifier(&decl_spec.ty)?;
self.string(" ")?;
self.decl_attr(&decl_spec.attrs)?; self.decl_attr(&decl_spec.attrs)?;
self.type_specifier(&decl_spec.ty)?;
Ok(()) Ok(())
} }
@ -142,7 +147,9 @@ impl<W: Write> PrettyPrinter<W> {
attrs.push("_Thread_local"); attrs.push("_Thread_local");
} }
self.string(&attrs.join(" "))?; self.string(&attrs.join(" "))?;
self.string(" ")?; if !attrs.is_empty() {
self.string(" ")?;
}
Ok(()) Ok(())
} }
@ -176,6 +183,7 @@ impl<W: Write> PrettyPrinter<W> {
} }
first = false; first = false;
self.decl_spec(&decl.decl_spec.0)?; self.decl_spec(&decl.decl_spec.0)?;
self.string(" ")?;
self.declarator(&decl.declarator.0)?; self.declarator(&decl.declarator.0)?;
} }
Ok(()) Ok(())