Add a config variable with engine support (#332)

* Add a config variable with engine support

* Add a config variable with engine support

* Oops, cleanup
This commit is contained in:
JT
2021-11-15 08:25:57 +13:00
committed by GitHub
parent e76451866d
commit 0f107b2830
30 changed files with 333 additions and 96 deletions

View File

@ -136,13 +136,14 @@ pub struct EngineState {
pub const NU_VARIABLE_ID: usize = 0;
pub const SCOPE_VARIABLE_ID: usize = 1;
pub const IN_VARIABLE_ID: usize = 2;
pub const CONFIG_VARIABLE_ID: usize = 3;
impl EngineState {
pub fn new() -> Self {
Self {
files: im::vector![],
file_contents: im::vector![],
vars: im::vector![Type::Unknown, Type::Unknown, Type::Unknown],
vars: im::vector![Type::Unknown, Type::Unknown, Type::Unknown, Type::Unknown],
decls: im::vector![],
blocks: im::vector![],
scope: im::vector![ScopeFrame::new()],

View File

@ -1,6 +1,6 @@
use std::collections::HashMap;
use crate::{ShellError, Value, VarId};
use crate::{Config, ShellError, Value, VarId, CONFIG_VARIABLE_ID};
/// A runtime value stack used during evaluation
///
@ -42,6 +42,7 @@ impl Stack {
if let Some(v) = self.vars.get(&var_id) {
return Ok(v.clone());
}
Err(ShellError::InternalError("variable not found".into()))
}
@ -67,6 +68,11 @@ impl Stack {
// FIXME: this is probably slow
output.env_vars = self.env_vars.clone();
let config = self
.get_var(CONFIG_VARIABLE_ID)
.expect("internal error: config is missing");
output.vars.insert(CONFIG_VARIABLE_ID, config);
output
}
@ -81,6 +87,18 @@ impl Stack {
None
}
pub fn get_config(&self) -> Result<Config, ShellError> {
let config = self.get_var(CONFIG_VARIABLE_ID);
match config {
Ok(config) => config.into_config(),
Err(e) => {
println!("Can't find {} in {:?}", CONFIG_VARIABLE_ID, self);
Err(e)
}
}
}
pub fn print_stack(&self) {
println!("vars:");
for (var, val) in &self.vars {