Command line processsing, debug options
This commit is contained in:
@@ -1,36 +1,55 @@
|
||||
mod parser;
|
||||
mod evaluator;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn repl() {
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about = "Interpreter for Deelang")]
|
||||
struct Cli {
|
||||
#[clap(help="Specify a file to run")]
|
||||
file: Option<PathBuf>,
|
||||
#[clap(short, long, help="Only parse, do not evaluate")]
|
||||
parse_only: bool,
|
||||
}
|
||||
|
||||
fn repl(cli: &Cli) {
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
io::stdin().read_line(&mut line).unwrap();
|
||||
let tree = parser::parse_stmt(&line);
|
||||
match &tree {
|
||||
parser::Stmt::ReplPrint(expr) => println!("{}", evaluator::eval(expr)),
|
||||
_ => (),
|
||||
};
|
||||
if cli.parse_only {
|
||||
println!("{:#?}", tree);
|
||||
} else {
|
||||
match &tree {
|
||||
parser::Stmt::ReplPrint(expr) =>
|
||||
println!("{}", evaluator::eval(expr)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn script(filename: &str) {
|
||||
let mut file = File::open(filename).expect("Could not read file");
|
||||
fn script(cli: &Cli) {
|
||||
let mut file = File::open(cli.file.as_ref().unwrap()).expect("Could not read file");
|
||||
let mut prgm = String::new();
|
||||
file.read_to_string(&mut prgm).unwrap();
|
||||
let tree = parser::parse(&prgm);
|
||||
println!("{:#?}", tree);
|
||||
if cli.parse_only {
|
||||
println!("{:#?}", tree);
|
||||
} else {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() == 1 {
|
||||
repl();
|
||||
} else {
|
||||
script(&args[1]);
|
||||
let cli = Cli::parse();
|
||||
match cli.file {
|
||||
None => repl(&cli),
|
||||
Some(_) => script(&cli),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +160,6 @@ fn preprocess(input: &str) -> String {
|
||||
output.push_str(line.trim());
|
||||
output.push('\n');
|
||||
}
|
||||
println!("{}", output);
|
||||
output
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user