Compare commits

..

No commits in common. "1ad13b2693829083ee26f7f96cd6821c49fde957" and "fddc303ef65cf8ac1ef45c48f730e4ccc3422400" have entirely different histories.

2 changed files with 28 additions and 18 deletions

View file

@ -23,7 +23,6 @@
cargo cargo
rustc rustc
rustfmt rustfmt
bacon
; ;
inherit inherit
(pkgsFor.${system}.rustPackages) (pkgsFor.${system}.rustPackages)

View file

@ -1,6 +1,6 @@
use std::io; use std::io;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug)]
enum Symbol { enum Symbol {
Number, Number,
@ -44,27 +44,38 @@ fn get_user_input() -> io::Result<String> {
Ok(buffer) 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> { fn tokenize(input: &str) -> Result<Vec<Symbol>, ParseError> {
let mut tokens: Vec<Symbol> = vec![]; let mut tokens: Vec<Symbol> = vec![];
for (i, c) in input.chars().enumerate() { for (i, c) in input.chars().enumerate() {
match c { match c {
' ' => continue, ' ' => {
'(' => tokens.push(Symbol::LeftBracket), continue;
')' => tokens.push(Symbol::RightBracket), }
'+' => tokens.push(Symbol::Add), '(' => {
'-' => tokens.push(Symbol::Sub), tokens.push(Symbol::LeftBracket);
'*' => tokens.push(Symbol::Mul), }
'/' => tokens.push(Symbol::Div), ')' => {
'0'..='9' => tokens.push(Symbol::Number), tokens.push(Symbol::RightBracket);
'\n' => break, }
'+' => {
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 }), _ => return Err(ParseError::WrongTokenError { pos: i }),
} }
} }