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)
.style(mode.style)
.source(&source)
.disassemble();
.disassemble()
.expect("Failed to write disassembly to stdout");
return;
}

View File

@ -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(())
}

View File

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