main.rs: simplify tokenizer, add test

This commit is contained in:
Charlie Root 2025-03-27 09:33:33 +01:00
commit 1ad13b2693
Signed by: faukah
SSH key fingerprint: SHA256:Uj2AXqvtdCA4hn5Hq0ZonhIAyUqI1q4w2sMG3Z1TH7E

View file

@ -1,6 +1,6 @@
use std::io;
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
enum Symbol {
Number,
@ -44,38 +44,27 @@ fn get_user_input() -> io::Result<String> {
Ok(buffer)
}
#[test]
fn try_tokenizing() {
let tokenized_input = tokenize("1 + 1").unwrap();
let result = vec![Symbol::Number, Symbol::Add, Symbol::Number];
assert!(tokenized_input == result, "1 + 1 not working");
}
fn tokenize(input: &str) -> Result<Vec<Symbol>, ParseError> {
let mut tokens: Vec<Symbol> = vec![];
for (i, c) in input.chars().enumerate() {
match c {
' ' => {
continue;
}
'(' => {
tokens.push(Symbol::LeftBracket);
}
')' => {
tokens.push(Symbol::RightBracket);
}
'+' => {
tokens.push(Symbol::Add);
}
'-' => {
tokens.push(Symbol::Sub);
}
'*' => {
tokens.push(Symbol::Mul);
}
'/' => {
tokens.push(Symbol::Div);
}
'0'..='9' => {
tokens.push(Symbol::Number);
}
'\n' => {
break;
}
' ' => continue,
'(' => tokens.push(Symbol::LeftBracket),
')' => tokens.push(Symbol::RightBracket),
'+' => tokens.push(Symbol::Add),
'-' => tokens.push(Symbol::Sub),
'*' => tokens.push(Symbol::Mul),
'/' => tokens.push(Symbol::Div),
'0'..='9' => tokens.push(Symbol::Number),
'\n' => break,
_ => return Err(ParseError::WrongTokenError { pos: i }),
}
}