2021-09-01 21:20:53 +00:00
|
|
|
use std::{cell::RefCell, fmt::Debug, rc::Rc};
|
2021-08-15 22:33:34 +00:00
|
|
|
|
2021-09-04 21:52:57 +00:00
|
|
|
use crate::ast::RangeInclusion;
|
2021-09-02 01:29:43 +00:00
|
|
|
use crate::{span, BlockId, Span, Type};
|
2021-08-15 22:33:34 +00:00
|
|
|
|
|
|
|
use crate::ShellError;
|
|
|
|
|
2021-09-01 21:20:53 +00:00
|
|
|
#[derive(Clone)]
|
2021-09-03 02:57:18 +00:00
|
|
|
pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
|
2021-09-01 21:20:53 +00:00
|
|
|
|
|
|
|
impl ValueStream {
|
|
|
|
pub fn into_string(self) -> String {
|
|
|
|
format!(
|
|
|
|
"[{}]",
|
2021-09-04 07:59:38 +00:00
|
|
|
self.map(|x| x.into_string())
|
2021-09-01 21:20:53 +00:00
|
|
|
.collect::<Vec<String>>()
|
2021-09-04 07:59:38 +00:00
|
|
|
.join(", ")
|
2021-09-01 21:20:53 +00:00
|
|
|
)
|
|
|
|
}
|
2021-09-03 02:57:18 +00:00
|
|
|
|
|
|
|
pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream {
|
|
|
|
ValueStream(Rc::new(RefCell::new(input)))
|
|
|
|
}
|
2021-09-01 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for ValueStream {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("ValueStream").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for ValueStream {
|
|
|
|
type Item = Value;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
{
|
|
|
|
let mut iter = self.0.borrow_mut();
|
|
|
|
iter.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoValueStream {
|
|
|
|
fn into_value_stream(self) -> ValueStream;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:57:18 +00:00
|
|
|
impl<T> IntoValueStream for T
|
|
|
|
where
|
|
|
|
T: Iterator<Item = Value> + 'static,
|
|
|
|
{
|
2021-09-01 21:20:53 +00:00
|
|
|
fn into_value_stream(self) -> ValueStream {
|
2021-09-03 02:57:18 +00:00
|
|
|
ValueStream::from_stream(self)
|
2021-09-01 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RowStream(Rc<RefCell<dyn Iterator<Item = Vec<Value>>>>);
|
|
|
|
|
|
|
|
impl RowStream {
|
|
|
|
pub fn into_string(self, headers: Vec<String>) -> String {
|
|
|
|
format!(
|
|
|
|
"[{}]\n[{}]",
|
|
|
|
headers
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
2021-09-04 07:59:38 +00:00
|
|
|
.join(", "),
|
|
|
|
self.map(|x| {
|
|
|
|
x.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n")
|
2021-09-01 21:20:53 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for RowStream {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("ValueStream").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for RowStream {
|
|
|
|
type Item = Vec<Value>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
{
|
|
|
|
let mut iter = self.0.borrow_mut();
|
|
|
|
iter.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoRowStream {
|
|
|
|
fn into_row_stream(self) -> RowStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoRowStream for Vec<Vec<Value>> {
|
|
|
|
fn into_row_stream(self) -> RowStream {
|
|
|
|
RowStream(Rc::new(RefCell::new(self.into_iter())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 21:52:57 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct Range {
|
|
|
|
pub from: Value,
|
|
|
|
pub to: Value,
|
|
|
|
pub inclusion: RangeInclusion,
|
|
|
|
}
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Value {
|
2021-08-28 19:17:30 +00:00
|
|
|
Bool {
|
|
|
|
val: bool,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Int {
|
|
|
|
val: i64,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-04 21:52:57 +00:00
|
|
|
Range {
|
|
|
|
val: Box<Range>,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-08-28 19:17:30 +00:00
|
|
|
Float {
|
|
|
|
val: f64,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
String {
|
|
|
|
val: String,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-04 06:52:28 +00:00
|
|
|
ValueStream {
|
|
|
|
stream: ValueStream,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
RowStream {
|
|
|
|
headers: Vec<String>,
|
|
|
|
stream: RowStream,
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-08-28 19:17:30 +00:00
|
|
|
List {
|
2021-09-04 06:52:28 +00:00
|
|
|
val: Vec<Value>,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Table {
|
|
|
|
headers: Vec<String>,
|
2021-09-04 06:52:28 +00:00
|
|
|
val: Vec<Vec<Value>>,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Block {
|
|
|
|
val: BlockId,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Nothing {
|
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-05 23:16:27 +00:00
|
|
|
Error {
|
|
|
|
err: ShellError,
|
|
|
|
},
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn as_string(&self) -> Result<String, ShellError> {
|
|
|
|
match self {
|
|
|
|
Value::String { val, .. } => Ok(val.to_string()),
|
|
|
|
_ => Err(ShellError::CantConvert("string".into(), self.span())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
|
|
|
Value::Bool { span, .. } => *span,
|
|
|
|
Value::Int { span, .. } => *span,
|
|
|
|
Value::Float { span, .. } => *span,
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::String { span, .. } => *span,
|
|
|
|
Value::List { span, .. } => *span,
|
2021-08-28 19:17:30 +00:00
|
|
|
Value::Table { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Block { span, .. } => *span,
|
2021-09-04 06:52:28 +00:00
|
|
|
Value::RowStream { span, .. } => *span,
|
|
|
|
Value::ValueStream { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Nothing { span, .. } => *span,
|
2021-09-05 23:16:27 +00:00
|
|
|
Value::Error { .. } => Span::unknown(),
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_span(mut self, new_span: Span) -> Value {
|
|
|
|
match &mut self {
|
|
|
|
Value::Bool { span, .. } => *span = new_span,
|
|
|
|
Value::Int { span, .. } => *span = new_span,
|
|
|
|
Value::Float { span, .. } => *span = new_span,
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::String { span, .. } => *span = new_span,
|
2021-09-04 06:52:28 +00:00
|
|
|
Value::RowStream { span, .. } => *span = new_span,
|
|
|
|
Value::ValueStream { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { span, .. } => *span = new_span,
|
2021-08-28 19:17:30 +00:00
|
|
|
Value::Table { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Block { span, .. } => *span = new_span,
|
|
|
|
Value::Nothing { span, .. } => *span = new_span,
|
2021-09-05 23:16:27 +00:00
|
|
|
Value::Error { .. } => {}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_type(&self) -> Type {
|
|
|
|
match self {
|
|
|
|
Value::Bool { .. } => Type::Bool,
|
|
|
|
Value::Int { .. } => Type::Int,
|
|
|
|
Value::Float { .. } => Type::Float,
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { .. } => Type::Range,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::String { .. } => Type::String,
|
|
|
|
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
|
2021-08-28 19:17:30 +00:00
|
|
|
Value::Table { .. } => Type::Table, // FIXME
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Nothing { .. } => Type::Nothing,
|
|
|
|
Value::Block { .. } => Type::Block,
|
2021-09-04 06:52:28 +00:00
|
|
|
Value::ValueStream { .. } => Type::ValueStream,
|
|
|
|
Value::RowStream { .. } => Type::RowStream,
|
2021-09-05 23:16:27 +00:00
|
|
|
Value::Error { .. } => Type::Error,
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 21:20:53 +00:00
|
|
|
|
|
|
|
pub fn into_string(self) -> String {
|
|
|
|
match self {
|
|
|
|
Value::Bool { val, .. } => val.to_string(),
|
|
|
|
Value::Int { val, .. } => val.to_string(),
|
|
|
|
Value::Float { val, .. } => val.to_string(),
|
2021-09-04 21:52:57 +00:00
|
|
|
Value::Range { val, .. } => {
|
|
|
|
let vals: Vec<i64> = match (&val.from, &val.to) {
|
|
|
|
(Value::Int { val: from, .. }, Value::Int { val: to, .. }) => {
|
|
|
|
match val.inclusion {
|
|
|
|
RangeInclusion::Inclusive => (*from..=*to).collect(),
|
|
|
|
RangeInclusion::RightExclusive => (*from..*to).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
format!(
|
|
|
|
"range: [{}]",
|
|
|
|
vals.iter()
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
2021-09-04 22:40:15 +00:00
|
|
|
.join(", ")
|
2021-09-04 21:52:57 +00:00
|
|
|
)
|
2021-09-04 22:35:08 +00:00
|
|
|
}
|
2021-09-01 21:20:53 +00:00
|
|
|
Value::String { val, .. } => val,
|
2021-09-04 06:52:28 +00:00
|
|
|
Value::ValueStream { stream, .. } => stream.into_string(),
|
|
|
|
Value::List { val, .. } => val
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "),
|
|
|
|
Value::Table { val, .. } => val
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
x.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n"),
|
|
|
|
Value::RowStream {
|
|
|
|
headers, stream, ..
|
|
|
|
} => stream.into_string(headers),
|
2021-09-01 21:20:53 +00:00
|
|
|
Value::Block { val, .. } => format!("<Block {}>", val),
|
2021-09-02 01:29:43 +00:00
|
|
|
Value::Nothing { .. } => String::new(),
|
2021-09-05 23:16:27 +00:00
|
|
|
Value::Error { err } => format!("{:?}", err),
|
2021-09-01 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 02:15:01 +00:00
|
|
|
|
|
|
|
pub fn nothing() -> Value {
|
|
|
|
Value::Nothing {
|
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Block { val: b1, .. }, Value::Block { val: b2, .. }) => b1 == b2,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn add(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-15 22:33:34 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs + rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 + *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs + *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs + rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::String {
|
|
|
|
val: lhs.to_string() + 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(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
pub fn sub(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs - rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 - *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs - *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs - 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 mul(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs * rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 * *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs * *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs * 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 div(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0 {
|
|
|
|
Ok(Value::Int {
|
|
|
|
val: lhs / rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0.0 {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: *lhs as f64 / *rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0 {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: *lhs / *rhs as f64,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
|
|
|
if *rhs != 0.0 {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: lhs / rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(ShellError::DivisionByZero(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => 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 lt(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs < rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) < *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs < *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs < 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 lte(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs <= rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) <= *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs <= *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs <= 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 gt(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs > rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) > *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs > *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs > 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 gte(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs >= rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) >= *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs >= *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs >= 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 eq(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) == *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs == *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-09-04 06:52:28 +00:00
|
|
|
(Value::List { val: lhs, .. }, Value::List { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(
|
|
|
|
Value::Table {
|
|
|
|
val: lhs,
|
|
|
|
headers: lhs_headers,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
Value::Table {
|
|
|
|
val: rhs,
|
|
|
|
headers: rhs_headers,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) => Ok(Value::Bool {
|
|
|
|
val: lhs_headers == rhs_headers && lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => 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 ne(&self, op: Span, rhs: &Value) -> Result<Value, ShellError> {
|
2021-09-02 01:29:43 +00:00
|
|
|
let span = span(&[self.span(), rhs.span()]);
|
2021-08-25 19:29:36 +00:00
|
|
|
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: (*lhs as f64) != *rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: *lhs != *rhs as f64,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
// FIXME: these should consider machine epsilon
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
2021-09-04 06:52:28 +00:00
|
|
|
(Value::List { val: lhs, .. }, Value::List { val: rhs, .. }) => Ok(Value::Bool {
|
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(
|
|
|
|
Value::Table {
|
|
|
|
val: lhs,
|
|
|
|
headers: lhs_headers,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
Value::Table {
|
|
|
|
val: rhs,
|
|
|
|
headers: rhs_headers,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) => Ok(Value::Bool {
|
|
|
|
val: lhs_headers != rhs_headers || lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
|
2021-08-25 19:29:36 +00:00
|
|
|
_ => Err(ShellError::OperatorMismatch {
|
|
|
|
op_span: op,
|
|
|
|
lhs_ty: self.get_type(),
|
|
|
|
lhs_span: self.span(),
|
|
|
|
rhs_ty: rhs.get_type(),
|
|
|
|
rhs_span: rhs.span(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|