2022-03-24 16:33:10 +01:00
|
|
|
mod ast;
|
2022-03-21 15:47:35 +01:00
|
|
|
mod lexer;
|
|
|
|
|
|
|
|
use lexer::Lexer;
|
|
|
|
use std::{fs, io, path};
|
|
|
|
use tracing::Level;
|
|
|
|
use tracing_subscriber::{EnvFilter, FmtSubscriber};
|
|
|
|
|
|
|
|
fn run_file(file: &path::Path) -> Result<(), io::Error> {
|
|
|
|
let src = fs::read_to_string(file)?;
|
|
|
|
run(&src).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_repl() {
|
2022-03-21 18:42:47 +01:00
|
|
|
let mut line_buf = String::new();
|
2022-03-21 15:47:35 +01:00
|
|
|
loop {
|
|
|
|
match io::stdin().read_line(&mut line_buf) {
|
|
|
|
Ok(_) => {
|
|
|
|
let line = line_buf.trim();
|
|
|
|
run(line).unwrap();
|
|
|
|
line_buf.clear();
|
|
|
|
}
|
|
|
|
Err(_e) => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(code: &str) -> Result<(), Vec<lexer::LexingError>> {
|
|
|
|
let mut lexer = Lexer::new(code);
|
2022-03-24 16:33:10 +01:00
|
|
|
let tokens = lexer.scan_tokens()?;
|
|
|
|
println!("{:?}", tokens);
|
|
|
|
let mut parser = crate::ast::parser::Parser::new(tokens.into_iter());
|
|
|
|
let expressions = parser.scan_expressions().unwrap();
|
|
|
|
println!("{:?}", expressions);
|
|
|
|
|
2022-03-21 15:47:35 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dotenv::dotenv().ok();
|
|
|
|
let subscriber = FmtSubscriber::builder()
|
|
|
|
.with_max_level(Level::TRACE)
|
|
|
|
.with_env_filter(EnvFilter::from_env("LOG"))
|
|
|
|
.finish();
|
|
|
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
|
|
|
|
|
|
|
if let Some(file) = std::env::args_os().nth(1) {
|
|
|
|
run_file(file.as_ref()).unwrap();
|
|
|
|
} else {
|
|
|
|
run_repl()
|
|
|
|
}
|
|
|
|
}
|