This commit is contained in:
Tanishq Kancharla 2021-10-05 21:59:16 -04:00
parent 909b7d2160
commit d6d0bad7aa
4 changed files with 48 additions and 17 deletions

View file

@ -34,7 +34,9 @@ impl Highlighter for NuHighlighter {
.to_string(); .to_string();
output.push((Style::new(), gap)); output.push((Style::new(), gap));
} }
// println!("line: \n{}", line);
// println!("shape: \n{:#?}\n", shape);
// println!("global_span_offset: \n{:#?}\n", global_span_offset);
let next_token = line let next_token = line
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)] [(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
.to_string(); .to_string();

View file

@ -1,5 +1,7 @@
use std::borrow::Borrow;
use std::path::Path; use std::path::Path;
use nu_engine::{eval_block, eval_expression};
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext}; use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value}; use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
@ -30,8 +32,9 @@ impl Command for Source {
call: &Call, call: &Call,
input: Value, input: Value,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
Ok(Value::Nothing { span: call.head }) let block = &call.positional[0];
// source(_context, call, input)
Ok(eval_expression(_context, block)?)
} }
} }

View file

@ -549,8 +549,9 @@ pub fn parse_source(
if name == b"source" { if name == b"source" {
if let Some(decl_id) = working_set.find_decl(b"source") { if let Some(decl_id) = working_set.find_decl(b"source") {
let (call, call_span, _) = let (call, call_span, err) =
parse_internal_call(working_set, spans[0], &spans[1..], decl_id); parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
// println!("\nSpans: {:#?}", spans);
// Command and one file name // Command and one file name
if spans.len() >= 2 { if spans.len() >= 2 {
@ -570,19 +571,44 @@ pub fn parse_source(
&contents, &contents,
false, false,
); );
if let Some(_) = err { if let Some(_) = err {
// Unsuccessful parse of file // Unsuccessful parse of file
// return ( return (
// Statement::Pipeline(Pipeline::from_vec(vec![Expression { Statement::Pipeline(Pipeline::from_vec(vec![Expression {
// expr: Expr::Call(call), expr: Expr::Call(call),
// span: call_span, span: span(&spans[1..]),
// ty: Type::Unknown, ty: Type::Unknown,
// }])), custom_completion: None,
// None, }])),
// ); // Return the file parse error
err,
);
} else {
// Save the block into the working set
let block_id = working_set.add_block(block);
// println!("CALL:{:?}", call);
let mut call_with_block = call.clone();
// println!("CALL_WITH_BLOCK: {:?}", call_with_block);
call_with_block.positional.push(Expression {
expr: Expr::Block(block_id),
span: span(&spans[1..]),
ty: Type::Unknown,
custom_completion: None,
});
return (
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call_with_block),
span: call_span,
ty: Type::Unknown,
custom_completion: None,
}])),
None,
);
} }
} else {
// Source file couldn't be parsed correctly
} }
} }
} }

View file

@ -1,5 +1,5 @@
def greet [name] { def inc [x] {
echo "hello" $name $x + 1
} }
greet "world" inc 5