|
1 | 1 | use lean_vm::*; |
2 | 2 | use multilinear_toolkit::prelude::*; |
3 | 3 | use p3_util::log2_ceil_usize; |
4 | | -use std::collections::BTreeMap; |
| 4 | +use std::collections::{BTreeMap, BTreeSet}; |
5 | 5 | use std::fmt::{Display, Formatter}; |
6 | 6 | use utils::ToUsize; |
7 | 7 |
|
@@ -307,6 +307,9 @@ pub enum Line { |
307 | 307 | value: Expression, |
308 | 308 | arms: Vec<(usize, Vec<Self>)>, |
309 | 309 | }, |
| 310 | + ForwardDeclaration { |
| 311 | + var: Var, |
| 312 | + }, |
310 | 313 | Assignment { |
311 | 314 | var: Var, |
312 | 315 | value: Expression, |
@@ -378,6 +381,30 @@ pub enum Line { |
378 | 381 | location: SourceLineNumber, |
379 | 382 | }, |
380 | 383 | } |
| 384 | + |
| 385 | +/// A context specifying which variables are in scope. |
| 386 | +pub struct Context { |
| 387 | + /// A list of lexical scopes, innermost scope last. |
| 388 | + pub scopes: Vec<Scope>, |
| 389 | +} |
| 390 | + |
| 391 | +impl Context { |
| 392 | + pub fn defines(&self, var: &Var) -> bool { |
| 393 | + for scope in self.scopes.iter() { |
| 394 | + if scope.vars.contains(var) { |
| 395 | + return true; |
| 396 | + } |
| 397 | + } |
| 398 | + false |
| 399 | + } |
| 400 | +} |
| 401 | + |
| 402 | +#[derive(Default)] |
| 403 | +pub struct Scope { |
| 404 | + /// A set of declared variables. |
| 405 | + pub vars: BTreeSet<Var>, |
| 406 | +} |
| 407 | + |
381 | 408 | impl Display for Expression { |
382 | 409 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
383 | 410 | match self { |
@@ -418,6 +445,9 @@ impl Line { |
418 | 445 | .join("\n"); |
419 | 446 | format!("match {value} {{\n{arms_str}\n{spaces}}}") |
420 | 447 | } |
| 448 | + Self::ForwardDeclaration { var } => { |
| 449 | + format!("var {var}") |
| 450 | + } |
421 | 451 | Self::Assignment { var, value } => { |
422 | 452 | format!("{var} = {value}") |
423 | 453 | } |
|
0 commit comments