added basic variable functionality, doesn't really do anything at the
moment but you can assign and change variables.
This commit is contained in:
@@ -2,12 +2,10 @@ use crate::
|
||||
{
|
||||
// Internal code
|
||||
character,
|
||||
api,
|
||||
tokenise,
|
||||
UnwrapOrExit,
|
||||
parsing,
|
||||
//Libs
|
||||
Mutex,
|
||||
Arc,
|
||||
HashMap,
|
||||
VecDeque,
|
||||
info,
|
||||
@@ -19,10 +17,55 @@ use crate::
|
||||
pub fn identifier_parse
|
||||
(
|
||||
index: usize,
|
||||
identifier: &String,
|
||||
tokens: &[tokenise::Token],
|
||||
variables: &mut HashMap<String, tokenise::Value>,
|
||||
) -> Result<usize,(String,usize)>
|
||||
{
|
||||
let mut sum_index: usize = index;
|
||||
if ! variables.contains_key(identifier)
|
||||
{
|
||||
variables.insert(identifier.to_string(), tokenise::Value::Null);
|
||||
}
|
||||
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))?;
|
||||
match operator
|
||||
{
|
||||
// Changing a value
|
||||
tokenise::Operator::Assignment =>
|
||||
{
|
||||
variables.insert(identifier.to_string(), value);
|
||||
} ,
|
||||
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),
|
||||
(tokenise::Value::String(str1),tokenise::Value::String(str2)) => tokenise::Value::String(format!("{str1}{str2}")),
|
||||
_ => value.clone(), // otherwise invalid
|
||||
|
||||
};
|
||||
variables.insert(identifier.to_string(), result);
|
||||
},
|
||||
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);
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
debug!("{variables:?}");
|
||||
sum_index += 1;
|
||||
Ok(sum_index)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user