This commit is contained in:
JT 2021-08-09 19:53:06 +12:00
parent bf19918e3c
commit 3da4f02ffa
4 changed files with 32 additions and 7 deletions

View File

@ -111,13 +111,14 @@ pub struct State<'a> {
pub parser_state: &'a ParserState,
}
#[derive(Debug)]
pub struct StackFrame {
pub vars: HashMap<VarId, Value>,
pub env_vars: HashMap<String, String>,
pub parent: Option<Stack>,
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Stack(Rc<RefCell<StackFrame>>);
impl Default for Stack {

View File

@ -2256,14 +2256,30 @@ impl<'a> ParserWorkingSet<'a> {
pub fn parse_alias(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {
let name = self.get_span_contents(spans[0]);
if name == b"alias" && spans.len() >= 4 {
let alias_name = self.get_span_contents(spans[1]).to_vec();
let _equals = self.get_span_contents(spans[2]);
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);
let replacement = spans[3..].to_vec();
if spans.len() >= 4 {
let alias_name = self.get_span_contents(spans[1]).to_vec();
let _equals = self.get_span_contents(spans[2]);
self.add_alias(alias_name, replacement);
let replacement = spans[3..].to_vec();
self.add_alias(alias_name, replacement);
}
return (
Statement::Expression(Expression {
expr: Expr::Call(call),
span: call_span,
ty: Type::Unknown,
}),
None,
);
}
}
(
Statement::Expression(Expression {
expr: Expr::Garbage,

View File

@ -9,7 +9,7 @@ pub struct ParserState {
vars: Vec<Type>,
decls: Vec<Declaration>,
blocks: Vec<Block>,
scope: Vec<ScopeFrame>, // REMOVE
scope: Vec<ScopeFrame>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -83,6 +83,9 @@ impl ParserState {
for item in first.vars.into_iter() {
last.vars.insert(item.0, item.1);
}
for item in first.aliases.into_iter() {
last.aliases.insert(item.0, item.1);
}
}
}

View File

@ -138,3 +138,8 @@ fn floating_add() -> TestResult {
fn subcommand() -> TestResult {
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")
}