diff --git a/libs/sqf/src/compiler/optimizer/mod.rs b/libs/sqf/src/compiler/optimizer/mod.rs index 4f50dd191..637f82659 100644 --- a/libs/sqf/src/compiler/optimizer/mod.rs +++ b/libs/sqf/src/compiler/optimizer/mod.rs @@ -2,7 +2,7 @@ //! `ToDo`: what commands consume arrays //! use crate::{BinaryCommand, Expression, Statement, Statements, UnaryCommand}; -use std::ops::Range; +use std::{ops::Range, sync::Arc}; #[allow(unused_imports)] use tracing::{trace, warn}; @@ -104,6 +104,12 @@ impl Expression { right_o = consumable; } } + "tostring" => { + if let Some(eval) = self.op_uni_tostring_code(op_type, range, &right_o) + { + return eval; + } + } _ => {} }, _ => {} @@ -496,4 +502,29 @@ impl Expression { _ => None, } } + + /// Boilerplate for unary string operations + #[must_use] + fn op_uni_tostring_code( + &self, + #[allow(unused_variables)] op_type: &UnaryCommand, + range: &Range, + right: &Self, + ) -> Option { + if let Self::Code(statements) = right { + #[cfg(debug_assertions)] + trace!( + "optimizing [U:{}] ({}) => {}", + op_type.as_str(), + self.source(false), + statements.source() + ); + return Some(Self::String( + Arc::from(statements.source().to_owned()), + range.clone(), + crate::StringWrapper::DoubleQuote, + )); + } + None + } } diff --git a/libs/sqf/tests/optimizer.rs b/libs/sqf/tests/optimizer.rs index c7810b006..6ffb630f3 100644 --- a/libs/sqf/tests/optimizer.rs +++ b/libs/sqf/tests/optimizer.rs @@ -21,6 +21,7 @@ optimize!(static_math); optimize!(scalar); optimize!(string_case); optimize!(chain); +optimize!(to_string); const ROOT: &str = "tests/optimizer/"; diff --git a/libs/sqf/tests/optimizer/to_string.sqf b/libs/sqf/tests/optimizer/to_string.sqf new file mode 100644 index 000000000..565dc2321 --- /dev/null +++ b/libs/sqf/tests/optimizer/to_string.sqf @@ -0,0 +1,24 @@ +#define GVAR(x) GEE_##x +#define QUOTE(x) #x + +// b = {"double q"}; +// b1 = toString b; +b2 = toString {"double q"}; + +// c = {'single q'}; +// c1 = toString c; +c2 = toString {'single q'}; + +// d = { " hello 'you there' " }; +// d1 = toString d; +d2 = toString { " hello 'you there' " }; + +// e = { ' hello "you there" ' }; +// e1 = toString e; +e2 = toString { ' hello "you there" ' }; + +_f ctrlSetEventHandler ["ButtonClick", toString { + x = "clicked"; +}]; + +g = toString {123} + toString {456}; diff --git a/libs/sqf/tests/snapshots/optimizer__simple_to_string.snap b/libs/sqf/tests/snapshots/optimizer__simple_to_string.snap new file mode 100644 index 000000000..3577c3d7a --- /dev/null +++ b/libs/sqf/tests/snapshots/optimizer__simple_to_string.snap @@ -0,0 +1,84 @@ +--- +source: libs/sqf/tests/optimizer.rs +expression: optimize(stringify! (to_string)) +--- +Statements { + content: [ + AssignGlobal( + "b2", + String( + "\"double q\"", + 8..16, + DoubleQuote, + ), + 3..29, + ), + AssignGlobal( + "c2", + String( + "'single q'", + 39..47, + DoubleQuote, + ), + 34..60, + ), + AssignGlobal( + "d2", + String( + "\" hello 'you there' \"", + 70..78, + DoubleQuote, + ), + 65..104, + ), + AssignGlobal( + "e2", + String( + "' hello \"you there\" '", + 114..122, + DoubleQuote, + ), + 109..148, + ), + Expression( + BinaryCommand( + Named( + "ctrlSetEventHandler", + ), + Variable( + "_f", + 151..153, + ), + Array( + [ + String( + "ButtonClick", + 175..188, + DoubleQuote, + ), + String( + "x = \"clicked\";", + 190..198, + DoubleQuote, + ), + ], + 175..221, + ), + 154..173, + ), + 151..222, + ), + AssignGlobal( + "g", + String( + "123456", + 244..245, + DoubleQuote, + ), + 225..260, + ), + ], + source: "\n\n\nb2 = toString {\"double q\"};\n\n\n\nc2 = toString {'single q'};\n\n\n\nd2 = toString { \" hello 'you there' \" };\n\n\n\ne2 = toString { ' hello \"you there\" ' };\n\n_f ctrlSetEventHandler [\"ButtonClick\", toString {\n x = \"clicked\";\n}];\n\ng = toString {123} + toString {456};\n", + span: 3..261, + issues: [], +}