diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..321eb45 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rlox" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c0eacd4 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,47 @@ +use std::env; +use std::fs; +use std::io; +use std::io::Write; + +fn main() { + let args: Vec = 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 \ No newline at end of file