mirror of
https://github.com/nushell/nushell
synced 2024-12-28 22:13:10 +00:00
Add alias and external
This commit is contained in:
parent
eac02b55f6
commit
7b51c5c49f
2 changed files with 44 additions and 18 deletions
10
src/main.rs
10
src/main.rs
|
@ -32,6 +32,16 @@ fn main() -> std::io::Result<()> {
|
||||||
);
|
);
|
||||||
working_set.add_decl((b"let").to_vec(), sig);
|
working_set.add_decl((b"let").to_vec(), sig);
|
||||||
|
|
||||||
|
let sig = Signature::build("alias")
|
||||||
|
.required("var_name", SyntaxShape::Variable, "variable name")
|
||||||
|
.required("=", SyntaxShape::Literal(b"=".to_vec()), "equals sign")
|
||||||
|
.required(
|
||||||
|
"value",
|
||||||
|
SyntaxShape::Expression,
|
||||||
|
"the value to set the variable to",
|
||||||
|
);
|
||||||
|
working_set.add_decl((b"alias").to_vec(), sig);
|
||||||
|
|
||||||
//let file = std::fs::read(&path)?;
|
//let file = std::fs::read(&path)?;
|
||||||
//let (output, err) = working_set.parse_file(&path, file);
|
//let (output, err) = working_set.parse_file(&path, file);
|
||||||
let (output, err) = working_set.parse_source(path.as_bytes());
|
let (output, err) = working_set.parse_source(path.as_bytes());
|
||||||
|
|
|
@ -119,6 +119,7 @@ pub enum Expr {
|
||||||
Int(i64),
|
Int(i64),
|
||||||
Var(VarId),
|
Var(VarId),
|
||||||
Call(Box<Call>),
|
Call(Box<Call>),
|
||||||
|
ExternalCall(Vec<u8>, Vec<Vec<u8>>),
|
||||||
Operator(Operator),
|
Operator(Operator),
|
||||||
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
|
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
|
||||||
Subexpression(Box<Block>),
|
Subexpression(Box<Block>),
|
||||||
|
@ -300,7 +301,18 @@ fn span(spans: &[Span]) -> Span {
|
||||||
impl ParserWorkingSet {
|
impl ParserWorkingSet {
|
||||||
pub fn parse_external_call(&mut self, spans: &[Span]) -> (Expression, Option<ParseError>) {
|
pub fn parse_external_call(&mut self, spans: &[Span]) -> (Expression, Option<ParseError>) {
|
||||||
// TODO: add external parsing
|
// TODO: add external parsing
|
||||||
(Expression::garbage(spans[0]), None)
|
let mut args = vec![];
|
||||||
|
let name = self.get_span_contents(spans[0]).to_vec();
|
||||||
|
for span in &spans[1..] {
|
||||||
|
args.push(self.get_span_contents(*span).to_vec());
|
||||||
|
}
|
||||||
|
(
|
||||||
|
Expression {
|
||||||
|
expr: Expr::ExternalCall(name, args),
|
||||||
|
span: span(spans),
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_internal_call(
|
pub fn parse_internal_call(
|
||||||
|
@ -1143,24 +1155,28 @@ impl ParserWorkingSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_let(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {
|
pub fn parse_let(&mut self, spans: &[Span]) -> (Statement, Option<ParseError>) {
|
||||||
if let Some(decl_id) = self.find_decl(b"let") {
|
let name = self.get_span_contents(spans[0]);
|
||||||
let (mut call, call_span, err) = self.parse_internal_call(spans, decl_id);
|
|
||||||
|
|
||||||
if err.is_some() {
|
if name == b"let" {
|
||||||
return (
|
if let Some(decl_id) = self.find_decl(b"let") {
|
||||||
Statement::Expression(Expression {
|
let (mut call, call_span, err) = self.parse_internal_call(spans, decl_id);
|
||||||
expr: Expr::Call(call),
|
|
||||||
span: call_span,
|
if err.is_some() {
|
||||||
}),
|
return (
|
||||||
err,
|
Statement::Expression(Expression {
|
||||||
);
|
expr: Expr::Call(call),
|
||||||
} else if let Expression {
|
span: call_span,
|
||||||
expr: Expr::Var(var_id),
|
}),
|
||||||
..
|
err,
|
||||||
} = call.positional[0]
|
);
|
||||||
{
|
} else if let Expression {
|
||||||
let expression = call.positional.swap_remove(2);
|
expr: Expr::Var(var_id),
|
||||||
return (Statement::VarDecl(VarDecl { var_id, expression }), None);
|
..
|
||||||
|
} = call.positional[0]
|
||||||
|
{
|
||||||
|
let expression = call.positional.swap_remove(2);
|
||||||
|
return (Statement::VarDecl(VarDecl { var_id, expression }), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
|
|
Loading…
Reference in a new issue