2021-09-08 02:26:57 +00:00
|
|
|
mod range;
|
|
|
|
mod row;
|
|
|
|
mod stream;
|
|
|
|
|
|
|
|
pub use range::*;
|
|
|
|
pub use row::*;
|
|
|
|
pub use stream::*;
|
|
|
|
|
2021-09-08 02:32:28 +00:00
|
|
|
use std::fmt::Debug;
|
2021-08-15 22:33:34 +00:00
|
|
|
|
2021-09-06 22:02:24 +00:00
|
|
|
use crate::ast::{PathMember, 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-08 02:26:57 +00:00
|
|
|
/// Core structured values that pass through the pipeline in engine-q
|
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-07 07:07:11 +00:00
|
|
|
Record {
|
|
|
|
cols: Vec<String>,
|
|
|
|
vals: Vec<Value>,
|
2021-09-04 06:52:28 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
2021-09-07 07:35:59 +00:00
|
|
|
Stream {
|
2021-09-07 07:07:11 +00:00
|
|
|
stream: ValueStream,
|
2021-09-04 06:52:28 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
2021-08-28 19:17:30 +00:00
|
|
|
List {
|
2021-09-07 07:07:11 +00:00
|
|
|
vals: 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 {
|
2021-09-06 04:07:48 +00:00
|
|
|
error: ShellError,
|
2021-09-05 23:16:27 +00:00
|
|
|
},
|
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())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Get the span for the current value
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
2021-09-08 02:26:57 +00:00
|
|
|
Value::Error { .. } => Span::unknown(),
|
2021-08-15 22:33:34 +00:00
|
|
|
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,
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { span, .. } => *span,
|
|
|
|
Value::Block { span, .. } => *span,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { span, .. } => *span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::Nothing { span, .. } => *span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Update the value with a new span
|
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-07 07:07:11 +00:00
|
|
|
Value::Record { span, .. } => *span = new_span,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { span, .. } => *span = new_span,
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { span, .. } => *span = new_span,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Get the type of the current Value
|
2021-08-15 22:33:34 +00:00
|
|
|
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,
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { cols, vals, .. } => {
|
|
|
|
Type::Record(cols.clone(), vals.iter().map(|x| x.get_type()).collect())
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
|
|
|
|
Value::Nothing { .. } => Type::Nothing,
|
|
|
|
Value::Block { .. } => Type::Block,
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { .. } => Type::ValueStream,
|
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
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Convert Value into string. Note that Streams will be consumed.
|
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-07 07:35:59 +00:00
|
|
|
Value::Stream { stream, .. } => stream.into_string(),
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals: val, .. } => format!(
|
2021-09-06 22:02:24 +00:00
|
|
|
"[{}]",
|
|
|
|
val.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { cols, vals, .. } => format!(
|
|
|
|
"{{{}}}",
|
|
|
|
cols.iter()
|
|
|
|
.zip(vals.iter())
|
|
|
|
.map(|(x, y)| format!("{}: {}", x, y.clone().into_string()))
|
2021-09-06 22:02:24 +00:00
|
|
|
.collect::<Vec<_>>()
|
2021-09-07 07:07:11 +00:00
|
|
|
.join(", ")
|
2021-09-06 22:02:24 +00:00
|
|
|
),
|
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-06 04:07:48 +00:00
|
|
|
Value::Error { error } => format!("{:?}", error),
|
2021-09-01 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-03 02:15:01 +00:00
|
|
|
|
2021-09-08 02:26:57 +00:00
|
|
|
/// Create a new `Nothing` value
|
2021-09-03 02:15:01 +00:00
|
|
|
pub fn nothing() -> Value {
|
|
|
|
Value::Nothing {
|
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 22:02:24 +00:00
|
|
|
|
2021-09-07 07:07:11 +00:00
|
|
|
pub fn follow_cell_path(self, column_path: &[PathMember]) -> Result<Value, ShellError> {
|
2021-09-06 22:02:24 +00:00
|
|
|
let mut current = self;
|
|
|
|
for member in column_path {
|
|
|
|
// FIXME: this uses a few extra clones for simplicity, but there may be a way
|
|
|
|
// to traverse the path without them
|
|
|
|
match member {
|
|
|
|
PathMember::Int {
|
|
|
|
val: count,
|
|
|
|
span: origin_span,
|
|
|
|
} => {
|
|
|
|
// Treat a numeric path member as `nth <val>`
|
|
|
|
match &mut current {
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals: val, .. } => {
|
2021-09-06 22:02:24 +00:00
|
|
|
if let Some(item) = val.get(*count) {
|
|
|
|
current = item.clone();
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::AccessBeyondEnd(val.len(), *origin_span));
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 07:35:59 +00:00
|
|
|
Value::Stream { stream, .. } => {
|
2021-09-06 22:02:24 +00:00
|
|
|
if let Some(item) = stream.nth(*count) {
|
|
|
|
current = item;
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::AccessBeyondEndOfStream(*origin_span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => {
|
|
|
|
return Err(ShellError::IncompatiblePathAccess(
|
|
|
|
format!("{}", x.get_type()),
|
|
|
|
*origin_span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PathMember::String {
|
2021-09-07 07:07:11 +00:00
|
|
|
val: column_name,
|
2021-09-06 22:02:24 +00:00
|
|
|
span: origin_span,
|
|
|
|
} => match &mut current {
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record { cols, vals, .. } => {
|
2021-09-06 22:02:24 +00:00
|
|
|
let mut found = false;
|
2021-09-07 07:07:11 +00:00
|
|
|
for col in cols.iter().zip(vals.iter()) {
|
|
|
|
if col.0 == column_name {
|
|
|
|
current = col.1.clone();
|
2021-09-06 22:02:24 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return Err(ShellError::CantFindColumn(*origin_span));
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::List { vals, span } => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for val in vals {
|
|
|
|
if let Value::Record { cols, vals, .. } = val {
|
|
|
|
for col in cols.iter().enumerate() {
|
|
|
|
if col.1 == column_name {
|
|
|
|
output.push(vals[col.0].clone());
|
|
|
|
}
|
2021-09-07 07:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current = Value::List {
|
|
|
|
vals: output,
|
|
|
|
span: *span,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Value::Stream { stream, span } => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for val in stream {
|
|
|
|
if let Value::Record { cols, vals, .. } = val {
|
|
|
|
for col in cols.iter().enumerate() {
|
|
|
|
if col.1 == column_name {
|
|
|
|
output.push(vals[col.0].clone());
|
|
|
|
}
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 07:07:11 +00:00
|
|
|
current = Value::List {
|
|
|
|
vals: output,
|
|
|
|
span: *span,
|
|
|
|
};
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
|
|
|
x => {
|
|
|
|
return Err(ShellError::IncompatiblePathAccess(
|
|
|
|
format!("{}", x.get_type()),
|
|
|
|
*origin_span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(current)
|
|
|
|
}
|
2021-09-09 21:47:20 +00:00
|
|
|
|
2021-09-10 01:06:44 +00:00
|
|
|
pub fn string(s: &str, span: Span) -> Value {
|
|
|
|
Value::String {
|
|
|
|
val: s.into(),
|
|
|
|
span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:47:20 +00:00
|
|
|
pub fn is_true(&self) -> bool {
|
|
|
|
matches!(self, Value::Bool { val: true, .. })
|
|
|
|
}
|
2021-09-10 02:27:12 +00:00
|
|
|
|
|
|
|
pub fn columns(&self) -> Vec<String> {
|
|
|
|
match self {
|
|
|
|
Value::Record { cols, .. } => cols.clone(),
|
|
|
|
_ => vec![],
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2021-09-06 05:35:58 +00:00
|
|
|
if lhs % rhs == 0 {
|
|
|
|
Ok(Value::Int {
|
|
|
|
val: lhs / rhs,
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(Value::Float {
|
|
|
|
val: (*lhs as f64) / (*rhs as f64),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
2021-08-25 19:29:36 +00:00
|
|
|
} 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-07 07:07:11 +00:00
|
|
|
(Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => Ok(Value::Bool {
|
2021-09-04 06:52:28 +00:00
|
|
|
val: lhs == rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: lhs,
|
|
|
|
cols: lhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: rhs,
|
|
|
|
cols: rhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
) => 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-07 07:07:11 +00:00
|
|
|
(Value::List { vals: lhs, .. }, Value::List { vals: rhs, .. }) => Ok(Value::Bool {
|
2021-09-04 06:52:28 +00:00
|
|
|
val: lhs != rhs,
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
(
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: lhs,
|
|
|
|
cols: lhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
2021-09-07 07:07:11 +00:00
|
|
|
Value::Record {
|
|
|
|
vals: rhs,
|
|
|
|
cols: rhs_headers,
|
2021-09-04 06:52:28 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
) => 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
|
|
|
}
|