Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 32 additions & 1 deletion libs/sqf/src/compiler/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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;
}
}
_ => {}
},
_ => {}
Expand Down Expand Up @@ -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<usize>,
right: &Self,
) -> Option<Self> {
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
}
}
1 change: 1 addition & 0 deletions libs/sqf/tests/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ optimize!(static_math);
optimize!(scalar);
optimize!(string_case);
optimize!(chain);
optimize!(to_string);

const ROOT: &str = "tests/optimizer/";

Expand Down
24 changes: 24 additions & 0 deletions libs/sqf/tests/optimizer/to_string.sqf
Original file line number Diff line number Diff line change
@@ -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};
84 changes: 84 additions & 0 deletions libs/sqf/tests/snapshots/optimizer__simple_to_string.snap
Original file line number Diff line number Diff line change
@@ -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: [],
}