implement "if" statements with 2 operands + 1 operator
This commit is contained in:
parent
00df7bc711
commit
011938cadf
73
src/var.rs
73
src/var.rs
|
@ -4,6 +4,13 @@ use std::env;
|
||||||
|
|
||||||
pub const SCOPE_SEPARATOR: &str = "$";
|
pub const SCOPE_SEPARATOR: &str = "$";
|
||||||
|
|
||||||
|
pub const TOKEN_EQUALS: &str = "==";
|
||||||
|
pub const TOKEN_EQUALS_NOT: &str = "!=";
|
||||||
|
pub const TOKEN_GREATER_THAN: &str = ">";
|
||||||
|
pub const TOKEN_LESS_THAN: &str = "<";
|
||||||
|
pub const TOKEN_GREATER_EQUALS: &str = ">=";
|
||||||
|
pub const TOKEN_LESS_EQUALS: &str = "<=";
|
||||||
|
|
||||||
pub const DEFAULT_CHAT_SPEED: f32 = 10.0;
|
pub const DEFAULT_CHAT_SPEED: f32 = 10.0;
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
|
@ -281,7 +288,10 @@ impl GameVars {
|
||||||
|
|
||||||
pub fn evaluate_condition(&self, condition: &str, scope: &str) -> bool {
|
pub fn evaluate_condition(&self, condition: &str, scope: &str) -> bool {
|
||||||
let parts: Vec<&str> = condition.split(" ").collect();
|
let parts: Vec<&str> = condition.split(" ").collect();
|
||||||
if parts.len() == 1 {
|
if parts.len() == 0 {
|
||||||
|
// Got an empty string, this is always false.
|
||||||
|
return false;
|
||||||
|
} else if parts.len() == 1 {
|
||||||
// Got something like "if $somevar:".
|
// Got something like "if $somevar:".
|
||||||
// Check whether the variable evaluates to true.
|
// Check whether the variable evaluates to true.
|
||||||
let part = parts[0];
|
let part = parts[0];
|
||||||
|
@ -309,12 +319,67 @@ impl GameVars {
|
||||||
.get(Self::normalize_varname(scope, right.as_str()).as_str())
|
.get(Self::normalize_varname(scope, right.as_str()).as_str())
|
||||||
.unwrap_or("".to_string());
|
.unwrap_or("".to_string());
|
||||||
}
|
}
|
||||||
dbg!(&left);
|
|
||||||
dbg!(&right);
|
|
||||||
dbg!(left == right);
|
|
||||||
return left == right;
|
return left == right;
|
||||||
} else {
|
} else {
|
||||||
|
// Got something like "if $something != somethingelse bla bla"
|
||||||
|
let mut left: String = parts[0].to_string();
|
||||||
|
if left.contains(SCOPE_SEPARATOR) {
|
||||||
|
left = self
|
||||||
|
.get(Self::normalize_varname(scope, left.as_str()).as_str())
|
||||||
|
.unwrap_or("".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut right: String = parts[2..parts.len()].join(" ").to_string();
|
||||||
|
if right.contains(SCOPE_SEPARATOR) {
|
||||||
|
right = self
|
||||||
|
.get(Self::normalize_varname(scope, right.as_str()).as_str())
|
||||||
|
.unwrap_or("".to_string());
|
||||||
|
}
|
||||||
|
let floats = (left.parse::<f64>(), right.parse::<f64>());
|
||||||
|
let operator: &str = parts[1];
|
||||||
|
|
||||||
|
match operator {
|
||||||
|
TOKEN_EQUALS => {
|
||||||
|
if let (Ok(left), Ok(right)) = floats {
|
||||||
|
return left == right;
|
||||||
|
}
|
||||||
|
return left == right;
|
||||||
|
}
|
||||||
|
TOKEN_EQUALS_NOT => {
|
||||||
|
if let (Ok(left), Ok(right)) = floats {
|
||||||
|
return left != right;
|
||||||
|
}
|
||||||
|
return left != right;
|
||||||
|
}
|
||||||
|
TOKEN_GREATER_THAN => {
|
||||||
|
if let (Ok(left), Ok(right)) = floats {
|
||||||
|
return left > right;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TOKEN_GREATER_EQUALS => {
|
||||||
|
if let (Ok(left), Ok(right)) = floats {
|
||||||
|
return left >= right;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TOKEN_LESS_THAN => {
|
||||||
|
if let (Ok(left), Ok(right)) = floats {
|
||||||
|
return left < right;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TOKEN_LESS_EQUALS => {
|
||||||
|
if let (Ok(left), Ok(right)) = floats {
|
||||||
|
return left <= right;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
error!("Unknown operator '{operator}' in if-condition!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue