nushell/src/evaluate/evaluator.rs

148 lines
4.9 KiB
Rust
Raw Normal View History

use crate::data::base::Block;
use crate::errors::{ArgumentError, Description};
2019-06-22 03:36:57 +02:00
use crate::parser::{
hir::{self, Expression, RawExpression},
2019-08-01 03:58:42 +02:00
CommandRegistry, Text,
2019-06-22 03:36:57 +02:00
};
2019-05-28 08:45:18 +02:00
use crate::prelude::*;
use derive_new::new;
2019-06-03 07:11:21 +02:00
use indexmap::IndexMap;
2019-05-28 08:45:18 +02:00
#[derive(new)]
2019-07-24 00:22:11 +02:00
pub struct Scope {
2019-08-01 03:58:42 +02:00
it: Tagged<Value>,
2019-06-03 07:11:21 +02:00
#[new(default)]
2019-08-01 03:58:42 +02:00
vars: IndexMap<String, Tagged<Value>>,
2019-05-28 08:45:18 +02:00
}
impl Scope {
pub(crate) fn empty() -> Scope {
2019-05-28 08:45:18 +02:00
Scope {
2019-08-01 03:58:42 +02:00
it: Value::nothing().tagged_unknown(),
2019-06-03 07:11:21 +02:00
vars: IndexMap::new(),
2019-05-28 08:45:18 +02:00
}
}
2019-07-24 00:22:11 +02:00
pub(crate) fn it_value(value: Tagged<Value>) -> Scope {
2019-07-24 00:22:11 +02:00
Scope {
it: value,
vars: IndexMap::new(),
}
}
2019-05-28 08:45:18 +02:00
}
pub(crate) fn evaluate_baseline_expr(
2019-06-22 03:36:57 +02:00
expr: &Expression,
2019-07-24 00:22:11 +02:00
registry: &CommandRegistry,
2019-06-08 00:35:07 +02:00
scope: &Scope,
2019-06-22 22:46:16 +02:00
source: &Text,
2019-08-01 03:58:42 +02:00
) -> Result<Tagged<Value>, ShellError> {
2019-06-22 03:36:57 +02:00
match &expr.item {
RawExpression::Literal(literal) => Ok(evaluate_literal(expr.copy_span(literal), source)),
RawExpression::ExternalWord => Err(ShellError::argument_error(
"Invalid external word",
ArgumentError::InvalidExternalWord,
expr.span(),
)),
RawExpression::FilePath(path) => Ok(Value::path(path.clone()).tagged(expr.span())),
2019-08-09 06:51:21 +02:00
RawExpression::Synthetic(hir::Synthetic::String(s)) => Ok(Value::string(s).tagged_unknown()),
2019-06-22 03:36:57 +02:00
RawExpression::Variable(var) => evaluate_reference(var, scope, source),
RawExpression::ExternalCommand(external) => evaluate_external(external, scope, source),
2019-06-22 03:36:57 +02:00
RawExpression::Binary(binary) => {
let left = evaluate_baseline_expr(binary.left(), registry, scope, source)?;
let right = evaluate_baseline_expr(binary.right(), registry, scope, source)?;
2019-05-28 08:45:18 +02:00
2019-06-22 03:36:57 +02:00
match left.compare(binary.op(), &*right) {
Ok(result) => Ok(Tagged::from_simple_spanned_item(
Value::boolean(result),
expr.span(),
)),
2019-07-09 06:31:26 +02:00
Err((left_type, right_type)) => Err(ShellError::coerce_error(
binary.left().copy_span(left_type),
binary.right().copy_span(right_type),
2019-07-09 06:31:26 +02:00
)),
2019-06-22 03:36:57 +02:00
}
}
2019-08-02 21:15:07 +02:00
RawExpression::List(list) => {
let mut exprs = vec![];
for expr in list {
let expr = evaluate_baseline_expr(expr, registry, scope, source)?;
exprs.push(expr);
}
Ok(Value::Table(exprs).tagged(Tag::unknown_origin(expr.span())))
2019-08-02 21:15:07 +02:00
}
RawExpression::Block(block) => Ok(Tagged::from_simple_spanned_item(
Value::Block(Block::new(block.clone(), source.clone(), expr.span())),
expr.span(),
)),
2019-06-22 03:36:57 +02:00
RawExpression::Path(path) => {
let value = evaluate_baseline_expr(path.head(), registry, scope, source)?;
2019-06-24 02:55:31 +02:00
let mut item = value;
2019-05-28 08:45:18 +02:00
2019-06-22 03:36:57 +02:00
for name in path.tail() {
2019-06-24 02:55:31 +02:00
let next = item.get_data_by_key(name);
2019-05-28 08:45:18 +02:00
2019-06-22 03:36:57 +02:00
match next {
2019-06-24 02:55:31 +02:00
None => {
2019-07-09 06:31:26 +02:00
return Err(ShellError::missing_property(
2019-08-01 03:58:42 +02:00
Description::from(item.tagged_type_name()),
2019-07-09 06:31:26 +02:00
Description::from(name.clone()),
))
2019-06-24 02:55:31 +02:00
}
Some(next) => {
item = Tagged::from_simple_spanned_item(
next.clone().item,
(expr.span().start, name.span().end),
)
2019-06-24 02:55:31 +02:00
}
2019-06-22 03:36:57 +02:00
};
}
2019-05-28 08:45:18 +02:00
Ok(Tagged::from_simple_spanned_item(
item.item().clone(),
expr.span(),
))
2019-06-22 03:36:57 +02:00
}
RawExpression::Boolean(_boolean) => unimplemented!(),
2019-05-28 08:45:18 +02:00
}
}
fn evaluate_literal(literal: Tagged<&hir::Literal>, source: &Text) -> Tagged<Value> {
2019-06-22 03:36:57 +02:00
let result = match literal.item {
hir::Literal::Number(int) => int.into(),
2019-06-24 09:59:23 +02:00
hir::Literal::Size(int, unit) => unit.compute(int),
hir::Literal::String(span) => Value::string(span.slice(source)),
hir::Literal::GlobPattern => Value::pattern(literal.span().slice(source)),
hir::Literal::Bare => Value::string(literal.span().slice(source)),
2019-06-22 03:36:57 +02:00
};
2019-05-28 08:45:18 +02:00
2019-06-22 03:36:57 +02:00
literal.map(|_| result)
2019-05-28 08:45:18 +02:00
}
2019-06-22 03:36:57 +02:00
fn evaluate_reference(
name: &hir::Variable,
scope: &Scope,
2019-06-22 22:46:16 +02:00
source: &Text,
2019-08-01 03:58:42 +02:00
) -> Result<Tagged<Value>, ShellError> {
2019-06-22 03:36:57 +02:00
match name {
hir::Variable::It(span) => Ok(scope.it.item.clone().simple_spanned(span)),
hir::Variable::Other(span) => Ok(scope
2019-07-08 18:44:53 +02:00
.vars
.get(span.slice(source))
2019-07-08 18:44:53 +02:00
.map(|v| v.clone())
.unwrap_or_else(|| Value::nothing().simple_spanned(span))),
2019-05-28 08:45:18 +02:00
}
}
fn evaluate_external(
external: &hir::ExternalCommand,
_scope: &Scope,
_source: &Text,
) -> Result<Tagged<Value>, ShellError> {
Err(ShellError::syntax_error(
"Unexpected external command".tagged(external.name()),
))
}