diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-05-25 13:50:33 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-05-25 13:59:06 +0200 |
| commit | c010cbc17dcbb2f0d6005d21530143bf57cb5871 (patch) | |
| tree | 937fe79f0c121bcc025480181287fd4a3d0c0f4f /src/eval/machine.rs | |
| parent | 6935cf8dfefff3d6cf234f077a7d61661fd5ca57 (diff) | |
Move route from context to VM
Diffstat (limited to 'src/eval/machine.rs')
| -rw-r--r-- | src/eval/machine.rs | 28 |
1 files changed, 25 insertions, 3 deletions
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<SourceId>, /// 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<SourceId>, 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<PathBuf> { + 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()); } } |
