added support for if elif else statements by returning a boolean

from the identifier syntax parsing
This commit is contained in:
2026-05-26 11:38:14 +01:00
parent 20369ef838
commit 4f7abb5f19
8 changed files with 189 additions and 40 deletions
+65 -16
View File
@@ -1,14 +1,9 @@
use crate::
{
// Internal code
character,
tokenise,
UnwrapOrExit,
parsing,
//Libs
HashMap,
VecDeque,
info,
warn,
debug,
};
@@ -20,18 +15,40 @@ pub fn identifier_parse
identifier: &String,
tokens: &[tokenise::Token],
variables: &mut HashMap<String, tokenise::Value>,
) -> Result<usize,(String,usize)>
) -> Result<(usize,bool),(String,usize)>
{
let mut sum_index: usize = index;
if ! variables.contains_key(identifier)
{
variables.insert(identifier.to_string(), tokenise::Value::Null);
}
let current = variables.get(identifier).unwrap().clone();
let operator = tokenise::get_operator_token(tokens, sum_index)
.map_err(|err| (err, sum_index))?;
sum_index += 1;
let value = tokenise::get_value_token(tokens, sum_index)
.map_err(|err| (err, sum_index))?;
let mut result = true;
match tokenise::get_value_token(tokens, sum_index)
{
Ok(value) => result = operator_match(&current,value,operator,identifier,variables),
Err(_) => todo!(),
}
sum_index += 1;
Ok((sum_index,result))
}
fn operator_match
(
current: &tokenise::Value,
value: tokenise::Value,
operator: tokenise::Operator,
identifier: &String,
variables: &mut HashMap<String, tokenise::Value>
)
-> bool
{
// Operator match box
match operator
{
// Changing a value
@@ -41,7 +58,6 @@ pub fn identifier_parse
} ,
tokenise::Operator::Add =>
{
let current = variables.get(identifier).unwrap();
let result: tokenise::Value = match (value.clone(), current)
{
(tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int1 + int2),
@@ -53,20 +69,53 @@ pub fn identifier_parse
},
tokenise::Operator::Sub =>
{
let current = variables.get(identifier).unwrap();
let result: tokenise::Value = match (value.clone(), current)
{
(tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int2 - int1),
_ => value.clone(), // otherwise invalid
};
// TODO comparisons :DDD
variables.insert(identifier.to_string(), result);
},
_ => (),
// TODO choice assignment
// TODO input assignment
// TODO comparisons :DDD
tokenise::Operator::Comparison(comp) =>
{
let result = match (current, &value)
{
// Integer
(tokenise::Value::Integer(current), tokenise::Value::Integer(comparing)) =>
{
match comp
{
tokenise::Comparison::Equate => current == comparing,
tokenise::Comparison::Greater => current > comparing,
tokenise::Comparison::Less => current < comparing,
tokenise::Comparison::GreaterOrEqual => current >= comparing,
tokenise::Comparison::LessOrEqual => current <= comparing,
}
},
// String
(tokenise::Value::String(current), tokenise::Value::String(comparing)) =>
{
match comp
{
tokenise::Comparison::Equate => current == comparing,
tokenise::Comparison::Greater => current > comparing,
tokenise::Comparison::Less => current < comparing,
tokenise::Comparison::GreaterOrEqual => current >= comparing,
tokenise::Comparison::LessOrEqual => current <= comparing,
}
},
_ => {
warn!("Invalid comparison of {current:?} and {value:?}, evaluating false");
false
},
};
debug!("Comparison {current:?} comp {value:?} evaluates to {result}");
return result;
}
}
debug!("{variables:?}");
sum_index += 1;
Ok(sum_index)
true
}