repl
This commit is contained in:
+2
-26
@@ -1,31 +1,7 @@
|
|||||||
mod lexer;
|
mod lexer;
|
||||||
|
mod repl;
|
||||||
mod token;
|
mod token;
|
||||||
|
|
||||||
use crate::lexer::LexerTraits;
|
|
||||||
use lexer::new;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = "let five = 5;
|
repl::start_repl();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user