mirror of
https://github.com/nushell/nushell.git
synced 2025-05-23 11:20:49 +02:00
aliases
This commit is contained in:
parent
bf19918e3c
commit
3da4f02ffa
@ -111,13 +111,14 @@ pub struct State<'a> {
|
|||||||
pub parser_state: &'a ParserState,
|
pub parser_state: &'a ParserState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct StackFrame {
|
pub struct StackFrame {
|
||||||
pub vars: HashMap<VarId, Value>,
|
pub vars: HashMap<VarId, Value>,
|
||||||
pub env_vars: HashMap<String, String>,
|
pub env_vars: HashMap<String, String>,
|
||||||
pub parent: Option<Stack>,
|
pub parent: Option<Stack>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Stack(Rc<RefCell<StackFrame>>);
|
pub struct Stack(Rc<RefCell<StackFrame>>);
|
||||||
|
|
||||||
impl Default for Stack {
|
impl Default for Stack {
|
||||||
|
@ -2256,7 +2256,11 @@ impl<'a> ParserWorkingSet<'a> {
|
|||||||
pub fn parse_alias(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {
|
pub fn parse_alias(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {
|
||||||
let name = self.get_span_contents(spans[0]);
|
let name = self.get_span_contents(spans[0]);
|
||||||
|
|
||||||
if name == b"alias" && spans.len() >= 4 {
|
if name == b"alias" {
|
||||||
|
if let Some(decl_id) = self.find_decl(b"alias") {
|
||||||
|
let (call, call_span, _) = self.parse_internal_call(spans[0], &spans[1..], decl_id);
|
||||||
|
|
||||||
|
if spans.len() >= 4 {
|
||||||
let alias_name = self.get_span_contents(spans[1]).to_vec();
|
let alias_name = self.get_span_contents(spans[1]).to_vec();
|
||||||
let _equals = self.get_span_contents(spans[2]);
|
let _equals = self.get_span_contents(spans[2]);
|
||||||
|
|
||||||
@ -2264,6 +2268,18 @@ impl<'a> ParserWorkingSet<'a> {
|
|||||||
|
|
||||||
self.add_alias(alias_name, replacement);
|
self.add_alias(alias_name, replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
Statement::Expression(Expression {
|
||||||
|
expr: Expr::Call(call),
|
||||||
|
span: call_span,
|
||||||
|
ty: Type::Unknown,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
(
|
(
|
||||||
Statement::Expression(Expression {
|
Statement::Expression(Expression {
|
||||||
expr: Expr::Garbage,
|
expr: Expr::Garbage,
|
||||||
|
@ -9,7 +9,7 @@ pub struct ParserState {
|
|||||||
vars: Vec<Type>,
|
vars: Vec<Type>,
|
||||||
decls: Vec<Declaration>,
|
decls: Vec<Declaration>,
|
||||||
blocks: Vec<Block>,
|
blocks: Vec<Block>,
|
||||||
scope: Vec<ScopeFrame>, // REMOVE
|
scope: Vec<ScopeFrame>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
@ -83,6 +83,9 @@ impl ParserState {
|
|||||||
for item in first.vars.into_iter() {
|
for item in first.vars.into_iter() {
|
||||||
last.vars.insert(item.0, item.1);
|
last.vars.insert(item.0, item.1);
|
||||||
}
|
}
|
||||||
|
for item in first.aliases.into_iter() {
|
||||||
|
last.aliases.insert(item.0, item.1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,3 +138,8 @@ fn floating_add() -> TestResult {
|
|||||||
fn subcommand() -> TestResult {
|
fn subcommand() -> TestResult {
|
||||||
run_test("def foo [] {}; def \"foo bar\" [] {3}; foo bar", "3")
|
run_test("def foo [] {}; def \"foo bar\" [] {3}; foo bar", "3")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alias_1() -> TestResult {
|
||||||
|
run_test("def foo [$x] { $x + 10 }; alias f = foo; f 100", "110")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user