Allow operator in constants (#10212)

This pr fixes https://github.com/nushell/nushell/issues/10200

# Description

Allow unary and binary operators in constants, e.g.

```bash
const a = 1 + 2
const b = [0, 1, 2, 3] ++ [4]
```

# User-Facing Changes

Now constants can contain operators.

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

None

---------

Co-authored-by: Horasal <horsal@horsal.dev>
This commit is contained in:
Horasal
2023-09-05 23:35:58 +09:00
committed by GitHub
parent 7a728340de
commit 54394fe9af
5 changed files with 199 additions and 19 deletions

View File

@ -1,8 +1,10 @@
use crate::Span;
use crate::{ShellError, Span};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use super::{Expr, Expression};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Comparison {
Equal,
@ -128,3 +130,16 @@ impl Display for RangeOperator {
}
}
}
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
match op {
Expression {
expr: Expr::Operator(operator),
..
} => Ok(operator.clone()),
Expression { span, expr, .. } => Err(ShellError::UnknownOperator {
op_token: format!("{expr:?}"),
span: *span,
}),
}
}