forked from AbleOS/holey-bytes
s
This commit is contained in:
parent
12b39c5b3f
commit
416f646957
|
@ -550,30 +550,27 @@ pub fn run_compiler(
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let parsed = parse_from_fs(options.extra_threads, root_file)?;
|
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)?;
|
let source = std::fs::read_to_string(&*ast.path)?;
|
||||||
parser::with_fmt_source(&source, || {
|
parser::with_fmt_source(&source, || {
|
||||||
for expr in ast.exprs() {
|
for (i, expr) in ast.exprs().iter().enumerate() {
|
||||||
use std::io::Write;
|
write!(out, "{expr}")?;
|
||||||
writeln!(std::io::stdout(), "{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(())
|
std::io::Result::Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_ast(ast: parser::Ast) -> std::io::Result<()> {
|
fn format_ast(ast: parser::Ast) -> std::io::Result<()> {
|
||||||
let source = std::fs::read_to_string(&*ast.path)?;
|
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
parser::with_fmt_source(&source, || {
|
format_to(&ast, &mut output)?;
|
||||||
for expr in ast.exprs() {
|
|
||||||
use std::io::Write;
|
|
||||||
writeln!(output, "{expr}")?;
|
|
||||||
}
|
|
||||||
std::io::Result::Ok(())
|
|
||||||
})?;
|
|
||||||
|
|
||||||
std::fs::write(&*ast.path, output)?;
|
std::fs::write(&*ast.path, output)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -582,7 +579,7 @@ pub fn run_compiler(
|
||||||
format_ast(parsed)?;
|
format_ast(parsed)?;
|
||||||
}
|
}
|
||||||
} else if options.fmt_current {
|
} 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 {
|
} else {
|
||||||
let mut codegen = codegen::Codegen::default();
|
let mut codegen = codegen::Codegen::default();
|
||||||
codegen.files = parsed;
|
codegen.files = parsed;
|
||||||
|
|
|
@ -942,9 +942,6 @@ impl<'a> std::fmt::Display for Expr<'a> {
|
||||||
write!(f, "fn(")?;
|
write!(f, "fn(")?;
|
||||||
fmt_list(f, false, "", args, |arg, f| write!(f, "{}: {}", arg.name, arg.ty))?;
|
fmt_list(f, false, "", args, |arg, f| write!(f, "{}: {}", arg.name, arg.ty))?;
|
||||||
write!(f, "): {ret} {body}")?;
|
write!(f, "): {ret} {body}")?;
|
||||||
if !matches!(body, Self::Block { .. }) {
|
|
||||||
write!(f, ";")?;
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Self::Call { func, args, trailing_comma } => {
|
Self::Call { func, args, trailing_comma } => {
|
||||||
|
@ -966,9 +963,7 @@ impl<'a> std::fmt::Display for Expr<'a> {
|
||||||
write!(f, "{stmt}")?;
|
write!(f, "{stmt}")?;
|
||||||
if let Some(expr) = stmts.get(i + 1)
|
if let Some(expr) = stmts.get(i + 1)
|
||||||
&& let Some(rest) = source.get(expr.pos() as usize..)
|
&& let Some(rest) = source.get(expr.pos() as usize..)
|
||||||
&& let kind = lexer::Lexer::new(rest).next().kind
|
&& insert_needed_semicolon(rest)
|
||||||
&& (kind.precedence().is_some()
|
|
||||||
|| matches!(kind, TokenKind::Struct | TokenKind::Tupl))
|
|
||||||
{
|
{
|
||||||
write!(f, ";")?;
|
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)]
|
#[repr(C)]
|
||||||
pub struct AstInner<T: ?Sized> {
|
pub struct AstInner<T: ?Sized> {
|
||||||
ref_count: AtomicUsize,
|
ref_count: AtomicUsize,
|
||||||
|
|
Loading…
Reference in a new issue