Skip to content

Commit eda3c78

Browse files
committed
added logic for float and clean up passer
1 parent 3b9bd27 commit eda3c78

4 files changed

Lines changed: 112 additions & 137 deletions

File tree

src/lexericals/lexer.rs

Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lexericals::tokenizer::{Token,Tokenkind};
1+
use crate::lexericals::tokenizer::{Token, Tokenkind};
22

33
#[derive(Debug)]
44
pub struct Lexer {
@@ -9,7 +9,7 @@ pub struct Lexer {
99
}
1010

1111
impl Lexer {
12-
pub fn new(input:&str) -> Lexer{
12+
pub fn new(input: &str) -> Lexer {
1313
let mut lex = Lexer {
1414
input: input.chars().collect(),
1515
current: 0,
@@ -20,19 +20,19 @@ impl Lexer {
2020
lex
2121
}
2222

23-
pub fn read_token(&mut self){
23+
pub fn read_token(&mut self) {
2424
let current_pos = self.next;
2525
if current_pos >= self.input.len() {
2626
self.ch = '\0'; // End of input
27-
}else{
28-
self.ch = self.input[current_pos];
27+
} else {
28+
self.ch = self.input[current_pos];
2929
}
3030
self.current = current_pos;
3131
self.next += 1;
3232
}
3333

3434
pub fn next_token(&mut self) -> Token {
35-
//"=+(){},;";
35+
//"=+(){},;";
3636
self.skip_whitespace(); // Skip whitespace characters
3737
let token = match self.ch {
3838
'=' => Token::new(Tokenkind::Assign, self.ch.to_string()),
@@ -46,30 +46,23 @@ impl Lexer {
4646
';' => Token::new(Tokenkind::Semicolon, self.ch.to_string()),
4747
'\0' => Token::new(Tokenkind::Eof, String::new()), // End of file token
4848

49-
_ => return if Lexer::is_letter(self.ch){
50-
let literal = self.read_identifier();
51-
let kind = Tokenkind::lookup_ident(&literal);
52-
Token::new(kind, literal)
53-
}else if Lexer::is_digit(self.ch) {
54-
let literal = self.read_number();
55-
let kind = if literal.contains('.') {
56-
Tokenkind::Float // If it contains a dot, treat it as a float
49+
_ => {
50+
return if Lexer::is_letter(self.ch) {
51+
let literal = self.read_identifier();
52+
let kind = Tokenkind::lookup_ident(&literal);
53+
Token::new(kind, literal)
54+
} else if Lexer::is_digit(self.ch) {
55+
let literal = self.read_number();
56+
let kind = if literal.contains('.') {
57+
Tokenkind::Float // If it contains a dot, treat it as a float
58+
} else {
59+
Tokenkind::Int // Otherwise, treat it as an integer
60+
};
61+
Token::new(kind, literal)
5762
} else {
58-
Tokenkind::Int // Otherwise, treat it as an integer
63+
Token::new(Tokenkind::Illegal, self.ch.to_string())
5964
};
60-
Token::new(kind, literal)
61-
6265
}
63-
//else if Lexer::is_float(self.ch){
64-
// let kind = Tokenkind::Float;
65-
// let literal = self.read_float();
66-
// Token::new(kind, literal)
67-
// }
68-
else {
69-
Token::new(Tokenkind::Illegal, self.ch.to_string())
70-
},
71-
72-
7366
};
7467
self.read_token(); // Move to the next character
7568
token
@@ -88,17 +81,15 @@ impl Lexer {
8881
fn is_letter(ch: char) -> bool {
8982
ch.is_alphabetic() || ch == '_'
9083
}
91-
92-
// fn is_float(ch: char) -> bool {
93-
// ch.is_numeric() || ch == '.'
94-
// }
95-
9684
fn read_number(&mut self) -> String {
9785
let mut literal = String::new();
9886
while Lexer::is_digit(self.ch) {
9987
literal.push(self.ch);
10088
self.read_token();
10189
}
90+
91+
// Check for a decimal point to handle floats
92+
// If the next character is a dot, we treat it as a float
10293
if self.ch == '.' {
10394
literal.push(self.ch);
10495
self.read_token();
@@ -110,37 +101,22 @@ impl Lexer {
110101
literal
111102
}
112103

113-
// fn read_float(&mut self) -> String {
114-
// let mut literal = String::new();
115-
// if self.ch == '.' {
116-
// literal.push(self.ch);
117-
// self.read_token();
118-
// }
119-
// while Lexer::is_float(self.ch) {
120-
// literal.push(self.ch);
121-
// self.read_token();
122-
// }
123-
// literal
124-
// }
125-
126104
fn read_identifier(&mut self) -> String {
127-
let mut literal = String::new();
128-
while Lexer::is_letter(self.ch){
105+
let mut literal = String::new();
106+
while Lexer::is_letter(self.ch) {
129107
literal.push(self.ch);
130108
self.read_token();
131109
}
132110
literal
133111
}
134-
135-
136112
}
137113

138114
#[cfg(test)]
139115
mod test {
140116
use super::*;
141117
#[test]
142118
fn read_token() {
143-
let input =r#"
119+
let input = r#"
144120
let five = 5000000;
145121
let floattest = 5.0;
146122
let ten = 10;
@@ -166,11 +142,6 @@ mod test {
166142
Token::new(Tokenkind::Assign, String::from("=")),
167143
Token::new(Tokenkind::Int, String::from("10")),
168144
Token::new(Tokenkind::Semicolon, String::from(";")),
169-
// Token::new(Tokenkind::Let, String::from("let")),
170-
// Token::new(Tokenkind::Ident, String::from("ten")),
171-
// Token::new(Tokenkind::Assign, String::from("=")),
172-
// Token::new(Tokenkind::Int, String::from("10")),
173-
// Token::new(Tokenkind::Semicolon, String::from(";")),
174145
Token::new(Tokenkind::Let, String::from("let")),
175146
Token::new(Tokenkind::Ident, String::from("add")),
176147
Token::new(Tokenkind::Assign, String::from("=")),
@@ -200,12 +171,18 @@ mod test {
200171
Token::new(Tokenkind::Eof, String::new()),
201172
];
202173
let mut lexer = Lexer::new(input);
203-
for (idx,exp_token) in expect_token.iter().enumerate() {
174+
for (idx, exp_token) in expect_token.iter().enumerate() {
204175
let recv_token = lexer.next_token();
205-
assert_eq!(exp_token.kind,recv_token.kind,
206-
"fail to read token at {} expected token {} found {}",idx,exp_token.kind,recv_token.kind);
207-
assert_eq!(exp_token.literal,recv_token.literal,
208-
"fail to read token at {} expectec token {} found {}",idx,exp_token.literal,recv_token.literal); // Move to the next character
176+
assert_eq!(
177+
exp_token.kind, recv_token.kind,
178+
"fail to read token at {} expected token {} found {}",
179+
idx, exp_token.kind, recv_token.kind
180+
);
181+
assert_eq!(
182+
exp_token.literal, recv_token.literal,
183+
"fail to read token at {} expectec token {} found {}",
184+
idx, exp_token.literal, recv_token.literal
185+
); // Move to the next character
209186
}
210187
}
211-
}
188+
}

src/lexericals/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod lexer;
2-
pub mod tokenizer;
2+
pub mod tokenizer;

src/lexericals/tokenizer.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::fmt::Display;
22

33
#[allow(dead_code, unused_variables)]
4-
#[derive(Debug,PartialEq)]
5-
pub struct Token{
4+
#[derive(Debug, PartialEq)]
5+
pub struct Token {
66
pub kind: Tokenkind,
77
pub literal: String,
88
}
@@ -11,11 +11,10 @@ impl Token {
1111
pub fn new(kind: Tokenkind, literal: String) -> Self {
1212
Self { kind, literal }
1313
}
14-
1514
}
1615

1716
#[allow(dead_code, unused_variables)]
18-
#[derive(Debug,PartialEq)]
17+
#[derive(Debug, PartialEq)]
1918
pub enum Tokenkind {
2019
Illegal,
2120
Eof,
@@ -46,7 +45,6 @@ pub enum Tokenkind {
4645
If,
4746
Else,
4847
Return,
49-
5048
}
5149

5250
impl Display for Tokenkind {
@@ -85,8 +83,8 @@ impl Display for Tokenkind {
8583
}
8684
}
8785

88-
impl Tokenkind{
89-
pub fn lookup_ident(ident: &String) -> Self{
86+
impl Tokenkind {
87+
pub fn lookup_ident(ident: &str) -> Self {
9088
match ident as &str {
9189
"fn" => Self::Function,
9290
"let" => Self::Let,
@@ -99,14 +97,14 @@ impl Tokenkind{
9997
}
10098

10199
#[cfg(test)]
102-
mod test{
100+
mod test {
103101
use super::*;
104102

105-
// impl Display for Token {
106-
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107-
// write!(f, "Token(kind: {}, literal: {})", self.kind, self.literal)
108-
// }
109-
// }
103+
// impl Display for Token {
104+
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105+
// write!(f, "Token(kind: {}, literal: {})", self.kind, self.literal)
106+
// }
107+
// }
110108

111109
#[test]
112110
fn test_token_display() {
@@ -121,7 +119,5 @@ mod test{
121119
fn test_tokenkind_display() {
122120
let token_kind = Tokenkind::Eof;
123121
assert_eq!(token_kind.to_string(), "Eof");
124-
}
125-
126-
127-
}
122+
}
123+
}

0 commit comments

Comments
 (0)