This commit is contained in:
mlokr 2024-07-19 14:17:45 +02:00
parent 12b39c5b3f
commit 416f646957
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
2 changed files with 18 additions and 21 deletions

View file

@ -550,30 +550,27 @@ pub fn run_compiler(
) -> io::Result<()> {
let parsed = parse_from_fs(options.extra_threads, root_file)?;
fn format_to_stdout(ast: parser::Ast) -> std::io::Result<()> {
fn format_to(ast: &parser::Ast, out: &mut impl std::io::Write) -> std::io::Result<()> {
let source = std::fs::read_to_string(&*ast.path)?;
parser::with_fmt_source(&source, || {
for expr in ast.exprs() {
use std::io::Write;
writeln!(std::io::stdout(), "{expr}")?;
for (i, expr) in ast.exprs().iter().enumerate() {
write!(out, "{expr}")?;
if let Some(expr) = ast.exprs().get(i + 1)
&& let Some(rest) = source.get(expr.pos() as usize..)
&& parser::insert_needed_semicolon(rest)
{
write!(out, ";")?;
}
writeln!(out)?;
}
std::io::Result::Ok(())
})
}
fn format_ast(ast: parser::Ast) -> std::io::Result<()> {
let source = std::fs::read_to_string(&*ast.path)?;
let mut output = Vec::new();
parser::with_fmt_source(&source, || {
for expr in ast.exprs() {
use std::io::Write;
writeln!(output, "{expr}")?;
}
std::io::Result::Ok(())
})?;
format_to(&ast, &mut output)?;
std::fs::write(&*ast.path, output)?;
Ok(())
}
@ -582,7 +579,7 @@ pub fn run_compiler(
format_ast(parsed)?;
}
} else if options.fmt_current {
format_to_stdout(parsed.into_iter().next().unwrap())?;
format_to(&parsed.into_iter().next().unwrap(), &mut std::io::stdout())?;
} else {
let mut codegen = codegen::Codegen::default();
codegen.files = parsed;

View file

@ -942,9 +942,6 @@ impl<'a> std::fmt::Display for Expr<'a> {
write!(f, "fn(")?;
fmt_list(f, false, "", args, |arg, f| write!(f, "{}: {}", arg.name, arg.ty))?;
write!(f, "): {ret} {body}")?;
if !matches!(body, Self::Block { .. }) {
write!(f, ";")?;
}
Ok(())
}
Self::Call { func, args, trailing_comma } => {
@ -966,9 +963,7 @@ impl<'a> std::fmt::Display for Expr<'a> {
write!(f, "{stmt}")?;
if let Some(expr) = stmts.get(i + 1)
&& let Some(rest) = source.get(expr.pos() as usize..)
&& let kind = lexer::Lexer::new(rest).next().kind
&& (kind.precedence().is_some()
|| matches!(kind, TokenKind::Struct | TokenKind::Tupl))
&& insert_needed_semicolon(rest)
{
write!(f, ";")?;
}
@ -1004,6 +999,11 @@ impl<'a> std::fmt::Display for Expr<'a> {
}
}
pub fn insert_needed_semicolon(source: &str) -> bool {
let kind = lexer::Lexer::new(source).next().kind;
kind.precedence().is_some() || matches!(kind, TokenKind::Struct | TokenKind::Tupl)
}
#[repr(C)]
pub struct AstInner<T: ?Sized> {
ref_count: AtomicUsize,