initial commit

This commit is contained in:
Charlie Root 2025-03-26 21:41:27 +01:00
commit fddc303ef6
Signed by: faukah
SSH key fingerprint: SHA256:Uj2AXqvtdCA4hn5Hq0ZonhIAyUqI1q4w2sMG3Z1TH7E
8 changed files with 197 additions and 0 deletions

83
src/main.rs Normal file
View file

@ -0,0 +1,83 @@
use std::io;
#[derive(Debug)]
enum Symbol {
Number,
LeftBracket,
RightBracket,
Add,
Sub,
Mul,
Div,
}
#[derive(Debug)]
enum ParseError {
WrongTokenError { pos: usize },
}
fn main() {
loop {
let user_input = get_user_input().expect("No input");
match tokenize(&user_input) {
Ok(tokenized_input) => {
for el in &tokenized_input {
println!("{:?}", el);
}
}
Err(ParseError::WrongTokenError { pos }) => {
for _ in 0..pos {
print!("-");
}
println!("^");
println!("Syntax Error\n");
}
}
}
}
fn get_user_input() -> io::Result<String> {
let mut buffer = String::new();
let stdin = io::stdin();
stdin.read_line(&mut buffer)?;
Ok(buffer)
}
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;
}
_ => return Err(ParseError::WrongTokenError { pos: i }),
}
}
Ok(tokens)
}