From 847f3fd0b7b707f47a0d883fbaa685346630d5dd Mon Sep 17 00:00:00 2001
From: Jeff <dev@jeffa.io>
Date: Tue, 10 Dec 2024 02:04:22 -0500
Subject: [PATCH] Fix implicit return parsing bug; Clean up

---
 dust-cli/src/main.rs                |  3 +-
 dust-lang/src/chunk/disassembler.rs | 49 +++++++++++++++--------------
 dust-lang/src/compiler/mod.rs       | 24 +++++++-------
 3 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/dust-cli/src/main.rs b/dust-cli/src/main.rs
index a44e2de..4afc9b5 100644
--- a/dust-cli/src/main.rs
+++ b/dust-cli/src/main.rs
@@ -121,7 +121,8 @@ fn main() {
             .disassembler(&mut stdout)
             .style(mode.style)
             .source(&source)
-            .disassemble();
+            .disassemble()
+            .expect("Failed to write disassembly to stdout");
 
         return;
     }
diff --git a/dust-lang/src/chunk/disassembler.rs b/dust-lang/src/chunk/disassembler.rs
index 1f51cc3..df9290e 100644
--- a/dust-lang/src/chunk/disassembler.rs
+++ b/dust-lang/src/chunk/disassembler.rs
@@ -12,31 +12,32 @@
 //!
 //! # Output
 //!
-//! The output of [Disassembler::disassemble] is a string that can be printed to the console or
-//! written to a file. Below is an example of the disassembly for a simple "Hello world!" program.
+//! The disassembler will output a human-readable representation of the chunk by writing to any type
+//! that implements the [Write][] trait.
 //!
 //! ```text
-//! ┌───────────────────────────────────────────────────────────────┐
-//! │                             dust                              │
-//! │                                                               │
-//! │                  write_line("Hello world!")                   │
-//! │                                                               │
-//! │      3 instructions, 1 constants, 0 locals, returns none      │
-//! │                                                               │
-//! │                         Instructions                          │
-//! │                         ------------                          │
-//! │  i   POSITION    OPERATION                 INFO               │
-//! │ --- ---------- ------------- -------------------------------- │
-//! │  0   (11, 25)  LOAD_CONSTANT             R0 = C0              │
-//! │  1   (0, 26)   CALL_NATIVE          write_line(R0..R1)        │
-//! │  2   (26, 26)  RETURN                                         │
-//! │┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈│
-//! │                           Constants                           │
-//! │                           ---------                           │
-//! │             i        TYPE             VALUE                   │
-//! │            --- ---------------- -----------------             │
-//! │             0        str          Hello world!                │
-//! └───────────────────────────────────────────────────────────────┘
+//! ╭─────────────────────────────────────────────────────────────────╮
+//! │                              dust                               │
+//! │                                                                 │
+//! │                   write_line("Hello world!")                    │
+//! │                                                                 │
+//! │       3 instructions, 1 constants, 0 locals, returns str        │
+//! │                                                                 │
+//! │                          Instructions                           │
+//! │ ╭─────┬────────────┬─────────────────┬────────────────────────╮ │
+//! │ │  i  │  POSITION  │    OPERATION    │          INFO          │ │
+//! │ ├─────┼────────────┼─────────────────┼────────────────────────┤ │
+//! │ │  0  │  (11, 25)  │  LOAD_CONSTANT  │        R0 = C0         │ │
+//! │ │  1  │  (0, 26)   │   CALL_NATIVE   │     write_line(R0)     │ │
+//! │ │  2  │  (26, 26)  │     RETURN      │         RETURN         │ │
+//! │ ╰─────┴────────────┴─────────────────┴────────────────────────╯ │
+//! │                            Constants                            │
+//! │  ╭─────┬──────────────────────────┬──────────────────────────╮  │
+//! │  │  i  │           TYPE           │          VALUE           │  │
+//! │  ├─────┼──────────────────────────┼──────────────────────────┤  │
+//! │  │  0  │           str            │       Hello world!       │  │
+//! │  ╰─────┴──────────────────────────┴──────────────────────────╯  │
+//! ╰─────────────────────────────────────────────────────────────────╯
 //! ```
 use std::{
     env::current_exe,
@@ -273,7 +274,7 @@ impl<'a, W: Write> Disassembler<'a, W> {
             self.write_centered_with_border(&row)?;
         }
 
-        self.write_centered_with_border_bold(INSTRUCTION_BORDERS[2])?;
+        self.write_centered_with_border(INSTRUCTION_BORDERS[2])?;
 
         Ok(())
     }
diff --git a/dust-lang/src/compiler/mod.rs b/dust-lang/src/compiler/mod.rs
index cec1ed3..e8a4279 100644
--- a/dust-lang/src/compiler/mod.rs
+++ b/dust-lang/src/compiler/mod.rs
@@ -1392,19 +1392,17 @@ impl<'src> Compiler<'src> {
 
             self.emit_instruction(r#return, Type::None, self.current_position);
         } else {
-            let previous_expression_type = self
-                .instructions
-                .iter()
-                .rev()
-                .find_map(|(instruction, r#type, _)| {
-                    if instruction.yields_value() {
-                        Some(r#type)
-                    } else {
-                        None
-                    }
-                })
-                .unwrap_or(&Type::None);
-            let should_return_value = previous_expression_type != &Type::None;
+            let previous_expression_type =
+                self.instructions
+                    .last()
+                    .map_or(Type::None, |(instruction, r#type, _)| {
+                        if instruction.yields_value() {
+                            r#type.clone()
+                        } else {
+                            Type::None
+                        }
+                    });
+            let should_return_value = previous_expression_type != Type::None;
             let r#return = Instruction::from(Return {
                 should_return_value,
             });