From c5a8b93eaff144d34361193a125a2da2a4a93ef7 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 1 Sep 2022 16:38:05 +0300 Subject: chore(tvix/eval): thread a codemap::File reference to the compiler This instantiates a codemap outside of the compiler and passes a reference to the file currently under compilation to it. Note that the "file" might just be a REPL line. Change-Id: I131ae1ddb6d718e1374750da9ba0b99608c6058d Reviewed-on: https://cl.tvl.fyi/c/depot/+/6378 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/compiler/mod.rs | 15 +++++++++++---- tvix/eval/src/eval.rs | 11 ++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index c9bbd8171d..522f1a4459 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -59,7 +59,7 @@ impl LambdaCtx { /// implicitly be resolvable in the global scope. type GlobalsMap = HashMap<&'static str, Rc>; -struct Compiler { +struct Compiler<'code> { contexts: Vec, warnings: Vec, errors: Vec, @@ -72,11 +72,16 @@ struct Compiler { /// an identifier is resolved against the scope poisoning logic, /// and a function that should emit code for the token. globals: GlobalsMap, + + /// File reference in the codemap contains all known source code + /// and is used to track the spans from which instructions where + /// derived. + file: &'code codemap::File, } // Helper functions for emitting code and metadata to the internal // structures of the compiler. -impl Compiler { +impl Compiler<'_> { fn context(&self) -> &LambdaCtx { &self.contexts[self.contexts.len() - 1] } @@ -110,7 +115,7 @@ impl Compiler { } // Actual code-emitting AST traversal methods. -impl Compiler { +impl Compiler<'_> { fn compile(&mut self, slot: Option, expr: ast::Expr) { match expr { ast::Expr::Literal(literal) => self.compile_literal(literal), @@ -1257,9 +1262,10 @@ fn prepare_globals(additional: HashMap<&'static str, Value>) -> GlobalsMap { globals } -pub fn compile( +pub fn compile<'code>( expr: ast::Expr, location: Option, + file: &'code codemap::File, globals: HashMap<&'static str, Value>, ) -> EvalResult { let mut root_dir = match location { @@ -1278,6 +1284,7 @@ pub fn compile( let mut c = Compiler { root_dir, + file, globals: prepare_globals(globals), contexts: vec![LambdaCtx::new()], warnings: vec![], diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs index d61e51e6af..98e7520e8b 100644 --- a/tvix/eval/src/eval.rs +++ b/tvix/eval/src/eval.rs @@ -7,6 +7,15 @@ use crate::{ }; pub fn interpret(code: &str, location: Option) -> EvalResult { + let mut codemap = codemap::CodeMap::new(); + let file = codemap.add_file( + location + .as_ref() + .map(|p| p.to_string_lossy().to_string()) + .unwrap_or_else(|| "".into()), + code.into(), + ); + let parsed = rnix::ast::Root::parse(code); let errors = parsed.errors(); @@ -27,7 +36,7 @@ pub fn interpret(code: &str, location: Option) -> EvalResult { println!("{:?}", root_expr); } - let result = crate::compiler::compile(root_expr, location, global_builtins())?; + let result = crate::compiler::compile(root_expr, location, &file, global_builtins())?; #[cfg(feature = "disassembler")] crate::disassembler::disassemble_chunk(&result.lambda.chunk); -- cgit 1.4.1