mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:16:32 +02:00
Initial support for parse-time constants (#7436)
This commit is contained in:
@ -173,6 +173,9 @@ impl EngineState {
|
||||
for item in delta_overlay.vars.into_iter() {
|
||||
existing_overlay.vars.insert(item.0, item.1);
|
||||
}
|
||||
for item in delta_overlay.constants.into_iter() {
|
||||
existing_overlay.constants.insert(item.0, item.1);
|
||||
}
|
||||
for item in delta_overlay.aliases.into_iter() {
|
||||
existing_overlay.aliases.insert(item.0, item.1);
|
||||
}
|
||||
@ -626,6 +629,16 @@ impl EngineState {
|
||||
output
|
||||
}
|
||||
|
||||
pub fn find_constant(&self, var_id: VarId, removed_overlays: &[Vec<u8>]) -> Option<&Value> {
|
||||
for overlay_frame in self.active_overlays(removed_overlays).iter().rev() {
|
||||
if let Some(val) = overlay_frame.constants.get(&var_id) {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_span_contents(&self, span: &Span) -> &[u8] {
|
||||
for (contents, start, finish) in &self.file_contents {
|
||||
if span.start >= *start && span.end <= *finish {
|
||||
@ -1654,6 +1667,29 @@ impl<'a> StateWorkingSet<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_constant(&mut self, var_id: VarId, val: Value) {
|
||||
self.last_overlay_mut().constants.insert(var_id, val);
|
||||
}
|
||||
|
||||
pub fn find_constant(&self, var_id: VarId) -> Option<&Value> {
|
||||
let mut removed_overlays = vec![];
|
||||
|
||||
for scope_frame in self.delta.scope.iter().rev() {
|
||||
for overlay_frame in scope_frame
|
||||
.active_overlays(&mut removed_overlays)
|
||||
.iter()
|
||||
.rev()
|
||||
{
|
||||
if let Some(val) = overlay_frame.constants.get(&var_id) {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.permanent_state
|
||||
.find_constant(var_id, &removed_overlays)
|
||||
}
|
||||
|
||||
pub fn get_variable(&self, var_id: VarId) -> &Variable {
|
||||
let num_permanent_vars = self.permanent_state.num_vars();
|
||||
if var_id < num_permanent_vars {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{AliasId, DeclId, ModuleId, OverlayId, Type, VarId};
|
||||
use crate::{AliasId, DeclId, ModuleId, OverlayId, Type, Value, VarId};
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@ -199,6 +199,7 @@ impl ScopeFrame {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OverlayFrame {
|
||||
pub vars: HashMap<Vec<u8>, VarId>,
|
||||
pub constants: HashMap<VarId, Value>,
|
||||
pub predecls: HashMap<Vec<u8>, DeclId>, // temporary storage for predeclarations
|
||||
pub decls: HashMap<(Vec<u8>, Type), DeclId>,
|
||||
pub aliases: HashMap<Vec<u8>, AliasId>,
|
||||
@ -212,6 +213,7 @@ impl OverlayFrame {
|
||||
pub fn from_origin(origin: ModuleId, prefixed: bool) -> Self {
|
||||
Self {
|
||||
vars: HashMap::new(),
|
||||
constants: HashMap::new(),
|
||||
predecls: HashMap::new(),
|
||||
decls: HashMap::new(),
|
||||
aliases: HashMap::new(),
|
||||
|
Reference in New Issue
Block a user