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, });