Lox interpreter in rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

64 lines
1.1 KiB

mod scanner;
mod tokens;
use std::io::Write;
use std::{env, fs, io};
fn main() {
let args: Vec<String> = env::args().collect();
match args.len() {
d if d > 2 => {
println!("Usage: {} [script]", args[0]);
}
2 => {
run_file(&args[1]);
}
_ => {
run_prompt();
}
}
}
fn run_file(path: &str) {
Interpreter::new().run(&fs::read_to_string(path).unwrap());
}
fn run_prompt() {
let mut interpreter = Interpreter::new();
loop {
let mut input = String::new();
print!("> ");
io::stdout().flush().unwrap();
match io::stdin().read_line(&mut input) {
Ok(_) => {
interpreter.run(&input);
interpreter.had_error = false;
}
Err(_) => return,
}
}
}
struct Interpreter {
had_error: bool,
}
impl Interpreter {
fn new() -> Self {
Self { had_error: false }
}
fn run(&mut self, source: &str) {
let tokens = source.split(' ');
for token in tokens {
println!("{}", token)
}
}
fn error(&mut self, line: usize, msg: &str) {
self.report(line, "", msg);
}
fn report(&mut self, line: usize, location: &str, msg: &str) {
println!("[line {}] Error {}: {}", line, location, msg);
}
}