Init commit

This commit is contained in:
2026-06-13 01:20:34 +03:00
commit b82fbc168a
5 changed files with 97 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
use core::fmt;
use std::io;
use std::io::Write;
fn main() {
let stdin = io::stdin();
let mut buffer = String::new();
loop {
print!("> ");
io::stdout().flush().unwrap();
stdin.read_line(&mut buffer).unwrap();
let tokens = tokenisator(&buffer);
buffer.clear();
}
}
fn tokenisator(task: &str) -> Vec<Token> {
let mut tokens = Vec::<Token>::new();
let chars: Vec<char> = task.chars().collect();
let mut pointer = 0;
while pointer < task.len() {
dbg!(pointer, task.len());
let c = chars[pointer];
match c {
'0' .. '9' => {
let mut end = pointer;
while end < task.len() {
let cur_c = chars[end];
if !cur_c.is_ascii_digit() {
break;
}
end += 1;
}
tokens.push(Token::Number(
task.get(pointer..end).unwrap().parse::<i32>().unwrap()
));
pointer = end-1;
}
'+' => tokens.push(Token::Addition),
'-' => tokens.push(Token::Substruction),
'*' => tokens.push(Token::Multiplication),
'/' => tokens.push(Token::Division),
'(' => tokens.push(Token::BracketOpen),
')' => tokens.push(Token::BracketClose),
_ => {}
}
pointer += 1;
}
println!("{}", task);
for t in &tokens {
println!("{:?}", t);
}
return tokens;
}
#[derive(Debug)]
enum Token {
Number(i32),
Addition,
Substruction,
Multiplication,
Division,
BracketOpen,
BracketClose,
}