This commit is contained in:
mlokr 2024-07-19 15:51:02 +02:00
parent f964520641
commit 5555b9865a
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
4 changed files with 21 additions and 18 deletions

View file

@ -557,10 +557,14 @@ pub fn run_compiler(
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)
{
if parser::insert_needed_semicolon(rest) {
write!(out, ";")?;
}
for _ in 1..parser::preserve_newlines(&source[..expr.pos() as usize]) {
writeln!(out)?;
}
}
writeln!(out)?;
}
std::io::Result::Ok(())

View file

@ -875,17 +875,6 @@ impl<'a> std::fmt::Display for Expr<'a> {
}
let source = unsafe { &*FMT_SOURCE.with(|s| s.get()) };
{
let pos = self.pos();
if let Some(before) = source.get(..pos as usize) {
let trailing_whitespace = &before[before.trim_end().len()..];
let ncount = trailing_whitespace.chars().filter(|&c| c == '\n').count();
if ncount > 1 {
writeln!(f)?;
}
}
}
match *self {
Self::Ct { value, .. } => write!(f, "$: {}", value),
@ -963,10 +952,14 @@ 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..)
&& insert_needed_semicolon(rest)
{
if insert_needed_semicolon(rest) {
write!(f, ";")?;
}
for _ in 1..preserve_newlines(&source[..expr.pos() as usize]) {
writeln!(f)?;
}
}
writeln!(f)?;
}
Ok(())
@ -1006,6 +999,11 @@ impl<'a> std::fmt::Display for Expr<'a> {
}
}
pub fn preserve_newlines(source: &str) -> usize {
let trailing_whitespace = &source[source.trim_end().len()..];
trailing_whitespace.chars().filter(|&c| c == '\n').count().min(2)
}
pub fn insert_needed_semicolon(source: &str) -> bool {
let kind = lexer::Lexer::new(source).next().kind;
kind.precedence().is_some() || matches!(kind, TokenKind::Ctor | TokenKind::Tupl)

View file

@ -1,7 +1,9 @@
foo := 0;
.{global, fib} := @use("pkg.hb")
main := fn(a: int): int {
return fib(global)
g := global
return fib(g)
}

View file

@ -1,6 +1,5 @@
global := 10
fib := fn(n: int): int {
return n + 1
}