1
0

Fix implicit return parsing bug; Clean up

This commit is contained in:
Jeff 2024-12-10 02:04:22 -05:00
parent 942b494b7f
commit 847f3fd0b7
3 changed files with 38 additions and 38 deletions

View File

@ -121,7 +121,8 @@ fn main() {
.disassembler(&mut stdout) .disassembler(&mut stdout)
.style(mode.style) .style(mode.style)
.source(&source) .source(&source)
.disassemble(); .disassemble()
.expect("Failed to write disassembly to stdout");
return; return;
} }

View File

@ -12,31 +12,32 @@
//! //!
//! # Output //! # Output
//! //!
//! The output of [Disassembler::disassemble] is a string that can be printed to the console or //! The disassembler will output a human-readable representation of the chunk by writing to any type
//! written to a file. Below is an example of the disassembly for a simple "Hello world!" program. //! that implements the [Write][] trait.
//! //!
//! ```text //! ```text
//! ─────────────────────────────────────────────────────────────── //! ╭─────────────────────────────────────────────────────────────────
//! │ dust │ //! │ dust │
//! │ │ //! │ │
//! │ write_line("Hello world!") │ //! │ write_line("Hello world!") │
//! │ │ //! │ │
//! │ 3 instructions, 1 constants, 0 locals, returns none //! │ 3 instructions, 1 constants, 0 locals, returns str
//! │ │ //! │ │
//! │ Instructions │ //! │ Instructions │
//! │ ------------ //! │ ╭─────┬────────────┬─────────────────┬────────────────────────╮
//! │ i POSITION OPERATION INFO //! │ │ i │ POSITION │ OPERATION │ INFO │
//! │ --- ---------- ------------- -------------------------------- //! │ ├─────┼────────────┼─────────────────┼────────────────────────┤
//! │ 0 (11, 25) LOAD_CONSTANT R0 = C0 //! │ │ 0 │ (11, 25) │ LOAD_CONSTANT │ R0 = C0 │
//! │ 1 (0, 26) CALL_NATIVE write_line(R0..R1) //! │ │ 1 │ (0, 26) │ CALL_NATIVE │ write_line(R0) │
//! │ 2 (26, 26) RETURN //! │ │ 2 │ (26, 26) │ RETURN │ RETURN │
//! │┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ //! │ ╰─────┴────────────┴─────────────────┴────────────────────────╯
//! │ Constants │ //! │ Constants │
//! │ --------- │ //! │ ╭─────┬──────────────────────────┬──────────────────────────╮ │
//! │ i TYPE VALUE │ //! │ │ i │ TYPE │ VALUE │ │
//! │ --- ---------------- ----------------- │ //! │ ├─────┼──────────────────────────┼──────────────────────────┤ │
//! │ 0 str Hello world! │ //! │ │ 0 │ str │ Hello world! │ │
//! └───────────────────────────────────────────────────────────────┘ //! │ ╰─────┴──────────────────────────┴──────────────────────────╯ │
//! ╰─────────────────────────────────────────────────────────────────╯
//! ``` //! ```
use std::{ use std::{
env::current_exe, 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(&row)?;
} }
self.write_centered_with_border_bold(INSTRUCTION_BORDERS[2])?; self.write_centered_with_border(INSTRUCTION_BORDERS[2])?;
Ok(()) Ok(())
} }

View File

@ -1392,19 +1392,17 @@ impl<'src> Compiler<'src> {
self.emit_instruction(r#return, Type::None, self.current_position); self.emit_instruction(r#return, Type::None, self.current_position);
} else { } else {
let previous_expression_type = self let previous_expression_type =
.instructions self.instructions
.iter() .last()
.rev() .map_or(Type::None, |(instruction, r#type, _)| {
.find_map(|(instruction, r#type, _)| {
if instruction.yields_value() { if instruction.yields_value() {
Some(r#type) r#type.clone()
} else { } else {
None Type::None
} }
}) });
.unwrap_or(&Type::None); let should_return_value = previous_expression_type != Type::None;
let should_return_value = previous_expression_type != &Type::None;
let r#return = Instruction::from(Return { let r#return = Instruction::from(Return {
should_return_value, should_return_value,
}); });