added support for if elif else statements by returning a boolean
from the identifier syntax parsing
This commit is contained in:
@@ -12,6 +12,7 @@ use crate::
|
||||
info,
|
||||
mpsc::Receiver,
|
||||
};
|
||||
use super::identifier_parse;
|
||||
|
||||
pub fn keyword_parse(
|
||||
tokens: &[tokenise::Token],
|
||||
@@ -19,6 +20,7 @@ pub fn keyword_parse(
|
||||
mut index: usize,
|
||||
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
|
||||
labels: &HashMap<String, usize>,
|
||||
variables: &mut HashMap<String, tokenise::Value>,
|
||||
rx: &Receiver<(usize,String)>,
|
||||
)
|
||||
-> Result<usize, String>
|
||||
@@ -41,6 +43,60 @@ pub fn keyword_parse(
|
||||
};
|
||||
index = choice;
|
||||
},
|
||||
"if" =>
|
||||
{
|
||||
// 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??
|
||||
{
|
||||
index += 1;
|
||||
let mut result: bool = true;
|
||||
if keyword != "else"
|
||||
{
|
||||
let identifier = tokenise::get_identifier_token(tokens, index)
|
||||
.map(|err| err)?;
|
||||
(index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables)
|
||||
{
|
||||
Ok((increment, result)) => (increment,result),
|
||||
Err((err,increment)) =>
|
||||
{
|
||||
warn!("{err}");
|
||||
(increment,false)
|
||||
}
|
||||
}
|
||||
}
|
||||
if result { index += 1; break; }
|
||||
else
|
||||
{
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"else" =>
|
||||
{
|
||||
index += 1;
|
||||
index = tokenise::get_closing_index(tokens, index)
|
||||
.map(|err| err)?;
|
||||
},
|
||||
"elif" =>
|
||||
{
|
||||
let current = "elif".to_string();
|
||||
loop
|
||||
{
|
||||
index += 1;
|
||||
let Ok(new_index) = tokenise::get_closing_index(tokens, index)
|
||||
else { continue; };
|
||||
index = new_index;
|
||||
break
|
||||
}
|
||||
},
|
||||
"or" =>
|
||||
{
|
||||
info!("OR command, jumping over");
|
||||
|
||||
Reference in New Issue
Block a user