clippy lints and moved the operator matchbox to it's own function
This commit is contained in:
Generated
+1
-1
@@ -457,7 +457,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "happening-server"
|
||||
version = "0.0.2"
|
||||
version = "0.0.3"
|
||||
dependencies = [
|
||||
"env_logger",
|
||||
"log",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "happening-server"
|
||||
version = "0.0.2"
|
||||
version = "0.0.3"
|
||||
edition = "2024"
|
||||
license = "GPL-3"
|
||||
repository = "https://git.javalsai.tuxcord.net/deadvey/happening/"
|
||||
|
||||
@@ -18,20 +18,18 @@ pub fn identifier_parse
|
||||
) -> 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 current = variables
|
||||
.entry(identifier.clone())
|
||||
.or_insert(tokenise::Value::Null)
|
||||
.clone();
|
||||
let operator = tokenise::get_operator_token(tokens, sum_index)
|
||||
.map_err(|err| (err, sum_index))?;
|
||||
sum_index += 1;
|
||||
let mut result = true;
|
||||
match tokenise::get_value_token(tokens, sum_index)
|
||||
let result: bool = match tokenise::get_value_token(tokens, sum_index)
|
||||
{
|
||||
Ok(value) => result = operator_match(¤t,value,operator,identifier,variables),
|
||||
Ok(value) => operator_match(¤t,value,operator,identifier,variables),
|
||||
Err(_) => todo!(),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
sum_index += 1;
|
||||
@@ -54,7 +52,7 @@ fn operator_match
|
||||
// Changing a value
|
||||
tokenise::Operator::Assignment =>
|
||||
{
|
||||
variables.insert(identifier.to_string(), value);
|
||||
variables.insert(identifier.to_owned(), value);
|
||||
} ,
|
||||
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::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 =>
|
||||
{
|
||||
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
|
||||
_ => value, // otherwise invalid
|
||||
|
||||
};
|
||||
variables.insert(identifier.to_string(), result);
|
||||
variables.insert(identifier.to_owned(), result);
|
||||
},
|
||||
// TODO choice assignment
|
||||
// TODO input assignment
|
||||
|
||||
@@ -47,14 +47,13 @@ pub fn keyword_parse(
|
||||
{
|
||||
// TODO can this go in a function?
|
||||
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;
|
||||
let mut result: bool = true;
|
||||
if keyword != "else"
|
||||
{
|
||||
let identifier = tokenise::get_identifier_token(tokens, index)
|
||||
.map(|err| err)?;
|
||||
let identifier = tokenise::get_identifier_token(tokens, index)?;
|
||||
(index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables)
|
||||
{
|
||||
Ok((increment, result)) => (increment,result),
|
||||
@@ -66,28 +65,22 @@ pub fn keyword_parse(
|
||||
}
|
||||
}
|
||||
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)
|
||||
.map(|err| err)?;
|
||||
index += 1;
|
||||
keyword = match tokenise::get_keyword_token(tokens,index)
|
||||
{
|
||||
Ok(keyword) => keyword,
|
||||
Err(_) => break,
|
||||
}
|
||||
Ok(keyword) => keyword,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
},
|
||||
"else" =>
|
||||
{
|
||||
index += 1;
|
||||
index = tokenise::get_closing_index(tokens, index)
|
||||
.map(|err| err)?;
|
||||
index = tokenise::get_closing_index(tokens, index)?;
|
||||
},
|
||||
"elif" =>
|
||||
{
|
||||
let current = "elif".to_string();
|
||||
loop
|
||||
{
|
||||
index += 1;
|
||||
|
||||
+8
-12
@@ -13,7 +13,7 @@ pub enum Token
|
||||
Bracket((Bracket,usize)), // Stores the index of the matching deliminator
|
||||
Character(String),
|
||||
}
|
||||
#[derive(Debug,Clone,PartialEq)]
|
||||
#[derive(Debug,Clone,PartialEq,Eq)]
|
||||
pub enum Value
|
||||
{
|
||||
String(String),
|
||||
@@ -142,15 +142,11 @@ pub fn tokenise(file_contents: &str)
|
||||
"<=" => Some(Operator::Comparison(Comparison::LessOrEqual)),
|
||||
_ => None,
|
||||
};
|
||||
match op
|
||||
if let Some(op) = op
|
||||
{
|
||||
Some(op) =>
|
||||
{
|
||||
tokenised_data.push(Token::Operator(op));
|
||||
index += 1;
|
||||
continue // skip the rest of this loop
|
||||
},
|
||||
None => (),
|
||||
tokenised_data.push(Token::Operator(op));
|
||||
index += 1;
|
||||
continue // skip the rest of this loop
|
||||
}
|
||||
// Characters
|
||||
if item.starts_with('@')
|
||||
@@ -168,9 +164,9 @@ pub fn tokenise(file_contents: &str)
|
||||
item = new_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"
|
||||
{
|
||||
@@ -181,7 +177,7 @@ pub fn tokenise(file_contents: &str)
|
||||
tokenised_data.push(Token::Value(Value::Bool(false)));
|
||||
}
|
||||
// variable/identifier
|
||||
else if item.starts_with("$")
|
||||
else if item.starts_with('$')
|
||||
{
|
||||
let mut chars = item.chars();
|
||||
chars.next();
|
||||
|
||||
Reference in New Issue
Block a user