Compare commits
No commits in common. "1ad13b2693829083ee26f7f96cd6821c49fde957" and "fddc303ef65cf8ac1ef45c48f730e4ccc3422400" have entirely different histories.
1ad13b2693
...
fddc303ef6
2 changed files with 28 additions and 18 deletions
|
@ -23,7 +23,6 @@
|
|||
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, PartialEq, Eq)]
|
||||
#[derive(Debug)]
|
||||
enum Symbol {
|
||||
Number,
|
||||
|
||||
|
@ -44,27 +44,38 @@ 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