diff options
author | Vincent Ambo <mail@tazj.in> | 2022-10-04T14·05+0300 |
---|---|---|
committer | tazjin <tazjin@tvl.su> | 2022-10-05T10·29+0000 |
commit | 3530404a4a1cc363d87e559ac24780aa318adb19 (patch) | |
tree | 71458cd9fdc83efd71c3c0187e2381a5434fc7f7 /tvix/eval/src/chunk.rs | |
parent | 2ff764ceb700a1ef18fb532fbbc1ff937ed63f8a (diff) |
refactor(tvix/eval): introduce source::SourceCode type r/5035
This type hides away the lower-level handling of most codemap data structures, especially to library consumers (see corresponding changes in tvixbolt). This will help with implement `import` by giving us central control over how the codemap works. Change-Id: Ifcea36776879725871b30c518aeb96ab5fda035a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6855 Tested-by: BuildkiteCI Reviewed-by: wpcarro <wpcarro@gmail.com>
Diffstat (limited to 'tvix/eval/src/chunk.rs')
-rw-r--r-- | tvix/eval/src/chunk.rs | 18 |
1 files changed, 4 insertions, 14 deletions
diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index 8810f4e1c441..9958b551a1e9 100644 --- a/tvix/eval/src/chunk.rs +++ b/tvix/eval/src/chunk.rs @@ -1,10 +1,9 @@ use std::io::Write; use std::ops::Index; -use codemap::CodeMap; - use crate::opcode::{CodeIdx, ConstantIdx, OpCode}; use crate::value::Value; +use crate::SourceCode; /// Represents a source location from which one or more operations /// were compiled. @@ -117,21 +116,12 @@ impl Chunk { panic!("compiler error: chunk missing span for offset {}", offset.0); } - /// Retrieve the line from which the instruction at `offset` was - /// compiled in the specified codemap. - pub fn get_line(&self, codemap: &codemap::CodeMap, offset: CodeIdx) -> usize { - let span = self.get_span(offset); - // lines are 0-indexed in the codemap, but users probably want - // real line numbers - codemap.look_up_span(span).begin.line + 1 - } - /// Write the disassembler representation of the operation at /// `idx` to the specified writer. pub fn disassemble_op<W: Write>( &self, writer: &mut W, - codemap: &CodeMap, + source: &SourceCode, width: usize, idx: CodeIdx, ) -> Result<(), std::io::Error> { @@ -139,8 +129,8 @@ impl Chunk { // Print continuation character if the previous operation was at // the same line, otherwise print the line. - let line = self.get_line(codemap, idx); - if idx.0 > 0 && self.get_line(codemap, CodeIdx(idx.0 - 1)) == line { + let line = source.get_line(self.get_span(idx)); + if idx.0 > 0 && source.get_line(self.get_span(CodeIdx(idx.0 - 1))) == line { write!(writer, " |\t")?; } else { write!(writer, "{:4}\t", line)?; |