Compare commits
2 commits
fddc303ef6
...
1ad13b2693
Author | SHA1 | Date | |
---|---|---|---|
1ad13b2693 |
|||
7842cb4b84 |
2 changed files with 18 additions and 28 deletions
|
@ -23,6 +23,7 @@
|
|||
cargo
|
||||
rustc
|
||||
rustfmt
|
||||
bacon
|
||||
;
|
||||
inherit
|
||||
(pkgsFor.${system}.rustPackages)
|
||||
|
|
45
src/main.rs
45
src/main.rs
|
@ -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 }),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue