added multi-char operators and separators

This commit is contained in:
afonya2 2025-05-23 20:45:50 +02:00
parent b4886eff87
commit 27000668c2
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC

View file

@ -31,10 +31,20 @@ fn is_operator(char: &str) -> bool {
let chars = vec!["+","-","*","/","^","%","|","&","!"];
return chars.contains(&char);
}
fn is_mul_operator(char: &str, next_char: &str) -> bool {
let chars = vec!["==","!=","<=",">="];
let check = String::from(char) + next_char;
return chars.contains(&check.as_str());
}
fn is_sep(char: &str) -> bool {
let chars = vec!["(",")","[","]","{","}",",",".","="];
return chars.contains(&char);
}
fn is_mul_sep(char: &str, next_char: &str) -> bool {
let chars = vec!["=>"];
let check = String::from(char) + next_char;
return chars.contains(&check.as_str());
}
fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec<Token>) {
let mut str = String::from("");
@ -227,8 +237,12 @@ pub fn lex(input: String) -> Vec<Token> {
read_comment(&splitted, &mut pos, false);
} else if splitted.len() >= pos+1 && splitted[(pos-1)..(pos+1)].join("") == "/*" {
read_comment(&splitted, &mut pos, true);
} else if is_mul_operator(char, splitted[pos]) {
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
} else if is_operator(char) {
out.push(Token { typ: TokenType::OPERATOR, value: String::from(char), pos: pos-1 });
} else if is_mul_sep(char, splitted[pos]) {
out.push(Token { typ: TokenType::SEPARATOR, value: String::from(char) + splitted[pos], pos: pos-1 });
} else if is_sep(char) {
out.push(Token { typ: TokenType::SEPARATOR, value: String::from(char), pos: pos-1 });
} else {