From 3530404a4a1cc363d87e559ac24780aa318adb19 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 4 Oct 2022 17:05:34 +0300 Subject: refactor(tvix/eval): introduce source::SourceCode type 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 --- corp/tvixbolt/src/main.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'corp') diff --git a/corp/tvixbolt/src/main.rs b/corp/tvixbolt/src/main.rs index c4b28f0fd490..a389a33f04ed 100644 --- a/corp/tvixbolt/src/main.rs +++ b/corp/tvixbolt/src/main.rs @@ -1,9 +1,9 @@ use std::fmt::Write; use serde::{Deserialize, Serialize}; -use std::rc::Rc; use tvix_eval::observer::TracingObserver; use tvix_eval::observer::{DisassemblingObserver, NoOpObserver}; +use tvix_eval::SourceCode; use web_sys::HtmlInputElement; use web_sys::HtmlTextAreaElement; use yew::prelude::*; @@ -230,9 +230,6 @@ fn eval(trace: bool, code: &str) -> Output { return out; } - let mut codemap = codemap::CodeMap::new(); - let file = codemap.add_file("nixbolt".to_string(), code.into()); - let parsed = rnix::ast::Root::parse(code); let errors = parsed.errors(); @@ -250,8 +247,10 @@ fn eval(trace: bool, code: &str) -> Output { .expr() .expect("expression should exist if no errors occured"); - let codemap = Rc::new(codemap); - let mut compilation_observer = DisassemblingObserver::new(codemap.clone(), &mut out.bytecode); + let source = SourceCode::new(); + let file = source.add_file("nixbolt".to_string(), code.into()); + + let mut compilation_observer = DisassemblingObserver::new(source.clone(), &mut out.bytecode); let result = tvix_eval::compile( &root_expr, @@ -266,7 +265,7 @@ fn eval(trace: bool, code: &str) -> Output { writeln!( &mut out.warnings, "{}\n", - warning.fancy_format_str(&codemap).trim(), + warning.fancy_format_str(&source).trim(), ) .unwrap(); } @@ -276,7 +275,7 @@ fn eval(trace: bool, code: &str) -> Output { writeln!( &mut out.compiler_errors, "{}\n", - error.fancy_format_str(&codemap).trim(), + error.fancy_format_str(&source).trim(), ) .unwrap(); } @@ -295,7 +294,7 @@ fn eval(trace: bool, code: &str) -> Output { Err(err) => writeln!( &mut out.runtime_errors, "{}", - err.fancy_format_str(&codemap).trim() + err.fancy_format_str(&source).trim() ) .unwrap(), }; -- cgit 1.4.1