Skip to content

Commit 5f106f8

Browse files
committed
work on REPL
1 parent 97b9b45 commit 5f106f8

4 files changed

Lines changed: 136 additions & 120 deletions

File tree

src/lexericals/lexer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ mod test {
175175
x + y;
176176
};
177177
let result = add(five, ten);
178-
==
178+
== True; False;
179179
"#;
180180

181181
let expect_token = vec![
@@ -221,6 +221,10 @@ mod test {
221221
Token::new(Tokenkind::Rparen, String::from(")")),
222222
Token::new(Tokenkind::Semicolon, String::from(";")),
223223
Token::new(Tokenkind::Eq, String::from("==")),
224+
Token::new(Tokenkind::Bool, String::from("True")),
225+
Token::new(Tokenkind::Semicolon, String::from(";")),
226+
Token::new(Tokenkind::Bool, String::from("False")),
227+
Token::new(Tokenkind::Semicolon, String::from(";")),
224228
Token::new(Tokenkind::Eof, String::new()),
225229
];
226230
let mut lexer = Lexer::new(input);

src/lexericals/tokenizer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub enum Tokenkind {
2727
Float,
2828
String,
2929
Char,
30+
Bool,
3031

3132
// mathematical Operators
3233
Plus,
@@ -77,6 +78,7 @@ impl Display for Tokenkind {
7778
Tokenkind::Float => write!(f, "Float"),
7879
Tokenkind::String => write!(f, "String"),
7980
Tokenkind::Char => write!(f, "Char"),
81+
Tokenkind::Bool => write!(f, "Bool"),
8082

8183

8284

@@ -125,6 +127,8 @@ impl Tokenkind {
125127
"if" => Self::If,
126128
"else" => Self::Else,
127129
"return" => Self::Return,
130+
"True" => Self::Bool,
131+
"False" => Self::Bool,
128132
_ => Self::Ident,
129133
}
130134
}

src/main.rs

Lines changed: 78 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
11
mod lexericals;
2+
mod repl;
3+
use std::io;
4+
25
use crate::lexericals::lexer::Lexer;
36
use crate::lexericals::tokenizer::{Token, Tokenkind};
7+
use crate::repl::start;
48

59
fn main() {
6-
// let token = Token {
7-
// kind: Tokenkind::Ident,
8-
// literal: String::from("example"),
9-
// };
10-
// println!("Token kind: {:?}, literal: {}", token.kind, token.literal);
11-
// println!("Token {:?}", token);
12-
13-
let input = r#"
14-
let five = 5000000;
15-
let floattest = 5.0;
16-
let ten = 10;
17-
let add = fn(x, y) {
18-
x + y;
19-
};
20-
let result = add(five, ten);
21-
==
22-
"#;
10+
// Initialize the lexer with the input string
11+
println!("this is the issionlang terminal \nwrite your code below");
12+
println!("press ctrl+c to exit the terminal");
13+
start(io::stdin(), io::stdout());
14+
//print_tokens();
2315

24-
let mut lexer = Lexer::new(input);
25-
let runer = read();
26-
//println!("Lexer initialized with input: {}", input);
16+
}
2717

28-
// let mut tokens = Vec::new();
29-
// // let mut token = lexer.next_token();
30-
// // while token.kind != lexericals::tokenizer::Tokenkind::Eof {
31-
// // tokens.push(token.literal);
32-
// // token = lexer.next_token();
33-
// // }
3418

35-
// loop {
36-
// let token = lexer.next_token();
37-
// if token.kind == lexericals::tokenizer::Tokenkind::Eof {
38-
// break;
39-
// }
40-
// tokens.push(token.literal);
41-
// }
42-
// println!("Tokens: {:?}", tokens);
43-
// println!("Tokens: {:?}", lexer);
19+
fn print_tokens() {
20+
let mut lexer = Lexer::new(read_input());
21+
let runer = expected_token();
4422

4523
for (idx, token) in runer.into_iter().enumerate() {
4624
// println!("charater at index:{} token:{:?}", idx, token);
@@ -54,94 +32,75 @@ fn main() {
5432
idx, token.literal, inside_token.literal
5533
);
5634
}
57-
58-
// println!(" last Current character: {:?}", lexer.next_token());
5935
}
6036

61-
// fn read_string(ch:char) -> String {
62-
// todo!("Implement string reading logic");
63-
// }
6437

65-
fn read() -> Vec<Token> {
66-
let expected_token = vec![
67-
Token::new(Tokenkind::Let, String::from("let")),
68-
Token::new(Tokenkind::Ident, String::from("five")),
69-
Token::new(Tokenkind::Assign, String::from("=")),
70-
Token::new(Tokenkind::Int, String::from("5000000")),
71-
Token::new(Tokenkind::Semicolon, String::from(";")),
72-
Token::new(Tokenkind::Let, String::from("let")),
73-
Token::new(Tokenkind::Ident, String::from("floattest")),
74-
Token::new(Tokenkind::Assign, String::from("=")),
75-
Token::new(Tokenkind::Float, String::from("5.0")),
76-
Token::new(Tokenkind::Semicolon, String::from(";")),
77-
Token::new(Tokenkind::Let, String::from("let")),
78-
Token::new(Tokenkind::Ident, String::from("ten")),
79-
Token::new(Tokenkind::Assign, String::from("=")),
80-
Token::new(Tokenkind::Int, String::from("10")),
81-
Token::new(Tokenkind::Semicolon, String::from(";")),
82-
// Token::new(Tokenkind::Let, String::from("let")),
83-
// Token::new(Tokenkind::Ident, String::from("ten")),
84-
// Token::new(Tokenkind::Assign, String::from("=")),
85-
// Token::new(Tokenkind::Int, String::from("10")),
86-
// Token::new(Tokenkind::Semicolon, String::from(";")),
87-
Token::new(Tokenkind::Let, String::from("let")),
88-
Token::new(Tokenkind::Ident, String::from("add")),
89-
Token::new(Tokenkind::Assign, String::from("=")),
90-
Token::new(Tokenkind::Function, String::from("fn")),
91-
Token::new(Tokenkind::Lparen, String::from("(")),
92-
Token::new(Tokenkind::Ident, String::from("x")),
93-
Token::new(Tokenkind::Comma, String::from(",")),
94-
Token::new(Tokenkind::Ident, String::from("y")),
95-
Token::new(Tokenkind::Rparen, String::from(")")),
96-
Token::new(Tokenkind::Lbrace, String::from("{")),
97-
Token::new(Tokenkind::Ident, String::from("x")),
98-
Token::new(Tokenkind::Plus, String::from("+")),
99-
Token::new(Tokenkind::Ident, String::from("y")),
100-
Token::new(Tokenkind::Semicolon, String::from(";")),
101-
Token::new(Tokenkind::Rbrace, String::from("}")),
102-
Token::new(Tokenkind::Semicolon, String::from(";")),
103-
Token::new(Tokenkind::Let, String::from("let")),
104-
Token::new(Tokenkind::Ident, String::from("result")),
105-
Token::new(Tokenkind::Assign, String::from("=")),
106-
Token::new(Tokenkind::Ident, String::from("add")),
107-
Token::new(Tokenkind::Lparen, String::from("(")),
108-
Token::new(Tokenkind::Ident, String::from("five")),
109-
Token::new(Tokenkind::Comma, String::from(",")),
110-
Token::new(Tokenkind::Ident, String::from("ten")),
111-
Token::new(Tokenkind::Rparen, String::from(")")),
112-
Token::new(Tokenkind::Semicolon, String::from(";")),
113-
Token::new(Tokenkind::Eq, String::from("==")),
114-
Token::new(Tokenkind::Eof, String::new()),
115-
];
116-
expected_token
117-
}
118-
119-
// fn main() {
120-
// let input = r#"
121-
// let five = 5;
122-
// let ten = 10;
123-
// let add = fn(x, y) {
124-
// x + y;
125-
// };
126-
// let result = add(five, ten);
127-
// "#;
38+
fn read_input() -> &'static str {
39+
let input = r#"
40+
let five = 5000000;
41+
let floattest = 5.0;
42+
let ten = 10;
43+
let add = fn(x, y) {
44+
x + y;
45+
};
46+
let result = add(five, ten);
47+
== True; False;
48+
"#;
12849

129-
// let mut lexer = Lexer::new(input);
130-
// println!("Lexer initialized with input: {}", input);
50+
input
51+
}
52+
fn expected_token() -> Vec<Token> {
13153

132-
// let mut tokens = Vec::new();
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+
];
103+
expect_tokens
104+
}
133105

134-
// loop {
135-
// let token = lexer.next_token();
136-
// if token.kind == lexericals::tokenizer::Tokenkind::Eof {
137-
// break;
138-
// }
139-
// tokens.push(token); // push the whole token (no clone)
140-
// }
141106

142-
// for (idx, token) in tokens.into_iter().enumerate() {
143-
// println!("character at index:{} token:{:?}", idx, token);
144-
// println!("Tokenkind at index:{} is:{:?}", idx, token.kind);
145-
// println!("literal at index:{} is:{} ", idx, token.literal);
146-
// }
147-
// }

src/repl.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::io::{Stdout,Stdin,Write};
2+
3+
use crate::lexericals::{lexer::Lexer, tokenizer::Tokenkind};
4+
5+
6+
pub fn start(stdin: Stdin, mut stdout: Stdout) {
7+
loop{
8+
write!(stdout, ">>> ").expect("Failed to write >> to stdout");
9+
stdout.flush().expect("Failed to flush stdout");
10+
11+
let mut input = String::new();
12+
13+
if let Err(e) = stdin.read_line(&mut input) {
14+
writeln!(stdout, "Error reading input: {}", e).expect("Failed to write error to stdout");
15+
stdout.flush().expect("Failed to flush stdout");
16+
return;
17+
}
18+
let mut lexer = Lexer::new(&input);
19+
loop {
20+
let token = lexer.next_token();
21+
if token.kind == Tokenkind::Eof {
22+
break;
23+
}
24+
writeln!(stdout, "Token: {:?}", token).expect("Failed to write token to stdout");
25+
stdout.flush().expect("Failed to flush stdout");
26+
}
27+
writeln!(stdout, "End of input").expect("Failed to write end of input to stdout");
28+
stdout.flush().expect("Failed to flush stdout");
29+
}
30+
}
31+
32+
33+
// #[cfg(test)]
34+
// mod tests {
35+
// use super::*;
36+
37+
// #[test]
38+
// fn test_start() {
39+
// let input = "let x = 5;\nlet y = 10;\n";
40+
// let mut stdin = std::io::Cursor::new(input);
41+
// let mut stdout = std::io::Cursor::new(Vec::new());
42+
43+
// start(stdin, stdout);
44+
45+
// let output = String::from_utf8(stdout.into_inner()).expect("Failed to convert output to string");
46+
// assert!(output.contains("Token:"));
47+
// assert!(output.contains("End of input"));
48+
// }
49+
// }

0 commit comments

Comments
 (0)