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-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)]
|
|
|
|
pub struct ValueStream(Rc<RefCell<dyn Iterator<Item = Value>>>);
|
|
|
|
|
|
|
|
impl ValueStream {
|
|
|
|
pub fn into_string(self) -> String {
|
|
|
|
let val: Vec<Value> = self.collect();
|
|
|
|
format!(
|
|
|
|
"[{}]",
|
|
|
|
val.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ".into())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoValueStream for Vec<Value> {
|
|
|
|
fn into_value_stream(self) -> ValueStream {
|
|
|
|
ValueStream(Rc::new(RefCell::new(self.into_iter())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RowStream(Rc<RefCell<dyn Iterator<Item = Vec<Value>>>>);
|
|
|
|
|
|
|
|
impl RowStream {
|
|
|
|
pub fn into_string(self, headers: Vec<String>) -> String {
|
|
|
|
let val: Vec<Vec<Value>> = self.collect();
|
|
|
|
format!(
|
|
|
|
"[{}]\n[{}]",
|
|
|
|
headers
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ".into()),
|
|
|
|
val.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
x.into_iter()
|
|
|
|
.map(|x| x.into_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ".into())
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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,
|
|
|
|
},
|
|
|
|
Float {
|
|
|
|
val: f64,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
String {
|
|
|
|
val: String,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
List {
|
2021-09-01 21:20:53 +00:00
|
|
|
val: ValueStream,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Table {
|
|
|
|
headers: Vec<String>,
|
2021-09-01 21:20:53 +00:00
|
|
|
val: RowStream,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Block {
|
|
|
|
val: BlockId,
|
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Nothing {
|
|
|
|
span: Span,
|
|
|
|
},
|
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,
|
|
|
|
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,
|
|
|
|
Value::Nothing { span, .. } => *span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Value::String { span, .. } => *span = new_span,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_type(&self) -> Type {
|
|
|
|
match self {
|
|
|
|
Value::Bool { .. } => Type::Bool,
|
|
|
|
Value::Int { .. } => Type::Int,
|
|
|
|
Value::Float { .. } => Type::Float,
|
|
|
|
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-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(),
|
|
|
|
Value::String { val, .. } => val,
|
|
|
|
Value::List { val, .. } => val.into_string(),
|
|
|
|
Value::Table { headers, val, .. } => val.into_string(headers),
|
|
|
|
Value::Block { val, .. } => format!("<Block {}>", val),
|
2021-09-02 01:29:43 +00:00
|
|
|
Value::Nothing { .. } => String::new(),
|
2021-09-01 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
}),
|
|
|
|
_ => 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,
|
|
|
|
}),
|
|
|
|
_ => 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
|
|
|
}
|