Skip to content

Commit 9e05814

Browse files
committed
fmt of the code and added parser
1 parent 6884f55 commit 9e05814

6 files changed

Lines changed: 73 additions & 77 deletions

File tree

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"rust-analyzer.cargo.features": "all",
4+
// "rust-analyzer.cargo.loadOutDirsFromCheck": true,
5+
// "rust-analyzer.checkOnSave.command": "clippy",
6+
// "rust-analyzer.checkOnSave.allTargets": true,
7+
}

src/lexericals/lexer.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::lexericals::tokenizer::{Token, Tokenkind};
33
#[derive(Debug)]
44
pub struct Lexer {
55
pub input: Vec<char>,
6-
// pub current: usize,
6+
// pub current: usize,
77
pub next: usize,
88
pub ch: char,
99
}
@@ -39,10 +39,10 @@ impl Lexer {
3939
self.read_token(); // Read the next character to check for '=='
4040
if self.ch == '=' {
4141
Token::new(Tokenkind::Eq, "==".to_string())
42-
}else {
42+
} else {
4343
Token::new(Tokenkind::Assign, "=".to_string())
4444
}
45-
},
45+
}
4646
'+' => Token::new(Tokenkind::Plus, self.ch.to_string()),
4747
'-' => Token::new(Tokenkind::Minus, self.ch.to_string()),
4848
',' => Token::new(Tokenkind::Comma, self.ch.to_string()),
@@ -66,7 +66,7 @@ impl Lexer {
6666
} else {
6767
Token::new(Tokenkind::Illegal, "|".to_string())
6868
}
69-
},
69+
}
7070
'*' => Token::new(Tokenkind::Asterisk, self.ch.to_string()),
7171
'/' => Token::new(Tokenkind::Slash, self.ch.to_string()),
7272
'%' => Token::new(Tokenkind::Pencentage, self.ch.to_string()),
@@ -77,16 +77,15 @@ impl Lexer {
7777
} else {
7878
Token::new(Tokenkind::Lthan, "<".to_string())
7979
}
80-
81-
},
82-
'>' =>{
80+
}
81+
'>' => {
8382
self.read_token(); // Read the next character to check for '>='
8483
if self.ch == '=' {
8584
Token::new(Tokenkind::Gthanoreq, ">=".to_string())
8685
} else {
8786
Token::new(Tokenkind::Gthan, ">".to_string())
8887
}
89-
},
88+
}
9089
'&' => {
9190
self.read_token(); // Read the next character to check for '&&'
9291
if self.ch == '&' {
@@ -257,7 +256,7 @@ mod test {
257256
let mut lexer = Lexer::new(input);
258257
let token1 = lexer.next_token();
259258
assert_eq!(token1.kind, Tokenkind::Int);
260-
assert_eq!(token1.literal, "12345");
259+
assert_eq!(token1.literal, "12345");
261260
}
262261
#[test]
263262
fn test_read_float() {
@@ -294,7 +293,6 @@ mod test {
294293
let token5 = lexer.next_token();
295294
assert_eq!(token5.kind, Tokenkind::Return);
296295
assert_eq!(token5.literal, "return");
297-
298296
}
299297
#[test]
300298
fn test_read_identifiers() {

src/lexericals/tokenizer.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub enum Tokenkind {
2020
Illegal,
2121
Eof,
2222
Ident,
23-
2423

2524
// Types
2625
Int,
@@ -71,7 +70,6 @@ impl Display for Tokenkind {
7170
Tokenkind::Illegal => write!(f, "Illegal"),
7271
Tokenkind::Eof => write!(f, "Eof"),
7372
Tokenkind::Ident => write!(f, "Ident"),
74-
7573

7674
// Types
7775
Tokenkind::Int => write!(f, "Int"),
@@ -80,8 +78,6 @@ impl Display for Tokenkind {
8078
Tokenkind::Char => write!(f, "Char"),
8179
Tokenkind::Bool => write!(f, "Bool"),
8280

83-
84-
8581
// mathematical Operators
8682
Tokenkind::Plus => write!(f, "Plus"),
8783
Tokenkind::Minus => write!(f, "Minus"),
@@ -108,7 +104,7 @@ impl Display for Tokenkind {
108104
Tokenkind::OR => write!(f, "OR"),
109105
Tokenkind::And => write!(f, "And"),
110106
Tokenkind::Bang => write!(f, "Bang"),
111-
107+
112108
// Keywords
113109
Tokenkind::Function => write!(f, "Function"),
114110
Tokenkind::Let => write!(f, "Let"),
@@ -121,7 +117,7 @@ impl Display for Tokenkind {
121117

122118
impl Tokenkind {
123119
pub fn lookup_ident(ident: &str) -> Self {
124-
match ident{
120+
match ident {
125121
"fn" => Self::Function,
126122
"let" => Self::Let,
127123
"if" => Self::If,

src/main.rs

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod lexericals;
2+
mod parsers;
23
mod repl;
34
use std::io;
45

@@ -12,10 +13,8 @@ fn main() {
1213
println!("press ctrl+c to exit the terminal");
1314
start(io::stdin(), io::stdout());
1415
//print_tokens();
15-
1616
}
1717

18-
1918
fn print_tokens() {
2019
let mut lexer = Lexer::new(read_input());
2120
let runer = expected_token();
@@ -34,7 +33,6 @@ fn print_tokens() {
3433
}
3534
}
3635

37-
3836
fn read_input() -> &'static str {
3937
let input = r#"
4038
let five = 5000000;
@@ -50,57 +48,54 @@ fn read_input() -> &'static str {
5048
input
5149
}
5250
fn expected_token() -> Vec<Token> {
53-
54-
let expect_tokens = vec![
55-
Token::new(Tokenkind::Let, String::from("let")),
56-
Token::new(Tokenkind::Ident, String::from("five")),
57-
Token::new(Tokenkind::Assign, String::from("=")),
58-
Token::new(Tokenkind::Int, String::from("5000000")),
59-
Token::new(Tokenkind::Semicolon, String::from(";")),
60-
Token::new(Tokenkind::Let, String::from("let")),
61-
Token::new(Tokenkind::Ident, String::from("floattest")),
62-
Token::new(Tokenkind::Assign, String::from("=")),
63-
Token::new(Tokenkind::Float, String::from("5.0")),
64-
Token::new(Tokenkind::Semicolon, String::from(";")),
65-
Token::new(Tokenkind::Let, String::from("let")),
66-
Token::new(Tokenkind::Ident, String::from("ten")),
67-
Token::new(Tokenkind::Assign, String::from("=")),
68-
Token::new(Tokenkind::Int, String::from("10")),
69-
Token::new(Tokenkind::Semicolon, String::from(";")),
70-
Token::new(Tokenkind::Let, String::from("let")),
71-
Token::new(Tokenkind::Ident, String::from("add")),
72-
Token::new(Tokenkind::Assign, String::from("=")),
73-
Token::new(Tokenkind::Function, String::from("fn")),
74-
Token::new(Tokenkind::Lparen, String::from("(")),
75-
Token::new(Tokenkind::Ident, String::from("x")),
76-
Token::new(Tokenkind::Comma, String::from(",")),
77-
Token::new(Tokenkind::Ident, String::from("y")),
78-
Token::new(Tokenkind::Rparen, String::from(")")),
79-
Token::new(Tokenkind::Lbrace, String::from("{")),
80-
Token::new(Tokenkind::Ident, String::from("x")),
81-
Token::new(Tokenkind::Plus, String::from("+")),
82-
Token::new(Tokenkind::Ident, String::from("y")),
83-
Token::new(Tokenkind::Semicolon, String::from(";")),
84-
Token::new(Tokenkind::Rbrace, String::from("}")),
85-
Token::new(Tokenkind::Semicolon, String::from(";")),
86-
Token::new(Tokenkind::Let, String::from("let")),
87-
Token::new(Tokenkind::Ident, String::from("result")),
88-
Token::new(Tokenkind::Assign, String::from("=")),
89-
Token::new(Tokenkind::Ident, String::from("add")),
90-
Token::new(Tokenkind::Lparen, String::from("(")),
91-
Token::new(Tokenkind::Ident, String::from("five")),
92-
Token::new(Tokenkind::Comma, String::from(",")),
93-
Token::new(Tokenkind::Ident, String::from("ten")),
94-
Token::new(Tokenkind::Rparen, String::from(")")),
95-
Token::new(Tokenkind::Semicolon, String::from(";")),
96-
Token::new(Tokenkind::Eq, String::from("==")),
97-
Token::new(Tokenkind::Bool, String::from("True")),
98-
Token::new(Tokenkind::Semicolon, String::from(";")),
99-
Token::new(Tokenkind::Bool, String::from("False")),
100-
Token::new(Tokenkind::Semicolon, String::from(";")),
101-
Token::new(Tokenkind::Eof, String::new()),
102-
];
51+
let expect_tokens = vec![
52+
Token::new(Tokenkind::Let, String::from("let")),
53+
Token::new(Tokenkind::Ident, String::from("five")),
54+
Token::new(Tokenkind::Assign, String::from("=")),
55+
Token::new(Tokenkind::Int, String::from("5000000")),
56+
Token::new(Tokenkind::Semicolon, String::from(";")),
57+
Token::new(Tokenkind::Let, String::from("let")),
58+
Token::new(Tokenkind::Ident, String::from("floattest")),
59+
Token::new(Tokenkind::Assign, String::from("=")),
60+
Token::new(Tokenkind::Float, String::from("5.0")),
61+
Token::new(Tokenkind::Semicolon, String::from(";")),
62+
Token::new(Tokenkind::Let, String::from("let")),
63+
Token::new(Tokenkind::Ident, String::from("ten")),
64+
Token::new(Tokenkind::Assign, String::from("=")),
65+
Token::new(Tokenkind::Int, String::from("10")),
66+
Token::new(Tokenkind::Semicolon, String::from(";")),
67+
Token::new(Tokenkind::Let, String::from("let")),
68+
Token::new(Tokenkind::Ident, String::from("add")),
69+
Token::new(Tokenkind::Assign, String::from("=")),
70+
Token::new(Tokenkind::Function, String::from("fn")),
71+
Token::new(Tokenkind::Lparen, String::from("(")),
72+
Token::new(Tokenkind::Ident, String::from("x")),
73+
Token::new(Tokenkind::Comma, String::from(",")),
74+
Token::new(Tokenkind::Ident, String::from("y")),
75+
Token::new(Tokenkind::Rparen, String::from(")")),
76+
Token::new(Tokenkind::Lbrace, String::from("{")),
77+
Token::new(Tokenkind::Ident, String::from("x")),
78+
Token::new(Tokenkind::Plus, String::from("+")),
79+
Token::new(Tokenkind::Ident, String::from("y")),
80+
Token::new(Tokenkind::Semicolon, String::from(";")),
81+
Token::new(Tokenkind::Rbrace, String::from("}")),
82+
Token::new(Tokenkind::Semicolon, String::from(";")),
83+
Token::new(Tokenkind::Let, String::from("let")),
84+
Token::new(Tokenkind::Ident, String::from("result")),
85+
Token::new(Tokenkind::Assign, String::from("=")),
86+
Token::new(Tokenkind::Ident, String::from("add")),
87+
Token::new(Tokenkind::Lparen, String::from("(")),
88+
Token::new(Tokenkind::Ident, String::from("five")),
89+
Token::new(Tokenkind::Comma, String::from(",")),
90+
Token::new(Tokenkind::Ident, String::from("ten")),
91+
Token::new(Tokenkind::Rparen, String::from(")")),
92+
Token::new(Tokenkind::Semicolon, String::from(";")),
93+
Token::new(Tokenkind::Eq, String::from("==")),
94+
Token::new(Tokenkind::Bool, String::from("True")),
95+
Token::new(Tokenkind::Semicolon, String::from(";")),
96+
Token::new(Tokenkind::Bool, String::from("False")),
97+
Token::new(Tokenkind::Semicolon, String::from(";")),
98+
Token::new(Tokenkind::Eof, String::new()),
99+
];
103100
expect_tokens
104101
}
105-
106-

src/parsers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/repl.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use std::io::{Stdout,Stdin,Write};
1+
use std::io::{Stdin, Stdout, Write};
22

33
use crate::lexericals::{lexer::Lexer, tokenizer::Tokenkind};
44

5-
65
pub fn start(stdin: Stdin, mut stdout: Stdout) {
7-
loop{
6+
loop {
87
write!(stdout, ">>> ").expect("Failed to write >> to stdout");
98
stdout.flush().expect("Failed to flush stdout");
109

1110
let mut input = String::new();
1211

1312
if let Err(e) = stdin.read_line(&mut input) {
14-
writeln!(stdout, "Error reading input: {}", e).expect("Failed to write error to stdout");
13+
writeln!(stdout, "Error reading input: {}", e)
14+
.expect("Failed to write error to stdout");
1515
stdout.flush().expect("Failed to flush stdout");
1616
return;
1717
}
@@ -29,7 +29,6 @@ pub fn start(stdin: Stdin, mut stdout: Stdout) {
2929
}
3030
}
3131

32-
3332
// #[cfg(test)]
3433
// mod tests {
3534
// use super::*;
@@ -46,4 +45,4 @@ pub fn start(stdin: Stdin, mut stdout: Stdout) {
4645
// assert!(output.contains("Token:"));
4746
// assert!(output.contains("End of input"));
4847
// }
49-
// }
48+
// }

0 commit comments

Comments
 (0)