From ce9dfad6cb4822089574284e0264aeb509852dd3 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 18 Sep 2022 17:22:12 -0400 Subject: refactor(tvix/eval): Define a Compiler::new function Change-Id: I6b9283d16447c83dd3978371d9a6ac1beb985926 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6657 Autosubmit: grfn Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/compiler/mod.rs | 66 +++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 27 deletions(-) (limited to 'tvix') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 793f0566e9..fb04581bfc 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -98,6 +98,44 @@ struct Compiler<'observer> { observer: &'observer mut dyn Observer, } +/// Compiler construction +impl<'observer> Compiler<'observer> { + pub(crate) fn new( + location: Option, + file: Arc, + globals: HashMap<&'static str, Value>, + observer: &'observer mut dyn Observer, + ) -> EvalResult { + let mut root_dir = match location { + Some(dir) => Ok(dir), + None => std::env::current_dir().map_err(|e| Error { + kind: ErrorKind::PathResolution(format!( + "could not determine current directory: {}", + e + )), + span: file.span, + }), + }?; + + // If the path passed from the caller points to a file, the + // filename itself needs to be truncated as this must point to a + // directory. + if root_dir.is_file() { + root_dir.pop(); + } + + Ok(Self { + root_dir, + file, + observer, + globals: prepare_globals(globals), + contexts: vec![LambdaCtx::new()], + warnings: vec![], + errors: vec![], + }) + } +} + // Helper functions for emitting code and metadata to the internal // structures of the compiler. impl Compiler<'_> { @@ -1392,33 +1430,7 @@ pub fn compile( globals: HashMap<&'static str, Value>, observer: &mut dyn Observer, ) -> EvalResult { - let mut root_dir = match location { - Some(dir) => Ok(dir), - None => std::env::current_dir().map_err(|e| Error { - kind: ErrorKind::PathResolution(format!( - "could not determine current directory: {}", - e - )), - span: file.span, - }), - }?; - - // If the path passed from the caller points to a file, the - // filename itself needs to be truncated as this must point to a - // directory. - if root_dir.is_file() { - root_dir.pop(); - } - - let mut c = Compiler { - root_dir, - file, - observer, - globals: prepare_globals(globals), - contexts: vec![LambdaCtx::new()], - warnings: vec![], - errors: vec![], - }; + let mut c = Compiler::new(location, file, globals, observer)?; let root_span = c.span_for(&expr); let root_slot = c.scope_mut().declare_phantom(root_span, false); -- cgit 1.4.1