int and float parsing!

hell yes
This commit is contained in:
Schrottkatze 2021-12-20 18:36:04 +01:00
parent 500eb972bd
commit 73e7b1b7c7
2 changed files with 43 additions and 6 deletions

View file

@ -1,6 +1,10 @@
use super::preprocessor::preprocess; use super::preprocessor::preprocess;
use super::token::Token; use super::token::Token;
const digits: &str = "0123456789";
const alphabet: &str = "abcdefghijklmnopqrstuvwxyz";
const alphabet_c: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
pub struct Lexer { pub struct Lexer {
code: String, code: String,
pos: usize, pos: usize,
@ -31,16 +35,27 @@ impl Lexer {
loop { loop {
let token: Option<Token> = match self.current { 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::ADD),
Some('-') => Some(Token::SUBTRACT), Some('-') => Some(Token::SUBTRACT),
Some('*') => Some(Token::MULTIPLY), Some('*') => Some(Token::MULTIPLY),
Some('/') => Some(Token::DIVIDE), Some('/') => Some(Token::DIVIDE),
Some('(') => Some(Token::LBRACK), Some('(') => Some(Token::LBRACK),
Some(')') => Some(Token::RBRACK), Some(')') => Some(Token::RBRACK),
Some(' ') => None,
Some('\n') => None, // REST
None => break, Some(c) => {
Some(_) => None, if digits.contains(c) {
Some(self.make_nr_token())
} else {
None
}
},
}; };
match token { match token {
@ -54,4 +69,26 @@ impl Lexer {
println!("{:?}", tokens); 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());
}
}
} }

View file

@ -1,7 +1,7 @@
#[derive(Debug)] #[derive(Debug)]
pub enum Token { pub enum Token {
INT, INT(i32),
FLOAT, FLOAT(f32),
ADD, ADD,
SUBTRACT, SUBTRACT,
MULTIPLY, MULTIPLY,