mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:54:57 +02:00
Merge master
This commit is contained in:
@ -2,7 +2,7 @@ use crate::errors::Description;
|
||||
use crate::object::base::Block;
|
||||
use crate::parser::{
|
||||
hir::{self, Expression, RawExpression},
|
||||
CommandRegistry, Spanned, Text,
|
||||
CommandRegistry, Text,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use derive_new::new;
|
||||
@ -10,20 +10,20 @@ use indexmap::IndexMap;
|
||||
|
||||
#[derive(new)]
|
||||
pub struct Scope {
|
||||
it: Spanned<Value>,
|
||||
it: Tagged<Value>,
|
||||
#[new(default)]
|
||||
vars: IndexMap<String, Spanned<Value>>,
|
||||
vars: IndexMap<String, Tagged<Value>>,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
crate fn empty() -> Scope {
|
||||
Scope {
|
||||
it: Value::nothing().spanned_unknown(),
|
||||
it: Value::nothing().tagged_unknown(),
|
||||
vars: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
crate fn it_value(value: Spanned<Value>) -> Scope {
|
||||
crate fn it_value(value: Tagged<Value>) -> Scope {
|
||||
Scope {
|
||||
it: value,
|
||||
vars: IndexMap::new(),
|
||||
@ -36,19 +36,20 @@ crate fn evaluate_baseline_expr(
|
||||
registry: &CommandRegistry,
|
||||
scope: &Scope,
|
||||
source: &Text,
|
||||
) -> Result<Spanned<Value>, ShellError> {
|
||||
) -> Result<Tagged<Value>, ShellError> {
|
||||
match &expr.item {
|
||||
RawExpression::Literal(literal) => Ok(evaluate_literal(expr.copy_span(*literal), source)),
|
||||
RawExpression::Synthetic(hir::Synthetic::String(s)) => {
|
||||
Ok(Value::string(s).spanned_unknown())
|
||||
}
|
||||
RawExpression::Synthetic(hir::Synthetic::String(s)) => Ok(Value::string(s).tagged_unknown()),
|
||||
RawExpression::Variable(var) => evaluate_reference(var, scope, source),
|
||||
RawExpression::Binary(binary) => {
|
||||
let left = evaluate_baseline_expr(binary.left(), registry, scope, source)?;
|
||||
let right = evaluate_baseline_expr(binary.right(), registry, scope, source)?;
|
||||
|
||||
match left.compare(binary.op(), &*right) {
|
||||
Ok(result) => Ok(Spanned::from_item(Value::boolean(result), *expr.span())),
|
||||
Ok(result) => Ok(Tagged::from_simple_spanned_item(
|
||||
Value::boolean(result),
|
||||
expr.span(),
|
||||
)),
|
||||
Err((left_type, right_type)) => Err(ShellError::coerce_error(
|
||||
binary.left().copy_span(left_type),
|
||||
binary.right().copy_span(right_type),
|
||||
@ -63,10 +64,10 @@ crate fn evaluate_baseline_expr(
|
||||
exprs.push(expr);
|
||||
}
|
||||
|
||||
Ok(Value::List(exprs).spanned(expr.span()))
|
||||
Ok(Value::List(exprs).tagged(Tag::unknown_origin(expr.span())))
|
||||
}
|
||||
RawExpression::Block(block) => Ok(Spanned::from_item(
|
||||
Value::Block(Block::new(block.clone(), source.clone(), *expr.span())),
|
||||
RawExpression::Block(block) => Ok(Tagged::from_simple_spanned_item(
|
||||
Value::Block(Block::new(block.clone(), source.clone(), expr.span())),
|
||||
expr.span(),
|
||||
)),
|
||||
RawExpression::Path(path) => {
|
||||
@ -79,12 +80,12 @@ crate fn evaluate_baseline_expr(
|
||||
match next {
|
||||
None => {
|
||||
return Err(ShellError::missing_property(
|
||||
Description::from(item.spanned_type_name()),
|
||||
Description::from(item.tagged_type_name()),
|
||||
Description::from(name.clone()),
|
||||
))
|
||||
}
|
||||
Some(next) => {
|
||||
item = Spanned::from_item(
|
||||
item = Tagged::from_simple_spanned_item(
|
||||
next.clone().item,
|
||||
(expr.span().start, name.span().end),
|
||||
)
|
||||
@ -92,13 +93,16 @@ crate fn evaluate_baseline_expr(
|
||||
};
|
||||
}
|
||||
|
||||
Ok(Spanned::from_item(item.item().clone(), expr.span()))
|
||||
Ok(Tagged::from_simple_spanned_item(
|
||||
item.item().clone(),
|
||||
expr.span(),
|
||||
))
|
||||
}
|
||||
RawExpression::Boolean(_boolean) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn evaluate_literal(literal: Spanned<hir::Literal>, source: &Text) -> Spanned<Value> {
|
||||
fn evaluate_literal(literal: Tagged<hir::Literal>, source: &Text) -> Tagged<Value> {
|
||||
let result = match literal.item {
|
||||
hir::Literal::Integer(int) => Value::int(int),
|
||||
hir::Literal::Size(int, unit) => unit.compute(int),
|
||||
@ -113,13 +117,13 @@ fn evaluate_reference(
|
||||
name: &hir::Variable,
|
||||
scope: &Scope,
|
||||
source: &Text,
|
||||
) -> Result<Spanned<Value>, ShellError> {
|
||||
) -> Result<Tagged<Value>, ShellError> {
|
||||
match name {
|
||||
hir::Variable::It(span) => Ok(Spanned::from_item(scope.it.item.clone(), span)),
|
||||
hir::Variable::It(span) => Ok(scope.it.item.clone().simple_spanned(span)),
|
||||
hir::Variable::Other(span) => Ok(scope
|
||||
.vars
|
||||
.get(span.slice(source))
|
||||
.map(|v| v.clone())
|
||||
.unwrap_or_else(|| Value::nothing().spanned(span))),
|
||||
.unwrap_or_else(|| Value::nothing().simple_spanned(span))),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user