Add starts with operator (#5061)

* add starts_with operator

* added a test
This commit is contained in:
Darren Schroeder
2022-04-01 13:35:46 -05:00
committed by GitHub
parent a088081695
commit 2cb815b7b4
8 changed files with 68 additions and 0 deletions

View File

@ -3940,6 +3940,7 @@ pub fn parse_operator(
b">" => Operator::GreaterThan,
b">=" => Operator::GreaterThanOrEqual,
b"=~" => Operator::Contains,
b"=^" => Operator::StartsWith,
b"!~" => Operator::NotContains,
b"+" => Operator::Plus,
b"-" => Operator::Minus,

View File

@ -319,6 +319,24 @@ pub fn math_result_type(
)
}
},
Operator::StartsWith => match (&lhs.ty, &rhs.ty) {
(Type::String, Type::String) => (Type::Bool, None),
(Type::Unknown, _) => (Type::Bool, None),
(_, Type::Unknown) => (Type::Bool, None),
_ => {
*op = Expression::garbage(op.span);
(
Type::Unknown,
Some(ParseError::UnsupportedOperation(
op.span,
lhs.span,
lhs.ty.clone(),
rhs.span,
rhs.ty.clone(),
)),
)
}
},
Operator::In => match (&lhs.ty, &rhs.ty) {
(t, Type::List(u)) if type_compatible(t, u) => (Type::Bool, None),
(Type::Int | Type::Float, Type::Range) => (Type::Bool, None),