-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathscan.rs
More file actions
213 lines (193 loc) · 8.72 KB
/
scan.rs
File metadata and controls
213 lines (193 loc) · 8.72 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
//! This module is for the scanning of the Hir by the interpreter.
//! In this initial step, the Hir is scanned for `Comptime` nodes
//! without actually executing anything until such a node is found.
//! Once such a node is found, the interpreter will call the relevant
//! evaluate method on that node type, insert the result into the Ast,
//! and continue scanning the rest of the program.
//!
//! Since it mostly just needs to recur on the Hir looking for Comptime
//! nodes, this pass is fairly simple. The only thing it really needs to
//! ensure to do is to push and pop scopes on the interpreter as needed
//! so that any variables defined within e.g. an `if` statement containing
//! a `Comptime` block aren't accessible outside of the `if`.
use crate::{
hir_def::{
expr::{
HirArrayLiteral, HirBlockExpression, HirCallExpression, HirConstructorExpression,
HirIfExpression, HirIndexExpression, HirInfixExpression, HirLambda,
HirMethodCallExpression,
},
stmt::HirForStatement,
},
macros_api::{HirExpression, HirLiteral, HirStatement},
node_interner::{ExprId, FuncId, StmtId},
};
use super::{
errors::{IResult, InterpreterError},
interpreter::Interpreter,
};
#[allow(dead_code)]
impl<'interner> Interpreter<'interner> {
/// Scan through a function, evaluating any Comptime nodes found.
/// These nodes will be modified in place, replaced with the
/// result of their evaluation.
pub fn scan_function(&mut self, function: FuncId) -> IResult<()> {
// Don't scan through functions that are already comptime. They may use comptime-only
// features (most likely HirExpression::Quote) that we'd otherwise error for.
if self.interner.function_modifiers(&function).is_comptime {
return Ok(());
}
let function = self.interner.function(&function);
let state = self.enter_function();
self.scan_expression(function.as_expr())?;
self.exit_function(state);
Ok(())
}
fn scan_expression(&mut self, expr: ExprId) -> IResult<()> {
match self.interner.expression(&expr) {
HirExpression::Ident(_) => Ok(()),
HirExpression::Literal(literal) => self.scan_literal(literal),
HirExpression::Block(block) => self.scan_block(block),
HirExpression::Prefix(prefix) => self.scan_expression(prefix.rhs),
HirExpression::Infix(infix) => self.scan_infix(infix),
HirExpression::Index(index) => self.scan_index(index),
HirExpression::Constructor(constructor) => self.scan_constructor(constructor),
HirExpression::MemberAccess(member_access) => self.scan_expression(member_access.lhs),
HirExpression::Call(call) => self.scan_call(call),
HirExpression::MethodCall(method_call) => self.scan_method_call(method_call),
HirExpression::Cast(cast) => self.scan_expression(cast.lhs),
HirExpression::If(if_) => self.scan_if(if_),
HirExpression::Tuple(tuple) => self.scan_tuple(tuple),
HirExpression::Lambda(lambda) => self.scan_lambda(lambda),
HirExpression::Comptime(block) => {
let location = self.interner.expr_location(&expr);
let new_expr =
self.evaluate_block(block)?.into_expression(self.interner, location)?;
let new_expr = self.interner.expression(&new_expr);
self.interner.replace_expr(&expr, new_expr);
Ok(())
}
HirExpression::Quote(_) => {
// This error could be detected much earlier in the compiler pipeline but
// it just makes sense for the comptime code to handle comptime things.
let location = self.interner.expr_location(&expr);
Err(InterpreterError::QuoteInRuntimeCode { location })
}
HirExpression::Error => Ok(()),
// Unquote should only be inserted by the comptime interpreter while expanding macros
// and is removed by the Hir -> Ast conversion pass which converts it into a normal block.
// If we find one now during scanning it most likely means the Hir -> Ast conversion
// missed it somehow. In the future we may allow users to manually write unquote
// expressions in their code but for now this is unreachable.
HirExpression::Unquote(block) => {
unreachable!("Found unquote block while scanning: {block}")
}
}
}
fn scan_literal(&mut self, literal: HirLiteral) -> IResult<()> {
match literal {
HirLiteral::Array(elements) | HirLiteral::Slice(elements) => match elements {
HirArrayLiteral::Standard(elements) => {
for element in elements {
self.scan_expression(element)?;
}
Ok(())
}
HirArrayLiteral::Repeated { repeated_element, length: _ } => {
self.scan_expression(repeated_element)
}
},
HirLiteral::Bool(_)
| HirLiteral::Integer(_, _)
| HirLiteral::Str(_)
| HirLiteral::FmtStr(_, _)
| HirLiteral::Unit => Ok(()),
}
}
fn scan_block(&mut self, block: HirBlockExpression) -> IResult<()> {
self.push_scope();
for statement in &block.statements {
self.scan_statement(*statement)?;
}
self.pop_scope();
Ok(())
}
fn scan_infix(&mut self, infix: HirInfixExpression) -> IResult<()> {
self.scan_expression(infix.lhs)?;
self.scan_expression(infix.rhs)
}
fn scan_index(&mut self, index: HirIndexExpression) -> IResult<()> {
self.scan_expression(index.collection)?;
self.scan_expression(index.index)
}
fn scan_constructor(&mut self, constructor: HirConstructorExpression) -> IResult<()> {
for (_, field) in constructor.fields {
self.scan_expression(field)?;
}
Ok(())
}
fn scan_call(&mut self, call: HirCallExpression) -> IResult<()> {
self.scan_expression(call.func)?;
for arg in call.arguments {
self.scan_expression(arg)?;
}
Ok(())
}
fn scan_method_call(&mut self, method_call: HirMethodCallExpression) -> IResult<()> {
self.scan_expression(method_call.object)?;
for arg in method_call.arguments {
self.scan_expression(arg)?;
}
Ok(())
}
fn scan_if(&mut self, if_: HirIfExpression) -> IResult<()> {
self.scan_expression(if_.condition)?;
self.push_scope();
self.scan_expression(if_.consequence)?;
self.pop_scope();
if let Some(alternative) = if_.alternative {
self.push_scope();
self.scan_expression(alternative)?;
self.pop_scope();
}
Ok(())
}
fn scan_tuple(&mut self, tuple: Vec<ExprId>) -> IResult<()> {
for field in tuple {
self.scan_expression(field)?;
}
Ok(())
}
fn scan_lambda(&mut self, lambda: HirLambda) -> IResult<()> {
self.scan_expression(lambda.body)
}
fn scan_statement(&mut self, statement: StmtId) -> IResult<()> {
match self.interner.statement(&statement) {
HirStatement::Let(let_) => self.scan_expression(let_.expression),
HirStatement::Constrain(constrain) => self.scan_expression(constrain.0),
HirStatement::Assign(assign) => self.scan_expression(assign.expression),
HirStatement::For(for_) => self.scan_for(for_),
HirStatement::Break => Ok(()),
HirStatement::Continue => Ok(()),
HirStatement::Expression(expression) => self.scan_expression(expression),
HirStatement::Semi(semi) => self.scan_expression(semi),
HirStatement::Error => Ok(()),
HirStatement::Comptime(comptime) => {
let location = self.interner.statement_location(comptime);
let new_expr =
self.evaluate_comptime(comptime)?.into_expression(self.interner, location)?;
self.interner.replace_statement(statement, HirStatement::Expression(new_expr));
Ok(())
}
}
}
fn scan_for(&mut self, for_: HirForStatement) -> IResult<()> {
// We don't need to set self.in_loop since we're not actually evaluating this loop.
// We just need to push a scope so that if there's a `comptime { .. }` expr inside this
// loop, any variables it defines aren't accessible outside of it.
self.push_scope();
self.scan_expression(for_.block)?;
self.pop_scope();
Ok(())
}
}