From c010cbc17dcbb2f0d6005d21530143bf57cb5871 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 25 May 2022 13:50:33 +0200 Subject: Move route from context to VM --- src/eval/machine.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/eval/machine.rs') diff --git a/src/eval/machine.rs b/src/eval/machine.rs index 168cedcb..14633978 100644 --- a/src/eval/machine.rs +++ b/src/eval/machine.rs @@ -1,12 +1,18 @@ +use std::path::PathBuf; + use super::{Scopes, Value}; -use crate::diag::TypError; +use crate::diag::{StrResult, TypError}; +use crate::source::SourceId; use crate::syntax::Span; +use crate::util::PathExt; use crate::Context; /// A virtual machine. pub struct Machine<'a> { /// The core context. pub ctx: &'a mut Context, + /// The route of source ids at which the machine is located. + pub route: Vec, /// The stack of scopes. pub scopes: Scopes<'a>, /// A control flow event that is currently happening. @@ -15,8 +21,24 @@ pub struct Machine<'a> { impl<'a> Machine<'a> { /// Create a new virtual machine. - pub fn new(ctx: &'a mut Context, scopes: Scopes<'a>) -> Self { - Self { ctx, scopes, flow: None } + pub fn new(ctx: &'a mut Context, route: Vec, scopes: Scopes<'a>) -> Self { + Self { ctx, route, scopes, flow: None } + } + + /// Resolve a user-entered path to be relative to the compilation + /// environment's root. + pub fn locate(&self, path: &str) -> StrResult { + if let Some(&id) = self.route.last() { + if let Some(path) = path.strip_prefix('/') { + return Ok(self.ctx.config.root.join(path).normalize()); + } + + if let Some(dir) = self.ctx.sources.get(id).path().parent() { + return Ok(dir.join(path).normalize()); + } + } + + return Err("cannot access file system from here".into()); } } -- cgit v1.2.3