about summary refs log tree commit diff
path: root/tvix/eval/src/compiler/mod.rs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2022-09-18T21·22-0400
committergrfn <grfn@gws.fyi>2022-09-20T21·19+0000
commitce9dfad6cb4822089574284e0264aeb509852dd3 (patch)
treeeff31b2152fce38fa45dfe4a88782fa48955963d /tvix/eval/src/compiler/mod.rs
parentbe18df1dab501f3fff0da6272697bb1b5a893bfb (diff)
refactor(tvix/eval): Define a Compiler::new function r/4939
Change-Id: I6b9283d16447c83dd3978371d9a6ac1beb985926
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6657
Autosubmit: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/eval/src/compiler/mod.rs')
-rw-r--r--tvix/eval/src/compiler/mod.rs66
1 files changed, 39 insertions, 27 deletions
diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs
index 793f0566e9de..fb04581bfcc2 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<PathBuf>,
+        file: Arc<codemap::File>,
+        globals: HashMap<&'static str, Value>,
+        observer: &'observer mut dyn Observer,
+    ) -> EvalResult<Self> {
+        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<CompilationOutput> {
-    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);