1
0
Fork 0
forked from koniifer/ableos
This commit is contained in:
mlokr 2024-07-19 15:51:02 +02:00
parent 757c7894d9
commit 87726395e1
4 changed files with 21 additions and 18 deletions

View file

@ -557,10 +557,14 @@ pub fn run_compiler(
write!(out, "{expr}")?; write!(out, "{expr}")?;
if let Some(expr) = ast.exprs().get(i + 1) if let Some(expr) = ast.exprs().get(i + 1)
&& let Some(rest) = source.get(expr.pos() as usize..) && let Some(rest) = source.get(expr.pos() as usize..)
&& parser::insert_needed_semicolon(rest)
{ {
if parser::insert_needed_semicolon(rest) {
write!(out, ";")?; write!(out, ";")?;
} }
for _ in 1..parser::preserve_newlines(&source[..expr.pos() as usize]) {
writeln!(out)?;
}
}
writeln!(out)?; writeln!(out)?;
} }
std::io::Result::Ok(()) 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 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 { match *self {
Self::Ct { value, .. } => write!(f, "$: {}", value), Self::Ct { value, .. } => write!(f, "$: {}", value),
@ -963,10 +952,14 @@ 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..)
&& insert_needed_semicolon(rest)
{ {
if insert_needed_semicolon(rest) {
write!(f, ";")?; write!(f, ";")?;
} }
for _ in 1..preserve_newlines(&source[..expr.pos() as usize]) {
writeln!(f)?;
}
}
writeln!(f)?; writeln!(f)?;
} }
Ok(()) 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 { pub fn insert_needed_semicolon(source: &str) -> bool {
let kind = lexer::Lexer::new(source).next().kind; let kind = lexer::Lexer::new(source).next().kind;
kind.precedence().is_some() || matches!(kind, TokenKind::Ctor | TokenKind::Tupl) kind.precedence().is_some() || matches!(kind, TokenKind::Ctor | TokenKind::Tupl)

View file

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

View file

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