From 13d5a20c741180e79604fe39c5ef712f7003ffff Mon Sep 17 00:00:00 2001 From: YannAhlgrim Date: Tue, 12 May 2026 18:39:33 +0200 Subject: [PATCH] repl --- src/main.rs | 28 ++-------------------------- src/repl/mod.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 src/repl/mod.rs diff --git a/src/main.rs b/src/main.rs index b6b35bf..db5320b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,7 @@ mod lexer; +mod repl; mod token; -use crate::lexer::LexerTraits; -use lexer::new; - fn main() { - let input = "let five = 5; - let ten = 10; - let add = fn(x, y) { - x + y; - }; - let result = add(five, ten); - !-/*5; - 5 < 10 > 5; - - if (5 < 10) { - return true; - } else { - return false; - } - - 10 == 10; - 10 != 9;"; - - let mut l = new(input.to_string()); - - for (_i, tok) in (0..80).map(|_| l.next_token()).enumerate() { - println!("{}: {:?}", tok.type_, tok.literal); - } + repl::start_repl(); } diff --git a/src/repl/mod.rs b/src/repl/mod.rs new file mode 100644 index 0000000..4781a87 --- /dev/null +++ b/src/repl/mod.rs @@ -0,0 +1,27 @@ +use crate::lexer::LexerTraits; +use crate::lexer::new; + +const PROMPT: &str = ">> "; + +pub fn start_repl() { + println!("Welcome to the REPL! Type 'exit' to quit."); + loop { + print!("{}", PROMPT); + let mut input = String::new(); + std::io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + let input = input.trim(); + if input == "exit" { + break; + } + let mut lexer = new(input.to_string()); + loop { + let token = lexer.next_token(); + if token.type_ == "EOF" { + break; + } + println!("Token: {:?}, Literal: {:?}", token.type_, token.literal); + } + } +}