Created
September 4, 2015 20:29
-
-
Save anonymous/945d102a14ca519644f4 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Scanner { | |
input: String, | |
output: Vec<Token> | |
} | |
#[derive(Debug)] | |
enum Token { | |
Integer(i64), | |
Addition, | |
Subtraction, | |
Multiplication, | |
Division, | |
Modulus, | |
Exponent, | |
Assignment, | |
Terminator, | |
Variable(char), | |
Unknown(char), | |
} | |
impl Scanner { | |
fn new(input: &str) -> Scanner { | |
Scanner{ | |
input: input.to_string(), | |
output: Vec::new(), | |
} | |
} | |
fn scan(&mut self) { | |
let mut chars = self.input.chars().peekable(); | |
while let Some(c) = chars.next() { | |
let tok = match c { | |
'0'...'9' => { | |
let mut number = 0; | |
let mut current = c; | |
loop { | |
number *= 10; | |
number += ((current as u8) - ('0' as u8)) as i64; | |
match chars.peek() { | |
Some(&next) if '0' <= next && next <= '9' => { | |
current = next; | |
chars.next(); | |
} | |
_ => break, | |
}; | |
} | |
Token::Integer(number) | |
}, | |
'+' => Token::Addition, | |
'-' => Token::Subtraction, | |
'*' => Token::Multiplication, | |
'/' => Token::Division, | |
'%' => Token::Modulus, | |
'^' => Token::Exponent, | |
'=' => Token::Assignment, | |
';' => Token::Terminator, | |
'a'...'z' => Token::Variable(c), | |
'A'...'Z' => Token::Variable(std::ascii::AsciiExt::to_ascii_lowercase(&c)), | |
_ => Token::Unknown(c) | |
}; | |
self.output.push(tok); | |
} | |
} | |
fn output(&self) -> &[Token] { | |
&self.output | |
} | |
} | |
fn main() { | |
let program = "+ - 34 ; quit a 3 3 - - 1 * ^ 7"; | |
let mut s = Scanner::new(program); | |
s.scan(); | |
for line in s.output() { | |
println!("{:?}",line); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment