diff --git a/ti-basic-optimizer/src/analyze/control_flow/parser.rs b/ti-basic-optimizer/src/analyze/control_flow/parser.rs index 7aaef97..4b6833a 100644 --- a/ti-basic-optimizer/src/analyze/control_flow/parser.rs +++ b/ti-basic-optimizer/src/analyze/control_flow/parser.rs @@ -206,7 +206,14 @@ impl Program { } for (statement, failure_path) in block_failure_paths.iter() { - if matches!(self.lines[*statement], Statement::ControlFlow(ControlFlow::IfThen(_))) && matches!(self.lines[*failure_path - 1], Statement::ControlFlow(ControlFlow::Else)) && literals.contains(&(failure_path - 1)) { + if matches!( + self.lines[*statement], + Statement::ControlFlow(ControlFlow::IfThen(_)) + ) && matches!( + self.lines[*failure_path - 1], + Statement::ControlFlow(ControlFlow::Else) + ) && literals.contains(&(failure_path - 1)) + { literals.insert(*statement); } } diff --git a/ti-basic-optimizer/src/analyze/control_flow/parser/graphviz.rs b/ti-basic-optimizer/src/analyze/control_flow/parser/graphviz.rs index 944e6a8..f4c6f00 100644 --- a/ti-basic-optimizer/src/analyze/control_flow/parser/graphviz.rs +++ b/ti-basic-optimizer/src/analyze/control_flow/parser/graphviz.rs @@ -45,14 +45,15 @@ impl<'a> Visualize> for BasicBlock { node.set("shape", shape, false); - let content = match self.flow { - Flow::Goto(label_name) => label_name.stringify(config.tokenizer), - _ => self - .lines - .iter() - .map(|statement| statement.stringify(config.tokenizer)) - .join("\n"), - }; + let content = self + .lines + .iter() + .map(|statement| statement.stringify(config.tokenizer)) + .join("\n"); + + if let Flow::Goto(label_name) = self.flow { + node.set("xlabel", &label_name.stringify(config.tokenizer), false); + } node.set_label(&escape(&content).replace( "\\n", @@ -193,7 +194,13 @@ impl<'a> Visualize> for LabelFragment { .set_label(&self.name.stringify(config.tokenizer)); } - segment.visualize(&mut inner_context, CFGVisSettings { tokenizer: config.tokenizer, namespace: namespace.clone() }); + segment.visualize( + &mut inner_context, + CFGVisSettings { + tokenizer: config.tokenizer, + namespace: namespace.clone(), + }, + ); last_namespace = namespace; } diff --git a/ti-basic-optimizer/src/error_reporting.rs b/ti-basic-optimizer/src/error_reporting.rs index bc3f28f..330b6f8 100644 --- a/ti-basic-optimizer/src/error_reporting.rs +++ b/ti-basic-optimizer/src/error_reporting.rs @@ -1,6 +1,7 @@ use std::ops::Range; use titokens::tokenizer::TokenBoundaries; +/// Grab the next token, or throw a generic "end of input" error. macro_rules! next_or_err { ($tokens: ident) => { $tokens.next().ok_or_else(|| { diff --git a/ti-basic-optimizer/src/optimize/control_flow/label_name.rs b/ti-basic-optimizer/src/optimize/control_flow/label_name.rs index 54ed0f0..bf60b34 100644 --- a/ti-basic-optimizer/src/optimize/control_flow/label_name.rs +++ b/ti-basic-optimizer/src/optimize/control_flow/label_name.rs @@ -7,7 +7,7 @@ use crate::parse::statements::control_flow::Menu; use crate::parse::{ - statements::{Statement, ControlFlow, LabelName}, + statements::{ControlFlow, LabelName, Statement}, Program, }; use itertools::Itertools; diff --git a/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs b/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs index 4508ed3..c6bcab7 100644 --- a/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs +++ b/ti-basic-optimizer/src/optimize/expressions/parenthesis_optimization.rs @@ -5,8 +5,8 @@ use std::mem; use titokens::Token; -use crate::parse::statements::{Statement, ControlFlow, Generic}; -use crate::parse::components::{ListIndex, MatrixIndex, StoreTarget}; +use crate::parse::components::{EquationIndex, ListIndex, MatrixIndex, Rand, StoreTarget}; +use crate::parse::statements::{ControlFlow, Generic, Statement}; use crate::parse::{ components::{Operand, Operator}, expression::Expression, @@ -96,13 +96,12 @@ impl Expression { } } - Expression::Operand(Operand::ListAccess(ListIndex { index, .. })) => { - 1 + index.optimize_parentheses() - } - - Expression::Operand(Operand::MatrixAccess(MatrixIndex { col, .. })) => { - 1 + col.optimize_parentheses() - } + Expression::Operand( + Operand::ListAccess(ListIndex { index, .. }) + | Operand::MatrixAccess(MatrixIndex { col: index, .. }) + | Operand::EquationAccess(EquationIndex { index, .. }) + | Operand::Rand(Rand { count: Some(index) }), + ) => 1 + index.optimize_parentheses(), Expression::Operand(Operand::StringLiteral(_) | Operand::ListLiteral(_)) => 1, @@ -196,11 +195,11 @@ impl Statement { index.col.optimize_parentheses(); } - _ => { /* no closing parentheses possible */ } + _ => { /* no closing parentheses savings possible */ } } } - _ => { /* no closing parentheses possible */ } + _ => { /* no closing parentheses savings possible */ } } } } diff --git a/ti-basic-optimizer/src/parse/mod.rs b/ti-basic-optimizer/src/parse/mod.rs index 3f579da..fb5192f 100644 --- a/ti-basic-optimizer/src/parse/mod.rs +++ b/ti-basic-optimizer/src/parse/mod.rs @@ -1,10 +1,10 @@ use itertools::Itertools; use titokens::{Token, Tokens}; -pub mod statements; pub mod components; pub mod expression; mod program; +pub mod statements; pub use program::Program;