Implemented negation for most types

This commit is contained in:
Erin 2021-08-30 23:19:25 +02:00 committed by ondra05
parent 82239be160
commit 14e9eb5c6b
2 changed files with 25 additions and 3 deletions

View file

@ -133,7 +133,7 @@ pub enum Token {
String(String), String(String),
/// Integer /// Integer
#[regex(r"[0-9]+", get_int)] #[regex(r"-?[0-9]+", get_int)]
Integer(i32), Integer(i32),
/// A C-complaint identifier /// A C-complaint identifier

View file

@ -422,7 +422,23 @@ impl ops::Not for Value {
type Output = Value; type Output = Value;
fn not(self) -> Self::Output { fn not(self) -> Self::Output {
todo!() match self {
Value::Nul => Value::Nul,
Value::Str(s) => Value::Str(s.chars().rev().collect()),
Value::Int(i) => Value::Int(i.swap_bytes()),
Value::Bool(b) => Value::Bool(!b),
Value::Abool(a) => Value::Abool(match a {
Abool::Never => Abool::Always,
Abool::Sometimes => Abool::Sometimes,
Abool::Always => Abool::Never,
}),
Value::Functio(_) => todo!(),
Value::Cart(c) => Value::Cart(
c.into_iter()
.map(|(k, v)| (v.borrow().clone(), Rc::new(RefCell::new(k))))
.collect(),
),
}
} }
} }
@ -431,7 +447,13 @@ impl PartialEq for Value {
let other = other.clone(); let other = other.clone();
match self { match self {
Value::Nul => other == Value::Nul, Value::Nul => {
if let Value::Nul = other {
true
} else {
false
}
}
Value::Str(s) => *s == other.to_string(), Value::Str(s) => *s == other.to_string(),
Value::Int(i) => *i == other.into_i32(), Value::Int(i) => *i == other.into_i32(),
Value::Bool(b) => *b == other.into_bool(), Value::Bool(b) => *b == other.into_bool(),