Skip to content

Commit

Permalink
added prefixes - and +
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertasJ committed Oct 20, 2024
1 parent 78aca93 commit 32bc349
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
44 changes: 35 additions & 9 deletions crates/torin/src/values/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,11 @@ impl<'a> DynamicCalculationEvaluator<'a> {
Some(lhs)
}

/// Parse and evaluate the value with the following grammar:
/// ```ebnf
/// value = percentage | pixels ;
/// percentage = number, "%" ;
/// pixels = number ;
/// ```
/// Parse and evaluate the term, implements implicit multiplication. only parenthesis count as
/// a seperator, so syntax like 50 50 isnt correct, but 50(50) is because the parenthesis act
/// as a seperator
fn parse_term(&mut self) -> Option<f32> {
let prefix = self.parse_prefix()?;
let mut lhs = None;
// set to true so that the first value is multiplied and counts as normal syntax
let mut last_is_seperator = true;
Expand All @@ -283,10 +281,22 @@ impl<'a> DynamicCalculationEvaluator<'a> {
}
last_is_seperator = seperator;
}

lhs
if let Some(prefix) = prefix {
match prefix {
DynamicCalculation::Add => lhs,
DynamicCalculation::Sub => lhs.map(|v| v * -1.0),
_ => unreachable!(),
}
} else {
lhs
}
}

/// parse and evaluate the value with the following grammar:
/// ```ebnf
/// value = percentage | pixels ;
/// percentage = number, "%" ;
/// pixels = number ;
/// `
fn parse_value(&mut self) -> Option<(f32, bool)> {
match self.current? {
DynamicCalculation::Percentage(value) => {
Expand All @@ -302,6 +312,8 @@ impl<'a> DynamicCalculationEvaluator<'a> {
Some((*value, false))
}
DynamicCalculation::OpenParenthesis => {
// function should return on DynamicCalculation::ClosedParenthesis because it does
// not have a precedence, thats how it actually works
let val = self.parse_expression(0);
self.current = self.calcs.next();
Some((val?, true))
Expand All @@ -310,6 +322,20 @@ impl<'a> DynamicCalculationEvaluator<'a> {
}
}

fn parse_prefix(&mut self) -> Option<Option<DynamicCalculation>> {
match self.current? {
DynamicCalculation::Add => {
self.current = self.calcs.next();
Some(Some(DynamicCalculation::Add))
}
DynamicCalculation::Sub => {
self.current = self.calcs.next();
Some(Some(DynamicCalculation::Sub))
}
_ => Some(None),
}
}

/// Get the precedence of the operator if current token is an operator or None otherwise.
fn operator_precedence(&self) -> Option<usize> {
match self.current? {
Expand Down
21 changes: 19 additions & 2 deletions crates/torin/tests/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,21 +860,23 @@ pub fn test_calc() {
PARENT_VALUE,
PARENT_VALUE
),
None
// becasue +10 is just 10
Some(10.0)
);

assert_eq!(
run_calculations(
&vec![
DynamicCalculation::Pixels(10.0),
DynamicCalculation::Add,
// counts as a prefix
DynamicCalculation::Add,
DynamicCalculation::Pixels(10.0)
],
PARENT_VALUE,
PARENT_VALUE
),
None
Some(20.0)
);

assert_eq!(
Expand Down Expand Up @@ -925,4 +927,19 @@ pub fn test_calc() {
),
Some((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0))
);

assert_eq!(
run_calculations(
&vec![
DynamicCalculation::Sub,
DynamicCalculation::OpenParenthesis,
DynamicCalculation::Pixels(10.0),
DynamicCalculation::ClosedParenthesis,
DynamicCalculation::Pixels(20.0)
],
PARENT_VALUE,
PARENT_VALUE
),
Some(-1.0 * 10.0 * 20.0)
);
}

0 comments on commit 32bc349

Please sign in to comment.