Add environment variable support for modules (#331)

* Add 'expor env' dummy command

* (WIP) Abstract away module exportables as Overlay

* Switch to Overlays for use/hide

Works for decls only right now.

* Fix passing import patterns of hide to eval

* Simplify use/hide of decls

* Add ImportPattern as Expr; Add use env eval

Still no parsing of "export env" so I can't test it yet.

* Refactor export parsing; Add InternalError

* Add env var export and activation; Misc changes

Now it is possible to `use` env var that was exported from a module.

This commit also adds some new errors and other small changes.

* Add env var hiding

* Fix eval not recognizing hidden decls

Without this change, calling `hide foo`, the evaluator does not know
whether a custom command named "foo" was hidden during parsing,
therefore, it is not possible to reliably throw an error about the "foo"
name not found.

* Add use/hide/export env var tests; Cleanup; Notes

* Ignore hide env related tests for now

* Fix main branch merge mess

* Fixed multi-word export def

* Fix hiding tests on Windows

* Remove env var hiding for now
This commit is contained in:
Jakub Žádník
2021-11-16 01:16:06 +02:00
committed by GitHub
parent ab22619f4a
commit 5459d30a24
22 changed files with 1050 additions and 261 deletions

View File

@@ -1,6 +1,6 @@
use std::ops::{Index, IndexMut};
use crate::{DeclId, Signature, VarId};
use crate::{Overlay, Signature, VarId};
use super::Statement;
@@ -8,7 +8,7 @@ use super::Statement;
pub struct Block {
pub signature: Box<Signature>,
pub stmts: Vec<Statement>,
pub exports: Vec<(Vec<u8>, DeclId)>, // Assuming just defs for now
pub overlay: Overlay,
pub captures: Vec<VarId>,
}
@@ -47,16 +47,16 @@ impl Block {
Self {
signature: Box::new(Signature::new("")),
stmts: vec![],
exports: vec![],
overlay: Overlay::new(),
captures: vec![],
}
}
pub fn with_exports(self, exports: Vec<(Vec<u8>, DeclId)>) -> Self {
pub fn with_overlay(self, overlay: Overlay) -> Self {
Self {
signature: self.signature,
stmts: self.stmts,
exports,
overlay,
captures: self.captures,
}
}
@@ -70,7 +70,7 @@ where
Self {
signature: Box::new(Signature::new("")),
stmts: stmts.collect(),
exports: vec![],
overlay: Overlay::new(),
captures: vec![],
}
}

View File

@@ -1,5 +1,5 @@
use super::{Call, CellPath, Expression, FullCellPath, Operator, RangeOperator};
use crate::{BlockId, Signature, Span, Spanned, Unit, VarId};
use crate::{ast::ImportPattern, BlockId, Signature, Span, Spanned, Unit, VarId};
#[derive(Debug, Clone)]
pub enum Expr {
@@ -31,6 +31,7 @@ pub enum Expr {
String(String),
CellPath(CellPath),
FullCellPath(Box<FullCellPath>),
ImportPattern(ImportPattern),
Signature(Box<Signature>),
Garbage,
}

View File

@@ -131,6 +131,7 @@ impl Expression {
}
false
}
Expr::ImportPattern(_) => false,
Expr::Filepath(_) => false,
Expr::Float(_) => false,
Expr::FullCellPath(full_cell_path) => {
@@ -282,6 +283,7 @@ impl Expression {
.head
.replace_in_variable(working_set, new_var_id);
}
Expr::ImportPattern(_) => {}
Expr::Garbage => {}
Expr::GlobPattern(_) => {}
Expr::Int(_) => {}

View File

@@ -1,4 +1,4 @@
use crate::Span;
use crate::{span, Span};
#[derive(Debug, Clone)]
pub enum ImportPatternMember {
@@ -7,8 +7,34 @@ pub enum ImportPatternMember {
List { names: Vec<(Vec<u8>, Span)> },
}
#[derive(Debug, Clone)]
pub struct ImportPatternHead {
pub name: Vec<u8>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct ImportPattern {
pub head: Vec<u8>,
pub head: ImportPatternHead,
pub members: Vec<ImportPatternMember>,
}
impl ImportPattern {
pub fn span(&self) -> Span {
let mut spans = vec![self.head.span];
for member in &self.members {
match member {
ImportPatternMember::Glob { span } => spans.push(*span),
ImportPatternMember::Name { name: _, span } => spans.push(*span),
ImportPatternMember::List { names } => {
for (_, span) in names {
spans.push(*span);
}
}
}
}
span(&spans)
}
}