Merge branch 'main' into source-command

This commit is contained in:
Tanishq Kancharla
2021-10-03 14:25:00 -04:00
committed by GitHub
38 changed files with 1168 additions and 126 deletions

View File

@ -45,4 +45,8 @@ impl Call {
None
}
pub fn nth(&self, pos: usize) -> Option<Expression> {
self.positional.get(pos).cloned()
}
}

View File

@ -1,17 +1,36 @@
use super::Expression;
use crate::Span;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PathMember {
String { val: String, span: Span },
Int { val: usize, span: Span },
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellPath {
pub members: Vec<PathMember>,
}
impl CellPath {
pub fn into_string(&self) -> String {
let mut output = String::new();
for (idx, elem) in self.members.iter().enumerate() {
if idx > 0 {
output.push('.');
}
match elem {
PathMember::Int { val, .. } => output.push_str(&format!("{}", val)),
PathMember::String { val, .. } => output.push_str(val),
}
}
output
}
}
#[derive(Debug, Clone)]
pub struct FullCellPath {
pub head: Expression,

View File

@ -1,4 +1,4 @@
use super::{Call, Expression, FullCellPath, Operator, RangeOperator};
use super::{Call, CellPath, Expression, FullCellPath, Operator, RangeOperator};
use crate::{BlockId, Signature, Span, VarId};
#[derive(Debug, Clone)]
@ -24,6 +24,7 @@ pub enum Expr {
Table(Vec<Expression>, Vec<Vec<Expression>>),
Keyword(Vec<u8>, Span, Box<Expression>),
String(String), // FIXME: improve this in the future?
CellPath(CellPath),
FullCellPath(Box<FullCellPath>),
Signature(Box<Signature>),
Garbage,

View File

@ -1,7 +1,10 @@
use super::Command;
use crate::{ast::Block, BlockId, DeclId, Signature, Span, Type, VarId};
use core::panic;
use std::{collections::HashMap, slice::Iter};
use std::{
collections::{HashMap, HashSet},
slice::Iter,
};
pub struct EngineState {
files: Vec<(String, usize, usize)>,
@ -18,6 +21,7 @@ pub struct ScopeFrame {
decls: HashMap<Vec<u8>, DeclId>,
aliases: HashMap<Vec<u8>, Vec<Span>>,
modules: HashMap<Vec<u8>, BlockId>,
hiding: HashSet<DeclId>,
}
impl ScopeFrame {
@ -27,6 +31,7 @@ impl ScopeFrame {
decls: HashMap::new(),
aliases: HashMap::new(),
modules: HashMap::new(),
hiding: HashSet::new(),
}
}
@ -81,6 +86,9 @@ impl EngineState {
for item in first.modules.into_iter() {
last.modules.insert(item.0, item.1);
}
for item in first.hiding.into_iter() {
last.hiding.insert(item);
}
}
}
@ -124,9 +132,15 @@ impl EngineState {
}
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
let mut hiding: HashSet<DeclId> = HashSet::new();
for scope in self.scope.iter().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.get(name) {
return Some(*decl_id);
if !hiding.contains(decl_id) {
return Some(*decl_id);
}
}
}
@ -238,9 +252,10 @@ pub struct StateWorkingSet<'a> {
pub struct StateDelta {
files: Vec<(String, usize, usize)>,
pub(crate) file_contents: Vec<u8>,
vars: Vec<Type>, // indexed by VarId
decls: Vec<Box<dyn Command>>, // indexed by DeclId
blocks: Vec<Block>, // indexed by BlockId
vars: Vec<Type>, // indexed by VarId
decls: Vec<Box<dyn Command>>, // indexed by DeclId
blocks: Vec<Block>, // indexed by BlockId
predecls: HashMap<Vec<u8>, DeclId>, // this should get erased after every def call
pub scope: Vec<ScopeFrame>,
}
@ -274,6 +289,7 @@ impl<'a> StateWorkingSet<'a> {
file_contents: vec![],
vars: vec![],
decls: vec![],
predecls: HashMap::new(),
blocks: vec![],
scope: vec![ScopeFrame::new()],
},
@ -304,11 +320,71 @@ impl<'a> StateWorkingSet<'a> {
.scope
.last_mut()
.expect("internal error: missing required scope frame");
scope_frame.decls.insert(name, decl_id);
decl_id
}
pub fn add_predecl(&mut self, decl: Box<dyn Command>) -> Option<DeclId> {
let name = decl.name().as_bytes().to_vec();
self.delta.decls.push(decl);
let decl_id = self.num_decls() - 1;
self.delta.predecls.insert(name, decl_id)
}
pub fn merge_predecl(&mut self, name: &[u8]) -> Option<DeclId> {
if let Some(decl_id) = self.delta.predecls.remove(name) {
let scope_frame = self
.delta
.scope
.last_mut()
.expect("internal error: missing required scope frame");
scope_frame.decls.insert(name.into(), decl_id);
return Some(decl_id);
}
None
}
pub fn hide_decl(&mut self, name: &[u8]) -> Option<DeclId> {
let mut hiding: HashSet<DeclId> = HashSet::new();
// Since we can mutate scope frames in delta, remove the id directly
for scope in self.delta.scope.iter_mut().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.remove(name) {
return Some(decl_id);
}
}
// We cannot mutate the permanent state => store the information in the current scope frame
let last_scope_frame = self
.delta
.scope
.last_mut()
.expect("internal error: missing required scope frame");
for scope in self.permanent_state.scope.iter().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.get(name) {
if !hiding.contains(decl_id) {
// Do not hide already hidden decl
last_scope_frame.hiding.insert(*decl_id);
return Some(*decl_id);
}
}
}
None
}
pub fn add_block(&mut self, block: Block) -> BlockId {
self.delta.blocks.push(block);
@ -416,15 +492,27 @@ impl<'a> StateWorkingSet<'a> {
}
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
let mut hiding: HashSet<DeclId> = HashSet::new();
if let Some(decl_id) = self.delta.predecls.get(name) {
return Some(*decl_id);
}
for scope in self.delta.scope.iter().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.get(name) {
return Some(*decl_id);
}
}
for scope in self.permanent_state.scope.iter().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.get(name) {
return Some(*decl_id);
if !hiding.contains(decl_id) {
return Some(*decl_id);
}
}
}

