Skip to content

Commit

Permalink
use generic version of value for param types
Browse files Browse the repository at this point in the history
  • Loading branch information
PabstMirror committed Nov 10, 2024
1 parent 0e37ace commit 8838a34
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
7 changes: 5 additions & 2 deletions libs/sqf/src/analyze/inspector/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Emulates engine commands
use std::{collections::HashSet, ops::Range};

use crate::{analyze::inspector::VarSource, parser::database::Database, Expression};
Expand Down Expand Up @@ -86,7 +88,7 @@ impl SciptScope {
for type_p in &arg_array[2] {
if let GameValue::Array(Some(type_array)) = type_p {
for type_i in type_array {
var_types.extend(type_i.iter().cloned());
var_types.extend(type_i.iter().map(GameValue::make_generic));
}
}
}
Expand All @@ -95,7 +97,8 @@ impl SciptScope {
var_types.insert(GameValue::Anything);
}
// Add the default value to types
// It should be possible to move this above the is_empty check but not always safe
// It would be nice to move this above the is_empty check but not always safe
// ie: assume `params ["_z", ""]` is type string, but this is not guarentted
if arg_array.len() > 1 && !arg_array[1].is_empty() {
var_types.insert(arg_array[1][0].clone());
}
Expand Down
2 changes: 2 additions & 0 deletions libs/sqf/src/analyze/inspector/external_functions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Emulate how common external functions will handle code
use std::collections::HashSet;

use crate::{analyze::inspector::VarSource, parser::database::Database, Expression};
Expand Down
29 changes: 19 additions & 10 deletions libs/sqf/src/analyze/inspector/game_value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Game Values and mapping them from commands
use std::collections::HashSet;

use arma3_wiki::model::{Arg, Call, Param, Value};
Expand Down Expand Up @@ -129,7 +131,7 @@ impl GameValue {
"inAreaArray",
];
if !WIKI_CMDS_IGNORE_MISSING_PARAM.contains(&cmd_name) {
warn!("cmd {cmd_name} - param {name} not found");
// warn!("cmd {cmd_name} - param {name} not found");
}
return true;
};
Expand All @@ -146,17 +148,15 @@ impl GameValue {
return true;
}

let test = set.iter().any(|s| {
set.iter().any(|s| {
match s {
Self::Anything | Self::Array(None) => {
// println!("array (any/generic) pass");
true
}
Self::Array(Some(gv_array)) => {
// println!("array (gv: {}) expected (arg: {})", gv_array.len(), arg_array.len());
// if gv_array.len() > arg_array.len() {
// not really an error, some syntaxes take more than others
// }
// note: some syntaxes take more than others
for (index, arg) in arg_array.iter().enumerate() {
let possible = if index < gv_array.len() {
gv_array[index].iter().cloned().collect()
Expand All @@ -171,9 +171,7 @@ impl GameValue {
}
_ => false,
}
});

test
})
}
}
}
Expand All @@ -190,7 +188,6 @@ impl GameValue {

#[must_use]
/// matches values are compatible (Anything will always match)
/// todo: think about how nil and any interact?
pub fn match_values(left: &Self, right: &Self) -> bool {
if matches!(left, Self::Anything) {
return true;
Expand Down Expand Up @@ -247,7 +244,19 @@ impl GameValue {
}
}
}

#[must_use]
/// Gets a generic version of a type
pub fn make_generic(&self) -> Self {
match self {
Self::Array(_) => Self::Array(None),
Self::Boolean(_) => Self::Boolean(None),
Self::Code(_) => Self::Code(None),
Self::ForType(_) => Self::ForType(None),
Self::Number(_) => Self::Number(None),
Self::String(_) => Self::String(None),
_ => self.clone()
}
}
#[must_use]
/// Get as a string for debugging
pub fn as_debug(&self) -> String {
Expand Down
1 change: 1 addition & 0 deletions libs/sqf/src/analyze/inspector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ impl SciptScope {
}
"do" => {
// from While, With, For, and Switch
// todo: handle switch return value
Some(self.cmd_b_do(&lhs_set, &rhs_set, database))
}
"from" | "to" | "step" => Some(self.cmd_b_from_chain(&lhs_set, &rhs_set)),
Expand Down
5 changes: 5 additions & 0 deletions libs/sqf/tests/inspector/test_1.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ call _varO;

params [["_someString", "abc", [""]], ["_someCode", { 60 setGusts _someString }]];
call _someCode; // InvalidArgs for setGusts

// ensure we use a generic version of the array param types or format would have an error
params [["_varP", "", ["", []]]];
format _varP;

0 comments on commit 8838a34

Please sign in to comment.