summaryrefslogtreecommitdiff
path: root/src/model/vm.rs
blob: 614e2a9f3971ab1f5b08a45b0d99c949f66a7fb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::path::PathBuf;

use comemo::Tracked;

use super::{Route, Scopes, Value};
use crate::diag::{SourceError, StrResult};
use crate::syntax::{SourceId, Span};
use crate::util::PathExt;
use crate::{LangItems, World};

/// A virtual machine.
pub struct Vm<'a> {
    /// The core context.
    pub world: Tracked<'a, dyn World>,
    /// The route of source ids the machine took to reach its current location.
    pub route: Tracked<'a, Route>,
    /// The current location.
    pub location: Option<SourceId>,
    /// The stack of scopes.
    pub scopes: Scopes<'a>,
    /// A control flow event that is currently happening.
    pub flow: Option<Flow>,
}

impl<'a> Vm<'a> {
    /// Create a new virtual machine.
    pub fn new(
        world: Tracked<'a, dyn World>,
        route: Tracked<'a, Route>,
        location: Option<SourceId>,
        scopes: Scopes<'a>,
    ) -> Self {
        Self {
            world,
            route,
            location,
            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.location {
            if let Some(path) = path.strip_prefix('/') {
                return Ok(self.world.config().root.join(path).normalize());
            }

            if let Some(dir) = self.world.source(id).path().parent() {
                return Ok(dir.join(path).normalize());
            }
        }

        return Err("cannot access file system from here".into());
    }

    /// The language items.
    pub fn items(&self) -> &LangItems {
        &self.world.config().items
    }
}

/// A control flow event that occurred during evaluation.
#[derive(Debug, Clone, PartialEq)]
pub enum Flow {
    /// Stop iteration in a loop.
    Break(Span),
    /// Skip the remainder of the current iteration in a loop.
    Continue(Span),
    /// Stop execution of a function early, optionally returning an explicit
    /// value.
    Return(Span, Option<Value>),
}

impl Flow {
    /// Return an error stating that this control flow is forbidden.
    pub fn forbidden(&self) -> SourceError {
        match *self {
            Self::Break(span) => {
                error!(span, "cannot break outside of loop")
            }
            Self::Continue(span) => {
                error!(span, "cannot continue outside of loop")
            }
            Self::Return(span, _) => {
                error!(span, "cannot return outside of function")
            }
        }
    }
}