Skip to main content

hara_native/vm/
source_map.rs

1//! Per-instruction source positions for diagnostics and the disassembler.
2
3use crate::kernel::Position;
4
5/// Maps instruction indexes to the source position of the form that
6/// produced them. Stored as a parallel vector so lookup during error
7/// construction is a single index.
8#[derive(Debug, Clone, Default, PartialEq)]
9pub struct SourceMap {
10    positions: Vec<Option<Position>>,
11}
12
13impl SourceMap {
14    pub(crate) fn record(&mut self, position: Option<Position>) {
15        self.positions.push(position);
16    }
17
18    pub(crate) fn pop(&mut self) {
19        self.positions.pop();
20    }
21
22    /// The source position recorded for an instruction, when available.
23    pub fn position(&self, instruction: usize) -> Option<Position> {
24        self.positions.get(instruction).copied().flatten()
25    }
26
27    pub fn len(&self) -> usize {
28        self.positions.len()
29    }
30
31    pub fn is_empty(&self) -> bool {
32        self.positions.is_empty()
33    }
34}