2021-08-15 22:33:34 +00:00
|
|
|
use std::time::Instant;
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-09-02 01:29:43 +00:00
|
|
|
use crate::state::State;
|
|
|
|
use nu_parser::{Block, Call, Expr, Expression, Operator, Statement};
|
|
|
|
use nu_protocol::{IntoRowStream, IntoValueStream, ShellError, Span, Value};
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
match op {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Operator(operator),
|
|
|
|
..
|
|
|
|
} => Ok(operator.clone()),
|
2021-08-10 05:55:25 +00:00
|
|
|
Expression { span, .. } => Err(ShellError::Unsupported(*span)),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
|
2021-08-16 02:30:31 +00:00
|
|
|
let parser_state = state.parser_state.borrow();
|
|
|
|
let decl = parser_state.get_decl(call.decl_id);
|
2021-07-23 05:14:49 +00:00
|
|
|
if let Some(block_id) = decl.body {
|
2021-08-15 22:33:34 +00:00
|
|
|
let state = state.enter_scope();
|
2021-08-27 02:30:10 +00:00
|
|
|
for (arg, param) in call.positional.iter().zip(
|
|
|
|
decl.signature
|
|
|
|
.required_positional
|
|
|
|
.iter()
|
|
|
|
.chain(decl.signature.optional_positional.iter()),
|
|
|
|
) {
|
2021-08-15 22:33:34 +00:00
|
|
|
let result = eval_expression(&state, arg)?;
|
2021-07-23 21:19:30 +00:00
|
|
|
let var_id = param
|
|
|
|
.var_id
|
|
|
|
.expect("internal error: all custom parameters must have var_ids");
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
state.add_var(var_id, result);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-16 02:30:31 +00:00
|
|
|
let parser_state = state.parser_state.borrow();
|
|
|
|
let block = parser_state.get_block(block_id);
|
2021-08-15 22:33:34 +00:00
|
|
|
eval_block(&state, block)
|
2021-07-29 22:56:51 +00:00
|
|
|
} else if decl.signature.name == "let" {
|
|
|
|
let var_id = call.positional[0]
|
|
|
|
.as_var()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
let rhs = eval_expression(state, keyword_expr)?;
|
2021-07-29 22:56:51 +00:00
|
|
|
|
2021-07-30 07:30:11 +00:00
|
|
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
2021-07-29 22:56:51 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
state.add_var(var_id, rhs);
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
|
|
|
} else if decl.signature.name == "let-env" {
|
|
|
|
let env_var = call.positional[0]
|
|
|
|
.as_string()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
let rhs = eval_expression(state, keyword_expr)?;
|
2021-08-08 21:02:47 +00:00
|
|
|
let rhs = rhs.as_string()?;
|
|
|
|
|
|
|
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
state.add_env_var(env_var, rhs);
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
2021-07-29 22:56:51 +00:00
|
|
|
} else if decl.signature.name == "if" {
|
|
|
|
let cond = &call.positional[0];
|
|
|
|
let then_block = call.positional[1]
|
|
|
|
.as_block()
|
|
|
|
.expect("internal error: expected block");
|
|
|
|
let else_case = call.positional.get(2);
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
let result = eval_expression(state, cond)?;
|
2021-07-29 22:56:51 +00:00
|
|
|
match result {
|
2021-08-08 21:02:47 +00:00
|
|
|
Value::Bool { val, span } => {
|
2021-08-16 02:30:31 +00:00
|
|
|
let parser_state = state.parser_state.borrow();
|
2021-07-29 22:56:51 +00:00
|
|
|
if val {
|
2021-08-16 02:30:31 +00:00
|
|
|
let block = parser_state.get_block(then_block);
|
2021-08-15 22:33:34 +00:00
|
|
|
let state = state.enter_scope();
|
|
|
|
eval_block(&state, block)
|
2021-07-29 22:56:51 +00:00
|
|
|
} else if let Some(else_case) = else_case {
|
|
|
|
if let Some(else_expr) = else_case.as_keyword() {
|
|
|
|
if let Some(block_id) = else_expr.as_block() {
|
2021-08-16 02:30:31 +00:00
|
|
|
let block = parser_state.get_block(block_id);
|
2021-08-15 22:33:34 +00:00
|
|
|
let state = state.enter_scope();
|
|
|
|
eval_block(&state, block)
|
2021-07-24 06:44:38 +00:00
|
|
|
} else {
|
2021-08-15 22:33:34 +00:00
|
|
|
eval_expression(state, else_expr)
|
2021-07-24 06:44:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-15 22:33:34 +00:00
|
|
|
eval_expression(state, else_case)
|
2021-07-24 06:44:38 +00:00
|
|
|
}
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing { span })
|
2021-07-24 06:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 05:55:25 +00:00
|
|
|
_ => Err(ShellError::CantConvert("bool".into(), result.span())),
|
2021-07-23 21:46:55 +00:00
|
|
|
}
|
2021-07-30 03:26:06 +00:00
|
|
|
} else if decl.signature.name == "build-string" {
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for expr in &call.positional {
|
2021-08-15 22:33:34 +00:00
|
|
|
let val = eval_expression(state, expr)?;
|
2021-07-30 03:26:06 +00:00
|
|
|
|
2021-09-01 21:20:53 +00:00
|
|
|
output.push(val.into_string());
|
2021-07-30 03:26:06 +00:00
|
|
|
}
|
|
|
|
Ok(Value::String {
|
|
|
|
val: output.join(""),
|
|
|
|
span: call.head,
|
|
|
|
})
|
2021-07-30 06:10:40 +00:00
|
|
|
} else if decl.signature.name == "benchmark" {
|
|
|
|
let block = call.positional[0]
|
|
|
|
.as_block()
|
|
|
|
.expect("internal error: expected block");
|
2021-08-16 02:30:31 +00:00
|
|
|
let parser_state = state.parser_state.borrow();
|
|
|
|
let block = parser_state.get_block(block);
|
2021-07-30 06:10:40 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
let state = state.enter_scope();
|
2021-07-30 06:10:40 +00:00
|
|
|
let start_time = Instant::now();
|
2021-08-15 22:33:34 +00:00
|
|
|
eval_block(&state, block)?;
|
2021-07-30 06:10:40 +00:00
|
|
|
let end_time = Instant::now();
|
|
|
|
println!("{} ms", (end_time - start_time).as_millis());
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
2021-07-30 06:10:40 +00:00
|
|
|
} else if decl.signature.name == "for" {
|
|
|
|
let var_id = call.positional[0]
|
|
|
|
.as_var()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
2021-08-15 22:33:34 +00:00
|
|
|
let end_val = eval_expression(state, keyword_expr)?;
|
2021-07-30 06:10:40 +00:00
|
|
|
|
|
|
|
let block = call.positional[2]
|
|
|
|
.as_block()
|
|
|
|
.expect("internal error: expected block");
|
2021-08-16 02:30:31 +00:00
|
|
|
let parser_state = state.parser_state.borrow();
|
|
|
|
let block = parser_state.get_block(block);
|
2021-07-30 06:10:40 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
let state = state.enter_scope();
|
2021-07-30 06:10:40 +00:00
|
|
|
|
2021-07-30 07:50:39 +00:00
|
|
|
let mut x = Value::Int {
|
|
|
|
val: 0,
|
|
|
|
span: Span::unknown(),
|
|
|
|
};
|
2021-07-30 06:10:40 +00:00
|
|
|
|
|
|
|
loop {
|
2021-07-30 07:50:39 +00:00
|
|
|
if x == end_val {
|
2021-07-30 06:10:40 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2021-08-15 22:33:34 +00:00
|
|
|
state.add_var(var_id, x.clone());
|
|
|
|
eval_block(&state, block)?;
|
2021-07-30 07:50:39 +00:00
|
|
|
}
|
|
|
|
if let Value::Int { ref mut val, .. } = x {
|
|
|
|
*val += 1
|
2021-07-30 06:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
2021-08-08 21:34:21 +00:00
|
|
|
} else if decl.signature.name == "vars" {
|
2021-08-16 02:30:31 +00:00
|
|
|
state.parser_state.borrow().print_vars();
|
2021-08-17 00:26:05 +00:00
|
|
|
Ok(Value::Nothing { span: call.head })
|
2021-08-08 21:34:21 +00:00
|
|
|
} else if decl.signature.name == "decls" {
|
2021-08-16 02:30:31 +00:00
|
|
|
state.parser_state.borrow().print_decls();
|
2021-08-08 21:34:21 +00:00
|
|
|
Ok(Value::Nothing { span: call.head })
|
|
|
|
} else if decl.signature.name == "blocks" {
|
2021-08-16 02:30:31 +00:00
|
|
|
state.parser_state.borrow().print_blocks();
|
2021-08-08 21:34:21 +00:00
|
|
|
Ok(Value::Nothing { span: call.head })
|
|
|
|
} else if decl.signature.name == "stack" {
|
2021-08-15 22:33:34 +00:00
|
|
|
state.print_stack();
|
2021-08-08 21:34:21 +00:00
|
|
|
Ok(Value::Nothing { span: call.head })
|
2021-08-10 05:08:10 +00:00
|
|
|
} else if decl.signature.name == "def" || decl.signature.name == "alias" {
|
2021-08-09 00:19:07 +00:00
|
|
|
Ok(Value::Nothing { span: call.head })
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
2021-08-08 21:02:47 +00:00
|
|
|
Err(ShellError::Unsupported(call.head))
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn eval_expression(state: &State, expr: &Expression) -> Result<Value, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
match &expr.expr {
|
2021-07-24 05:57:17 +00:00
|
|
|
Expr::Bool(b) => Ok(Value::Bool {
|
|
|
|
val: *b,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Int(i) => Ok(Value::Int {
|
|
|
|
val: *i,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 20:21:21 +00:00
|
|
|
Expr::Float(f) => Ok(Value::Float {
|
|
|
|
val: *f,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-15 22:33:34 +00:00
|
|
|
Expr::Var(var_id) => state
|
2021-07-30 21:57:22 +00:00
|
|
|
.get_var(*var_id)
|
|
|
|
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
2021-08-15 22:33:34 +00:00
|
|
|
Expr::Call(call) => eval_call(state, call),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
2021-08-10 06:31:34 +00:00
|
|
|
let op_span = op.span;
|
2021-08-15 22:33:34 +00:00
|
|
|
let lhs = eval_expression(state, lhs)?;
|
|
|
|
let op = eval_operator(op)?;
|
|
|
|
let rhs = eval_expression(state, rhs)?;
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
match op {
|
2021-08-10 06:31:34 +00:00
|
|
|
Operator::Plus => lhs.add(op_span, &rhs),
|
2021-08-25 19:29:36 +00:00
|
|
|
Operator::Minus => lhs.sub(op_span, &rhs),
|
|
|
|
Operator::Multiply => lhs.mul(op_span, &rhs),
|
|
|
|
Operator::Divide => lhs.div(op_span, &rhs),
|
|
|
|
Operator::LessThan => lhs.lt(op_span, &rhs),
|
|
|
|
Operator::LessThanOrEqual => lhs.lte(op_span, &rhs),
|
|
|
|
Operator::GreaterThan => lhs.gt(op_span, &rhs),
|
|
|
|
Operator::GreaterThanOrEqual => lhs.gte(op_span, &rhs),
|
|
|
|
Operator::Equal => lhs.eq(op_span, &rhs),
|
|
|
|
Operator::NotEqual => lhs.ne(op_span, &rhs),
|
|
|
|
_ => Err(ShellError::Unsupported(op_span)),
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
Expr::Subexpression(block_id) => {
|
2021-08-16 02:30:31 +00:00
|
|
|
let parser_state = state.parser_state.borrow();
|
|
|
|
let block = parser_state.get_block(*block_id);
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
let state = state.enter_scope();
|
|
|
|
eval_block(&state, block)
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Block(block_id) => Ok(Value::Block {
|
|
|
|
val: *block_id,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::List(x) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in x {
|
2021-08-15 22:33:34 +00:00
|
|
|
output.push(eval_expression(state, expr)?);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::List {
|
2021-09-01 21:20:53 +00:00
|
|
|
val: output.into_value_stream(),
|
2021-08-08 21:02:47 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-28 19:17:30 +00:00
|
|
|
Expr::Table(headers, vals) => {
|
|
|
|
let mut output_headers = vec![];
|
|
|
|
for expr in headers {
|
|
|
|
output_headers.push(eval_expression(state, expr)?.as_string()?);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output_rows = vec![];
|
|
|
|
for val in vals {
|
|
|
|
let mut row = vec![];
|
|
|
|
for expr in val {
|
|
|
|
row.push(eval_expression(state, expr)?);
|
|
|
|
}
|
|
|
|
output_rows.push(row);
|
|
|
|
}
|
|
|
|
Ok(Value::Table {
|
|
|
|
headers: output_headers,
|
2021-09-01 21:20:53 +00:00
|
|
|
val: output_rows.into_row_stream(),
|
2021-08-28 19:17:30 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
Expr::Keyword(_, _, expr) => eval_expression(state, expr),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::String(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
|
|
|
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn eval_block(state: &State, block: &Block) -> Result<Value, ShellError> {
|
2021-08-08 21:02:47 +00:00
|
|
|
let mut last = Ok(Value::Nothing {
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
});
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
for stmt in &block.stmts {
|
2021-07-29 22:56:51 +00:00
|
|
|
if let Statement::Expression(expression) = stmt {
|
2021-08-15 22:33:34 +00:00
|
|
|
last = Ok(eval_expression(state, expression)?);
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
last
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|