refactor(tvix/eval): Define a Compiler::new function
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>
This commit is contained in:
parent
be18df1dab
commit
ce9dfad6cb
1 changed files with 39 additions and 27 deletions
|
@ -98,6 +98,44 @@ struct Compiler<'observer> {
|
||||||
observer: &'observer mut dyn 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
|
// Helper functions for emitting code and metadata to the internal
|
||||||
// structures of the compiler.
|
// structures of the compiler.
|
||||||
impl Compiler<'_> {
|
impl Compiler<'_> {
|
||||||
|
@ -1392,33 +1430,7 @@ pub fn compile(
|
||||||
globals: HashMap<&'static str, Value>,
|
globals: HashMap<&'static str, Value>,
|
||||||
observer: &mut dyn Observer,
|
observer: &mut dyn Observer,
|
||||||
) -> EvalResult<CompilationOutput> {
|
) -> EvalResult<CompilationOutput> {
|
||||||
let mut root_dir = match location {
|
let mut c = Compiler::new(location, file, globals, observer)?;
|
||||||
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 root_span = c.span_for(&expr);
|
let root_span = c.span_for(&expr);
|
||||||
let root_slot = c.scope_mut().declare_phantom(root_span, false);
|
let root_slot = c.scope_mut().declare_phantom(root_span, false);
|
||||||
|
|
Loading…
Add table
Reference in a new issue