forked from nushell/new-nu-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.rs
More file actions
262 lines (223 loc) · 7.69 KB
/
compiler.rs
File metadata and controls
262 lines (223 loc) · 7.69 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use crate::errors::SourceError;
use crate::parser::{AstNode, Block, NodeId, Pipeline};
use crate::protocol::Command;
use crate::resolver::{
DeclId, Frame, NameBindings, ScopeId, TypeDecl, TypeDeclId, VarId, Variable,
};
use crate::typechecker::{TypeId, Types};
use std::collections::HashMap;
pub struct RollbackPoint {
idx_span_start: usize,
idx_nodes: usize,
idx_errors: usize,
idx_blocks: usize,
token_pos: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
}
#[derive(Debug, PartialEq)]
pub struct Spanned<T> {
pub item: T,
pub span: Span,
}
impl<T> Spanned<T> {
pub fn new(item: T, span: Span) -> Self {
Spanned { item, span }
}
}
#[derive(Clone)]
pub struct Compiler {
// Core information, indexed by NodeId:
pub spans: Vec<Span>,
pub ast_nodes: Vec<AstNode>,
pub node_types: Vec<TypeId>,
// node_lifetimes: Vec<AllocationLifetime>,
pub blocks: Vec<Block>, // Blocks, indexed by BlockId
pub pipelines: Vec<Pipeline>, // Pipelines, indexed by PipelineId
pub source: Vec<u8>,
pub file_offsets: Vec<(String, usize, usize)>, // fname, start, end
// name bindings:
/// All scope frames ever entered, indexed by ScopeId
pub scope: Vec<Frame>,
/// Stack of currently entered scope frames
pub scope_stack: Vec<ScopeId>,
/// Variables, indexed by VarId
pub variables: Vec<Variable>,
/// Mapping of variable's name node -> Variable
pub var_resolution: HashMap<NodeId, VarId>,
/// Type declarations, indexed by TypeDeclId
pub type_decls: Vec<TypeDecl>,
/// Mapping of type decl's name node -> TypeDecl
pub type_resolution: HashMap<NodeId, TypeDeclId>,
/// Declarations (commands, aliases, externs), indexed by DeclId
pub decls: Vec<Box<dyn Command>>,
/// Declaration NodeIds, indexed by DeclId
pub decl_nodes: Vec<NodeId>,
/// Mapping of decl's name node -> Command
pub decl_resolution: HashMap<NodeId, DeclId>,
// Definitions:
// indexed by FunId
// pub functions: Vec<Function>,
// indexed by TypeId
// types: Vec<Type>,
// Use/def
// pub call_resolution: HashMap<NodeId, CallTarget>,
pub errors: Vec<SourceError>,
}
impl Default for Compiler {
fn default() -> Self {
Self::new()
}
}
impl Compiler {
pub fn new() -> Self {
Self {
spans: vec![],
ast_nodes: vec![],
node_types: vec![],
blocks: vec![],
pipelines: vec![],
source: vec![],
file_offsets: vec![],
scope: vec![],
scope_stack: vec![],
variables: vec![],
var_resolution: HashMap::new(),
type_decls: vec![],
type_resolution: HashMap::new(),
decls: vec![],
decl_nodes: vec![],
decl_resolution: HashMap::new(),
// variables: vec![],
// functions: vec![],
// types: vec![],
// call_resolution: HashMap::new(),
errors: vec![],
}
}
pub fn print(&self) {
let output = self.display_state();
print!("{output}");
}
#[allow(clippy::format_collect)]
pub fn display_state(&self) -> String {
// TODO: This should say PARSER, not COMPILER
let mut result = "==== COMPILER ====\n".to_string();
for (idx, ast_node) in self.ast_nodes.iter().enumerate() {
result.push_str(&format!(
"{}: {:?} ({} to {})",
idx, ast_node, self.spans[idx].start, self.spans[idx].end
));
if matches!(
ast_node,
AstNode::Name | AstNode::Variable | AstNode::Int | AstNode::Float | AstNode::String
) {
result.push_str(&format!(
" \"{}\"",
String::from_utf8_lossy(self.get_span_contents(NodeId(idx)))
));
}
result.push('\n');
}
if !self.errors.is_empty() {
result.push_str("==== COMPILER ERRORS ====\n");
for error in &self.errors {
result.push_str(&format!(
"{:?} (NodeId {}): {}\n",
error.severity, error.node_id.0, error.message
));
}
}
result
}
pub fn merge_name_bindings(&mut self, name_bindings: NameBindings) {
self.scope.extend(name_bindings.scope);
self.scope_stack.extend(name_bindings.scope_stack);
self.variables.extend(name_bindings.variables);
self.var_resolution.extend(name_bindings.var_resolution);
self.type_decls.extend(name_bindings.type_decls);
self.type_resolution.extend(name_bindings.type_resolution);
self.decls.extend(name_bindings.decls);
self.decl_nodes.extend(name_bindings.decl_nodes);
self.decl_resolution.extend(name_bindings.decl_resolution);
self.errors.extend(name_bindings.errors);
}
pub fn merge_types(&mut self, types: Types) {
self.node_types.extend(types.node_types);
self.errors.extend(types.errors);
}
pub fn add_file(&mut self, fname: &str, contents: &[u8]) {
let span_offset = self.source.len();
self.file_offsets
.push((fname.to_string(), span_offset, span_offset + contents.len()));
self.source.extend_from_slice(contents);
}
pub fn span_offset(&self) -> usize {
self.source.len()
}
pub fn get_node(&self, node_id: NodeId) -> &AstNode {
&self.ast_nodes[node_id.0]
}
pub fn get_node_mut(&mut self, node_id: NodeId) -> &mut AstNode {
&mut self.ast_nodes[node_id.0]
}
pub fn push_node(&mut self, ast_node: AstNode) -> NodeId {
self.ast_nodes.push(ast_node);
NodeId(self.ast_nodes.len() - 1)
}
pub fn get_rollback_point(&self, token_pos: usize) -> RollbackPoint {
RollbackPoint {
idx_span_start: self.spans.len(),
idx_nodes: self.ast_nodes.len(),
idx_errors: self.errors.len(),
idx_blocks: self.blocks.len(),
token_pos,
}
}
pub fn apply_compiler_rollback(&mut self, rbp: RollbackPoint) -> usize {
self.blocks.truncate(rbp.idx_blocks);
self.ast_nodes.truncate(rbp.idx_nodes);
self.errors.truncate(rbp.idx_errors);
self.spans.truncate(rbp.idx_span_start);
rbp.token_pos
}
/// Get span of node
pub fn get_span(&self, node_id: NodeId) -> Span {
*self
.spans
.get(node_id.0)
.expect("internal error: missing span of node")
}
/// Get the source contents of a span of a node
pub fn get_span_contents(&self, node_id: NodeId) -> &[u8] {
let span = self.get_span(node_id);
self.source
.get(span.start..span.end)
.expect("internal error: missing source of span")
}
/// Get the source contents of a span
pub fn get_span_contents_manual(&self, span_start: usize, span_end: usize) -> &[u8] {
self.source
.get(span_start..span_end)
.expect("internal error: missing source of span")
}
/// Get the source contents of a node
pub fn node_as_str(&self, node_id: NodeId) -> &str {
std::str::from_utf8(self.get_span_contents(node_id))
.expect("internal error: expected utf8 string")
}
/// Get the source contents of a node as i64
pub fn node_as_i64(&self, node_id: NodeId) -> i64 {
self.node_as_str(node_id)
.parse::<i64>()
.expect("internal error: expected i64")
}
}