Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaris13 committed Dec 8, 2024
1 parent 1bb316f commit d0ed623
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
86 changes: 43 additions & 43 deletions src/expression_literal_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,6 @@ pub enum LiteralValue {
},
}

impl PartialEq for LiteralValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::IntValue(a), Self::IntValue(b)) => a == b,
(Self::FValue(a), Self::FValue(b)) => (a - b).abs() < f64::EPSILON,
(Self::StringValue(a), Self::StringValue(b)) => a == b,
(Self::True, Self::True) => true,
(Self::False, Self::False) => true,
(Self::Nil, Self::Nil) => true,
(
Self::Callable {
name: a_name,
arity: a_arity,
fun: _,
},
Self::Callable {
name: b_name,
arity: b_arity,
fun: _,
},
) => a_name == b_name && a_arity == b_arity,
_ => false,
}
}
}

impl fmt::Debug for LiteralValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IntValue(i) => write!(f, "{}", i),
Self::FValue(fl) => write!(f, "{}", fl),
Self::StringValue(s) => write!(f, "\"{}\"", s),
Self::True => write!(f, "true"),
Self::False => write!(f, "false"),
Self::Nil => write!(f, "nil"),
Self::Callable { name, arity, .. } => {
write!(f, "Callable {{ name: {}, arity: {} }}", name, arity)
}
}
}
}

impl From<Token> for LiteralValue {
fn from(token: Token) -> Self {
match token.token_type {
Expand Down Expand Up @@ -118,7 +76,7 @@ impl From<LiteralValue> for bool {
}

impl Display for LiteralValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let str = match self {
IntValue(integer) => integer.to_string(),
FValue(float) => float.to_string(),
Expand All @@ -136,6 +94,48 @@ impl Display for LiteralValue {
}
}

impl PartialEq for LiteralValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(IntValue(a), IntValue(b)) => a == b,
(FValue(a), FValue(b)) => (a - b).abs() < f64::EPSILON,
(StringValue(a), StringValue(b)) => a == b,
(True, True) => true,
(False, False) => true,
(Nil, Nil) => true,
(
Callable {
name: a_name,
arity: a_arity,
fun: _,
},
Callable {
name: b_name,
arity: b_arity,
fun: _,
},
) => a_name == b_name && a_arity == b_arity,
_ => false,
}
}
}

impl fmt::Debug for LiteralValue {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
IntValue(i) => write!(f, "{}", i),
FValue(fl) => write!(f, "{}", fl),
StringValue(s) => write!(f, "\"{}\"", s),
True => write!(f, "true"),
False => write!(f, "false"),
Nil => write!(f, "nil"),
Callable { name, arity, .. } => {
write!(f, "Callable {{ name: {}, arity: {} }}", name, arity)
}
}
}
}

impl LiteralValue {
pub fn to_type(&self) -> &str {
match self {
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl Parser {
if !self.check(RightParen) {
loop {
if arguments.len() >= 255 {
return Err(format!("Cant't have more than 255 arguments"));
return Err(String::from("Can't have more than 255 arguments"));
}

arguments.push(self.expression()?);
Expand All @@ -399,7 +399,7 @@ impl Parser {

let right_paren = self.consume(RightParen, "Expected ')' after arguments.")?;

Ok(Expression::Call {
Ok(Call {
callee: Box::new(callee),
paren: right_paren,
arguments: arguments,
Expand Down

0 comments on commit d0ed623

Please sign in to comment.