initial commit
This commit is contained in:
commit
fddc303ef6
8 changed files with 197 additions and 0 deletions
83
src/main.rs
Normal file
83
src/main.rs
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue