From 79b9c63fb57d0e165d9f6f0e9e3c32c8447788a4 Mon Sep 17 00:00:00 2001 From: Ryan Pitasky <111201305+rpitasky@users.noreply.github.com> Date: Mon, 2 Dec 2024 02:58:02 -0500 Subject: [PATCH] Bugfix: parenthesis optimization There was some weirdness going on with associativity. Repro: ``` sin(A) and sin(B) and sin(C) and sin(cos(D ``` --- .../expressions/parenthesis_optimization.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs b/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs index b264b6c..a533c87 100644 --- a/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs +++ b/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs @@ -6,7 +6,7 @@ use std::mem; use titokens::Token; use crate::parse::components::{EquationIndex, ListIndex, MatrixIndex, Rand, StoreTarget}; -use crate::parse::statements::{ControlFlow, Generic, Statement}; +use crate::parse::statements::{ControlFlow, DelVarChain, Generic, Statement}; use crate::parse::{ components::{Operand, Operator}, expression::Expression, @@ -54,7 +54,7 @@ impl Expression { if let Expression::Operator(Operator::Binary(left_binop)) = binop.left.as_ref() { - if binop.precedence() >= left_binop.precedence() { + if binop.precedence() > left_binop.precedence() { left += 1; } } @@ -154,6 +154,13 @@ impl Expression { impl Statement { pub fn optimize_parentheses(&mut self) { match self { + Statement::DelVarChain(DelVarChain { + valence: Some(statement), + .. + }) => { + statement.optimize_parentheses(); + } + Statement::Generic(Generic { arguments, .. }) => { if let Some(last) = arguments.last_mut() { last.optimize_parentheses(); @@ -213,7 +220,7 @@ mod tests { #[test] fn parenthesis_optimization() { - let cases = [("1.txt", 3), ("2.txt", 1)]; + let cases = [("1.txt", 3), ("2.txt", 1), ("3.txt", 1), ("4.txt", 2)]; for (case_name, expected_savings) in cases { let mut tokens = load_test_data( &("/snippets/optimize/parentheses/maximization/".to_string() + case_name),