View File

@ -112,6 +112,10 @@ impl Stack {
self.0.borrow().env_vars.clone()
}
pub fn get_env_var(&self, name: &str) -> Option<String> {
self.0.borrow().env_vars.get(name).cloned()
}
pub fn print_stack(&self) {
println!("===frame===");
println!("vars:");

View File

@ -86,7 +86,7 @@ impl SyntaxShape {
SyntaxShape::Custom(custom, _) => custom.to_type(),
SyntaxShape::Duration => Type::Duration,
SyntaxShape::Expression => Type::Unknown,
SyntaxShape::FilePath => Type::FilePath,
SyntaxShape::FilePath => Type::String,
SyntaxShape::Filesize => Type::Filesize,
SyntaxShape::FullCellPath => Type::Unknown,
SyntaxShape::GlobPattern => Type::String,

View File

@ -9,7 +9,7 @@ pub use stream::*;
use std::fmt::Debug;
use crate::ast::PathMember;
use crate::ast::{CellPath, PathMember};
use crate::{span, BlockId, Span, Type};
use crate::ShellError;
@ -72,6 +72,10 @@ pub enum Value {
val: Vec<u8>,
span: Span,
},
CellPath {
val: CellPath,
span: Span,
},
}
impl Value {
@ -99,6 +103,7 @@ impl Value {
Value::Stream { span, .. } => *span,
Value::Nothing { span, .. } => *span,
Value::Binary { span, .. } => *span,
Value::CellPath { span, .. } => *span,
}
}
@ -119,6 +124,7 @@ impl Value {
Value::Nothing { span, .. } => *span = new_span,
Value::Error { .. } => {}
Value::Binary { span, .. } => *span = new_span,
Value::CellPath { span, .. } => *span = new_span,
}
self
@ -143,6 +149,7 @@ impl Value {
Value::Stream { .. } => Type::ValueStream,
Value::Error { .. } => Type::Error,
Value::Binary { .. } => Type::Binary,
Value::CellPath { .. } => Type::CellPath,
}
}
@ -184,6 +191,7 @@ impl Value {
Value::Nothing { .. } => String::new(),
Value::Error { error } => format!("{:?}", error),
Value::Binary { val, .. } => format!("{:?}", val),
Value::CellPath { val, .. } => val.into_string(),
}
}
@ -215,6 +223,7 @@ impl Value {
Value::Nothing { .. } => String::new(),
Value::Error { error } => format!("{:?}", error),
Value::Binary { val, .. } => format!("{:?}", val),
Value::CellPath { .. } => self.into_string(),
}
}