int and float parsing!
hell yes
This commit is contained in:
parent
500eb972bd
commit
73e7b1b7c7
2 changed files with 43 additions and 6 deletions
|
@ -1,6 +1,10 @@
|
|||
use super::preprocessor::preprocess;
|
||||
use super::token::Token;
|
||||
|
||||
const digits: &str = "0123456789";
|
||||
const alphabet: &str = "abcdefghijklmnopqrstuvwxyz";
|
||||
const alphabet_c: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
pub struct Lexer {
|
||||
code: String,
|
||||
pos: usize,
|
||||
|
@ -31,16 +35,27 @@ impl Lexer {
|
|||
|
||||
loop {
|
||||
let token: Option<Token> = match self.current {
|
||||
// SKIP OR BREAK
|
||||
Some(' ') => None,
|
||||
Some('\n') => None,
|
||||
None => break,
|
||||
|
||||
// OPERATORS
|
||||
Some('+') => Some(Token::ADD),
|
||||
Some('-') => Some(Token::SUBTRACT),
|
||||
Some('*') => Some(Token::MULTIPLY),
|
||||
Some('/') => Some(Token::DIVIDE),
|
||||
Some('(') => Some(Token::LBRACK),
|
||||
Some(')') => Some(Token::RBRACK),
|
||||
Some(' ') => None,
|
||||
Some('\n') => None,
|
||||
None => break,
|
||||
Some(_) => None,
|
||||
|
||||
// REST
|
||||
Some(c) => {
|
||||
if digits.contains(c) {
|
||||
Some(self.make_nr_token())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
match token {
|
||||
|
@ -54,4 +69,26 @@ impl Lexer {
|
|||
|
||||
println!("{:?}", tokens);
|
||||
}
|
||||
|
||||
fn make_nr_token(&mut self) -> Token {
|
||||
let mut nr: String = String::new();
|
||||
let mut dot_amount: u8 = 0;
|
||||
|
||||
while (self.current != None) && (digits.contains(self.current.unwrap()) || self.current.unwrap() == '.') {
|
||||
if self.current.unwrap() == '.' {
|
||||
if dot_amount == 1 {
|
||||
panic!("Unexpected additional '.' in Number.");
|
||||
}
|
||||
dot_amount += 1;
|
||||
}
|
||||
nr.push(self.current.unwrap());
|
||||
self.next();
|
||||
}
|
||||
|
||||
if dot_amount == 1 {
|
||||
return Token::FLOAT(nr.parse::<f32>().unwrap());
|
||||
} else {
|
||||
return Token::INT(nr.parse::<i32>().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#[derive(Debug)]
|
||||
pub enum Token {
|
||||
INT,
|
||||
FLOAT,
|
||||
INT(i32),
|
||||
FLOAT(f32),
|
||||
ADD,
|
||||
SUBTRACT,
|
||||
MULTIPLY,
|
||||
|
|
Loading…
Reference in a new issue