Initial commit
This commit is contained in:
47
src/main.rs
Normal file
47
src/main.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
match args.len() {
|
||||
1 => run_prompt(),
|
||||
2 => run_file(&args[1]),
|
||||
_ => {
|
||||
println!("Usage : rlox [script]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_file( file_path: &str ) {
|
||||
let contents = fs::read_to_string(file_path)
|
||||
.expect(&format!("Should have been able to read the file {file_path}"));
|
||||
|
||||
run(contents);
|
||||
}
|
||||
|
||||
fn run_prompt() {
|
||||
loop {
|
||||
print!("> ");
|
||||
io::stdout().flush().expect("Unable to flush stdout");
|
||||
let mut line = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut line)
|
||||
.expect("Failed to read line");
|
||||
|
||||
if line.trim().is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
run(line);
|
||||
}
|
||||
}
|
||||
|
||||
fn run( _script: String ) {
|
||||
|
||||
}
|
||||
|
||||
// http://www.craftinginterpreters.com/scanning.html#error-handling
|
||||
Reference in New Issue
Block a user