make ++ append lists (#6766)

* make `++` append lists

* fmt

* fix for database
This commit is contained in:
pwygab
2022-10-20 18:28:18 +08:00
committed by GitHub
parent 50e53e788a
commit 5e748ae8fc
8 changed files with 104 additions and 2 deletions

View File

@ -47,7 +47,8 @@ impl Expression {
| Operator::Equal
| Operator::NotEqual
| Operator::In
| Operator::NotIn => 80,
| Operator::NotIn
| Operator::Append => 80,
Operator::BitAnd => 75,
Operator::BitXor => 70,
Operator::BitOr => 60,

View File

@ -14,6 +14,7 @@ pub enum Operator {
RegexMatch,
NotRegexMatch,
Plus,
Append,
Minus,
Multiply,
Divide,
@ -43,6 +44,7 @@ impl Display for Operator {
Operator::RegexMatch => write!(f, "=~"),
Operator::NotRegexMatch => write!(f, "!~"),
Operator::Plus => write!(f, "+"),
Operator::Append => write!(f, "++"),
Operator::Minus => write!(f, "-"),
Operator::Multiply => write!(f, "*"),
Operator::Divide => write!(f, "/"),

View File

@ -1681,6 +1681,34 @@ impl Value {
}
}
pub fn append(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) {
(Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => {
let mut lhs = lhs.clone();
let mut rhs = rhs.clone();
lhs.append(&mut rhs);
Ok(Value::List { vals: lhs, span })
}
(Value::List { vals: lhs, .. }, val) => {
let mut lhs = lhs.clone();
lhs.push(val.clone());
Ok(Value::List { vals: lhs, span })
}
(val, Value::List { vals: rhs, .. }) => {
let mut rhs = rhs.clone();
rhs.insert(0, val.clone());
Ok(Value::List { vals: rhs, span })
}
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),
lhs_span: self.span()?,
rhs_ty: rhs.get_type(),
rhs_span: rhs.span()?,
}),
}
}
pub fn sub(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) {
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {