From 73e7b1b7c76f8c6365fc816e00c0196d856f9adb Mon Sep 17 00:00:00 2001 From: Gabriel <68819302+obsidianical@users.noreply.github.com> Date: Mon, 20 Dec 2021 18:36:04 +0100 Subject: [PATCH] int and float parsing! hell yes --- src/parsing/lexer.rs | 45 ++++++++++++++++++++++++++++++++++++++++---- src/parsing/token.rs | 4 ++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/parsing/lexer.rs b/src/parsing/lexer.rs index 78ae7da..88860c4 100644 --- a/src/parsing/lexer.rs +++ b/src/parsing/lexer.rs @@ -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 = 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::().unwrap()); + } else { + return Token::INT(nr.parse::().unwrap()); + } + } } diff --git a/src/parsing/token.rs b/src/parsing/token.rs index 9622421..d688b48 100644 --- a/src/parsing/token.rs +++ b/src/parsing/token.rs @@ -1,7 +1,7 @@ #[derive(Debug)] pub enum Token { - INT, - FLOAT, + INT(i32), + FLOAT(f32), ADD, SUBTRACT, MULTIPLY,