-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.rs
165 lines (148 loc) · 5.49 KB
/
environment.rs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2017, Sabre developers
//
// Licensed under the Apache License, Version 2.0 (see LICENSE.Apache in the
// root directory) or MIT license (see LICENSE.MIT in the root directory),
// at your option. This file may be copied, distributed, and modified only
// in accordance with the terms specified by the chosen license.
//! Lexical environments.
use std::collections::HashMap;
use std::rc::{Rc};
use locus::diagnostics::Span;
use reader::intern_pool::{Atom};
use expression::{Variable};
/// Lexical environment.
///
/// Environments describe meanings of identifiers in Scheme programs. They roughly correspond to
/// visibility scopes which are nested one into another.
pub struct Environment {
kind: EnvironmentKind,
variables: HashMap<Atom, EnvironmentVariable>,
parent: Option<Rc<Environment>>,
}
enum EnvironmentKind {
Local,
Global,
Imported,
}
struct EnvironmentVariable {
/// Kind of a variable stored in the environment.
kind: VariableKind,
/// Definition site of the variable.
span: Span,
}
enum VariableKind {
/// Run-time variable requiring storage.
Runtime {
/// Index of the variable storage location.
///
/// For local variables it's the stack frame, for global variables it's the global table,
/// for imported variables it's the import table.
index: usize,
},
}
/// Reference to a variable from environment.
pub struct VariableReference {
/// Kind of the variable referenced.
pub kind: ReferenceKind,
/// Location of the variable definition.
pub span: Span,
}
/// Kind of a referenced variable.
pub enum ReferenceKind {
/// Locally-bound variable, defined by a procedure.
///
/// Local variables are identified by their (zero-based) index in the activation record of
/// their defining procedure and by the nesting depth of the procedure (zero is current one).
Local {
depth: usize,
index: usize,
},
/// Globally-defined mutable variable.
///
/// Global variables are identified by their index in the global variable table.
Global {
index: usize,
},
/// Imported immutable variable.
///
/// Imported variables are identified by their index in the import table.
Imported {
index: usize,
},
}
impl Environment {
/// Create a new local environment with specified variables.
pub fn new_local(variables: &[Variable], parent: &Rc<Environment>) -> Rc<Environment> {
Rc::new(Environment {
kind: EnvironmentKind::Local,
variables: enumerate_runtime_variables(variables),
parent: Some(parent.clone()),
})
}
/// Create a new global environment with specified variables.
///
/// # Panics
///
/// The environment stack should contain exactly one global environment, so the parent must be
/// an imported environment, or else this function panics.
pub fn new_global(variables: &[Variable], parent: &Rc<Environment>) -> Rc<Environment> {
assert!(match parent.kind { EnvironmentKind::Imported => true, _ => false });
Rc::new(Environment {
kind: EnvironmentKind::Global,
variables: enumerate_runtime_variables(variables),
parent: Some(parent.clone()),
})
}
/// Create a new imported environment with specified variables.
///
/// Import environment is the base environment of a Scheme module, it does not have a parent.
pub fn new_imported(variables: &[Variable]) -> Rc<Environment> {
Rc::new(Environment {
kind: EnvironmentKind::Imported,
variables: enumerate_runtime_variables(variables),
parent: None,
})
}
/// Resolve a variable in this environment.
///
/// Returns Some variable from the environment or its parents, or None if the variable
/// could not be found in any environment.
pub fn resolve_variable(&self, name: Atom) -> Option<VariableReference> {
// First, try to resolve the name locally.
if let Some(variable) = self.variables.get(&name) {
let kind = match variable.kind {
VariableKind::Runtime { index } => {
match self.kind {
EnvironmentKind::Local => ReferenceKind::Local { index, depth: 0 },
EnvironmentKind::Global => ReferenceKind::Global { index },
EnvironmentKind::Imported => ReferenceKind::Imported { index },
}
}
};
return Some(VariableReference { kind, span: variable.span });
}
// If that fails then look into parent environment (if it's available).
if let Some(ref parent) = self.parent {
if let Some(mut variable) = parent.resolve_variable(name) {
// Bump the nesting depth for local variables.
if let ReferenceKind::Local { ref mut depth, .. } = variable.kind {
*depth += 1;
}
return Some(variable);
}
}
// The variable cannot be resolved if it is absent in all environments.
return None;
}
}
fn enumerate_runtime_variables(variables: &[Variable]) -> HashMap<Atom, EnvironmentVariable> {
variables.iter()
.enumerate()
.map(|(index, variable)| {
(variable.name, EnvironmentVariable {
kind: VariableKind::Runtime { index },
span: variable.span,
})
})
.collect()
}