Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement equality check #275

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Expr {
Literal(Value),
Ident(Ident),
Expand All @@ -122,6 +122,7 @@
Of(Ident, Box<Expr>),

Assign(Ident, Box<Expr>),
Equality(Box<Expr>, Box<Expr>),
Statements(Box<Expr>, Box<Expr>),
}

Expand Down Expand Up @@ -202,6 +203,11 @@
a.serialize(write)?;
b.serialize(write)?;
}
Self::Equality(a, b) => {
16u8.serialize(write)?;
a.serialize(write)?;
b.serialize(write)?;

Check warning on line 209 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L206-L209

Added lines #L206 - L209 were not covered by tests
}
}
Ok(())
}
Expand Down Expand Up @@ -252,6 +258,10 @@
Box::new(Self::deserialize(read)?),
Box::new(Self::deserialize(read)?),
),
16 => Self::Equality(
Box::new(Self::deserialize(read)?),
Box::new(Self::deserialize(read)?),

Check warning on line 263 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L262-L263

Added lines #L262 - L263 were not covered by tests
),
_ => return Err(FendError::DeserializationError),
})
}
Expand Down Expand Up @@ -309,6 +319,11 @@
a.format(attrs, ctx, int)?,
b.format(attrs, ctx, int)?
),
Self::Equality(a, b) => format!(
"{} == {}",
a.format(attrs, ctx, int)?,
b.format(attrs, ctx, int)?

Check warning on line 325 in core/src/ast.rs

View check run for this annotation

Codecov / codecov/patch

core/src/ast.rs#L322-L325

Added lines #L322 - L325 were not covered by tests
),
})
}
}
Expand Down Expand Up @@ -452,6 +467,12 @@
let _lhs = evaluate(*a, scope.clone(), attrs, context, int)?;
evaluate(*b, scope, attrs, context, int)?
}
Expr::Equality(a, b) => {
let lhs = evaluate(*a, scope.clone(), attrs, context, int)?;
let rhs = evaluate(*b, scope.clone(), attrs, context, int)?;

Value::Bool(lhs == rhs)
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
serialize::{Deserialize, Serialize},
};

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Ident(Cow<'static, str>);

impl Ident {
Expand Down
6 changes: 5 additions & 1 deletion core/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
ShiftLeft,
ShiftRight,
Semicolon,
Equals, // used for assignment
Equals, // used for assignment
DoubleEquals, // used for equality
Combination,
Permutation,
}
Expand Down Expand Up @@ -65,6 +66,7 @@
Self::ShiftRight => ">>",
Self::Semicolon => ";",
Self::Equals => "=",
Self::DoubleEquals => "==",

Check warning on line 69 in core/src/lexer.rs

View check run for this annotation

Codecov / codecov/patch

core/src/lexer.rs#L69

Added line #L69 was not covered by tests
Self::Combination => "nCr",
Self::Permutation => "nPr",
};
Expand Down Expand Up @@ -523,6 +525,8 @@
'=' => {
if test_next('>') {
Symbol::Fn
} else if test_next('=') {
Symbol::DoubleEquals
} else {
Symbol::Equals
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/num/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{fmt, io};
use super::real::Real;
use super::{Base, Exact, FormattingStyle};

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct Dist {
// invariant: probabilities must sum to 1
parts: HashMap<Complex, BigRat>,
Expand Down
4 changes: 2 additions & 2 deletions core/src/num/unit.rs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a custom PartialEq/Eq impl would be better. We should be able to ignore the ‘base’, ‘exact’, ‘format’ and ‘simplifiable’ fields. It would also be nice to take units into account, so that e.g. 2m is equal to 200cm.

Copy link
Contributor Author

@frectonz frectonz Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My latest commit implements comparing values with different units although i haven't tested it thoroughly.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use unit_exponent::UnitExponent;

use super::Exact;

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
#[allow(clippy::pedantic)]
pub(crate) struct Value {
#[allow(clippy::struct_field_names)]
Expand Down Expand Up @@ -982,7 +982,7 @@ impl fmt::Display for FormattedValue {
}
}

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
struct Unit {
components: Vec<UnitExponent>,
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/num/unit/unit_exponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::Interrupt;

use super::{base_unit::BaseUnit, named_unit::NamedUnit};

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct UnitExponent {
pub(crate) unit: NamedUnit,
pub(crate) exponent: Complex,
Expand Down
12 changes: 11 additions & 1 deletion core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,18 @@ fn parse_function(input: &[Token]) -> ParseResult<'_> {
Ok((lhs, input))
}

fn parse_assignment(input: &[Token]) -> ParseResult<'_> {
fn parse_equality(input: &[Token]) -> ParseResult<'_> {
let (lhs, input) = parse_function(input)?;
if let Ok(((), remaining)) = parse_fixed_symbol(input, Symbol::DoubleEquals) {
let (rhs, remaining) = parse_function(remaining)?;
Ok((Expr::Equality(Box::new(lhs), Box::new(rhs)), remaining))
} else {
Ok((lhs, input))
}
}

fn parse_assignment(input: &[Token]) -> ParseResult<'_> {
let (lhs, input) = parse_equality(input)?;
if let Ok(((), remaining)) = parse_fixed_symbol(input, Symbol::Equals) {
if let Expr::Ident(s) = lhs {
let (rhs, remaining) = parse_assignment(remaining)?;
Expand Down
4 changes: 2 additions & 2 deletions core/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::io;
use std::sync::Arc;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]

Check warning on line 10 in core/src/scope.rs

View check run for this annotation

Codecov / codecov/patch

core/src/scope.rs#L10

Added line #L10 was not covered by tests
enum ScopeValue {
//Variable(Value),
LazyVariable(Expr, Option<Arc<Scope>>),
Expand Down Expand Up @@ -55,7 +55,7 @@
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]

Check warning on line 58 in core/src/scope.rs

View check run for this annotation

Codecov / codecov/patch

core/src/scope.rs#L58

Added line #L58 was not covered by tests
pub(crate) struct Scope {
ident: Ident,
value: ScopeValue,
Expand Down
2 changes: 1 addition & 1 deletion core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) mod built_in_function;

use built_in_function::BuiltInFunction;

#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub(crate) enum Value {
Num(Box<Number>),
BuiltInFunction(BuiltInFunction),
Expand Down
6 changes: 6 additions & 0 deletions core/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5873,3 +5873,9 @@ fn test_superscript() {
test_eval("200²", "40000");
test_eval("13¹³ days", "302875106592253 days");
}

#[test]
fn test_equality() {
test_eval("1 + 2 == 3", "true");
test_eval("true == false", "false");
}
Loading