clippy lints and moved the operator matchbox to it's own function

This commit is contained in:
2026-05-26 11:54:53 +01:00
parent 4f7abb5f19
commit 59f32e975b
5 changed files with 30 additions and 43 deletions
+1 -1
View File
@@ -457,7 +457,7 @@ dependencies = [
[[package]] [[package]]
name = "happening-server" name = "happening-server"
version = "0.0.2" version = "0.0.3"
dependencies = [ dependencies = [
"env_logger", "env_logger",
"log", "log",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "happening-server" name = "happening-server"
version = "0.0.2" version = "0.0.3"
edition = "2024" edition = "2024"
license = "GPL-3" license = "GPL-3"
repository = "https://git.javalsai.tuxcord.net/deadvey/happening/" repository = "https://git.javalsai.tuxcord.net/deadvey/happening/"
+12 -14
View File
@@ -18,20 +18,18 @@ pub fn identifier_parse
) -> Result<(usize,bool),(String,usize)> ) -> Result<(usize,bool),(String,usize)>
{ {
let mut sum_index: usize = index; let mut sum_index: usize = index;
if ! variables.contains_key(identifier) let current = variables
{ .entry(identifier.clone())
variables.insert(identifier.to_string(), tokenise::Value::Null); .or_insert(tokenise::Value::Null)
} .clone();
let current = variables.get(identifier).unwrap().clone();
let operator = tokenise::get_operator_token(tokens, sum_index) let operator = tokenise::get_operator_token(tokens, sum_index)
.map_err(|err| (err, sum_index))?; .map_err(|err| (err, sum_index))?;
sum_index += 1; sum_index += 1;
let mut result = true; let result: bool = match tokenise::get_value_token(tokens, sum_index)
match tokenise::get_value_token(tokens, sum_index)
{ {
Ok(value) => result = operator_match(&current,value,operator,identifier,variables), Ok(value) => operator_match(&current,value,operator,identifier,variables),
Err(_) => todo!(), Err(_) => todo!(),
} };
sum_index += 1; sum_index += 1;
@@ -54,7 +52,7 @@ fn operator_match
// Changing a value // Changing a value
tokenise::Operator::Assignment => tokenise::Operator::Assignment =>
{ {
variables.insert(identifier.to_string(), value); variables.insert(identifier.to_owned(), value);
} , } ,
tokenise::Operator::Add => tokenise::Operator::Add =>
{ {
@@ -62,20 +60,20 @@ fn operator_match
{ {
(tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int1 + int2), (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}")), (tokenise::Value::String(str1),tokenise::Value::String(str2)) => tokenise::Value::String(format!("{str1}{str2}")),
_ => value.clone(), // otherwise invalid _ => value, // otherwise invalid
}; };
variables.insert(identifier.to_string(), result); variables.insert(identifier.to_owned(), result);
}, },
tokenise::Operator::Sub => tokenise::Operator::Sub =>
{ {
let result: tokenise::Value = match (value.clone(), current) let result: tokenise::Value = match (value.clone(), current)
{ {
(tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int2 - int1), (tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int2 - int1),
_ => value.clone(), // otherwise invalid _ => value, // otherwise invalid
}; };
variables.insert(identifier.to_string(), result); variables.insert(identifier.to_owned(), result);
}, },
// TODO choice assignment // TODO choice assignment
// TODO input assignment // TODO input assignment
+8 -15
View File
@@ -47,14 +47,13 @@ pub fn keyword_parse(
{ {
// TODO can this go in a function? // TODO can this go in a function?
let mut keyword = "if".to_string(); let mut keyword = "if".to_string();
while keyword == "if".to_string() || keyword == "elif".to_string() || keyword == "else".to_string() // TODO less beefy?? while keyword == "if" || keyword == "elif" || keyword == "else" // TODO less beefy??
{ {
index += 1; index += 1;
let mut result: bool = true; let mut result: bool = true;
if keyword != "else" if keyword != "else"
{ {
let identifier = tokenise::get_identifier_token(tokens, index) let identifier = tokenise::get_identifier_token(tokens, index)?;
.map(|err| err)?;
(index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables) (index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables)
{ {
Ok((increment, result)) => (increment,result), Ok((increment, result)) => (increment,result),
@@ -66,28 +65,22 @@ pub fn keyword_parse(
} }
} }
if result { index += 1; break; } if result { index += 1; break; }
else index = tokenise::get_closing_index(tokens,index)?;
index += 1;
keyword = match tokenise::get_keyword_token(tokens,index)
{ {
index = tokenise::get_closing_index(tokens, index) Ok(keyword) => keyword,
.map(|err| err)?; Err(_) => break,
index += 1;
keyword = match tokenise::get_keyword_token(tokens,index)
{
Ok(keyword) => keyword,
Err(_) => break,
}
} }
} }
}, },
"else" => "else" =>
{ {
index += 1; index += 1;
index = tokenise::get_closing_index(tokens, index) index = tokenise::get_closing_index(tokens, index)?;
.map(|err| err)?;
}, },
"elif" => "elif" =>
{ {
let current = "elif".to_string();
loop loop
{ {
index += 1; index += 1;
+8 -12
View File
@@ -13,7 +13,7 @@ pub enum Token
Bracket((Bracket,usize)), // Stores the index of the matching deliminator Bracket((Bracket,usize)), // Stores the index of the matching deliminator
Character(String), Character(String),
} }
#[derive(Debug,Clone,PartialEq)] #[derive(Debug,Clone,PartialEq,Eq)]
pub enum Value pub enum Value
{ {
String(String), String(String),
@@ -142,15 +142,11 @@ pub fn tokenise(file_contents: &str)
"<=" => Some(Operator::Comparison(Comparison::LessOrEqual)), "<=" => Some(Operator::Comparison(Comparison::LessOrEqual)),
_ => None, _ => None,
}; };
match op if let Some(op) = op
{ {
Some(op) => tokenised_data.push(Token::Operator(op));
{ index += 1;
tokenised_data.push(Token::Operator(op)); continue // skip the rest of this loop
index += 1;
continue // skip the rest of this loop
},
None => (),
} }
// Characters // Characters
if item.starts_with('@') if item.starts_with('@')
@@ -168,9 +164,9 @@ pub fn tokenise(file_contents: &str)
item = new_item; item = new_item;
tokenised_data.push(Token::Value(Value::String(item))); tokenised_data.push(Token::Value(Value::String(item)));
} }
else if item.parse::<i64>().is_ok() else if let Ok(value) = item.parse::<i64>()
{ {
tokenised_data.push(Token::Value(Value::Integer(item.parse::<i64>().unwrap()))); // unwrap is fine here because I checked tokenised_data.push(Token::Value(Value::Integer(value))); // unwrap is fine here because I checked
} }
else if item == "true" else if item == "true"
{ {
@@ -181,7 +177,7 @@ pub fn tokenise(file_contents: &str)
tokenised_data.push(Token::Value(Value::Bool(false))); tokenised_data.push(Token::Value(Value::Bool(false)));
} }
// variable/identifier // variable/identifier
else if item.starts_with("$") else if item.starts_with('$')
{ {
let mut chars = item.chars(); let mut chars = item.chars();
chars.next(); chars.next();