From 87726395e1c0eebbd63d053e9ed4cd349515e759 Mon Sep 17 00:00:00 2001 From: mlokr Date: Fri, 19 Jul 2024 15:51:02 +0200 Subject: [PATCH] v --- hblang/src/lib.rs | 8 ++++++-- hblang/src/parser.rs | 24 +++++++++++------------- hblang/text-prj/main.hb | 6 ++++-- hblang/text-prj/pkg.hb | 1 - 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index 8d0f4c8b..32d9b991 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -557,9 +557,13 @@ 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) { - write!(out, ";")?; + if parser::insert_needed_semicolon(rest) { + write!(out, ";")?; + } + for _ in 1..parser::preserve_newlines(&source[..expr.pos() as usize]) { + writeln!(out)?; + } } writeln!(out)?; } diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index 04aa7720..c3117846 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -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,9 +952,13 @@ 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) { - write!(f, ";")?; + if insert_needed_semicolon(rest) { + write!(f, ";")?; + } + for _ in 1..preserve_newlines(&source[..expr.pos() as usize]) { + writeln!(f)?; + } } writeln!(f)?; } @@ -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) diff --git a/hblang/text-prj/main.hb b/hblang/text-prj/main.hb index ae987781..f91a4ea4 100644 --- a/hblang/text-prj/main.hb +++ b/hblang/text-prj/main.hb @@ -1,7 +1,9 @@ foo := 0; + .{global, fib} := @use("pkg.hb") - main := fn(a: int): int { - return fib(global) + g := global + + return fib(g) } diff --git a/hblang/text-prj/pkg.hb b/hblang/text-prj/pkg.hb index 5c74adc7..0e27000d 100644 --- a/hblang/text-prj/pkg.hb +++ b/hblang/text-prj/pkg.hb @@ -1,6 +1,5 @@ global := 10 - fib := fn(n: int): int { return n + 1 }